miner.h 29 KB

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