miner.h 27 KB

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