miner.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  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 flip64(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 < 16; i++)
  557. dest[i] = swab32(src[i]);
  558. }
  559. static inline void flip80(void *dest_p, const void *src_p)
  560. {
  561. uint32_t *dest = dest_p;
  562. const uint32_t *src = src_p;
  563. int i;
  564. for (i = 0; i < 20; i++)
  565. dest[i] = swab32(src[i]);
  566. }
  567. extern void quit(int status, const char *format, ...);
  568. static inline void mutex_lock(pthread_mutex_t *lock)
  569. {
  570. if (unlikely(pthread_mutex_lock(lock)))
  571. quit(1, "WTF MUTEX ERROR ON LOCK!");
  572. }
  573. static inline void mutex_unlock(pthread_mutex_t *lock)
  574. {
  575. if (unlikely(pthread_mutex_unlock(lock)))
  576. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  577. }
  578. static inline int mutex_trylock(pthread_mutex_t *lock)
  579. {
  580. return pthread_mutex_trylock(lock);
  581. }
  582. static inline void wr_lock(pthread_rwlock_t *lock)
  583. {
  584. if (unlikely(pthread_rwlock_wrlock(lock)))
  585. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  586. }
  587. static inline void rd_lock(pthread_rwlock_t *lock)
  588. {
  589. if (unlikely(pthread_rwlock_rdlock(lock)))
  590. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  591. }
  592. static inline void rw_unlock(pthread_rwlock_t *lock)
  593. {
  594. if (unlikely(pthread_rwlock_unlock(lock)))
  595. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  596. }
  597. static inline void rd_unlock(pthread_rwlock_t *lock)
  598. {
  599. rw_unlock(lock);
  600. }
  601. static inline void wr_unlock(pthread_rwlock_t *lock)
  602. {
  603. rw_unlock(lock);
  604. }
  605. static inline void mutex_init(pthread_mutex_t *lock)
  606. {
  607. if (unlikely(pthread_mutex_init(lock, NULL)))
  608. quit(1, "Failed to pthread_mutex_init");
  609. }
  610. static inline void rwlock_init(pthread_rwlock_t *lock)
  611. {
  612. if (unlikely(pthread_rwlock_init(lock, NULL)))
  613. quit(1, "Failed to pthread_rwlock_init");
  614. }
  615. /* cgminer locks, a write biased variant of rwlocks */
  616. struct cglock {
  617. pthread_mutex_t mutex;
  618. pthread_rwlock_t rwlock;
  619. };
  620. typedef struct cglock cglock_t;
  621. static inline void cglock_init(cglock_t *lock)
  622. {
  623. mutex_init(&lock->mutex);
  624. rwlock_init(&lock->rwlock);
  625. }
  626. /* Read lock variant of cglock */
  627. static inline void cg_rlock(cglock_t *lock)
  628. {
  629. mutex_lock(&lock->mutex);
  630. rd_lock(&lock->rwlock);
  631. mutex_unlock(&lock->mutex);
  632. }
  633. /* Intermediate variant of cglock */
  634. static inline void cg_ilock(cglock_t *lock)
  635. {
  636. mutex_lock(&lock->mutex);
  637. }
  638. /* Upgrade intermediate variant to a write lock */
  639. static inline void cg_ulock(cglock_t *lock)
  640. {
  641. wr_lock(&lock->rwlock);
  642. }
  643. /* Write lock variant of cglock */
  644. static inline void cg_wlock(cglock_t *lock)
  645. {
  646. mutex_lock(&lock->mutex);
  647. wr_lock(&lock->rwlock);
  648. }
  649. /* Downgrade intermediate variant to a read lock */
  650. static inline void cg_dlock(cglock_t *lock)
  651. {
  652. rd_lock(&lock->rwlock);
  653. mutex_unlock(&lock->mutex);
  654. }
  655. static inline void cg_runlock(cglock_t *lock)
  656. {
  657. rd_unlock(&lock->rwlock);
  658. }
  659. static inline void cg_wunlock(cglock_t *lock)
  660. {
  661. wr_unlock(&lock->rwlock);
  662. mutex_unlock(&lock->mutex);
  663. }
  664. struct pool;
  665. extern bool opt_protocol;
  666. extern bool have_longpoll;
  667. extern char *opt_kernel_path;
  668. extern char *opt_socks_proxy;
  669. extern char *cgminer_path;
  670. extern bool opt_fail_only;
  671. extern bool opt_autofan;
  672. extern bool opt_autoengine;
  673. extern bool use_curses;
  674. extern char *opt_api_allow;
  675. extern char *opt_api_groups;
  676. extern char *opt_api_description;
  677. extern int opt_api_port;
  678. extern bool opt_api_listen;
  679. extern bool opt_api_network;
  680. extern bool opt_delaynet;
  681. extern bool opt_restart;
  682. extern char *opt_icarus_options;
  683. extern char *opt_icarus_timing;
  684. extern bool opt_worktime;
  685. #ifdef USE_AVALON
  686. extern char *opt_avalon_options;
  687. #endif
  688. #ifdef USE_USBUTILS
  689. extern char *opt_usb_select;
  690. extern int opt_usbdump;
  691. extern bool opt_usb_list_all;
  692. #endif
  693. #ifdef USE_BITFORCE
  694. extern bool opt_bfl_noncerange;
  695. #endif
  696. extern bool ping;
  697. extern int swork_id;
  698. extern pthread_rwlock_t netacc_lock;
  699. extern const uint32_t sha256_init_state[];
  700. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  701. const char *rpc_req, bool, bool, int *,
  702. struct pool *pool, bool);
  703. extern const char *proxytype(curl_proxytype proxytype);
  704. extern char *get_proxy(char *url, struct pool *pool);
  705. extern char *bin2hex(const unsigned char *p, size_t len);
  706. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  707. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  708. unsigned char *pdata,
  709. unsigned char *phash1, unsigned char *phash,
  710. const unsigned char *ptarget,
  711. uint32_t max_nonce,
  712. uint32_t *last_nonce,
  713. uint32_t nonce);
  714. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  715. extern int opt_queue;
  716. extern int opt_scantime;
  717. extern int opt_expiry;
  718. #ifdef USE_USBUTILS
  719. extern pthread_mutex_t cgusb_lock;
  720. #endif
  721. extern cglock_t control_lock;
  722. extern pthread_mutex_t hash_lock;
  723. extern pthread_mutex_t console_lock;
  724. extern cglock_t ch_lock;
  725. extern pthread_rwlock_t mining_thr_lock;
  726. extern pthread_rwlock_t devices_lock;
  727. extern pthread_mutex_t restart_lock;
  728. extern pthread_cond_t restart_cond;
  729. extern void thread_reportin(struct thr_info *thr);
  730. extern void clear_stratum_shares(struct pool *pool);
  731. extern int restart_wait(unsigned int mstime);
  732. extern void kill_work(void);
  733. extern void reinit_device(struct cgpu_info *cgpu);
  734. #ifdef HAVE_ADL
  735. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  736. extern int set_fanspeed(int gpu, int iFanSpeed);
  737. extern int set_vddc(int gpu, float fVddc);
  738. extern int set_engineclock(int gpu, int iEngineClock);
  739. extern int set_memoryclock(int gpu, int iMemoryClock);
  740. #endif
  741. extern void api(int thr_id);
  742. extern struct pool *current_pool(void);
  743. extern int enabled_pools;
  744. extern bool detect_stratum(struct pool *pool, char *url);
  745. extern void print_summary(void);
  746. extern struct pool *add_pool(void);
  747. extern bool add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  748. #define MAX_GPUDEVICES 16
  749. #define MIN_INTENSITY -10
  750. #define _MIN_INTENSITY_STR "-10"
  751. #ifdef USE_SCRYPT
  752. #define MAX_INTENSITY 20
  753. #define _MAX_INTENSITY_STR "20"
  754. #else
  755. #define MAX_INTENSITY 14
  756. #define _MAX_INTENSITY_STR "14"
  757. #endif
  758. extern bool hotplug_mode;
  759. extern int hotplug_time;
  760. extern struct list_head scan_devices;
  761. extern int nDevs;
  762. extern int opt_n_threads;
  763. extern int num_processors;
  764. extern int hw_errors;
  765. extern bool use_syslog;
  766. extern bool opt_quiet;
  767. extern struct thr_info *control_thr;
  768. extern struct thr_info **mining_thr;
  769. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  770. extern int gpu_threads;
  771. #ifdef USE_SCRYPT
  772. extern bool opt_scrypt;
  773. #else
  774. #define opt_scrypt (0)
  775. #endif
  776. extern double total_secs;
  777. extern int mining_threads;
  778. extern struct cgpu_info *cpus;
  779. extern int total_devices;
  780. extern struct cgpu_info **devices;
  781. extern int total_pools;
  782. extern struct pool **pools;
  783. extern const char *algo_names[];
  784. extern enum sha256_algos opt_algo;
  785. extern struct strategies strategies[];
  786. extern enum pool_strategy pool_strategy;
  787. extern int opt_rotate_period;
  788. extern double total_mhashes_done;
  789. extern unsigned int new_blocks;
  790. extern unsigned int found_blocks;
  791. extern int total_accepted, total_rejected, total_diff1;;
  792. extern int total_getworks, total_stale, total_discarded;
  793. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  794. extern unsigned int local_work;
  795. extern unsigned int total_go, total_ro;
  796. extern const int opt_cutofftemp;
  797. extern int opt_log_interval;
  798. extern unsigned long long global_hashrate;
  799. extern char *current_fullhash;
  800. extern double current_diff;
  801. extern uint64_t best_diff;
  802. extern struct timeval block_timeval;
  803. #ifdef HAVE_OPENCL
  804. typedef struct {
  805. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  806. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  807. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  808. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  809. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  810. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  811. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  812. cl_uint W16; cl_uint W17; cl_uint W2;
  813. cl_uint PreVal4; cl_uint T1;
  814. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  815. cl_uint PreVal4addT1; cl_uint T1substate0;
  816. cl_uint PreVal4_2;
  817. cl_uint PreVal0;
  818. cl_uint PreW18;
  819. cl_uint PreW19;
  820. cl_uint PreW31;
  821. cl_uint PreW32;
  822. /* For diakgcn */
  823. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  824. cl_uint zeroA, zeroB;
  825. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  826. #ifdef USE_SCRYPT
  827. struct work *work;
  828. #endif
  829. } dev_blk_ctx;
  830. #else
  831. typedef struct {
  832. uint32_t nonce;
  833. } dev_blk_ctx;
  834. #endif
  835. struct curl_ent {
  836. CURL *curl;
  837. struct list_head node;
  838. struct timeval tv;
  839. };
  840. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  841. * equal disabled */
  842. enum pool_enable {
  843. POOL_DISABLED,
  844. POOL_ENABLED,
  845. POOL_REJECTING,
  846. };
  847. struct stratum_work {
  848. char *job_id;
  849. char *prev_hash;
  850. char *coinbase1;
  851. char *coinbase2;
  852. char **merkle;
  853. char *bbversion;
  854. char *nbit;
  855. char *ntime;
  856. bool clean;
  857. size_t cb1_len;
  858. size_t cb2_len;
  859. size_t cb_len;
  860. size_t header_len;
  861. int merkles;
  862. double diff;
  863. };
  864. #define RBUFSIZE 8192
  865. #define RECVSIZE (RBUFSIZE - 4)
  866. struct pool {
  867. int pool_no;
  868. int prio;
  869. int accepted, rejected;
  870. int seq_rejects;
  871. int seq_getfails;
  872. int solved;
  873. int diff1;
  874. char diff[8];
  875. double diff_accepted;
  876. double diff_rejected;
  877. double diff_stale;
  878. bool submit_fail;
  879. bool idle;
  880. bool lagging;
  881. bool probed;
  882. enum pool_enable enabled;
  883. bool submit_old;
  884. bool removed;
  885. bool lp_started;
  886. char *hdr_path;
  887. char *lp_url;
  888. unsigned int getwork_requested;
  889. unsigned int stale_shares;
  890. unsigned int discarded_work;
  891. unsigned int getfail_occasions;
  892. unsigned int remotefail_occasions;
  893. struct timeval tv_idle;
  894. double utility;
  895. int last_shares, shares;
  896. char *rpc_req;
  897. char *rpc_url;
  898. char *rpc_userpass;
  899. char *rpc_user, *rpc_pass;
  900. curl_proxytype rpc_proxytype;
  901. char *rpc_proxy;
  902. pthread_mutex_t pool_lock;
  903. cglock_t data_lock;
  904. struct thread_q *submit_q;
  905. struct thread_q *getwork_q;
  906. pthread_t longpoll_thread;
  907. pthread_t test_thread;
  908. bool testing;
  909. int curls;
  910. pthread_cond_t cr_cond;
  911. struct list_head curlring;
  912. time_t last_share_time;
  913. double last_share_diff;
  914. uint64_t best_diff;
  915. struct cgminer_stats cgminer_stats;
  916. struct cgminer_pool_stats cgminer_pool_stats;
  917. /* Stratum variables */
  918. char *stratum_url;
  919. char *stratum_port;
  920. CURL *stratum_curl;
  921. SOCKETTYPE sock;
  922. char *sockbuf;
  923. size_t sockbuf_size;
  924. char *sockaddr_url; /* stripped url used for sockaddr */
  925. char *nonce1;
  926. size_t n1_len;
  927. uint32_t nonce2;
  928. int n2size;
  929. char *sessionid;
  930. bool has_stratum;
  931. bool stratum_active;
  932. bool stratum_init;
  933. bool stratum_notify;
  934. struct stratum_work swork;
  935. pthread_t stratum_thread;
  936. pthread_mutex_t stratum_lock;
  937. int sshares; /* stratum shares submitted waiting on response */
  938. /* GBT variables */
  939. bool has_gbt;
  940. cglock_t gbt_lock;
  941. unsigned char previousblockhash[32];
  942. unsigned char gbt_target[32];
  943. char *coinbasetxn;
  944. char *longpollid;
  945. char *gbt_workid;
  946. int gbt_expires;
  947. uint32_t gbt_version;
  948. uint32_t curtime;
  949. uint32_t gbt_bits;
  950. unsigned char *gbt_coinbase;
  951. unsigned char *txn_hashes;
  952. int gbt_txns;
  953. int coinbase_len;
  954. struct timeval tv_lastwork;
  955. };
  956. #define GETWORK_MODE_TESTPOOL 'T'
  957. #define GETWORK_MODE_POOL 'P'
  958. #define GETWORK_MODE_LP 'L'
  959. #define GETWORK_MODE_BENCHMARK 'B'
  960. #define GETWORK_MODE_STRATUM 'S'
  961. #define GETWORK_MODE_GBT 'G'
  962. struct work {
  963. unsigned char data[128];
  964. unsigned char midstate[32];
  965. unsigned char target[32];
  966. unsigned char hash[32];
  967. double device_diff;
  968. uint64_t share_diff;
  969. int rolls;
  970. dev_blk_ctx blk;
  971. struct thr_info *thr;
  972. int thr_id;
  973. struct pool *pool;
  974. struct timeval tv_staged;
  975. bool mined;
  976. bool clone;
  977. bool cloned;
  978. int rolltime;
  979. bool longpoll;
  980. bool stale;
  981. bool mandatory;
  982. bool block;
  983. bool queued;
  984. bool stratum;
  985. char *job_id;
  986. char *nonce2;
  987. char *ntime;
  988. double sdiff;
  989. char *nonce1;
  990. bool gbt;
  991. char *gbt_coinbase;
  992. int gbt_txns;
  993. unsigned int work_block;
  994. int id;
  995. UT_hash_handle hh;
  996. double work_difficulty;
  997. // Allow devices to identify work if multiple sub-devices
  998. int subid;
  999. // Allow devices to flag work for their own purposes
  1000. bool devflag;
  1001. struct timeval tv_getwork;
  1002. struct timeval tv_getwork_reply;
  1003. struct timeval tv_cloned;
  1004. struct timeval tv_work_start;
  1005. struct timeval tv_work_found;
  1006. char getwork_mode;
  1007. };
  1008. #ifdef USE_MODMINER
  1009. struct modminer_fpga_state {
  1010. bool work_running;
  1011. struct work running_work;
  1012. struct timeval tv_workstart;
  1013. uint32_t hashes;
  1014. char next_work_cmd[46];
  1015. char fpgaid;
  1016. bool overheated;
  1017. bool new_work;
  1018. uint32_t shares;
  1019. uint32_t shares_last_hw;
  1020. uint32_t hw_errors;
  1021. uint32_t shares_to_good;
  1022. uint32_t timeout_fail;
  1023. uint32_t success_more;
  1024. struct timeval last_changed;
  1025. struct timeval last_nonce;
  1026. struct timeval first_work;
  1027. bool death_stage_one;
  1028. bool tried_two_byte_temp;
  1029. bool one_byte_temp;
  1030. };
  1031. #endif
  1032. extern void get_datestamp(char *, struct timeval *);
  1033. extern void inc_hw_errors(struct thr_info *thr);
  1034. extern void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  1035. extern struct work *get_queued(struct cgpu_info *cgpu);
  1036. extern struct work *__find_work_bymidstate(struct work *que, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1037. extern struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1038. extern void work_completed(struct cgpu_info *cgpu, struct work *work);
  1039. extern void hash_queued_work(struct thr_info *mythr);
  1040. extern void tailsprintf(char *f, const char *fmt, ...);
  1041. extern void wlogprint(const char *f, ...);
  1042. extern int curses_int(const char *query);
  1043. extern char *curses_input(const char *query);
  1044. extern void kill_work(void);
  1045. extern void switch_pools(struct pool *selected);
  1046. extern void discard_work(struct work *work);
  1047. extern void remove_pool(struct pool *pool);
  1048. extern void write_config(FILE *fcfg);
  1049. extern void zero_bestshare(void);
  1050. extern void zero_stats(void);
  1051. extern void default_save_file(char *filename);
  1052. extern bool log_curses_only(int prio, const char *f, va_list ap);
  1053. extern void clear_logwin(void);
  1054. extern bool pool_tclear(struct pool *pool, bool *var);
  1055. extern struct thread_q *tq_new(void);
  1056. extern void tq_free(struct thread_q *tq);
  1057. extern bool tq_push(struct thread_q *tq, void *data);
  1058. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  1059. extern void tq_freeze(struct thread_q *tq);
  1060. extern void tq_thaw(struct thread_q *tq);
  1061. extern bool successful_connect;
  1062. extern void adl(void);
  1063. extern void app_restart(void);
  1064. extern void clean_work(struct work *work);
  1065. extern void free_work(struct work *work);
  1066. extern void __copy_work(struct work *work, struct work *base_work);
  1067. extern struct work *copy_work(struct work *base_work);
  1068. extern struct thr_info *get_thread(int thr_id);
  1069. extern struct cgpu_info *get_devices(int id);
  1070. enum api_data_type {
  1071. API_ESCAPE,
  1072. API_STRING,
  1073. API_CONST,
  1074. API_INT,
  1075. API_UINT,
  1076. API_UINT32,
  1077. API_UINT64,
  1078. API_DOUBLE,
  1079. API_ELAPSED,
  1080. API_BOOL,
  1081. API_TIMEVAL,
  1082. API_TIME,
  1083. API_MHS,
  1084. API_MHTOTAL,
  1085. API_TEMP,
  1086. API_UTILITY,
  1087. API_FREQ,
  1088. API_VOLTS,
  1089. API_HS,
  1090. API_DIFF
  1091. };
  1092. struct api_data {
  1093. enum api_data_type type;
  1094. char *name;
  1095. void *data;
  1096. bool data_was_malloc;
  1097. struct api_data *prev;
  1098. struct api_data *next;
  1099. };
  1100. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  1101. extern struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data);
  1102. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  1103. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  1104. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  1105. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  1106. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  1107. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  1108. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  1109. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  1110. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  1111. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  1112. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  1113. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  1114. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  1115. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  1116. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  1117. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  1118. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  1119. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  1120. #endif /* __MINER_H__ */