miner.h 28 KB

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