miner.h 28 KB

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