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