miner.h 27 KB

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