miner.h 27 KB

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