miner.h 27 KB

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