miner.h 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. #ifndef __MINER_H__
  2. #define __MINER_H__
  3. #include "config.h"
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <sys/time.h>
  7. #include <pthread.h>
  8. #include <jansson.h>
  9. #include <curl/curl.h>
  10. #include "elist.h"
  11. #include "uthash.h"
  12. #include "logging.h"
  13. #include "util.h"
  14. #ifdef HAVE_OPENCL
  15. #ifdef __APPLE_CC__
  16. #include <OpenCL/opencl.h>
  17. #else
  18. #include <CL/cl.h>
  19. #endif
  20. #endif /* HAVE_OPENCL */
  21. #ifdef STDC_HEADERS
  22. # include <stdlib.h>
  23. # include <stddef.h>
  24. #else
  25. # ifdef HAVE_STDLIB_H
  26. # include <stdlib.h>
  27. # endif
  28. #endif
  29. #ifdef HAVE_ALLOCA_H
  30. # include <alloca.h>
  31. #elif defined __GNUC__
  32. # ifndef WIN32
  33. # define alloca __builtin_alloca
  34. # else
  35. # include <malloc.h>
  36. # endif
  37. #elif defined _AIX
  38. # define alloca __alloca
  39. #elif defined _MSC_VER
  40. # include <malloc.h>
  41. # define alloca _alloca
  42. #else
  43. # ifndef HAVE_ALLOCA
  44. # ifdef __cplusplus
  45. extern "C"
  46. # endif
  47. void *alloca (size_t);
  48. # endif
  49. #endif
  50. #ifdef __MINGW32__
  51. #include <windows.h>
  52. #include <io.h>
  53. static inline int fsync (int fd)
  54. {
  55. return (FlushFileBuffers ((HANDLE) _get_osfhandle (fd))) ? 0 : -1;
  56. }
  57. #ifndef MSG_DONTWAIT
  58. # define MSG_DONTWAIT 0x1000000
  59. #endif
  60. #endif /* __MINGW32__ */
  61. #if defined (__linux)
  62. #ifndef LINUX
  63. #define LINUX
  64. #endif
  65. #endif
  66. #ifdef WIN32
  67. #ifndef timersub
  68. #define timersub(a, b, result) \
  69. do { \
  70. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  71. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  72. if ((result)->tv_usec < 0) { \
  73. --(result)->tv_sec; \
  74. (result)->tv_usec += 1000000; \
  75. } \
  76. } while (0)
  77. #endif
  78. #ifndef timeradd
  79. # define timeradd(a, b, result) \
  80. do { \
  81. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  82. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  83. if ((result)->tv_usec >= 1000000) \
  84. { \
  85. ++(result)->tv_sec; \
  86. (result)->tv_usec -= 1000000; \
  87. } \
  88. } while (0)
  89. #endif
  90. #endif
  91. #ifdef HAVE_ADL
  92. #include "ADL_SDK/adl_sdk.h"
  93. #endif
  94. #ifdef HAVE_LIBUSB
  95. #include <libusb-1.0/libusb.h>
  96. #endif
  97. #ifdef USE_ZTEX
  98. #include "libztex.h"
  99. #endif
  100. #if !defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  101. #define bswap_16 __builtin_bswap16
  102. #define bswap_32 __builtin_bswap32
  103. #define bswap_64 __builtin_bswap64
  104. #else
  105. #if HAVE_BYTESWAP_H
  106. #include <byteswap.h>
  107. #elif defined(USE_SYS_ENDIAN_H)
  108. #include <sys/endian.h>
  109. #elif defined(__APPLE__)
  110. #include <libkern/OSByteOrder.h>
  111. #define bswap_16 OSSwapInt16
  112. #define bswap_32 OSSwapInt32
  113. #define bswap_64 OSSwapInt64
  114. #else
  115. #define bswap_16(value) \
  116. ((((value) & 0xff) << 8) | ((value) >> 8))
  117. #define bswap_32(value) \
  118. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  119. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  120. #define bswap_64(value) \
  121. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  122. << 32) | \
  123. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  124. #endif
  125. #endif /* !defined(__GLXBYTEORDER_H__) */
  126. /* This assumes htobe32 is a macro in endian.h, and if it doesn't exist, then
  127. * htobe64 also won't exist */
  128. #ifndef htobe32
  129. # if __BYTE_ORDER == __LITTLE_ENDIAN
  130. # define be32toh(x) bswap_32(x)
  131. # define be64toh(x) bswap_64(x)
  132. # define htobe32(x) bswap_32(x)
  133. # define htobe64(x) bswap_64(x)
  134. # elif __BYTE_ORDER == __BIG_ENDIAN
  135. # define be32toh(x) (x)
  136. # define be64toh(x) (x)
  137. # define htobe32(x) (x)
  138. # define htobe64(x) (x)
  139. #else
  140. #error UNKNOWN BYTE ORDER
  141. #endif
  142. #endif
  143. #undef unlikely
  144. #undef likely
  145. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  146. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  147. #define likely(expr) (__builtin_expect(!!(expr), 1))
  148. #else
  149. #define unlikely(expr) (expr)
  150. #define likely(expr) (expr)
  151. #endif
  152. #define __maybe_unused __attribute__((unused))
  153. #define uninitialised_var(x) x = x
  154. #if defined(__i386__)
  155. #define WANT_CRYPTOPP_ASM32
  156. #endif
  157. #ifndef ARRAY_SIZE
  158. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  159. #endif
  160. enum alive {
  161. LIFE_WELL,
  162. LIFE_SICK,
  163. LIFE_DEAD,
  164. LIFE_NOSTART,
  165. LIFE_INIT,
  166. };
  167. enum pool_strategy {
  168. POOL_FAILOVER,
  169. POOL_ROUNDROBIN,
  170. POOL_ROTATE,
  171. POOL_LOADBALANCE,
  172. POOL_BALANCE,
  173. };
  174. #define TOP_STRATEGY (POOL_BALANCE)
  175. struct strategies {
  176. const char *s;
  177. };
  178. struct cgpu_info;
  179. #ifdef HAVE_ADL
  180. struct gpu_adl {
  181. ADLTemperature lpTemperature;
  182. int iAdapterIndex;
  183. int lpAdapterID;
  184. int iBusNumber;
  185. char strAdapterName[256];
  186. ADLPMActivity lpActivity;
  187. ADLODParameters lpOdParameters;
  188. ADLODPerformanceLevels *DefPerfLev;
  189. ADLFanSpeedInfo lpFanSpeedInfo;
  190. ADLFanSpeedValue lpFanSpeedValue;
  191. ADLFanSpeedValue DefFanSpeedValue;
  192. int iEngineClock;
  193. int iMemoryClock;
  194. int iVddc;
  195. int iPercentage;
  196. bool autofan;
  197. bool autoengine;
  198. bool managed; /* Were the values ever changed on this card */
  199. int lastengine;
  200. int lasttemp;
  201. int targetfan;
  202. int targettemp;
  203. int overtemp;
  204. int minspeed;
  205. int maxspeed;
  206. int gpu;
  207. bool has_fanspeed;
  208. struct gpu_adl *twin;
  209. };
  210. #endif
  211. struct api_data;
  212. struct thr_info;
  213. struct work;
  214. struct device_api {
  215. char*dname;
  216. char*name;
  217. // API-global functions
  218. void (*api_detect)();
  219. // Device-specific functions
  220. void (*reinit_device)(struct cgpu_info*);
  221. void (*get_statline_before)(char*, struct cgpu_info*);
  222. void (*get_statline)(char*, struct cgpu_info*);
  223. struct api_data *(*get_api_stats)(struct cgpu_info*);
  224. bool (*get_stats)(struct cgpu_info*);
  225. void (*identify_device)(struct cgpu_info*); // e.g. to flash a led
  226. // Thread-specific functions
  227. bool (*thread_prepare)(struct thr_info*);
  228. uint64_t (*can_limit_work)(struct thr_info*);
  229. bool (*thread_init)(struct thr_info*);
  230. void (*free_work)(struct thr_info*, struct work*);
  231. bool (*prepare_work)(struct thr_info*, struct work*);
  232. int64_t (*scanhash)(struct thr_info*, struct work*, int64_t);
  233. void (*hw_error)(struct thr_info*);
  234. void (*thread_shutdown)(struct thr_info*);
  235. void (*thread_enable)(struct thr_info*);
  236. };
  237. enum dev_enable {
  238. DEV_ENABLED,
  239. DEV_DISABLED,
  240. DEV_RECOVER,
  241. };
  242. enum cl_kernels {
  243. KL_NONE,
  244. KL_POCLBM,
  245. KL_PHATK,
  246. KL_DIAKGCN,
  247. KL_DIABLO,
  248. KL_SCRYPT,
  249. };
  250. enum dev_reason {
  251. REASON_THREAD_FAIL_INIT,
  252. REASON_THREAD_ZERO_HASH,
  253. REASON_THREAD_FAIL_QUEUE,
  254. REASON_DEV_SICK_IDLE_60,
  255. REASON_DEV_DEAD_IDLE_600,
  256. REASON_DEV_NOSTART,
  257. REASON_DEV_OVER_HEAT,
  258. REASON_DEV_THERMAL_CUTOFF,
  259. REASON_DEV_COMMS_ERROR,
  260. REASON_DEV_THROTTLE,
  261. };
  262. #define REASON_NONE "None"
  263. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  264. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  265. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  266. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  267. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  268. #define REASON_DEV_NOSTART_STR "Device failed to start"
  269. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  270. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  271. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  272. #define REASON_DEV_THROTTLE_STR "Device throttle"
  273. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  274. #define MIN_SEC_UNSET 99999999
  275. struct cgminer_stats {
  276. uint32_t getwork_calls;
  277. struct timeval getwork_wait;
  278. struct timeval getwork_wait_max;
  279. struct timeval getwork_wait_min;
  280. };
  281. // Just the actual network getworks to the pool
  282. struct cgminer_pool_stats {
  283. uint32_t getwork_calls;
  284. uint32_t getwork_attempts;
  285. struct timeval getwork_wait;
  286. struct timeval getwork_wait_max;
  287. struct timeval getwork_wait_min;
  288. double getwork_wait_rolling;
  289. bool hadrolltime;
  290. bool canroll;
  291. bool hadexpire;
  292. uint32_t rolltime;
  293. double min_diff;
  294. double max_diff;
  295. double last_diff;
  296. uint32_t min_diff_count;
  297. uint32_t max_diff_count;
  298. };
  299. struct cgpu_info {
  300. int cgminer_id;
  301. struct device_api *api;
  302. int device_id;
  303. char *name;
  304. char *device_path;
  305. FILE *device_file;
  306. union {
  307. #ifdef USE_ZTEX
  308. struct libztex_device *device_ztex;
  309. #endif
  310. int device_fd;
  311. };
  312. #ifdef USE_BITFORCE
  313. struct timeval work_start_tv;
  314. unsigned int wait_ms;
  315. unsigned int sleep_ms;
  316. double avg_wait_f;
  317. unsigned int avg_wait_d;
  318. uint32_t nonces;
  319. bool nonce_range;
  320. bool polling;
  321. bool flash_led;
  322. #endif
  323. pthread_mutex_t device_mutex;
  324. enum dev_enable deven;
  325. int accepted;
  326. int rejected;
  327. int hw_errors;
  328. double rolling;
  329. double total_mhashes;
  330. double utility;
  331. enum alive status;
  332. char init[40];
  333. struct timeval last_message_tv;
  334. int threads;
  335. struct thr_info **thr;
  336. int64_t max_hashes;
  337. const char *kname;
  338. #ifdef HAVE_OPENCL
  339. bool mapped;
  340. int virtual_gpu;
  341. int virtual_adl;
  342. int intensity;
  343. bool dynamic;
  344. cl_uint vwidth;
  345. size_t work_size;
  346. enum cl_kernels kernel;
  347. cl_ulong max_alloc;
  348. #ifdef USE_SCRYPT
  349. int opt_lg, lookup_gap;
  350. size_t opt_tc, thread_concurrency;
  351. size_t shaders;
  352. #endif
  353. struct timeval tv_gpustart;
  354. int intervals;
  355. #endif
  356. bool new_work;
  357. float temp;
  358. int cutofftemp;
  359. #ifdef HAVE_ADL
  360. bool has_adl;
  361. struct gpu_adl adl;
  362. int gpu_engine;
  363. int min_engine;
  364. int gpu_fan;
  365. int min_fan;
  366. int gpu_memclock;
  367. int gpu_memdiff;
  368. int gpu_powertune;
  369. float gpu_vddc;
  370. #endif
  371. int diff1;
  372. double diff_accepted;
  373. double diff_rejected;
  374. int last_share_pool;
  375. time_t last_share_pool_time;
  376. double last_share_diff;
  377. time_t device_last_well;
  378. time_t device_last_not_well;
  379. enum dev_reason device_not_well_reason;
  380. int thread_fail_init_count;
  381. int thread_zero_hash_count;
  382. int thread_fail_queue_count;
  383. int dev_sick_idle_60_count;
  384. int dev_dead_idle_600_count;
  385. int dev_nostart_count;
  386. int dev_over_heat_count; // It's a warning but worth knowing
  387. int dev_thermal_cutoff_count;
  388. int dev_comms_error_count;
  389. int dev_throttle_count;
  390. struct cgminer_stats cgminer_stats;
  391. };
  392. extern bool add_cgpu(struct cgpu_info*);
  393. struct thread_q {
  394. struct list_head q;
  395. bool frozen;
  396. pthread_mutex_t mutex;
  397. pthread_cond_t cond;
  398. };
  399. struct thr_info {
  400. int id;
  401. int device_thread;
  402. bool primary_thread;
  403. pthread_t pth;
  404. struct thread_q *q;
  405. struct cgpu_info *cgpu;
  406. void *cgpu_data;
  407. struct timeval last;
  408. struct timeval sick;
  409. bool pause;
  410. bool getwork;
  411. double rolling;
  412. bool work_restart;
  413. };
  414. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  415. extern void thr_info_cancel(struct thr_info *thr);
  416. extern void thr_info_freeze(struct thr_info *thr);
  417. extern void nmsleep(unsigned int msecs);
  418. extern double us_tdiff(struct timeval *end, struct timeval *start);
  419. extern double tdiff(struct timeval *end, struct timeval *start);
  420. struct string_elist {
  421. char *string;
  422. bool free_me;
  423. struct list_head list;
  424. };
  425. static inline void string_elist_add(const char *s, struct list_head *head)
  426. {
  427. struct string_elist *n;
  428. n = calloc(1, sizeof(*n));
  429. n->string = strdup(s);
  430. n->free_me = true;
  431. list_add_tail(&n->list, head);
  432. }
  433. static inline void string_elist_del(struct string_elist *item)
  434. {
  435. if (item->free_me)
  436. free(item->string);
  437. list_del(&item->list);
  438. }
  439. static inline uint32_t swab32(uint32_t v)
  440. {
  441. return bswap_32(v);
  442. }
  443. static inline void swap256(void *dest_p, const void *src_p)
  444. {
  445. uint32_t *dest = dest_p;
  446. const uint32_t *src = src_p;
  447. dest[0] = src[7];
  448. dest[1] = src[6];
  449. dest[2] = src[5];
  450. dest[3] = src[4];
  451. dest[4] = src[3];
  452. dest[5] = src[2];
  453. dest[6] = src[1];
  454. dest[7] = src[0];
  455. }
  456. static inline void swab256(void *dest_p, const void *src_p)
  457. {
  458. uint32_t *dest = dest_p;
  459. const uint32_t *src = src_p;
  460. dest[0] = swab32(src[7]);
  461. dest[1] = swab32(src[6]);
  462. dest[2] = swab32(src[5]);
  463. dest[3] = swab32(src[4]);
  464. dest[4] = swab32(src[3]);
  465. dest[5] = swab32(src[2]);
  466. dest[6] = swab32(src[1]);
  467. dest[7] = swab32(src[0]);
  468. }
  469. static inline void flip32(void *dest_p, const void *src_p)
  470. {
  471. uint32_t *dest = dest_p;
  472. const uint32_t *src = src_p;
  473. int i;
  474. for (i = 0; i < 8; i++)
  475. dest[i] = swab32(src[i]);
  476. }
  477. static inline void flip80(void *dest_p, const void *src_p)
  478. {
  479. uint32_t *dest = dest_p;
  480. const uint32_t *src = src_p;
  481. int i;
  482. for (i = 0; i < 20; i++)
  483. dest[i] = swab32(src[i]);
  484. }
  485. extern void quit(int status, const char *format, ...);
  486. static inline void mutex_lock(pthread_mutex_t *lock)
  487. {
  488. if (unlikely(pthread_mutex_lock(lock)))
  489. quit(1, "WTF MUTEX ERROR ON LOCK!");
  490. }
  491. static inline void mutex_unlock(pthread_mutex_t *lock)
  492. {
  493. if (unlikely(pthread_mutex_unlock(lock)))
  494. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  495. }
  496. static inline int mutex_trylock(pthread_mutex_t *lock)
  497. {
  498. return pthread_mutex_trylock(lock);
  499. }
  500. static inline void wr_lock(pthread_rwlock_t *lock)
  501. {
  502. if (unlikely(pthread_rwlock_wrlock(lock)))
  503. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  504. }
  505. static inline void rd_lock(pthread_rwlock_t *lock)
  506. {
  507. if (unlikely(pthread_rwlock_rdlock(lock)))
  508. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  509. }
  510. static inline void rw_unlock(pthread_rwlock_t *lock)
  511. {
  512. if (unlikely(pthread_rwlock_unlock(lock)))
  513. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  514. }
  515. static inline void rd_unlock(pthread_rwlock_t *lock)
  516. {
  517. rw_unlock(lock);
  518. }
  519. static inline void wr_unlock(pthread_rwlock_t *lock)
  520. {
  521. rw_unlock(lock);
  522. }
  523. static inline void mutex_init(pthread_mutex_t *lock)
  524. {
  525. if (unlikely(pthread_mutex_init(lock, NULL)))
  526. quit(1, "Failed to pthread_mutex_init");
  527. }
  528. static inline void rwlock_init(pthread_rwlock_t *lock)
  529. {
  530. if (unlikely(pthread_rwlock_init(lock, NULL)))
  531. quit(1, "Failed to pthread_rwlock_init");
  532. }
  533. struct pool;
  534. extern bool opt_protocol;
  535. extern bool have_longpoll;
  536. extern char *opt_kernel_path;
  537. extern char *opt_socks_proxy;
  538. extern char *cgminer_path;
  539. extern bool opt_fail_only;
  540. extern bool opt_autofan;
  541. extern bool opt_autoengine;
  542. extern bool use_curses;
  543. extern char *opt_api_allow;
  544. extern char *opt_api_groups;
  545. extern char *opt_api_description;
  546. extern int opt_api_port;
  547. extern bool opt_api_listen;
  548. extern bool opt_api_network;
  549. extern bool opt_delaynet;
  550. extern bool opt_restart;
  551. extern char *opt_icarus_options;
  552. extern char *opt_icarus_timing;
  553. extern bool opt_worktime;
  554. #ifdef USE_BITFORCE
  555. extern bool opt_bfl_noncerange;
  556. #endif
  557. extern int swork_id;
  558. extern pthread_rwlock_t netacc_lock;
  559. extern const uint32_t sha256_init_state[];
  560. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  561. const char *rpc_req, bool, bool, int *,
  562. struct pool *pool, bool);
  563. extern const char *proxytype(curl_proxytype proxytype);
  564. extern char *get_proxy(char *url, struct pool *pool);
  565. extern char *bin2hex(const unsigned char *p, size_t len);
  566. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  567. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  568. unsigned char *pdata,
  569. unsigned char *phash1, unsigned char *phash,
  570. const unsigned char *ptarget,
  571. uint32_t max_nonce,
  572. uint32_t *last_nonce,
  573. uint32_t nonce);
  574. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  575. extern int opt_queue;
  576. extern int opt_scantime;
  577. extern int opt_expiry;
  578. extern pthread_mutex_t console_lock;
  579. extern pthread_mutex_t ch_lock;
  580. extern pthread_mutex_t restart_lock;
  581. extern pthread_cond_t restart_cond;
  582. extern void thread_reportin(struct thr_info *thr);
  583. extern int restart_wait(unsigned int mstime);
  584. extern void kill_work(void);
  585. extern void reinit_device(struct cgpu_info *cgpu);
  586. #ifdef HAVE_ADL
  587. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  588. extern int set_fanspeed(int gpu, int iFanSpeed);
  589. extern int set_vddc(int gpu, float fVddc);
  590. extern int set_engineclock(int gpu, int iEngineClock);
  591. extern int set_memoryclock(int gpu, int iMemoryClock);
  592. #endif
  593. extern void api(int thr_id);
  594. extern struct pool *current_pool(void);
  595. extern int enabled_pools;
  596. extern bool detect_stratum(struct pool *pool, char *url);
  597. extern struct pool *add_pool(void);
  598. extern void add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  599. #define MAX_GPUDEVICES 16
  600. #define MIN_INTENSITY -10
  601. #define _MIN_INTENSITY_STR "-10"
  602. #ifdef USE_SCRYPT
  603. #define MAX_INTENSITY 20
  604. #define _MAX_INTENSITY_STR "20"
  605. #else
  606. #define MAX_INTENSITY 14
  607. #define _MAX_INTENSITY_STR "14"
  608. #endif
  609. extern struct list_head scan_devices;
  610. extern int nDevs;
  611. extern int opt_n_threads;
  612. extern int num_processors;
  613. extern int hw_errors;
  614. extern bool use_syslog;
  615. extern bool opt_quiet;
  616. extern struct thr_info *thr_info;
  617. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  618. extern int gpu_threads;
  619. #ifdef USE_SCRYPT
  620. extern bool opt_scrypt;
  621. #else
  622. #define opt_scrypt (0)
  623. #endif
  624. extern double total_secs;
  625. extern int mining_threads;
  626. extern struct cgpu_info *cpus;
  627. extern int total_devices;
  628. extern struct cgpu_info **devices;
  629. extern int total_pools;
  630. extern struct pool **pools;
  631. extern const char *algo_names[];
  632. extern enum sha256_algos opt_algo;
  633. extern struct strategies strategies[];
  634. extern enum pool_strategy pool_strategy;
  635. extern int opt_rotate_period;
  636. extern double total_mhashes_done;
  637. extern unsigned int new_blocks;
  638. extern unsigned int found_blocks;
  639. extern int total_accepted, total_rejected, total_diff1;;
  640. extern int total_getworks, total_stale, total_discarded;
  641. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  642. extern unsigned int local_work;
  643. extern unsigned int total_go, total_ro;
  644. extern const int opt_cutofftemp;
  645. extern int opt_log_interval;
  646. extern unsigned long long global_hashrate;
  647. extern char *current_fullhash;
  648. extern struct timeval block_timeval;
  649. #ifdef HAVE_OPENCL
  650. typedef struct {
  651. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  652. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  653. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  654. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  655. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  656. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  657. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  658. cl_uint W16; cl_uint W17; cl_uint W2;
  659. cl_uint PreVal4; cl_uint T1;
  660. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  661. cl_uint PreVal4addT1; cl_uint T1substate0;
  662. cl_uint PreVal4_2;
  663. cl_uint PreVal0;
  664. cl_uint PreW18;
  665. cl_uint PreW19;
  666. cl_uint PreW31;
  667. cl_uint PreW32;
  668. /* For diakgcn */
  669. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  670. cl_uint zeroA, zeroB;
  671. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  672. #ifdef USE_SCRYPT
  673. struct work *work;
  674. #endif
  675. } dev_blk_ctx;
  676. #else
  677. typedef struct {
  678. uint32_t nonce;
  679. } dev_blk_ctx;
  680. #endif
  681. struct curl_ent {
  682. CURL *curl;
  683. struct list_head node;
  684. struct timeval tv;
  685. };
  686. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  687. * equal disabled */
  688. enum pool_enable {
  689. POOL_DISABLED,
  690. POOL_ENABLED,
  691. POOL_REJECTING,
  692. };
  693. struct stratum_work {
  694. char *job_id;
  695. char *prev_hash;
  696. char *coinbase1;
  697. char *coinbase2;
  698. char **merkle;
  699. char *bbversion;
  700. char *nbit;
  701. char *ntime;
  702. bool clean;
  703. int merkles;
  704. int diff;
  705. };
  706. #define RECVSIZE 8192
  707. #define RBUFSIZE (RECVSIZE + 4)
  708. struct pool {
  709. int pool_no;
  710. int prio;
  711. int accepted, rejected;
  712. int seq_rejects;
  713. int seq_getfails;
  714. int solved;
  715. int diff1;
  716. double diff_accepted;
  717. double diff_rejected;
  718. double diff_stale;
  719. int queued;
  720. int staged;
  721. bool submit_fail;
  722. bool idle;
  723. bool lagging;
  724. bool probed;
  725. enum pool_enable enabled;
  726. bool submit_old;
  727. bool removed;
  728. bool lp_started;
  729. char *hdr_path;
  730. char *lp_url;
  731. unsigned int getwork_requested;
  732. unsigned int stale_shares;
  733. unsigned int discarded_work;
  734. unsigned int getfail_occasions;
  735. unsigned int remotefail_occasions;
  736. struct timeval tv_idle;
  737. double utility;
  738. int last_shares, shares;
  739. char *rpc_req;
  740. char *rpc_url;
  741. char *rpc_userpass;
  742. char *rpc_user, *rpc_pass;
  743. curl_proxytype rpc_proxytype;
  744. char *rpc_proxy;
  745. pthread_mutex_t pool_lock;
  746. struct thread_q *submit_q;
  747. struct thread_q *getwork_q;
  748. pthread_t longpoll_thread;
  749. pthread_t submit_thread;
  750. pthread_t getwork_thread;
  751. int curls;
  752. pthread_cond_t cr_cond;
  753. struct list_head curlring;
  754. time_t last_share_time;
  755. double last_share_diff;
  756. struct cgminer_stats cgminer_stats;
  757. struct cgminer_pool_stats cgminer_pool_stats;
  758. /* Stratum variables */
  759. char *stratum_url;
  760. char *stratum_port;
  761. CURL *stratum_curl;
  762. SOCKETTYPE sock;
  763. char sockbuf[RBUFSIZE];
  764. char *sockaddr_url; /* stripped url used for sockaddr */
  765. char *nonce1;
  766. uint32_t nonce2;
  767. int n2size;
  768. bool has_stratum;
  769. bool stratum_active;
  770. bool stratum_auth;
  771. struct stratum_work swork;
  772. pthread_t stratum_thread;
  773. pthread_mutex_t stratum_lock;
  774. /* GBT variables */
  775. bool has_gbt;
  776. pthread_mutex_t gbt_lock;
  777. unsigned char previousblockhash[32];
  778. unsigned char gbt_target[32];
  779. char *coinbasetxn;
  780. char *longpollid;
  781. int gbt_expires;
  782. uint32_t gbt_version;
  783. uint32_t curtime;
  784. bool gbt_submitold;
  785. uint32_t gbt_bits;
  786. unsigned char *gbt_coinbase;
  787. unsigned char *txn_hashes;
  788. int gbt_txns;
  789. int coinbase_len;
  790. };
  791. #define GETWORK_MODE_TESTPOOL 'T'
  792. #define GETWORK_MODE_POOL 'P'
  793. #define GETWORK_MODE_LP 'L'
  794. #define GETWORK_MODE_BENCHMARK 'B'
  795. #define GETWORK_MODE_STRATUM 'S'
  796. #define GETWORK_MODE_GBT 'G'
  797. struct work {
  798. unsigned char data[128];
  799. unsigned char hash1[64];
  800. unsigned char midstate[32];
  801. unsigned char target[32];
  802. unsigned char hash[32];
  803. uint32_t outputhash;
  804. int rolls;
  805. dev_blk_ctx blk;
  806. struct thr_info *thr;
  807. int thr_id;
  808. struct pool *pool;
  809. struct timeval tv_staged;
  810. bool mined;
  811. bool clone;
  812. bool cloned;
  813. int rolltime;
  814. bool longpoll;
  815. bool stale;
  816. bool mandatory;
  817. bool block;
  818. bool queued;
  819. bool stratum;
  820. /* These are arbitrary lengths as it is too hard to keep track of
  821. * dynamically allocated ram in work structs */
  822. char job_id[64];
  823. char nonce2[64];
  824. char ntime[16];
  825. int sdiff;
  826. bool gbt;
  827. char gbt_coinbase[512];
  828. int gbt_txns;
  829. unsigned int work_block;
  830. int id;
  831. UT_hash_handle hh;
  832. double work_difficulty;
  833. struct timeval tv_getwork;
  834. struct timeval tv_getwork_reply;
  835. struct timeval tv_cloned;
  836. struct timeval tv_work_start;
  837. struct timeval tv_work_found;
  838. char getwork_mode;
  839. };
  840. #ifdef USE_MODMINER
  841. struct modminer_fpga_state {
  842. bool work_running;
  843. struct work running_work;
  844. struct timeval tv_workstart;
  845. uint32_t hashes;
  846. char next_work_cmd[46];
  847. unsigned char clock;
  848. float temp;
  849. uint32_t shares;
  850. uint32_t shares_last_hw;
  851. uint32_t hw_errors;
  852. uint32_t shares_to_good;
  853. struct timeval last_changed;
  854. uint32_t no_nonce_counter;
  855. };
  856. #endif
  857. extern void get_datestamp(char *, struct timeval *);
  858. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  859. extern void tailsprintf(char *f, const char *fmt, ...);
  860. extern void wlogprint(const char *f, ...);
  861. extern int curses_int(const char *query);
  862. extern char *curses_input(const char *query);
  863. extern void kill_work(void);
  864. extern void switch_pools(struct pool *selected);
  865. extern void remove_pool(struct pool *pool);
  866. extern void write_config(FILE *fcfg);
  867. extern void default_save_file(char *filename);
  868. extern bool log_curses_only(int prio, const char *f, va_list ap);
  869. extern void clear_logwin(void);
  870. extern bool pool_tclear(struct pool *pool, bool *var);
  871. extern struct thread_q *tq_new(void);
  872. extern void tq_free(struct thread_q *tq);
  873. extern bool tq_push(struct thread_q *tq, void *data);
  874. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  875. extern void tq_freeze(struct thread_q *tq);
  876. extern void tq_thaw(struct thread_q *tq);
  877. extern bool successful_connect;
  878. extern void adl(void);
  879. extern void app_restart(void);
  880. enum api_data_type {
  881. API_ESCAPE,
  882. API_STRING,
  883. API_CONST,
  884. API_INT,
  885. API_UINT,
  886. API_UINT32,
  887. API_UINT64,
  888. API_DOUBLE,
  889. API_ELAPSED,
  890. API_BOOL,
  891. API_TIMEVAL,
  892. API_TIME,
  893. API_MHS,
  894. API_MHTOTAL,
  895. API_TEMP,
  896. API_UTILITY,
  897. API_FREQ,
  898. API_VOLTS,
  899. API_HS,
  900. API_DIFF
  901. };
  902. struct api_data {
  903. enum api_data_type type;
  904. char *name;
  905. void *data;
  906. bool data_was_malloc;
  907. struct api_data *prev;
  908. struct api_data *next;
  909. };
  910. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  911. extern struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data);
  912. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  913. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  914. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  915. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  916. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  917. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  918. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  919. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  920. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  921. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  922. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  923. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  924. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  925. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  926. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  927. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  928. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  929. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  930. #endif /* __MINER_H__ */