miner.h 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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 times_received;
  342. uint64_t bytes_received;
  343. };
  344. struct cgpu_info {
  345. int cgminer_id;
  346. const struct device_api *api;
  347. const char *devtype;
  348. int device_id;
  349. const char *name;
  350. const char *device_path;
  351. FILE *device_file;
  352. union {
  353. #ifdef USE_ZTEX
  354. struct libztex_device *device_ztex;
  355. #endif
  356. int device_fd;
  357. #ifdef USE_X6500
  358. struct ft232r_device_handle *device_ft232r;
  359. #endif
  360. };
  361. #ifdef USE_BITFORCE
  362. struct timeval work_start_tv;
  363. unsigned int wait_ms;
  364. unsigned int sleep_ms;
  365. double avg_wait_f;
  366. unsigned int avg_wait_d;
  367. uint32_t nonces;
  368. bool nonce_range;
  369. bool polling;
  370. #endif
  371. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  372. bool flash_led;
  373. #endif
  374. void *cgpu_data;
  375. pthread_mutex_t device_mutex;
  376. enum dev_enable deven;
  377. int accepted;
  378. int rejected;
  379. int hw_errors;
  380. double rolling;
  381. double total_mhashes;
  382. double utility;
  383. double utility_diff1;
  384. enum alive status;
  385. char init[40];
  386. struct timeval last_message_tv;
  387. int threads;
  388. struct thr_info **thr;
  389. int64_t max_hashes;
  390. const char *kname;
  391. #ifdef HAVE_OPENCL
  392. bool mapped;
  393. int virtual_gpu;
  394. int virtual_adl;
  395. int intensity;
  396. bool dynamic;
  397. cl_uint vwidth;
  398. size_t work_size;
  399. enum cl_kernels kernel;
  400. cl_ulong max_alloc;
  401. #ifdef USE_SCRYPT
  402. int opt_lg, lookup_gap;
  403. size_t opt_tc, thread_concurrency;
  404. size_t shaders;
  405. #endif
  406. struct timeval tv_gpustart;
  407. int intervals;
  408. #endif
  409. bool new_work;
  410. float temp;
  411. int cutofftemp;
  412. int targettemp;
  413. #ifdef HAVE_ADL
  414. bool has_adl;
  415. struct gpu_adl adl;
  416. int gpu_engine;
  417. int min_engine;
  418. int gpu_fan;
  419. int min_fan;
  420. int gpu_memclock;
  421. int gpu_memdiff;
  422. int gpu_powertune;
  423. float gpu_vddc;
  424. #endif
  425. int diff1;
  426. double diff_accepted;
  427. double diff_rejected;
  428. int last_share_pool;
  429. time_t last_share_pool_time;
  430. double last_share_diff;
  431. time_t device_last_well;
  432. time_t device_last_not_well;
  433. enum dev_reason device_not_well_reason;
  434. float reinit_backoff;
  435. int thread_fail_init_count;
  436. int thread_zero_hash_count;
  437. int thread_fail_queue_count;
  438. int dev_sick_idle_60_count;
  439. int dev_dead_idle_600_count;
  440. int dev_nostart_count;
  441. int dev_over_heat_count; // It's a warning but worth knowing
  442. int dev_thermal_cutoff_count;
  443. int dev_comms_error_count;
  444. int dev_throttle_count;
  445. struct cgminer_stats cgminer_stats;
  446. };
  447. extern void renumber_cgpu(struct cgpu_info *);
  448. extern bool add_cgpu(struct cgpu_info*);
  449. struct thread_q {
  450. struct list_head q;
  451. bool frozen;
  452. pthread_mutex_t mutex;
  453. pthread_cond_t cond;
  454. };
  455. struct thr_info {
  456. int id;
  457. int device_thread;
  458. bool primary_thread;
  459. pthread_t pth;
  460. struct thread_q *q;
  461. struct cgpu_info *cgpu;
  462. void *cgpu_data;
  463. struct timeval last;
  464. struct timeval sick;
  465. bool pause;
  466. time_t getwork;
  467. double rolling;
  468. bool work_restart;
  469. int work_restart_fd;
  470. int _work_restart_fd_w;
  471. };
  472. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  473. extern void thr_info_cancel(struct thr_info *thr);
  474. extern void thr_info_freeze(struct thr_info *thr);
  475. extern void nmsleep(unsigned int msecs);
  476. extern double us_tdiff(struct timeval *end, struct timeval *start);
  477. extern double tdiff(struct timeval *end, struct timeval *start);
  478. struct string_elist {
  479. char *string;
  480. bool free_me;
  481. struct list_head list;
  482. };
  483. static inline void string_elist_add(const char *s, struct list_head *head)
  484. {
  485. struct string_elist *n;
  486. n = calloc(1, sizeof(*n));
  487. n->string = strdup(s);
  488. n->free_me = true;
  489. list_add_tail(&n->list, head);
  490. }
  491. static inline void string_elist_del(struct string_elist *item)
  492. {
  493. if (item->free_me)
  494. free(item->string);
  495. list_del(&item->list);
  496. free(item);
  497. }
  498. static inline uint32_t swab32(uint32_t v)
  499. {
  500. return bswap_32(v);
  501. }
  502. static inline void swap256(void *dest_p, const void *src_p)
  503. {
  504. uint32_t *dest = dest_p;
  505. const uint32_t *src = src_p;
  506. dest[0] = src[7];
  507. dest[1] = src[6];
  508. dest[2] = src[5];
  509. dest[3] = src[4];
  510. dest[4] = src[3];
  511. dest[5] = src[2];
  512. dest[6] = src[1];
  513. dest[7] = src[0];
  514. }
  515. static inline void swap32yes(void*out, const void*in, size_t sz) {
  516. size_t swapcounter = 0;
  517. for (swapcounter = 0; swapcounter < sz; ++swapcounter)
  518. (((uint32_t*)out)[swapcounter]) = swab32(((uint32_t*)in)[swapcounter]);
  519. }
  520. #ifdef WORDS_BIGENDIAN
  521. # define swap32tobe(out, in, sz) (void)0
  522. # define swap32tole(out, in, sz) swap32yes(out, in, sz)
  523. #else
  524. # define swap32tobe(out, in, sz) swap32yes(out, in, sz)
  525. # define swap32tole(out, in, sz) (void)0
  526. #endif
  527. static inline void swab256(void *dest_p, const void *src_p)
  528. {
  529. uint32_t *dest = dest_p;
  530. const uint32_t *src = src_p;
  531. dest[0] = swab32(src[7]);
  532. dest[1] = swab32(src[6]);
  533. dest[2] = swab32(src[5]);
  534. dest[3] = swab32(src[4]);
  535. dest[4] = swab32(src[3]);
  536. dest[5] = swab32(src[2]);
  537. dest[6] = swab32(src[1]);
  538. dest[7] = swab32(src[0]);
  539. }
  540. extern void quit(int status, const char *format, ...);
  541. static inline void mutex_lock(pthread_mutex_t *lock)
  542. {
  543. if (unlikely(pthread_mutex_lock(lock)))
  544. quit(1, "WTF MUTEX ERROR ON LOCK!");
  545. }
  546. static inline void mutex_unlock(pthread_mutex_t *lock)
  547. {
  548. if (unlikely(pthread_mutex_unlock(lock)))
  549. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  550. }
  551. static inline int mutex_trylock(pthread_mutex_t *lock)
  552. {
  553. return pthread_mutex_trylock(lock);
  554. }
  555. static inline void wr_lock(pthread_rwlock_t *lock)
  556. {
  557. if (unlikely(pthread_rwlock_wrlock(lock)))
  558. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  559. }
  560. static inline void rd_lock(pthread_rwlock_t *lock)
  561. {
  562. if (unlikely(pthread_rwlock_rdlock(lock)))
  563. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  564. }
  565. static inline void rw_unlock(pthread_rwlock_t *lock)
  566. {
  567. if (unlikely(pthread_rwlock_unlock(lock)))
  568. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  569. }
  570. static inline void rd_unlock(pthread_rwlock_t *lock)
  571. {
  572. rw_unlock(lock);
  573. }
  574. static inline void wr_unlock(pthread_rwlock_t *lock)
  575. {
  576. rw_unlock(lock);
  577. }
  578. static inline void mutex_init(pthread_mutex_t *lock)
  579. {
  580. if (unlikely(pthread_mutex_init(lock, NULL)))
  581. quit(1, "Failed to pthread_mutex_init");
  582. }
  583. static inline void rwlock_init(pthread_rwlock_t *lock)
  584. {
  585. if (unlikely(pthread_rwlock_init(lock, NULL)))
  586. quit(1, "Failed to pthread_rwlock_init");
  587. }
  588. struct pool;
  589. extern bool opt_protocol;
  590. extern char *opt_coinbase_sig;
  591. extern bool have_longpoll;
  592. extern int opt_skip_checks;
  593. extern char *opt_kernel_path;
  594. extern char *opt_socks_proxy;
  595. extern char *cgminer_path;
  596. extern bool opt_fail_only;
  597. extern bool opt_autofan;
  598. extern bool opt_autoengine;
  599. extern bool use_curses;
  600. extern char *opt_api_allow;
  601. extern char *opt_api_groups;
  602. extern char *opt_api_description;
  603. extern int opt_api_port;
  604. extern bool opt_api_listen;
  605. extern bool opt_api_network;
  606. extern bool opt_delaynet;
  607. extern bool opt_restart;
  608. extern char *opt_icarus_options;
  609. extern char *opt_icarus_timing;
  610. extern bool opt_worktime;
  611. #ifdef USE_BITFORCE
  612. extern bool opt_bfl_noncerange;
  613. #endif
  614. extern int swork_id;
  615. extern pthread_rwlock_t netacc_lock;
  616. extern const uint32_t sha256_init_state[];
  617. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  618. const char *rpc_req, bool, bool, int *,
  619. struct pool *pool, bool);
  620. extern bool our_curl_supports_proxy_uris();
  621. extern char *bin2hex(const unsigned char *p, size_t len);
  622. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  623. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  624. unsigned char *pdata,
  625. unsigned char *phash1, unsigned char *phash,
  626. const unsigned char *ptarget,
  627. uint32_t max_nonce,
  628. uint32_t *last_nonce,
  629. uint32_t nonce);
  630. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  631. extern int opt_queue;
  632. extern int opt_scantime;
  633. extern int opt_expiry;
  634. extern pthread_mutex_t hash_lock;
  635. extern pthread_mutex_t console_lock;
  636. extern pthread_mutex_t ch_lock;
  637. extern pthread_mutex_t restart_lock;
  638. extern pthread_cond_t restart_cond;
  639. extern void thread_reportin(struct thr_info *thr);
  640. extern int restart_wait(unsigned int mstime);
  641. extern int stale_wait(unsigned int mstime, struct work*, bool checkend);
  642. extern void kill_work(void);
  643. extern void app_restart(void);
  644. extern void reinit_device(struct cgpu_info *cgpu);
  645. #ifdef HAVE_ADL
  646. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  647. extern int set_fanspeed(int gpu, int iFanSpeed);
  648. extern int set_vddc(int gpu, float fVddc);
  649. extern int set_engineclock(int gpu, int iEngineClock);
  650. extern int set_memoryclock(int gpu, int iMemoryClock);
  651. #endif
  652. extern void api(int thr_id);
  653. extern struct pool *current_pool(void);
  654. extern int enabled_pools;
  655. extern bool detect_stratum(struct pool *pool, char *url);
  656. extern struct pool *add_pool(void);
  657. extern void add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  658. #define MAX_GPUDEVICES 16
  659. #define MIN_INTENSITY -10
  660. #define _MIN_INTENSITY_STR "-10"
  661. #ifdef USE_SCRYPT
  662. #define MAX_INTENSITY 20
  663. #define _MAX_INTENSITY_STR "20"
  664. #else
  665. #define MAX_INTENSITY 14
  666. #define _MAX_INTENSITY_STR "14"
  667. #endif
  668. extern struct list_head scan_devices;
  669. extern bool opt_force_dev_init;
  670. extern int nDevs;
  671. extern int opt_n_threads;
  672. extern int num_processors;
  673. extern int hw_errors;
  674. extern bool use_syslog;
  675. extern bool opt_quiet;
  676. extern struct thr_info *thr_info;
  677. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  678. extern int gpu_threads;
  679. #ifdef USE_SCRYPT
  680. extern bool opt_scrypt;
  681. #else
  682. #define opt_scrypt (0)
  683. #endif
  684. extern double total_secs;
  685. extern int mining_threads;
  686. extern struct cgpu_info *cpus;
  687. extern int total_devices;
  688. extern struct cgpu_info **devices;
  689. extern int total_pools;
  690. extern struct pool **pools;
  691. extern const char *algo_names[];
  692. extern enum sha256_algos opt_algo;
  693. extern struct strategies strategies[];
  694. extern enum pool_strategy pool_strategy;
  695. extern int opt_rotate_period;
  696. extern double total_mhashes_done;
  697. extern unsigned int new_blocks;
  698. extern unsigned int found_blocks;
  699. extern int total_accepted, total_rejected, total_diff1;;
  700. extern int total_getworks, total_stale, total_discarded;
  701. extern uint64_t total_bytes_xfer;
  702. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  703. extern unsigned int local_work;
  704. extern unsigned int total_go, total_ro;
  705. extern const int opt_cutofftemp;
  706. extern int opt_hysteresis;
  707. extern int opt_fail_pause;
  708. extern int opt_log_interval;
  709. extern unsigned long long global_hashrate;
  710. extern char *current_fullhash;
  711. extern uint64_t best_diff;
  712. extern struct timeval block_timeval;
  713. #ifdef HAVE_OPENCL
  714. typedef struct {
  715. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  716. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  717. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  718. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  719. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  720. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  721. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  722. cl_uint W16; cl_uint W17; cl_uint W2;
  723. cl_uint PreVal4; cl_uint T1;
  724. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  725. cl_uint PreVal4addT1; cl_uint T1substate0;
  726. cl_uint PreVal4_2;
  727. cl_uint PreVal0;
  728. cl_uint PreW18;
  729. cl_uint PreW19;
  730. cl_uint PreW31;
  731. cl_uint PreW32;
  732. /* For diakgcn */
  733. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  734. cl_uint zeroA, zeroB;
  735. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  736. #ifdef USE_SCRYPT
  737. struct work *work;
  738. #endif
  739. } dev_blk_ctx;
  740. #else
  741. typedef struct {
  742. uint32_t nonce;
  743. } dev_blk_ctx;
  744. #endif
  745. struct curl_ent {
  746. CURL *curl;
  747. struct list_head node;
  748. struct timeval tv;
  749. };
  750. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  751. * equal disabled */
  752. enum pool_enable {
  753. POOL_DISABLED,
  754. POOL_ENABLED,
  755. POOL_REJECTING,
  756. };
  757. enum pool_protocol {
  758. PLP_NONE,
  759. PLP_GETWORK,
  760. PLP_GETBLOCKTEMPLATE,
  761. };
  762. struct stratum_work {
  763. char *job_id;
  764. char *prev_hash;
  765. char *coinbase1;
  766. char *coinbase2;
  767. char **merkle;
  768. char *bbversion;
  769. char *nbit;
  770. char *ntime;
  771. bool clean;
  772. int merkles;
  773. double diff;
  774. bool transparency_probed;
  775. time_t transparency_time;
  776. bool opaque;
  777. };
  778. #define RECVSIZE 8192
  779. #define RBUFSIZE (RECVSIZE + 4)
  780. struct data_buffer {
  781. void *buf;
  782. size_t len;
  783. curl_socket_t *idlemarker;
  784. };
  785. struct pool {
  786. int pool_no;
  787. int prio;
  788. int accepted, rejected;
  789. int seq_rejects;
  790. int seq_getfails;
  791. int solved;
  792. int diff1;
  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. struct cgminer_stats cgminer_stats;
  837. struct cgminer_pool_stats cgminer_pool_stats;
  838. /* Stratum variables */
  839. char *stratum_url;
  840. char *stratum_port;
  841. CURL *stratum_curl;
  842. SOCKETTYPE sock;
  843. struct data_buffer readbuf;
  844. char *sockaddr_url; /* stripped url used for sockaddr */
  845. char *nonce1;
  846. uint32_t nonce2;
  847. int n2size;
  848. bool has_stratum;
  849. bool stratum_active;
  850. time_t last_work_time; /* only set for Stratum right now */
  851. bool stratum_auth;
  852. struct stratum_work swork;
  853. pthread_t stratum_thread;
  854. pthread_mutex_t stratum_lock;
  855. pthread_mutex_t last_work_lock;
  856. struct work *last_work_copy;
  857. };
  858. #define GETWORK_MODE_TESTPOOL 'T'
  859. #define GETWORK_MODE_POOL 'P'
  860. #define GETWORK_MODE_LP 'L'
  861. #define GETWORK_MODE_BENCHMARK 'B'
  862. #define GETWORK_MODE_STRATUM 'S'
  863. #define GETWORK_MODE_GBT 'G'
  864. struct work {
  865. unsigned char data[128];
  866. unsigned char midstate[32];
  867. unsigned char target[32];
  868. unsigned char hash[32];
  869. uint64_t outputhash;
  870. int rolls;
  871. dev_blk_ctx blk;
  872. struct thr_info *thr;
  873. int thr_id;
  874. struct pool *pool;
  875. struct timeval tv_staged;
  876. bool mined;
  877. bool clone;
  878. bool cloned;
  879. int rolltime;
  880. bool longpoll;
  881. bool stale;
  882. bool mandatory;
  883. bool block;
  884. bool queued;
  885. bool stratum;
  886. char *job_id;
  887. char *nonce2;
  888. char *ntime;
  889. double sdiff;
  890. unsigned char work_restart_id;
  891. int id;
  892. UT_hash_handle hh;
  893. double work_difficulty;
  894. blktemplate_t *tmpl;
  895. int *tmpl_refcount;
  896. unsigned int dataid;
  897. struct timeval tv_getwork;
  898. struct timeval tv_getwork_reply;
  899. struct timeval tv_cloned;
  900. struct timeval tv_work_start;
  901. struct timeval tv_work_found;
  902. char getwork_mode;
  903. /* Used to queue shares in submit_waiting */
  904. struct list_head list;
  905. };
  906. extern void get_datestamp(char *, struct timeval *);
  907. enum test_nonce2_result {
  908. TNR_GOOD,
  909. TNR_HIGH,
  910. TNR_BAD,
  911. };
  912. extern enum test_nonce2_result _test_nonce2(struct work *, uint32_t nonce, bool checktarget);
  913. #define test_nonce(work, nonce, checktarget) (_test_nonce2(work, nonce, checktarget) == TNR_GOOD)
  914. #define test_nonce2(work, nonce) (_test_nonce2(work, nonce, true))
  915. extern void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  916. extern void tailsprintf(char *f, const char *fmt, ...);
  917. extern void wlogprint(const char *f, ...);
  918. extern int curses_int(const char *query);
  919. extern char *curses_input(const char *query);
  920. extern void kill_work(void);
  921. extern int prioritize_pools(char *param, int *pid);
  922. extern void validate_pool_priorities(void);
  923. extern void switch_pools(struct pool *selected);
  924. extern void remove_pool(struct pool *pool);
  925. extern void write_config(FILE *fcfg);
  926. extern void default_save_file(char *filename);
  927. extern bool log_curses_only(int prio, const char *f, va_list ap) FORMAT_SYNTAX_CHECK(printf, 2, 0);
  928. extern void clear_logwin(void);
  929. extern bool pool_tclear(struct pool *pool, bool *var);
  930. extern struct thread_q *tq_new(void);
  931. extern void tq_free(struct thread_q *tq);
  932. extern bool tq_push(struct thread_q *tq, void *data);
  933. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  934. extern void tq_freeze(struct thread_q *tq);
  935. extern void tq_thaw(struct thread_q *tq);
  936. extern bool successful_connect;
  937. extern void adl(void);
  938. extern void clean_work(struct work *work);
  939. extern void free_work(struct work *work);
  940. extern void __copy_work(struct work *work, struct work *base_work);
  941. extern struct work *copy_work(struct work *base_work);
  942. enum api_data_type {
  943. API_ESCAPE,
  944. API_STRING,
  945. API_CONST,
  946. API_INT,
  947. API_UINT,
  948. API_UINT32,
  949. API_UINT64,
  950. API_DOUBLE,
  951. API_ELAPSED,
  952. API_BOOL,
  953. API_TIMEVAL,
  954. API_TIME,
  955. API_MHS,
  956. API_MHTOTAL,
  957. API_TEMP,
  958. API_UTILITY,
  959. API_FREQ,
  960. API_VOLTS,
  961. API_HS,
  962. API_DIFF,
  963. API_JSON,
  964. };
  965. struct api_data {
  966. enum api_data_type type;
  967. char *name;
  968. void *data;
  969. bool data_was_malloc;
  970. struct api_data *prev;
  971. struct api_data *next;
  972. };
  973. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  974. extern struct api_data *api_add_string(struct api_data *root, char *name, const char *data, bool copy_data);
  975. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  976. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  977. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  978. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  979. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  980. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  981. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  982. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  983. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  984. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  985. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  986. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  987. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  988. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  989. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  990. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  991. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  992. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  993. extern struct api_data *api_add_json(struct api_data *root, char *name, json_t *data, bool copy_data);
  994. #endif /* __MINER_H__ */