miner.h 27 KB

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