miner.h 28 KB

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