miner.h 28 KB

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