miner.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. #ifndef __MINER_H__
  2. #define __MINER_H__
  3. #include "config.h"
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <sys/time.h>
  7. #include <pthread.h>
  8. #include <jansson.h>
  9. #include <curl/curl.h>
  10. #include "elist.h"
  11. #include "uthash.h"
  12. #include "logging.h"
  13. #include "util.h"
  14. #ifdef HAVE_OPENCL
  15. #ifdef __APPLE_CC__
  16. #include <OpenCL/opencl.h>
  17. #else
  18. #include <CL/cl.h>
  19. #endif
  20. #endif /* HAVE_OPENCL */
  21. #ifdef STDC_HEADERS
  22. # include <stdlib.h>
  23. # include <stddef.h>
  24. #else
  25. # ifdef HAVE_STDLIB_H
  26. # include <stdlib.h>
  27. # endif
  28. #endif
  29. #ifdef HAVE_ALLOCA_H
  30. # include <alloca.h>
  31. #elif defined __GNUC__
  32. # ifndef WIN32
  33. # define alloca __builtin_alloca
  34. # else
  35. # include <malloc.h>
  36. # endif
  37. #elif defined _AIX
  38. # define alloca __alloca
  39. #elif defined _MSC_VER
  40. # include <malloc.h>
  41. # define alloca _alloca
  42. #else
  43. # ifndef HAVE_ALLOCA
  44. # ifdef __cplusplus
  45. extern "C"
  46. # endif
  47. void *alloca (size_t);
  48. # endif
  49. #endif
  50. #ifdef __MINGW32__
  51. #include <windows.h>
  52. #include <io.h>
  53. static inline int fsync (int fd)
  54. {
  55. return (FlushFileBuffers ((HANDLE) _get_osfhandle (fd))) ? 0 : -1;
  56. }
  57. #ifndef EWOULDBLOCK
  58. # define EWOULDBLOCK EAGAIN
  59. #endif
  60. #ifndef MSG_DONTWAIT
  61. # define MSG_DONTWAIT 0x1000000
  62. #endif
  63. #endif /* __MINGW32__ */
  64. #if defined (__linux)
  65. #ifndef LINUX
  66. #define LINUX
  67. #endif
  68. #endif
  69. #ifdef WIN32
  70. #ifndef timersub
  71. #define timersub(a, b, result) \
  72. do { \
  73. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  74. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  75. if ((result)->tv_usec < 0) { \
  76. --(result)->tv_sec; \
  77. (result)->tv_usec += 1000000; \
  78. } \
  79. } while (0)
  80. #endif
  81. #ifndef timeradd
  82. # define timeradd(a, b, result) \
  83. do { \
  84. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  85. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  86. if ((result)->tv_usec >= 1000000) \
  87. { \
  88. ++(result)->tv_sec; \
  89. (result)->tv_usec -= 1000000; \
  90. } \
  91. } while (0)
  92. #endif
  93. #endif
  94. #ifdef HAVE_ADL
  95. #include "ADL_SDK/adl_sdk.h"
  96. #endif
  97. #ifdef HAVE_LIBUSB
  98. #include <libusb.h>
  99. #endif
  100. #ifdef USE_ZTEX
  101. #include "libztex.h"
  102. #endif
  103. #ifdef USE_USBUTILS
  104. #include "usbutils.h"
  105. #endif
  106. #if (!defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) \
  107. || (defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)))
  108. #define bswap_16 __builtin_bswap16
  109. #define bswap_32 __builtin_bswap32
  110. #define bswap_64 __builtin_bswap64
  111. #else
  112. #if HAVE_BYTESWAP_H
  113. #include <byteswap.h>
  114. #elif defined(USE_SYS_ENDIAN_H)
  115. #include <sys/endian.h>
  116. #elif defined(__APPLE__)
  117. #include <libkern/OSByteOrder.h>
  118. #define bswap_16 OSSwapInt16
  119. #define bswap_32 OSSwapInt32
  120. #define bswap_64 OSSwapInt64
  121. #else
  122. #define bswap_16(value) \
  123. ((((value) & 0xff) << 8) | ((value) >> 8))
  124. #define bswap_32(value) \
  125. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  126. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  127. #define bswap_64(value) \
  128. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  129. << 32) | \
  130. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  131. #endif
  132. #endif /* !defined(__GLXBYTEORDER_H__) */
  133. /* This assumes htobe32 is a macro in endian.h, and if it doesn't exist, then
  134. * htobe64 also won't exist */
  135. #ifndef htobe32
  136. # if __BYTE_ORDER == __LITTLE_ENDIAN
  137. # define htole16(x) (x)
  138. # define htole32(x) (x)
  139. # define be32toh(x) bswap_32(x)
  140. # define be64toh(x) bswap_64(x)
  141. # define htobe32(x) bswap_32(x)
  142. # define htobe64(x) bswap_64(x)
  143. # elif __BYTE_ORDER == __BIG_ENDIAN
  144. # define htole16(x) bswap_16(x)
  145. # define htole32(x) bswap_32(x)
  146. # define be32toh(x) (x)
  147. # define be64toh(x) (x)
  148. # define htobe32(x) (x)
  149. # define htobe64(x) (x)
  150. #else
  151. #error UNKNOWN BYTE ORDER
  152. #endif
  153. #endif
  154. #undef unlikely
  155. #undef likely
  156. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  157. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  158. #define likely(expr) (__builtin_expect(!!(expr), 1))
  159. #else
  160. #define unlikely(expr) (expr)
  161. #define likely(expr) (expr)
  162. #endif
  163. #define __maybe_unused __attribute__((unused))
  164. #define uninitialised_var(x) x = x
  165. #if defined(__i386__)
  166. #define WANT_CRYPTOPP_ASM32
  167. #endif
  168. #ifndef ARRAY_SIZE
  169. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  170. #endif
  171. #ifdef MIPSEB
  172. #ifndef roundl
  173. #define roundl(x) (long double)((long long)((x==0)?0.0:((x)+((x)>0)?0.5:-0.5)))
  174. #endif
  175. #endif
  176. #define MIN(x, y) ((x) > (y) ? (y) : (x))
  177. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  178. enum drv_driver {
  179. DRIVER_OPENCL = 0,
  180. DRIVER_ICARUS,
  181. DRIVER_BITFORCE,
  182. DRIVER_MODMINER,
  183. DRIVER_ZTEX,
  184. DRIVER_CPU,
  185. DRIVER_BFLSC,
  186. DRIVER_AVALON,
  187. DRIVER_MAX
  188. };
  189. enum alive {
  190. LIFE_WELL,
  191. LIFE_SICK,
  192. LIFE_DEAD,
  193. LIFE_NOSTART,
  194. LIFE_INIT,
  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 targettemp;
  232. int overtemp;
  233. int minspeed;
  234. int maxspeed;
  235. int gpu;
  236. bool has_fanspeed;
  237. struct gpu_adl *twin;
  238. };
  239. #endif
  240. extern void blank_get_statline_before(char *buf, struct cgpu_info __maybe_unused *cgpu);
  241. struct api_data;
  242. struct thr_info;
  243. struct work;
  244. struct device_drv {
  245. enum drv_driver drv_id;
  246. char *dname;
  247. char *name;
  248. // DRV-global functions
  249. void (*drv_detect)();
  250. // Device-specific functions
  251. void (*reinit_device)(struct cgpu_info *);
  252. void (*get_statline_before)(char *, struct cgpu_info *);
  253. void (*get_statline)(char *, struct cgpu_info *);
  254. struct api_data *(*get_api_stats)(struct cgpu_info *);
  255. bool (*get_stats)(struct cgpu_info *);
  256. void (*identify_device)(struct cgpu_info *); // e.g. to flash a led
  257. char *(*set_device)(struct cgpu_info *, char *option, char *setting, char *replybuf);
  258. // Thread-specific functions
  259. bool (*thread_prepare)(struct thr_info *);
  260. uint64_t (*can_limit_work)(struct thr_info *);
  261. bool (*thread_init)(struct thr_info *);
  262. bool (*prepare_work)(struct thr_info *, struct work *);
  263. /* Which hash work loop this driver uses. */
  264. void (*hash_work)(struct thr_info *);
  265. /* Two variants depending on whether the device divides work up into
  266. * small pieces or works with whole work items and may or may not have
  267. * a queue of its own. */
  268. int64_t (*scanhash)(struct thr_info *, struct work *, int64_t);
  269. int64_t (*scanwork)(struct thr_info *);
  270. /* Used to extract work from the hash table of queued work and tell
  271. * the main loop that it should not add any further work to the table.
  272. */
  273. bool (*queue_full)(struct cgpu_info *);
  274. void (*flush_work)(struct cgpu_info *);
  275. void (*hw_error)(struct thr_info *);
  276. void (*thread_shutdown)(struct thr_info *);
  277. void (*thread_enable)(struct thr_info *);
  278. // Does it need to be free()d?
  279. bool copy;
  280. /* Highest target diff the device supports */
  281. double max_diff;
  282. };
  283. extern struct device_drv *copy_drv(struct device_drv*);
  284. enum dev_enable {
  285. DEV_ENABLED,
  286. DEV_DISABLED,
  287. DEV_RECOVER,
  288. };
  289. enum cl_kernels {
  290. KL_NONE,
  291. KL_POCLBM,
  292. KL_PHATK,
  293. KL_DIAKGCN,
  294. KL_DIABLO,
  295. KL_SCRYPT,
  296. };
  297. enum dev_reason {
  298. REASON_THREAD_FAIL_INIT,
  299. REASON_THREAD_ZERO_HASH,
  300. REASON_THREAD_FAIL_QUEUE,
  301. REASON_DEV_SICK_IDLE_60,
  302. REASON_DEV_DEAD_IDLE_600,
  303. REASON_DEV_NOSTART,
  304. REASON_DEV_OVER_HEAT,
  305. REASON_DEV_THERMAL_CUTOFF,
  306. REASON_DEV_COMMS_ERROR,
  307. REASON_DEV_THROTTLE,
  308. };
  309. #define REASON_NONE "None"
  310. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  311. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  312. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  313. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  314. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  315. #define REASON_DEV_NOSTART_STR "Device failed to start"
  316. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  317. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  318. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  319. #define REASON_DEV_THROTTLE_STR "Device throttle"
  320. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  321. #define MIN_SEC_UNSET 99999999
  322. struct cgminer_stats {
  323. uint32_t getwork_calls;
  324. struct timeval getwork_wait;
  325. struct timeval getwork_wait_max;
  326. struct timeval getwork_wait_min;
  327. };
  328. // Just the actual network getworks to the pool
  329. struct cgminer_pool_stats {
  330. uint32_t getwork_calls;
  331. uint32_t getwork_attempts;
  332. struct timeval getwork_wait;
  333. struct timeval getwork_wait_max;
  334. struct timeval getwork_wait_min;
  335. double getwork_wait_rolling;
  336. bool hadrolltime;
  337. bool canroll;
  338. bool hadexpire;
  339. uint32_t rolltime;
  340. double min_diff;
  341. double max_diff;
  342. double last_diff;
  343. uint32_t min_diff_count;
  344. uint32_t max_diff_count;
  345. uint64_t times_sent;
  346. uint64_t bytes_sent;
  347. uint64_t net_bytes_sent;
  348. uint64_t times_received;
  349. uint64_t bytes_received;
  350. uint64_t net_bytes_received;
  351. };
  352. struct cgpu_info {
  353. int cgminer_id;
  354. struct device_drv *drv;
  355. int device_id;
  356. char *name;
  357. char *device_path;
  358. FILE *device_file;
  359. union {
  360. #ifdef USE_ZTEX
  361. struct libztex_device *device_ztex;
  362. #endif
  363. #ifdef USE_USBUTILS
  364. struct cg_usb_device *usbdev;
  365. #endif
  366. #ifdef USE_ICARUS
  367. int device_fd;
  368. #endif
  369. };
  370. #ifdef USE_USBUTILS
  371. struct cg_usb_info usbinfo;
  372. #endif
  373. #ifdef USE_MODMINER
  374. char fpgaid;
  375. unsigned char clock;
  376. pthread_mutex_t *modminer_mutex;
  377. #endif
  378. #ifdef USE_BITFORCE
  379. struct timeval work_start_tv;
  380. unsigned int wait_ms;
  381. unsigned int sleep_ms;
  382. double avg_wait_f;
  383. unsigned int avg_wait_d;
  384. uint32_t nonces;
  385. bool nonce_range;
  386. bool polling;
  387. bool flash_led;
  388. pthread_mutex_t device_mutex;
  389. #endif
  390. enum dev_enable deven;
  391. int accepted;
  392. int rejected;
  393. int hw_errors;
  394. double rolling;
  395. double total_mhashes;
  396. double utility;
  397. enum alive status;
  398. char init[40];
  399. struct timeval last_message_tv;
  400. int threads;
  401. struct thr_info **thr;
  402. int64_t max_hashes;
  403. const char *kname;
  404. #ifdef HAVE_OPENCL
  405. bool mapped;
  406. int virtual_gpu;
  407. int virtual_adl;
  408. int intensity;
  409. bool dynamic;
  410. cl_uint vwidth;
  411. size_t work_size;
  412. enum cl_kernels kernel;
  413. cl_ulong max_alloc;
  414. #ifdef USE_SCRYPT
  415. int opt_lg, lookup_gap;
  416. size_t opt_tc, thread_concurrency;
  417. size_t shaders;
  418. #endif
  419. struct timeval tv_gpustart;
  420. int intervals;
  421. #endif
  422. bool new_work;
  423. float temp;
  424. int cutofftemp;
  425. #ifdef HAVE_ADL
  426. bool has_adl;
  427. struct gpu_adl adl;
  428. int gpu_engine;
  429. int min_engine;
  430. int gpu_fan;
  431. int min_fan;
  432. int gpu_memclock;
  433. int gpu_memdiff;
  434. int gpu_powertune;
  435. float gpu_vddc;
  436. #endif
  437. int diff1;
  438. double diff_accepted;
  439. double diff_rejected;
  440. int last_share_pool;
  441. time_t last_share_pool_time;
  442. double last_share_diff;
  443. time_t last_device_valid_work;
  444. time_t device_last_well;
  445. time_t device_last_not_well;
  446. enum dev_reason device_not_well_reason;
  447. int thread_fail_init_count;
  448. int thread_zero_hash_count;
  449. int thread_fail_queue_count;
  450. int dev_sick_idle_60_count;
  451. int dev_dead_idle_600_count;
  452. int dev_nostart_count;
  453. int dev_over_heat_count; // It's a warning but worth knowing
  454. int dev_thermal_cutoff_count;
  455. int dev_comms_error_count;
  456. int dev_throttle_count;
  457. struct cgminer_stats cgminer_stats;
  458. pthread_rwlock_t qlock;
  459. struct work *queued_work;
  460. unsigned int queued_count;
  461. };
  462. extern bool add_cgpu(struct cgpu_info*);
  463. struct thread_q {
  464. struct list_head q;
  465. bool frozen;
  466. pthread_mutex_t mutex;
  467. pthread_cond_t cond;
  468. };
  469. struct thr_info {
  470. int id;
  471. int device_thread;
  472. bool primary_thread;
  473. pthread_t pth;
  474. struct thread_q *q;
  475. struct cgpu_info *cgpu;
  476. void *cgpu_data;
  477. struct timeval last;
  478. struct timeval sick;
  479. bool pause;
  480. bool getwork;
  481. double rolling;
  482. bool work_restart;
  483. };
  484. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  485. extern void thr_info_cancel(struct thr_info *thr);
  486. extern void thr_info_freeze(struct thr_info *thr);
  487. extern void nmsleep(unsigned int msecs);
  488. extern double us_tdiff(struct timeval *end, struct timeval *start);
  489. extern double tdiff(struct timeval *end, struct timeval *start);
  490. struct string_elist {
  491. char *string;
  492. bool free_me;
  493. struct list_head list;
  494. };
  495. static inline void string_elist_add(const char *s, struct list_head *head)
  496. {
  497. struct string_elist *n;
  498. n = calloc(1, sizeof(*n));
  499. n->string = strdup(s);
  500. n->free_me = true;
  501. list_add_tail(&n->list, head);
  502. }
  503. static inline void string_elist_del(struct string_elist *item)
  504. {
  505. if (item->free_me)
  506. free(item->string);
  507. list_del(&item->list);
  508. }
  509. static inline uint32_t swab32(uint32_t v)
  510. {
  511. return bswap_32(v);
  512. }
  513. static inline void swap256(void *dest_p, const void *src_p)
  514. {
  515. uint32_t *dest = dest_p;
  516. const uint32_t *src = src_p;
  517. dest[0] = src[7];
  518. dest[1] = src[6];
  519. dest[2] = src[5];
  520. dest[3] = src[4];
  521. dest[4] = src[3];
  522. dest[5] = src[2];
  523. dest[6] = src[1];
  524. dest[7] = src[0];
  525. }
  526. static inline void swab256(void *dest_p, const void *src_p)
  527. {
  528. uint32_t *dest = dest_p;
  529. const uint32_t *src = src_p;
  530. dest[0] = swab32(src[7]);
  531. dest[1] = swab32(src[6]);
  532. dest[2] = swab32(src[5]);
  533. dest[3] = swab32(src[4]);
  534. dest[4] = swab32(src[3]);
  535. dest[5] = swab32(src[2]);
  536. dest[6] = swab32(src[1]);
  537. dest[7] = swab32(src[0]);
  538. }
  539. static inline void flip32(void *dest_p, const void *src_p)
  540. {
  541. uint32_t *dest = dest_p;
  542. const uint32_t *src = src_p;
  543. int i;
  544. for (i = 0; i < 8; i++)
  545. dest[i] = swab32(src[i]);
  546. }
  547. static inline void flip80(void *dest_p, const void *src_p)
  548. {
  549. uint32_t *dest = dest_p;
  550. const uint32_t *src = src_p;
  551. int i;
  552. for (i = 0; i < 20; i++)
  553. dest[i] = swab32(src[i]);
  554. }
  555. extern void quit(int status, const char *format, ...);
  556. static inline void mutex_lock(pthread_mutex_t *lock)
  557. {
  558. if (unlikely(pthread_mutex_lock(lock)))
  559. quit(1, "WTF MUTEX ERROR ON LOCK!");
  560. }
  561. static inline void mutex_unlock(pthread_mutex_t *lock)
  562. {
  563. if (unlikely(pthread_mutex_unlock(lock)))
  564. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  565. }
  566. static inline int mutex_trylock(pthread_mutex_t *lock)
  567. {
  568. return pthread_mutex_trylock(lock);
  569. }
  570. static inline void wr_lock(pthread_rwlock_t *lock)
  571. {
  572. if (unlikely(pthread_rwlock_wrlock(lock)))
  573. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  574. }
  575. static inline void rd_lock(pthread_rwlock_t *lock)
  576. {
  577. if (unlikely(pthread_rwlock_rdlock(lock)))
  578. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  579. }
  580. static inline void rw_unlock(pthread_rwlock_t *lock)
  581. {
  582. if (unlikely(pthread_rwlock_unlock(lock)))
  583. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  584. }
  585. static inline void rd_unlock(pthread_rwlock_t *lock)
  586. {
  587. rw_unlock(lock);
  588. }
  589. static inline void wr_unlock(pthread_rwlock_t *lock)
  590. {
  591. rw_unlock(lock);
  592. }
  593. static inline void mutex_init(pthread_mutex_t *lock)
  594. {
  595. if (unlikely(pthread_mutex_init(lock, NULL)))
  596. quit(1, "Failed to pthread_mutex_init");
  597. }
  598. static inline void rwlock_init(pthread_rwlock_t *lock)
  599. {
  600. if (unlikely(pthread_rwlock_init(lock, NULL)))
  601. quit(1, "Failed to pthread_rwlock_init");
  602. }
  603. /* cgminer locks, a write biased variant of rwlocks */
  604. struct cglock {
  605. pthread_mutex_t mutex;
  606. pthread_rwlock_t rwlock;
  607. };
  608. typedef struct cglock cglock_t;
  609. static inline void cglock_init(cglock_t *lock)
  610. {
  611. mutex_init(&lock->mutex);
  612. rwlock_init(&lock->rwlock);
  613. }
  614. /* Read lock variant of cglock */
  615. static inline void cg_rlock(cglock_t *lock)
  616. {
  617. mutex_lock(&lock->mutex);
  618. rd_lock(&lock->rwlock);
  619. mutex_unlock(&lock->mutex);
  620. }
  621. /* Intermediate variant of cglock */
  622. static inline void cg_ilock(cglock_t *lock)
  623. {
  624. mutex_lock(&lock->mutex);
  625. }
  626. /* Upgrade intermediate variant to a write lock */
  627. static inline void cg_ulock(cglock_t *lock)
  628. {
  629. wr_lock(&lock->rwlock);
  630. }
  631. /* Write lock variant of cglock */
  632. static inline void cg_wlock(cglock_t *lock)
  633. {
  634. mutex_lock(&lock->mutex);
  635. wr_lock(&lock->rwlock);
  636. }
  637. /* Downgrade intermediate variant to a read lock */
  638. static inline void cg_dlock(cglock_t *lock)
  639. {
  640. rd_lock(&lock->rwlock);
  641. mutex_unlock(&lock->mutex);
  642. }
  643. static inline void cg_runlock(cglock_t *lock)
  644. {
  645. rd_unlock(&lock->rwlock);
  646. }
  647. static inline void cg_wunlock(cglock_t *lock)
  648. {
  649. wr_unlock(&lock->rwlock);
  650. mutex_unlock(&lock->mutex);
  651. }
  652. struct pool;
  653. extern bool opt_protocol;
  654. extern bool have_longpoll;
  655. extern char *opt_kernel_path;
  656. extern char *opt_socks_proxy;
  657. extern char *cgminer_path;
  658. extern bool opt_fail_only;
  659. extern bool opt_autofan;
  660. extern bool opt_autoengine;
  661. extern bool use_curses;
  662. extern char *opt_api_allow;
  663. extern char *opt_api_groups;
  664. extern char *opt_api_description;
  665. extern int opt_api_port;
  666. extern bool opt_api_listen;
  667. extern bool opt_api_network;
  668. extern bool opt_delaynet;
  669. extern bool opt_restart;
  670. extern char *opt_icarus_options;
  671. extern char *opt_icarus_timing;
  672. extern bool opt_worktime;
  673. #ifdef USE_USBUTILS
  674. extern char *opt_usb_select;
  675. extern int opt_usbdump;
  676. extern bool opt_usb_list_all;
  677. #endif
  678. #ifdef USE_BITFORCE
  679. extern bool opt_bfl_noncerange;
  680. #endif
  681. extern int swork_id;
  682. extern pthread_rwlock_t netacc_lock;
  683. extern const uint32_t sha256_init_state[];
  684. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  685. const char *rpc_req, bool, bool, int *,
  686. struct pool *pool, bool);
  687. extern const char *proxytype(curl_proxytype proxytype);
  688. extern char *get_proxy(char *url, struct pool *pool);
  689. extern char *bin2hex(const unsigned char *p, size_t len);
  690. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  691. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  692. unsigned char *pdata,
  693. unsigned char *phash1, unsigned char *phash,
  694. const unsigned char *ptarget,
  695. uint32_t max_nonce,
  696. uint32_t *last_nonce,
  697. uint32_t nonce);
  698. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  699. extern int opt_queue;
  700. extern int opt_scantime;
  701. extern int opt_expiry;
  702. #ifdef USE_USBUTILS
  703. extern pthread_mutex_t cgusb_lock;
  704. #endif
  705. extern cglock_t control_lock;
  706. extern pthread_mutex_t hash_lock;
  707. extern pthread_mutex_t console_lock;
  708. extern cglock_t ch_lock;
  709. extern pthread_rwlock_t mining_thr_lock;
  710. extern pthread_rwlock_t devices_lock;
  711. extern pthread_mutex_t restart_lock;
  712. extern pthread_cond_t restart_cond;
  713. extern void thread_reportin(struct thr_info *thr);
  714. extern void clear_stratum_shares(struct pool *pool);
  715. extern int restart_wait(unsigned int mstime);
  716. extern void kill_work(void);
  717. extern void reinit_device(struct cgpu_info *cgpu);
  718. #ifdef HAVE_ADL
  719. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  720. extern int set_fanspeed(int gpu, int iFanSpeed);
  721. extern int set_vddc(int gpu, float fVddc);
  722. extern int set_engineclock(int gpu, int iEngineClock);
  723. extern int set_memoryclock(int gpu, int iMemoryClock);
  724. #endif
  725. extern void api(int thr_id);
  726. extern struct pool *current_pool(void);
  727. extern int enabled_pools;
  728. extern bool detect_stratum(struct pool *pool, char *url);
  729. extern void print_summary(void);
  730. extern struct pool *add_pool(void);
  731. extern bool add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  732. #define MAX_GPUDEVICES 16
  733. #define MIN_INTENSITY -10
  734. #define _MIN_INTENSITY_STR "-10"
  735. #ifdef USE_SCRYPT
  736. #define MAX_INTENSITY 20
  737. #define _MAX_INTENSITY_STR "20"
  738. #else
  739. #define MAX_INTENSITY 14
  740. #define _MAX_INTENSITY_STR "14"
  741. #endif
  742. extern bool hotplug_mode;
  743. extern int hotplug_time;
  744. extern struct list_head scan_devices;
  745. extern int nDevs;
  746. extern int opt_n_threads;
  747. extern int num_processors;
  748. extern int hw_errors;
  749. extern bool use_syslog;
  750. extern bool opt_quiet;
  751. extern struct thr_info *control_thr;
  752. extern struct thr_info **mining_thr;
  753. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  754. extern int gpu_threads;
  755. #ifdef USE_SCRYPT
  756. extern bool opt_scrypt;
  757. #else
  758. #define opt_scrypt (0)
  759. #endif
  760. extern double total_secs;
  761. extern int mining_threads;
  762. extern struct cgpu_info *cpus;
  763. extern int total_devices;
  764. extern struct cgpu_info **devices;
  765. extern int total_pools;
  766. extern struct pool **pools;
  767. extern const char *algo_names[];
  768. extern enum sha256_algos opt_algo;
  769. extern struct strategies strategies[];
  770. extern enum pool_strategy pool_strategy;
  771. extern int opt_rotate_period;
  772. extern double total_mhashes_done;
  773. extern unsigned int new_blocks;
  774. extern unsigned int found_blocks;
  775. extern int total_accepted, total_rejected, total_diff1;;
  776. extern int total_getworks, total_stale, total_discarded;
  777. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  778. extern unsigned int local_work;
  779. extern unsigned int total_go, total_ro;
  780. extern const int opt_cutofftemp;
  781. extern int opt_log_interval;
  782. extern unsigned long long global_hashrate;
  783. extern char *current_fullhash;
  784. extern double current_diff;
  785. extern uint64_t best_diff;
  786. extern struct timeval block_timeval;
  787. #ifdef HAVE_OPENCL
  788. typedef struct {
  789. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  790. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  791. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  792. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  793. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  794. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  795. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  796. cl_uint W16; cl_uint W17; cl_uint W2;
  797. cl_uint PreVal4; cl_uint T1;
  798. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  799. cl_uint PreVal4addT1; cl_uint T1substate0;
  800. cl_uint PreVal4_2;
  801. cl_uint PreVal0;
  802. cl_uint PreW18;
  803. cl_uint PreW19;
  804. cl_uint PreW31;
  805. cl_uint PreW32;
  806. /* For diakgcn */
  807. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  808. cl_uint zeroA, zeroB;
  809. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  810. #ifdef USE_SCRYPT
  811. struct work *work;
  812. #endif
  813. } dev_blk_ctx;
  814. #else
  815. typedef struct {
  816. uint32_t nonce;
  817. } dev_blk_ctx;
  818. #endif
  819. struct curl_ent {
  820. CURL *curl;
  821. struct list_head node;
  822. struct timeval tv;
  823. };
  824. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  825. * equal disabled */
  826. enum pool_enable {
  827. POOL_DISABLED,
  828. POOL_ENABLED,
  829. POOL_REJECTING,
  830. };
  831. struct stratum_work {
  832. char *job_id;
  833. char *prev_hash;
  834. char *coinbase1;
  835. char *coinbase2;
  836. char **merkle;
  837. char *bbversion;
  838. char *nbit;
  839. char *ntime;
  840. bool clean;
  841. size_t cb1_len;
  842. size_t cb2_len;
  843. size_t cb_len;
  844. size_t header_len;
  845. int merkles;
  846. double diff;
  847. };
  848. #define RBUFSIZE 8192
  849. #define RECVSIZE (RBUFSIZE - 4)
  850. struct pool {
  851. int pool_no;
  852. int prio;
  853. int accepted, rejected;
  854. int seq_rejects;
  855. int seq_getfails;
  856. int solved;
  857. int diff1;
  858. char diff[8];
  859. double diff_accepted;
  860. double diff_rejected;
  861. double diff_stale;
  862. bool submit_fail;
  863. bool idle;
  864. bool lagging;
  865. bool probed;
  866. enum pool_enable enabled;
  867. bool submit_old;
  868. bool removed;
  869. bool lp_started;
  870. char *hdr_path;
  871. char *lp_url;
  872. unsigned int getwork_requested;
  873. unsigned int stale_shares;
  874. unsigned int discarded_work;
  875. unsigned int getfail_occasions;
  876. unsigned int remotefail_occasions;
  877. struct timeval tv_idle;
  878. double utility;
  879. int last_shares, shares;
  880. char *rpc_req;
  881. char *rpc_url;
  882. char *rpc_userpass;
  883. char *rpc_user, *rpc_pass;
  884. curl_proxytype rpc_proxytype;
  885. char *rpc_proxy;
  886. pthread_mutex_t pool_lock;
  887. cglock_t data_lock;
  888. struct thread_q *submit_q;
  889. struct thread_q *getwork_q;
  890. pthread_t longpoll_thread;
  891. pthread_t test_thread;
  892. bool testing;
  893. int curls;
  894. pthread_cond_t cr_cond;
  895. struct list_head curlring;
  896. time_t last_share_time;
  897. double last_share_diff;
  898. uint64_t best_diff;
  899. struct cgminer_stats cgminer_stats;
  900. struct cgminer_pool_stats cgminer_pool_stats;
  901. /* Stratum variables */
  902. char *stratum_url;
  903. char *stratum_port;
  904. CURL *stratum_curl;
  905. SOCKETTYPE sock;
  906. char *sockbuf;
  907. size_t sockbuf_size;
  908. char *sockaddr_url; /* stripped url used for sockaddr */
  909. char *nonce1;
  910. size_t n1_len;
  911. uint32_t nonce2;
  912. int n2size;
  913. char *sessionid;
  914. bool has_stratum;
  915. bool stratum_active;
  916. bool stratum_init;
  917. bool stratum_notify;
  918. struct stratum_work swork;
  919. pthread_t stratum_thread;
  920. pthread_mutex_t stratum_lock;
  921. int sshares; /* stratum shares submitted waiting on response */
  922. /* GBT variables */
  923. bool has_gbt;
  924. cglock_t gbt_lock;
  925. unsigned char previousblockhash[32];
  926. unsigned char gbt_target[32];
  927. char *coinbasetxn;
  928. char *longpollid;
  929. char *gbt_workid;
  930. int gbt_expires;
  931. uint32_t gbt_version;
  932. uint32_t curtime;
  933. uint32_t gbt_bits;
  934. unsigned char *gbt_coinbase;
  935. unsigned char *txn_hashes;
  936. int gbt_txns;
  937. int coinbase_len;
  938. struct timeval tv_lastwork;
  939. };
  940. #define GETWORK_MODE_TESTPOOL 'T'
  941. #define GETWORK_MODE_POOL 'P'
  942. #define GETWORK_MODE_LP 'L'
  943. #define GETWORK_MODE_BENCHMARK 'B'
  944. #define GETWORK_MODE_STRATUM 'S'
  945. #define GETWORK_MODE_GBT 'G'
  946. struct work {
  947. unsigned char data[128];
  948. unsigned char midstate[32];
  949. unsigned char target[32];
  950. unsigned char hash[32];
  951. double device_diff;
  952. uint64_t share_diff;
  953. int rolls;
  954. dev_blk_ctx blk;
  955. struct thr_info *thr;
  956. int thr_id;
  957. struct pool *pool;
  958. struct timeval tv_staged;
  959. bool mined;
  960. bool clone;
  961. bool cloned;
  962. int rolltime;
  963. bool longpoll;
  964. bool stale;
  965. bool mandatory;
  966. bool block;
  967. bool queued;
  968. bool stratum;
  969. char *job_id;
  970. char *nonce2;
  971. char *ntime;
  972. double sdiff;
  973. char *nonce1;
  974. bool gbt;
  975. char *gbt_coinbase;
  976. int gbt_txns;
  977. unsigned int work_block;
  978. int id;
  979. UT_hash_handle hh;
  980. double work_difficulty;
  981. // Allow devices to identify work if multiple sub-devices
  982. int subid;
  983. // Allow devices to flag work for their own purposes
  984. bool devflag;
  985. struct timeval tv_getwork;
  986. struct timeval tv_getwork_reply;
  987. struct timeval tv_cloned;
  988. struct timeval tv_work_start;
  989. struct timeval tv_work_found;
  990. char getwork_mode;
  991. };
  992. #ifdef USE_MODMINER
  993. struct modminer_fpga_state {
  994. bool work_running;
  995. struct work running_work;
  996. struct timeval tv_workstart;
  997. uint32_t hashes;
  998. char next_work_cmd[46];
  999. char fpgaid;
  1000. bool overheated;
  1001. bool new_work;
  1002. uint32_t shares;
  1003. uint32_t shares_last_hw;
  1004. uint32_t hw_errors;
  1005. uint32_t shares_to_good;
  1006. uint32_t timeout_fail;
  1007. uint32_t success_more;
  1008. struct timeval last_changed;
  1009. struct timeval last_nonce;
  1010. struct timeval first_work;
  1011. bool death_stage_one;
  1012. bool tried_two_byte_temp;
  1013. bool one_byte_temp;
  1014. };
  1015. #endif
  1016. extern void get_datestamp(char *, struct timeval *);
  1017. extern void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  1018. extern struct work *get_queued(struct cgpu_info *cgpu);
  1019. extern struct work *__find_work_bymidstate(struct work *que, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1020. extern struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1021. extern void work_completed(struct cgpu_info *cgpu, struct work *work);
  1022. extern void hash_queued_work(struct thr_info *mythr);
  1023. extern void tailsprintf(char *f, const char *fmt, ...);
  1024. extern void wlogprint(const char *f, ...);
  1025. extern int curses_int(const char *query);
  1026. extern char *curses_input(const char *query);
  1027. extern void kill_work(void);
  1028. extern void switch_pools(struct pool *selected);
  1029. extern void discard_work(struct work *work);
  1030. extern void remove_pool(struct pool *pool);
  1031. extern void write_config(FILE *fcfg);
  1032. extern void zero_bestshare(void);
  1033. extern void zero_stats(void);
  1034. extern void default_save_file(char *filename);
  1035. extern bool log_curses_only(int prio, const char *f, va_list ap);
  1036. extern void clear_logwin(void);
  1037. extern bool pool_tclear(struct pool *pool, bool *var);
  1038. extern struct thread_q *tq_new(void);
  1039. extern void tq_free(struct thread_q *tq);
  1040. extern bool tq_push(struct thread_q *tq, void *data);
  1041. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  1042. extern void tq_freeze(struct thread_q *tq);
  1043. extern void tq_thaw(struct thread_q *tq);
  1044. extern bool successful_connect;
  1045. extern void adl(void);
  1046. extern void app_restart(void);
  1047. extern void clean_work(struct work *work);
  1048. extern void free_work(struct work *work);
  1049. extern void __copy_work(struct work *work, struct work *base_work);
  1050. extern struct work *copy_work(struct work *base_work);
  1051. extern struct thr_info *get_thread(int thr_id);
  1052. extern struct cgpu_info *get_devices(int id);
  1053. enum api_data_type {
  1054. API_ESCAPE,
  1055. API_STRING,
  1056. API_CONST,
  1057. API_INT,
  1058. API_UINT,
  1059. API_UINT32,
  1060. API_UINT64,
  1061. API_DOUBLE,
  1062. API_ELAPSED,
  1063. API_BOOL,
  1064. API_TIMEVAL,
  1065. API_TIME,
  1066. API_MHS,
  1067. API_MHTOTAL,
  1068. API_TEMP,
  1069. API_UTILITY,
  1070. API_FREQ,
  1071. API_VOLTS,
  1072. API_HS,
  1073. API_DIFF
  1074. };
  1075. struct api_data {
  1076. enum api_data_type type;
  1077. char *name;
  1078. void *data;
  1079. bool data_was_malloc;
  1080. struct api_data *prev;
  1081. struct api_data *next;
  1082. };
  1083. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  1084. extern struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data);
  1085. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  1086. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  1087. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  1088. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  1089. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  1090. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  1091. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  1092. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  1093. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  1094. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  1095. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  1096. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  1097. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  1098. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  1099. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  1100. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  1101. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  1102. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  1103. #endif /* __MINER_H__ */