miner.h 27 KB

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