miner.h 27 KB

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