miner.h 27 KB

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