miner.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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 "elist.h"
  11. #include "uthash.h"
  12. #include "logging.h"
  13. #include "util.h"
  14. #ifdef HAVE_OPENCL
  15. #ifdef __APPLE_CC__
  16. #include <OpenCL/opencl.h>
  17. #else
  18. #include <CL/cl.h>
  19. #endif
  20. #endif /* HAVE_OPENCL */
  21. #ifdef STDC_HEADERS
  22. # include <stdlib.h>
  23. # include <stddef.h>
  24. #else
  25. # ifdef HAVE_STDLIB_H
  26. # include <stdlib.h>
  27. # endif
  28. #endif
  29. #ifdef HAVE_ALLOCA_H
  30. # include <alloca.h>
  31. #elif defined __GNUC__
  32. # ifndef WIN32
  33. # define alloca __builtin_alloca
  34. # else
  35. # include <malloc.h>
  36. # endif
  37. #elif defined _AIX
  38. # define alloca __alloca
  39. #elif defined _MSC_VER
  40. # include <malloc.h>
  41. # define alloca _alloca
  42. #else
  43. # ifndef HAVE_ALLOCA
  44. # ifdef __cplusplus
  45. extern "C"
  46. # endif
  47. void *alloca (size_t);
  48. # endif
  49. #endif
  50. #ifdef __MINGW32__
  51. #include <windows.h>
  52. #include <io.h>
  53. static inline int fsync (int fd)
  54. {
  55. return (FlushFileBuffers ((HANDLE) _get_osfhandle (fd))) ? 0 : -1;
  56. }
  57. #ifndef EWOULDBLOCK
  58. # define EWOULDBLOCK EAGAIN
  59. #endif
  60. #ifndef MSG_DONTWAIT
  61. # define MSG_DONTWAIT 0x1000000
  62. #endif
  63. #endif /* __MINGW32__ */
  64. #if defined (__linux)
  65. #ifndef LINUX
  66. #define LINUX
  67. #endif
  68. #endif
  69. #ifdef WIN32
  70. #ifndef timersub
  71. #define timersub(a, b, result) \
  72. do { \
  73. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  74. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  75. if ((result)->tv_usec < 0) { \
  76. --(result)->tv_sec; \
  77. (result)->tv_usec += 1000000; \
  78. } \
  79. } while (0)
  80. #endif
  81. #ifndef timeradd
  82. # define timeradd(a, b, result) \
  83. do { \
  84. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  85. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  86. if ((result)->tv_usec >= 1000000) \
  87. { \
  88. ++(result)->tv_sec; \
  89. (result)->tv_usec -= 1000000; \
  90. } \
  91. } while (0)
  92. #endif
  93. #endif
  94. #ifdef HAVE_ADL
  95. #include "ADL_SDK/adl_sdk.h"
  96. #endif
  97. #ifdef HAVE_LIBUSB
  98. #include <libusb.h>
  99. #endif
  100. #ifdef USE_ZTEX
  101. #include "libztex.h"
  102. #endif
  103. #ifdef USE_USBUTILS
  104. #include "usbutils.h"
  105. #endif
  106. #if (!defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) \
  107. || (defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)))
  108. #ifndef bswap_16
  109. #define bswap_16 __builtin_bswap16
  110. #define bswap_32 __builtin_bswap32
  111. #define bswap_64 __builtin_bswap64
  112. #endif
  113. #else
  114. #if HAVE_BYTESWAP_H
  115. #include <byteswap.h>
  116. #elif defined(USE_SYS_ENDIAN_H)
  117. #include <sys/endian.h>
  118. #elif defined(__APPLE__)
  119. #include <libkern/OSByteOrder.h>
  120. #define bswap_16 OSSwapInt16
  121. #define bswap_32 OSSwapInt32
  122. #define bswap_64 OSSwapInt64
  123. #else
  124. #define bswap_16(value) \
  125. ((((value) & 0xff) << 8) | ((value) >> 8))
  126. #define bswap_32(value) \
  127. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  128. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  129. #define bswap_64(value) \
  130. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  131. << 32) | \
  132. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  133. #endif
  134. #endif /* !defined(__GLXBYTEORDER_H__) */
  135. /* This assumes htobe32 is a macro in endian.h, and if it doesn't exist, then
  136. * htobe64 also won't exist */
  137. #ifndef htobe32
  138. # if __BYTE_ORDER == __LITTLE_ENDIAN
  139. # define htole16(x) (x)
  140. # define htole32(x) (x)
  141. # define le32toh(x) (x)
  142. # define be32toh(x) bswap_32(x)
  143. # define be64toh(x) bswap_64(x)
  144. # define htobe32(x) bswap_32(x)
  145. # define htobe64(x) bswap_64(x)
  146. # elif __BYTE_ORDER == __BIG_ENDIAN
  147. # define htole16(x) bswap_16(x)
  148. # define htole32(x) bswap_32(x)
  149. # define le32toh(x) bswap_32(x)
  150. # define be32toh(x) (x)
  151. # define be64toh(x) (x)
  152. # define htobe32(x) (x)
  153. # define htobe64(x) (x)
  154. #else
  155. #error UNKNOWN BYTE ORDER
  156. #endif
  157. #endif
  158. #undef unlikely
  159. #undef likely
  160. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  161. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  162. #define likely(expr) (__builtin_expect(!!(expr), 1))
  163. #else
  164. #define unlikely(expr) (expr)
  165. #define likely(expr) (expr)
  166. #endif
  167. #define __maybe_unused __attribute__((unused))
  168. #define uninitialised_var(x) x = x
  169. #if defined(__i386__)
  170. #define WANT_CRYPTOPP_ASM32
  171. #endif
  172. #ifndef ARRAY_SIZE
  173. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  174. #endif
  175. #ifdef MIPSEB
  176. #ifndef roundl
  177. #define roundl(x) (long double)((long long)((x==0)?0.0:((x)+((x)>0)?0.5:-0.5)))
  178. #endif
  179. #endif
  180. #define MIN(x, y) ((x) > (y) ? (y) : (x))
  181. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  182. enum drv_driver {
  183. DRIVER_OPENCL = 0,
  184. DRIVER_ICARUS,
  185. DRIVER_BITFORCE,
  186. DRIVER_MODMINER,
  187. DRIVER_ZTEX,
  188. DRIVER_CPU,
  189. DRIVER_BFLSC,
  190. DRIVER_AVALON,
  191. DRIVER_MAX
  192. };
  193. enum alive {
  194. LIFE_WELL,
  195. LIFE_SICK,
  196. LIFE_DEAD,
  197. LIFE_NOSTART,
  198. LIFE_INIT,
  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 targettemp;
  236. int overtemp;
  237. int minspeed;
  238. int maxspeed;
  239. int gpu;
  240. bool has_fanspeed;
  241. struct gpu_adl *twin;
  242. };
  243. #endif
  244. extern void blank_get_statline_before(char *buf, struct cgpu_info __maybe_unused *cgpu);
  245. struct api_data;
  246. struct thr_info;
  247. struct work;
  248. struct device_drv {
  249. enum drv_driver drv_id;
  250. char *dname;
  251. char *name;
  252. // DRV-global functions
  253. void (*drv_detect)();
  254. // Device-specific functions
  255. void (*reinit_device)(struct cgpu_info *);
  256. void (*get_statline_before)(char *, struct cgpu_info *);
  257. void (*get_statline)(char *, struct cgpu_info *);
  258. struct api_data *(*get_api_stats)(struct cgpu_info *);
  259. bool (*get_stats)(struct cgpu_info *);
  260. void (*identify_device)(struct cgpu_info *); // e.g. to flash a led
  261. char *(*set_device)(struct cgpu_info *, char *option, char *setting, char *replybuf);
  262. // Thread-specific functions
  263. bool (*thread_prepare)(struct thr_info *);
  264. uint64_t (*can_limit_work)(struct thr_info *);
  265. bool (*thread_init)(struct thr_info *);
  266. bool (*prepare_work)(struct thr_info *, struct work *);
  267. /* Which hash work loop this driver uses. */
  268. void (*hash_work)(struct thr_info *);
  269. /* Two variants depending on whether the device divides work up into
  270. * small pieces or works with whole work items and may or may not have
  271. * a queue of its own. */
  272. int64_t (*scanhash)(struct thr_info *, struct work *, int64_t);
  273. int64_t (*scanwork)(struct thr_info *);
  274. /* Used to extract work from the hash table of queued work and tell
  275. * the main loop that it should not add any further work to the table.
  276. */
  277. bool (*queue_full)(struct cgpu_info *);
  278. void (*flush_work)(struct cgpu_info *);
  279. void (*hw_error)(struct thr_info *);
  280. void (*thread_shutdown)(struct thr_info *);
  281. void (*thread_enable)(struct thr_info *);
  282. // Does it need to be free()d?
  283. bool copy;
  284. /* Highest target diff the device supports */
  285. double max_diff;
  286. };
  287. extern struct device_drv *copy_drv(struct device_drv*);
  288. enum dev_enable {
  289. DEV_ENABLED,
  290. DEV_DISABLED,
  291. DEV_RECOVER,
  292. };
  293. enum cl_kernels {
  294. KL_NONE,
  295. KL_POCLBM,
  296. KL_PHATK,
  297. KL_DIAKGCN,
  298. KL_DIABLO,
  299. KL_SCRYPT,
  300. };
  301. enum dev_reason {
  302. REASON_THREAD_FAIL_INIT,
  303. REASON_THREAD_ZERO_HASH,
  304. REASON_THREAD_FAIL_QUEUE,
  305. REASON_DEV_SICK_IDLE_60,
  306. REASON_DEV_DEAD_IDLE_600,
  307. REASON_DEV_NOSTART,
  308. REASON_DEV_OVER_HEAT,
  309. REASON_DEV_THERMAL_CUTOFF,
  310. REASON_DEV_COMMS_ERROR,
  311. REASON_DEV_THROTTLE,
  312. };
  313. #define REASON_NONE "None"
  314. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  315. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  316. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  317. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  318. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  319. #define REASON_DEV_NOSTART_STR "Device failed to start"
  320. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  321. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  322. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  323. #define REASON_DEV_THROTTLE_STR "Device throttle"
  324. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  325. #define MIN_SEC_UNSET 99999999
  326. struct cgminer_stats {
  327. uint32_t getwork_calls;
  328. struct timeval getwork_wait;
  329. struct timeval getwork_wait_max;
  330. struct timeval getwork_wait_min;
  331. };
  332. // Just the actual network getworks to the pool
  333. struct cgminer_pool_stats {
  334. uint32_t getwork_calls;
  335. uint32_t getwork_attempts;
  336. struct timeval getwork_wait;
  337. struct timeval getwork_wait_max;
  338. struct timeval getwork_wait_min;
  339. double getwork_wait_rolling;
  340. bool hadrolltime;
  341. bool canroll;
  342. bool hadexpire;
  343. uint32_t rolltime;
  344. double min_diff;
  345. double max_diff;
  346. double last_diff;
  347. uint32_t min_diff_count;
  348. uint32_t max_diff_count;
  349. uint64_t times_sent;
  350. uint64_t bytes_sent;
  351. uint64_t net_bytes_sent;
  352. uint64_t times_received;
  353. uint64_t bytes_received;
  354. uint64_t net_bytes_received;
  355. };
  356. struct cgpu_info {
  357. int cgminer_id;
  358. struct device_drv *drv;
  359. int device_id;
  360. char *name;
  361. char *device_path;
  362. FILE *device_file;
  363. union {
  364. #ifdef USE_ZTEX
  365. struct libztex_device *device_ztex;
  366. #endif
  367. #ifdef USE_USBUTILS
  368. struct cg_usb_device *usbdev;
  369. #endif
  370. #if defined(USE_ICARUS) || defined(USE_AVALON)
  371. int device_fd;
  372. #endif
  373. };
  374. #ifdef USE_AVALON
  375. struct work **works;
  376. int work_array;
  377. int queued;
  378. int results;
  379. #endif
  380. #ifdef USE_USBUTILS
  381. struct cg_usb_info usbinfo;
  382. #endif
  383. #ifdef USE_MODMINER
  384. char fpgaid;
  385. unsigned char clock;
  386. pthread_mutex_t *modminer_mutex;
  387. #endif
  388. #ifdef USE_BITFORCE
  389. struct timeval work_start_tv;
  390. unsigned int wait_ms;
  391. unsigned int sleep_ms;
  392. double avg_wait_f;
  393. unsigned int avg_wait_d;
  394. uint32_t nonces;
  395. bool nonce_range;
  396. bool polling;
  397. bool flash_led;
  398. pthread_mutex_t device_mutex;
  399. #endif
  400. enum dev_enable deven;
  401. int accepted;
  402. int rejected;
  403. int hw_errors;
  404. double rolling;
  405. double total_mhashes;
  406. double utility;
  407. enum alive status;
  408. char init[40];
  409. struct timeval last_message_tv;
  410. int threads;
  411. struct thr_info **thr;
  412. int64_t max_hashes;
  413. const char *kname;
  414. #ifdef HAVE_OPENCL
  415. bool mapped;
  416. int virtual_gpu;
  417. int virtual_adl;
  418. int intensity;
  419. bool dynamic;
  420. cl_uint vwidth;
  421. size_t work_size;
  422. enum cl_kernels kernel;
  423. cl_ulong max_alloc;
  424. #ifdef USE_SCRYPT
  425. int opt_lg, lookup_gap;
  426. size_t opt_tc, thread_concurrency;
  427. size_t shaders;
  428. #endif
  429. struct timeval tv_gpustart;
  430. int intervals;
  431. #endif
  432. bool new_work;
  433. float temp;
  434. int cutofftemp;
  435. #ifdef HAVE_ADL
  436. bool has_adl;
  437. struct gpu_adl adl;
  438. int gpu_engine;
  439. int min_engine;
  440. int gpu_fan;
  441. int min_fan;
  442. int gpu_memclock;
  443. int gpu_memdiff;
  444. int gpu_powertune;
  445. float gpu_vddc;
  446. #endif
  447. int diff1;
  448. double diff_accepted;
  449. double diff_rejected;
  450. int last_share_pool;
  451. time_t last_share_pool_time;
  452. double last_share_diff;
  453. time_t last_device_valid_work;
  454. time_t device_last_well;
  455. time_t device_last_not_well;
  456. enum dev_reason device_not_well_reason;
  457. int thread_fail_init_count;
  458. int thread_zero_hash_count;
  459. int thread_fail_queue_count;
  460. int dev_sick_idle_60_count;
  461. int dev_dead_idle_600_count;
  462. int dev_nostart_count;
  463. int dev_over_heat_count; // It's a warning but worth knowing
  464. int dev_thermal_cutoff_count;
  465. int dev_comms_error_count;
  466. int dev_throttle_count;
  467. struct cgminer_stats cgminer_stats;
  468. pthread_rwlock_t qlock;
  469. struct work *queued_work;
  470. unsigned int queued_count;
  471. };
  472. extern bool add_cgpu(struct cgpu_info*);
  473. struct thread_q {
  474. struct list_head q;
  475. bool frozen;
  476. pthread_mutex_t mutex;
  477. pthread_cond_t cond;
  478. };
  479. struct thr_info {
  480. int id;
  481. int device_thread;
  482. bool primary_thread;
  483. pthread_t pth;
  484. struct thread_q *q;
  485. struct cgpu_info *cgpu;
  486. void *cgpu_data;
  487. struct timeval last;
  488. struct timeval sick;
  489. bool pause;
  490. bool getwork;
  491. double rolling;
  492. bool work_restart;
  493. };
  494. struct string_elist {
  495. char *string;
  496. bool free_me;
  497. struct list_head list;
  498. };
  499. static inline void string_elist_add(const char *s, struct list_head *head)
  500. {
  501. struct string_elist *n;
  502. n = calloc(1, sizeof(*n));
  503. n->string = strdup(s);
  504. n->free_me = true;
  505. list_add_tail(&n->list, head);
  506. }
  507. static inline void string_elist_del(struct string_elist *item)
  508. {
  509. if (item->free_me)
  510. free(item->string);
  511. list_del(&item->list);
  512. }
  513. static inline uint32_t swab32(uint32_t v)
  514. {
  515. return bswap_32(v);
  516. }
  517. static inline void swap256(void *dest_p, const void *src_p)
  518. {
  519. uint32_t *dest = dest_p;
  520. const uint32_t *src = src_p;
  521. dest[0] = src[7];
  522. dest[1] = src[6];
  523. dest[2] = src[5];
  524. dest[3] = src[4];
  525. dest[4] = src[3];
  526. dest[5] = src[2];
  527. dest[6] = src[1];
  528. dest[7] = src[0];
  529. }
  530. static inline void swab256(void *dest_p, const void *src_p)
  531. {
  532. uint32_t *dest = dest_p;
  533. const uint32_t *src = src_p;
  534. dest[0] = swab32(src[7]);
  535. dest[1] = swab32(src[6]);
  536. dest[2] = swab32(src[5]);
  537. dest[3] = swab32(src[4]);
  538. dest[4] = swab32(src[3]);
  539. dest[5] = swab32(src[2]);
  540. dest[6] = swab32(src[1]);
  541. dest[7] = swab32(src[0]);
  542. }
  543. static inline void flip32(void *dest_p, const void *src_p)
  544. {
  545. uint32_t *dest = dest_p;
  546. const uint32_t *src = src_p;
  547. int i;
  548. for (i = 0; i < 8; i++)
  549. dest[i] = swab32(src[i]);
  550. }
  551. static inline void flip80(void *dest_p, const void *src_p)
  552. {
  553. uint32_t *dest = dest_p;
  554. const uint32_t *src = src_p;
  555. int i;
  556. for (i = 0; i < 20; i++)
  557. dest[i] = swab32(src[i]);
  558. }
  559. extern void quit(int status, const char *format, ...);
  560. static inline void mutex_lock(pthread_mutex_t *lock)
  561. {
  562. if (unlikely(pthread_mutex_lock(lock)))
  563. quit(1, "WTF MUTEX ERROR ON LOCK!");
  564. }
  565. static inline void mutex_unlock(pthread_mutex_t *lock)
  566. {
  567. if (unlikely(pthread_mutex_unlock(lock)))
  568. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  569. }
  570. static inline int mutex_trylock(pthread_mutex_t *lock)
  571. {
  572. return pthread_mutex_trylock(lock);
  573. }
  574. static inline void wr_lock(pthread_rwlock_t *lock)
  575. {
  576. if (unlikely(pthread_rwlock_wrlock(lock)))
  577. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  578. }
  579. static inline void rd_lock(pthread_rwlock_t *lock)
  580. {
  581. if (unlikely(pthread_rwlock_rdlock(lock)))
  582. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  583. }
  584. static inline void rw_unlock(pthread_rwlock_t *lock)
  585. {
  586. if (unlikely(pthread_rwlock_unlock(lock)))
  587. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  588. }
  589. static inline void rd_unlock(pthread_rwlock_t *lock)
  590. {
  591. rw_unlock(lock);
  592. }
  593. static inline void wr_unlock(pthread_rwlock_t *lock)
  594. {
  595. rw_unlock(lock);
  596. }
  597. static inline void mutex_init(pthread_mutex_t *lock)
  598. {
  599. if (unlikely(pthread_mutex_init(lock, NULL)))
  600. quit(1, "Failed to pthread_mutex_init");
  601. }
  602. static inline void rwlock_init(pthread_rwlock_t *lock)
  603. {
  604. if (unlikely(pthread_rwlock_init(lock, NULL)))
  605. quit(1, "Failed to pthread_rwlock_init");
  606. }
  607. /* cgminer locks, a write biased variant of rwlocks */
  608. struct cglock {
  609. pthread_mutex_t mutex;
  610. pthread_rwlock_t rwlock;
  611. };
  612. typedef struct cglock cglock_t;
  613. static inline void cglock_init(cglock_t *lock)
  614. {
  615. mutex_init(&lock->mutex);
  616. rwlock_init(&lock->rwlock);
  617. }
  618. /* Read lock variant of cglock */
  619. static inline void cg_rlock(cglock_t *lock)
  620. {
  621. mutex_lock(&lock->mutex);
  622. rd_lock(&lock->rwlock);
  623. mutex_unlock(&lock->mutex);
  624. }
  625. /* Intermediate variant of cglock */
  626. static inline void cg_ilock(cglock_t *lock)
  627. {
  628. mutex_lock(&lock->mutex);
  629. }
  630. /* Upgrade intermediate variant to a write lock */
  631. static inline void cg_ulock(cglock_t *lock)
  632. {
  633. wr_lock(&lock->rwlock);
  634. }
  635. /* Write lock variant of cglock */
  636. static inline void cg_wlock(cglock_t *lock)
  637. {
  638. mutex_lock(&lock->mutex);
  639. wr_lock(&lock->rwlock);
  640. }
  641. /* Downgrade intermediate variant to a read lock */
  642. static inline void cg_dlock(cglock_t *lock)
  643. {
  644. rd_lock(&lock->rwlock);
  645. mutex_unlock(&lock->mutex);
  646. }
  647. static inline void cg_runlock(cglock_t *lock)
  648. {
  649. rd_unlock(&lock->rwlock);
  650. }
  651. static inline void cg_wunlock(cglock_t *lock)
  652. {
  653. wr_unlock(&lock->rwlock);
  654. mutex_unlock(&lock->mutex);
  655. }
  656. struct pool;
  657. extern bool opt_protocol;
  658. extern bool have_longpoll;
  659. extern char *opt_kernel_path;
  660. extern char *opt_socks_proxy;
  661. extern char *cgminer_path;
  662. extern bool opt_fail_only;
  663. extern bool opt_autofan;
  664. extern bool opt_autoengine;
  665. extern bool use_curses;
  666. extern char *opt_api_allow;
  667. extern char *opt_api_groups;
  668. extern char *opt_api_description;
  669. extern int opt_api_port;
  670. extern bool opt_api_listen;
  671. extern bool opt_api_network;
  672. extern bool opt_delaynet;
  673. extern bool opt_restart;
  674. extern char *opt_icarus_options;
  675. extern char *opt_icarus_timing;
  676. extern bool opt_worktime;
  677. #ifdef USE_AVALON
  678. extern char *opt_avalon_options;
  679. #endif
  680. #ifdef USE_USBUTILS
  681. extern char *opt_usb_select;
  682. extern int opt_usbdump;
  683. extern bool opt_usb_list_all;
  684. #endif
  685. #ifdef USE_BITFORCE
  686. extern bool opt_bfl_noncerange;
  687. #endif
  688. extern bool ping;
  689. extern int swork_id;
  690. extern pthread_rwlock_t netacc_lock;
  691. extern const uint32_t sha256_init_state[];
  692. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  693. const char *rpc_req, bool, bool, int *,
  694. struct pool *pool, bool);
  695. extern const char *proxytype(curl_proxytype proxytype);
  696. extern char *get_proxy(char *url, struct pool *pool);
  697. extern char *bin2hex(const unsigned char *p, size_t len);
  698. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  699. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  700. unsigned char *pdata,
  701. unsigned char *phash1, unsigned char *phash,
  702. const unsigned char *ptarget,
  703. uint32_t max_nonce,
  704. uint32_t *last_nonce,
  705. uint32_t nonce);
  706. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  707. extern int opt_queue;
  708. extern int opt_scantime;
  709. extern int opt_expiry;
  710. #ifdef USE_USBUTILS
  711. extern pthread_mutex_t cgusb_lock;
  712. #endif
  713. extern cglock_t control_lock;
  714. extern pthread_mutex_t hash_lock;
  715. extern pthread_mutex_t console_lock;
  716. extern cglock_t ch_lock;
  717. extern pthread_rwlock_t mining_thr_lock;
  718. extern pthread_rwlock_t devices_lock;
  719. extern pthread_mutex_t restart_lock;
  720. extern pthread_cond_t restart_cond;
  721. extern void thread_reportin(struct thr_info *thr);
  722. extern void clear_stratum_shares(struct pool *pool);
  723. extern int restart_wait(unsigned int mstime);
  724. extern void kill_work(void);
  725. extern void reinit_device(struct cgpu_info *cgpu);
  726. #ifdef HAVE_ADL
  727. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  728. extern int set_fanspeed(int gpu, int iFanSpeed);
  729. extern int set_vddc(int gpu, float fVddc);
  730. extern int set_engineclock(int gpu, int iEngineClock);
  731. extern int set_memoryclock(int gpu, int iMemoryClock);
  732. #endif
  733. extern void api(int thr_id);
  734. extern struct pool *current_pool(void);
  735. extern int enabled_pools;
  736. extern bool detect_stratum(struct pool *pool, char *url);
  737. extern void print_summary(void);
  738. extern struct pool *add_pool(void);
  739. extern bool add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  740. #define MAX_GPUDEVICES 16
  741. #define MIN_INTENSITY -10
  742. #define _MIN_INTENSITY_STR "-10"
  743. #ifdef USE_SCRYPT
  744. #define MAX_INTENSITY 20
  745. #define _MAX_INTENSITY_STR "20"
  746. #else
  747. #define MAX_INTENSITY 14
  748. #define _MAX_INTENSITY_STR "14"
  749. #endif
  750. extern bool hotplug_mode;
  751. extern int hotplug_time;
  752. extern struct list_head scan_devices;
  753. extern int nDevs;
  754. extern int opt_n_threads;
  755. extern int num_processors;
  756. extern int hw_errors;
  757. extern bool use_syslog;
  758. extern bool opt_quiet;
  759. extern struct thr_info *control_thr;
  760. extern struct thr_info **mining_thr;
  761. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  762. extern int gpu_threads;
  763. #ifdef USE_SCRYPT
  764. extern bool opt_scrypt;
  765. #else
  766. #define opt_scrypt (0)
  767. #endif
  768. extern double total_secs;
  769. extern int mining_threads;
  770. extern struct cgpu_info *cpus;
  771. extern int total_devices;
  772. extern struct cgpu_info **devices;
  773. extern int total_pools;
  774. extern struct pool **pools;
  775. extern const char *algo_names[];
  776. extern enum sha256_algos opt_algo;
  777. extern struct strategies strategies[];
  778. extern enum pool_strategy pool_strategy;
  779. extern int opt_rotate_period;
  780. extern double total_mhashes_done;
  781. extern unsigned int new_blocks;
  782. extern unsigned int found_blocks;
  783. extern int total_accepted, total_rejected, total_diff1;;
  784. extern int total_getworks, total_stale, total_discarded;
  785. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  786. extern unsigned int local_work;
  787. extern unsigned int total_go, total_ro;
  788. extern const int opt_cutofftemp;
  789. extern int opt_log_interval;
  790. extern unsigned long long global_hashrate;
  791. extern char *current_fullhash;
  792. extern double current_diff;
  793. extern uint64_t best_diff;
  794. extern struct timeval block_timeval;
  795. #ifdef HAVE_OPENCL
  796. typedef struct {
  797. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  798. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  799. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  800. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  801. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  802. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  803. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  804. cl_uint W16; cl_uint W17; cl_uint W2;
  805. cl_uint PreVal4; cl_uint T1;
  806. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  807. cl_uint PreVal4addT1; cl_uint T1substate0;
  808. cl_uint PreVal4_2;
  809. cl_uint PreVal0;
  810. cl_uint PreW18;
  811. cl_uint PreW19;
  812. cl_uint PreW31;
  813. cl_uint PreW32;
  814. /* For diakgcn */
  815. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  816. cl_uint zeroA, zeroB;
  817. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  818. #ifdef USE_SCRYPT
  819. struct work *work;
  820. #endif
  821. } dev_blk_ctx;
  822. #else
  823. typedef struct {
  824. uint32_t nonce;
  825. } dev_blk_ctx;
  826. #endif
  827. struct curl_ent {
  828. CURL *curl;
  829. struct list_head node;
  830. struct timeval tv;
  831. };
  832. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  833. * equal disabled */
  834. enum pool_enable {
  835. POOL_DISABLED,
  836. POOL_ENABLED,
  837. POOL_REJECTING,
  838. };
  839. struct stratum_work {
  840. char *job_id;
  841. char *prev_hash;
  842. char *coinbase1;
  843. char *coinbase2;
  844. char **merkle;
  845. char *bbversion;
  846. char *nbit;
  847. char *ntime;
  848. bool clean;
  849. size_t cb1_len;
  850. size_t cb2_len;
  851. size_t cb_len;
  852. size_t header_len;
  853. int merkles;
  854. double diff;
  855. };
  856. #define RBUFSIZE 8192
  857. #define RECVSIZE (RBUFSIZE - 4)
  858. struct pool {
  859. int pool_no;
  860. int prio;
  861. int accepted, rejected;
  862. int seq_rejects;
  863. int seq_getfails;
  864. int solved;
  865. int diff1;
  866. char diff[8];
  867. double diff_accepted;
  868. double diff_rejected;
  869. double diff_stale;
  870. bool submit_fail;
  871. bool idle;
  872. bool lagging;
  873. bool probed;
  874. enum pool_enable enabled;
  875. bool submit_old;
  876. bool removed;
  877. bool lp_started;
  878. char *hdr_path;
  879. char *lp_url;
  880. unsigned int getwork_requested;
  881. unsigned int stale_shares;
  882. unsigned int discarded_work;
  883. unsigned int getfail_occasions;
  884. unsigned int remotefail_occasions;
  885. struct timeval tv_idle;
  886. double utility;
  887. int last_shares, shares;
  888. char *rpc_req;
  889. char *rpc_url;
  890. char *rpc_userpass;
  891. char *rpc_user, *rpc_pass;
  892. curl_proxytype rpc_proxytype;
  893. char *rpc_proxy;
  894. pthread_mutex_t pool_lock;
  895. cglock_t data_lock;
  896. struct thread_q *submit_q;
  897. struct thread_q *getwork_q;
  898. pthread_t longpoll_thread;
  899. pthread_t test_thread;
  900. bool testing;
  901. int curls;
  902. pthread_cond_t cr_cond;
  903. struct list_head curlring;
  904. time_t last_share_time;
  905. double last_share_diff;
  906. uint64_t best_diff;
  907. struct cgminer_stats cgminer_stats;
  908. struct cgminer_pool_stats cgminer_pool_stats;
  909. /* Stratum variables */
  910. char *stratum_url;
  911. char *stratum_port;
  912. CURL *stratum_curl;
  913. SOCKETTYPE sock;
  914. char *sockbuf;
  915. size_t sockbuf_size;
  916. char *sockaddr_url; /* stripped url used for sockaddr */
  917. char *nonce1;
  918. size_t n1_len;
  919. uint32_t nonce2;
  920. int n2size;
  921. char *sessionid;
  922. bool has_stratum;
  923. bool stratum_active;
  924. bool stratum_init;
  925. bool stratum_notify;
  926. struct stratum_work swork;
  927. pthread_t stratum_thread;
  928. pthread_mutex_t stratum_lock;
  929. int sshares; /* stratum shares submitted waiting on response */
  930. /* GBT variables */
  931. bool has_gbt;
  932. cglock_t gbt_lock;
  933. unsigned char previousblockhash[32];
  934. unsigned char gbt_target[32];
  935. char *coinbasetxn;
  936. char *longpollid;
  937. char *gbt_workid;
  938. int gbt_expires;
  939. uint32_t gbt_version;
  940. uint32_t curtime;
  941. uint32_t gbt_bits;
  942. unsigned char *gbt_coinbase;
  943. unsigned char *txn_hashes;
  944. int gbt_txns;
  945. int coinbase_len;
  946. struct timeval tv_lastwork;
  947. };
  948. #define GETWORK_MODE_TESTPOOL 'T'
  949. #define GETWORK_MODE_POOL 'P'
  950. #define GETWORK_MODE_LP 'L'
  951. #define GETWORK_MODE_BENCHMARK 'B'
  952. #define GETWORK_MODE_STRATUM 'S'
  953. #define GETWORK_MODE_GBT 'G'
  954. struct work {
  955. unsigned char data[128];
  956. unsigned char midstate[32];
  957. unsigned char target[32];
  958. unsigned char hash[32];
  959. double device_diff;
  960. uint64_t share_diff;
  961. int rolls;
  962. dev_blk_ctx blk;
  963. struct thr_info *thr;
  964. int thr_id;
  965. struct pool *pool;
  966. struct timeval tv_staged;
  967. bool mined;
  968. bool clone;
  969. bool cloned;
  970. int rolltime;
  971. bool longpoll;
  972. bool stale;
  973. bool mandatory;
  974. bool block;
  975. bool queued;
  976. bool stratum;
  977. char *job_id;
  978. char *nonce2;
  979. char *ntime;
  980. double sdiff;
  981. char *nonce1;
  982. bool gbt;
  983. char *gbt_coinbase;
  984. int gbt_txns;
  985. unsigned int work_block;
  986. int id;
  987. UT_hash_handle hh;
  988. double work_difficulty;
  989. // Allow devices to identify work if multiple sub-devices
  990. int subid;
  991. // Allow devices to flag work for their own purposes
  992. bool devflag;
  993. struct timeval tv_getwork;
  994. struct timeval tv_getwork_reply;
  995. struct timeval tv_cloned;
  996. struct timeval tv_work_start;
  997. struct timeval tv_work_found;
  998. char getwork_mode;
  999. };
  1000. #ifdef USE_MODMINER
  1001. struct modminer_fpga_state {
  1002. bool work_running;
  1003. struct work running_work;
  1004. struct timeval tv_workstart;
  1005. uint32_t hashes;
  1006. char next_work_cmd[46];
  1007. char fpgaid;
  1008. bool overheated;
  1009. bool new_work;
  1010. uint32_t shares;
  1011. uint32_t shares_last_hw;
  1012. uint32_t hw_errors;
  1013. uint32_t shares_to_good;
  1014. uint32_t timeout_fail;
  1015. uint32_t success_more;
  1016. struct timeval last_changed;
  1017. struct timeval last_nonce;
  1018. struct timeval first_work;
  1019. bool death_stage_one;
  1020. bool tried_two_byte_temp;
  1021. bool one_byte_temp;
  1022. };
  1023. #endif
  1024. extern void get_datestamp(char *, struct timeval *);
  1025. extern void inc_hw_errors(struct thr_info *thr);
  1026. extern void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  1027. extern struct work *get_queued(struct cgpu_info *cgpu);
  1028. extern struct work *__find_work_bymidstate(struct work *que, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1029. extern struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1030. extern void work_completed(struct cgpu_info *cgpu, struct work *work);
  1031. extern void hash_queued_work(struct thr_info *mythr);
  1032. extern void tailsprintf(char *f, const char *fmt, ...);
  1033. extern void wlogprint(const char *f, ...);
  1034. extern int curses_int(const char *query);
  1035. extern char *curses_input(const char *query);
  1036. extern void kill_work(void);
  1037. extern void switch_pools(struct pool *selected);
  1038. extern void discard_work(struct work *work);
  1039. extern void remove_pool(struct pool *pool);
  1040. extern void write_config(FILE *fcfg);
  1041. extern void zero_bestshare(void);
  1042. extern void zero_stats(void);
  1043. extern void default_save_file(char *filename);
  1044. extern bool log_curses_only(int prio, const char *f, va_list ap);
  1045. extern void clear_logwin(void);
  1046. extern bool pool_tclear(struct pool *pool, bool *var);
  1047. extern struct thread_q *tq_new(void);
  1048. extern void tq_free(struct thread_q *tq);
  1049. extern bool tq_push(struct thread_q *tq, void *data);
  1050. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  1051. extern void tq_freeze(struct thread_q *tq);
  1052. extern void tq_thaw(struct thread_q *tq);
  1053. extern bool successful_connect;
  1054. extern void adl(void);
  1055. extern void app_restart(void);
  1056. extern void clean_work(struct work *work);
  1057. extern void free_work(struct work *work);
  1058. extern void __copy_work(struct work *work, struct work *base_work);
  1059. extern struct work *copy_work(struct work *base_work);
  1060. extern struct thr_info *get_thread(int thr_id);
  1061. extern struct cgpu_info *get_devices(int id);
  1062. enum api_data_type {
  1063. API_ESCAPE,
  1064. API_STRING,
  1065. API_CONST,
  1066. API_INT,
  1067. API_UINT,
  1068. API_UINT32,
  1069. API_UINT64,
  1070. API_DOUBLE,
  1071. API_ELAPSED,
  1072. API_BOOL,
  1073. API_TIMEVAL,
  1074. API_TIME,
  1075. API_MHS,
  1076. API_MHTOTAL,
  1077. API_TEMP,
  1078. API_UTILITY,
  1079. API_FREQ,
  1080. API_VOLTS,
  1081. API_HS,
  1082. API_DIFF
  1083. };
  1084. struct api_data {
  1085. enum api_data_type type;
  1086. char *name;
  1087. void *data;
  1088. bool data_was_malloc;
  1089. struct api_data *prev;
  1090. struct api_data *next;
  1091. };
  1092. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  1093. extern struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data);
  1094. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  1095. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  1096. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  1097. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  1098. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  1099. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  1100. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  1101. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  1102. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  1103. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  1104. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  1105. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  1106. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  1107. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  1108. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  1109. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  1110. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  1111. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  1112. #endif /* __MINER_H__ */