miner.h 30 KB

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