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