miner.h 28 KB

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