miner.h 27 KB

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