miner.h 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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 (*get_dev_statline_before)(char *, struct cgpu_info *);
  232. void (*get_dev_statline_after)(char *, struct cgpu_info *);
  233. // Processor-specific functions
  234. void (*reinit_device)(struct cgpu_info *);
  235. void (*get_statline_before)(char *, struct cgpu_info *);
  236. void (*get_statline)(char *, struct cgpu_info *);
  237. struct api_data* (*get_api_extra_device_detail)(struct cgpu_info *);
  238. struct api_data* (*get_api_extra_device_status)(struct cgpu_info *);
  239. struct api_data *(*get_api_stats)(struct cgpu_info *);
  240. bool (*get_stats)(struct cgpu_info *);
  241. bool (*identify_device)(struct cgpu_info *); // e.g. to flash a led
  242. char *(*set_device)(struct cgpu_info *, char *option, char *setting, char *replybuf);
  243. // Thread-specific functions
  244. bool (*thread_prepare)(struct thr_info *);
  245. uint64_t (*can_limit_work)(struct thr_info *);
  246. bool (*thread_init)(struct thr_info *);
  247. bool (*prepare_work)(struct thr_info *, struct work *);
  248. int64_t (*scanhash)(struct thr_info *, struct work *, int64_t);
  249. void (*hw_error)(struct thr_info *);
  250. void (*thread_shutdown)(struct thr_info *);
  251. void (*thread_enable)(struct thr_info *);
  252. };
  253. enum dev_enable {
  254. DEV_ENABLED,
  255. DEV_DISABLED,
  256. DEV_RECOVER,
  257. DEV_RECOVER_ERR,
  258. };
  259. enum cl_kernels {
  260. KL_NONE,
  261. KL_POCLBM,
  262. KL_PHATK,
  263. KL_DIAKGCN,
  264. KL_DIABLO,
  265. KL_SCRYPT,
  266. };
  267. enum dev_reason {
  268. REASON_THREAD_FAIL_INIT,
  269. REASON_THREAD_ZERO_HASH,
  270. REASON_THREAD_FAIL_QUEUE,
  271. REASON_DEV_SICK_IDLE_60,
  272. REASON_DEV_DEAD_IDLE_600,
  273. REASON_DEV_NOSTART,
  274. REASON_DEV_OVER_HEAT,
  275. REASON_DEV_THERMAL_CUTOFF,
  276. REASON_DEV_COMMS_ERROR,
  277. REASON_DEV_THROTTLE,
  278. };
  279. #define REASON_NONE "None"
  280. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  281. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  282. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  283. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  284. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  285. #define REASON_DEV_NOSTART_STR "Device failed to start"
  286. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  287. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  288. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  289. #define REASON_DEV_THROTTLE_STR "Device throttle"
  290. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  291. #define MIN_SEC_UNSET 99999999
  292. enum {
  293. MSG_NOPOOL = 8,
  294. MSG_MISPID = 25,
  295. MSG_INVPID = 26,
  296. MSG_DUPPID = 74,
  297. MSG_POOLPRIO = 73,
  298. };
  299. struct cgminer_stats {
  300. uint32_t getwork_calls;
  301. struct timeval getwork_wait;
  302. struct timeval getwork_wait_max;
  303. struct timeval getwork_wait_min;
  304. };
  305. // Just the actual network getworks to the pool
  306. struct cgminer_pool_stats {
  307. uint32_t getwork_calls;
  308. uint32_t getwork_attempts;
  309. struct timeval getwork_wait;
  310. struct timeval getwork_wait_max;
  311. struct timeval getwork_wait_min;
  312. double getwork_wait_rolling;
  313. bool hadrolltime;
  314. bool canroll;
  315. bool hadexpire;
  316. uint32_t rolltime;
  317. double min_diff;
  318. double max_diff;
  319. double last_diff;
  320. uint32_t min_diff_count;
  321. uint32_t max_diff_count;
  322. uint64_t times_sent;
  323. uint64_t bytes_sent;
  324. uint64_t net_bytes_sent;
  325. uint64_t times_received;
  326. uint64_t bytes_received;
  327. uint64_t net_bytes_received;
  328. };
  329. #define PRIprepr "-6s"
  330. #define PRIpreprv "s"
  331. struct cgpu_info {
  332. int cgminer_id;
  333. int device_line_id;
  334. const struct device_api *api;
  335. const char *devtype;
  336. int device_id;
  337. char *dev_repr;
  338. char *dev_repr_ns;
  339. const char *name;
  340. int procs;
  341. int proc_id;
  342. char proc_repr[8];
  343. char proc_repr_ns[8];
  344. struct cgpu_info *device;
  345. struct cgpu_info *next_proc;
  346. const char *device_path;
  347. FILE *device_file;
  348. union {
  349. #ifdef USE_ZTEX
  350. struct libztex_device *device_ztex;
  351. #endif
  352. int device_fd;
  353. #ifdef USE_X6500
  354. struct ft232r_device_handle *device_ft232r;
  355. #endif
  356. };
  357. #ifdef USE_BITFORCE
  358. struct timeval work_start_tv;
  359. unsigned int wait_ms;
  360. unsigned int sleep_ms;
  361. double avg_wait_f;
  362. unsigned int avg_wait_d;
  363. uint32_t nonces;
  364. bool nonce_range;
  365. bool polling;
  366. #endif
  367. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  368. bool flash_led;
  369. #endif
  370. void *cgpu_data;
  371. pthread_mutex_t device_mutex;
  372. enum dev_enable deven;
  373. int accepted;
  374. int rejected;
  375. int hw_errors;
  376. double rolling;
  377. double total_mhashes;
  378. double utility;
  379. double utility_diff1;
  380. enum alive status;
  381. char init[40];
  382. struct timeval last_message_tv;
  383. int threads;
  384. struct thr_info **thr;
  385. int64_t max_hashes;
  386. const char *kname;
  387. #ifdef HAVE_OPENCL
  388. bool mapped;
  389. int virtual_gpu;
  390. int virtual_adl;
  391. int intensity;
  392. bool dynamic;
  393. cl_uint vwidth;
  394. size_t work_size;
  395. enum cl_kernels kernel;
  396. cl_ulong max_alloc;
  397. #ifdef USE_SCRYPT
  398. int opt_lg, lookup_gap;
  399. size_t opt_tc, thread_concurrency;
  400. size_t shaders;
  401. #endif
  402. struct timeval tv_gpustart;
  403. int intervals;
  404. #endif
  405. bool new_work;
  406. float temp;
  407. int cutofftemp;
  408. int targettemp;
  409. #ifdef HAVE_ADL
  410. bool has_adl;
  411. struct gpu_adl adl;
  412. int gpu_engine;
  413. int min_engine;
  414. int gpu_fan;
  415. int min_fan;
  416. int gpu_memclock;
  417. int gpu_memdiff;
  418. int gpu_powertune;
  419. float gpu_vddc;
  420. #endif
  421. int diff1;
  422. double diff_accepted;
  423. double diff_rejected;
  424. int last_share_pool;
  425. time_t last_share_pool_time;
  426. double last_share_diff;
  427. time_t device_last_well;
  428. time_t device_last_not_well;
  429. enum dev_reason device_not_well_reason;
  430. float reinit_backoff;
  431. int thread_fail_init_count;
  432. int thread_zero_hash_count;
  433. int thread_fail_queue_count;
  434. int dev_sick_idle_60_count;
  435. int dev_dead_idle_600_count;
  436. int dev_nostart_count;
  437. int dev_over_heat_count; // It's a warning but worth knowing
  438. int dev_thermal_cutoff_count;
  439. int dev_comms_error_count;
  440. int dev_throttle_count;
  441. struct cgminer_stats cgminer_stats;
  442. };
  443. extern void renumber_cgpu(struct cgpu_info *);
  444. extern bool add_cgpu(struct cgpu_info*);
  445. struct thread_q {
  446. struct list_head q;
  447. bool frozen;
  448. pthread_mutex_t mutex;
  449. pthread_cond_t cond;
  450. };
  451. struct thr_info {
  452. int id;
  453. int device_thread;
  454. bool primary_thread;
  455. pthread_t pth;
  456. struct thread_q *q;
  457. struct cgpu_info *cgpu;
  458. void *cgpu_data;
  459. struct timeval last;
  460. struct timeval sick;
  461. bool pause;
  462. time_t getwork;
  463. double rolling;
  464. bool work_restart;
  465. int work_restart_fd;
  466. int _work_restart_fd_w;
  467. };
  468. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  469. extern void thr_info_cancel(struct thr_info *thr);
  470. extern void thr_info_freeze(struct thr_info *thr);
  471. extern void nmsleep(unsigned int msecs);
  472. extern double us_tdiff(struct timeval *end, struct timeval *start);
  473. extern double tdiff(struct timeval *end, struct timeval *start);
  474. struct string_elist {
  475. char *string;
  476. bool free_me;
  477. struct list_head list;
  478. };
  479. static inline void string_elist_add(const char *s, struct list_head *head)
  480. {
  481. struct string_elist *n;
  482. n = calloc(1, sizeof(*n));
  483. n->string = strdup(s);
  484. n->free_me = true;
  485. list_add_tail(&n->list, head);
  486. }
  487. static inline void string_elist_del(struct string_elist *item)
  488. {
  489. if (item->free_me)
  490. free(item->string);
  491. list_del(&item->list);
  492. free(item);
  493. }
  494. static inline uint32_t swab32(uint32_t v)
  495. {
  496. return bswap_32(v);
  497. }
  498. static inline void swap256(void *dest_p, const void *src_p)
  499. {
  500. uint32_t *dest = dest_p;
  501. const uint32_t *src = src_p;
  502. dest[0] = src[7];
  503. dest[1] = src[6];
  504. dest[2] = src[5];
  505. dest[3] = src[4];
  506. dest[4] = src[3];
  507. dest[5] = src[2];
  508. dest[6] = src[1];
  509. dest[7] = src[0];
  510. }
  511. static inline void swap32yes(void*out, const void*in, size_t sz) {
  512. size_t swapcounter = 0;
  513. for (swapcounter = 0; swapcounter < sz; ++swapcounter)
  514. (((uint32_t*)out)[swapcounter]) = swab32(((uint32_t*)in)[swapcounter]);
  515. }
  516. #define LOCAL_swap32(type, var, sz) \
  517. type __swapped_ ## var[sz * 4 / sizeof(type)]; \
  518. swap32yes(__swapped_ ## var, var, sz); \
  519. var = __swapped_ ## var; \
  520. // end
  521. #ifdef WORDS_BIGENDIAN
  522. # define swap32tobe(out, in, sz) ((out == in) ? (void)0 : memmove(out, in, sz))
  523. # define LOCAL_swap32be(type, var, sz) ;
  524. # define swap32tole(out, in, sz) swap32yes(out, in, sz)
  525. # define LOCAL_swap32le(type, var, sz) LOCAL_swap32(type, var, sz)
  526. #else
  527. # define swap32tobe(out, in, sz) swap32yes(out, in, sz)
  528. # define LOCAL_swap32be(type, var, sz) LOCAL_swap32(type, var, sz)
  529. # define swap32tole(out, in, sz) ((out == in) ? (void)0 : memmove(out, in, sz))
  530. # define LOCAL_swap32le(type, var, sz) ;
  531. #endif
  532. static inline void swab256(void *dest_p, const void *src_p)
  533. {
  534. uint32_t *dest = dest_p;
  535. const uint32_t *src = src_p;
  536. dest[0] = swab32(src[7]);
  537. dest[1] = swab32(src[6]);
  538. dest[2] = swab32(src[5]);
  539. dest[3] = swab32(src[4]);
  540. dest[4] = swab32(src[3]);
  541. dest[5] = swab32(src[2]);
  542. dest[6] = swab32(src[1]);
  543. dest[7] = swab32(src[0]);
  544. }
  545. #define flip32(dest_p, src_p) swap32yes(dest_p, src_p, 32 / 4)
  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 int opt_skip_checks;
  599. extern char *opt_kernel_path;
  600. extern char *opt_socks_proxy;
  601. extern char *cgminer_path;
  602. extern bool opt_fail_only;
  603. extern bool opt_autofan;
  604. extern bool opt_autoengine;
  605. extern bool use_curses;
  606. extern char *opt_api_allow;
  607. extern char *opt_api_groups;
  608. extern char *opt_api_description;
  609. extern int opt_api_port;
  610. extern bool opt_api_listen;
  611. extern bool opt_api_network;
  612. extern bool opt_delaynet;
  613. extern bool opt_restart;
  614. extern char *opt_icarus_options;
  615. extern char *opt_icarus_timing;
  616. extern bool opt_worktime;
  617. #ifdef USE_BITFORCE
  618. extern bool opt_bfl_noncerange;
  619. #endif
  620. extern int swork_id;
  621. extern pthread_rwlock_t netacc_lock;
  622. extern const uint32_t sha256_init_state[];
  623. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  624. const char *rpc_req, bool, bool, int *,
  625. struct pool *pool, bool);
  626. extern bool our_curl_supports_proxy_uris();
  627. extern char *bin2hex(const unsigned char *p, size_t len);
  628. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  629. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  630. unsigned char *pdata,
  631. unsigned char *phash1, unsigned char *phash,
  632. const unsigned char *ptarget,
  633. uint32_t max_nonce,
  634. uint32_t *last_nonce,
  635. uint32_t nonce);
  636. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  637. extern int opt_queue;
  638. extern int opt_scantime;
  639. extern int opt_expiry;
  640. extern pthread_mutex_t hash_lock;
  641. extern pthread_mutex_t console_lock;
  642. extern pthread_mutex_t ch_lock;
  643. extern pthread_mutex_t restart_lock;
  644. extern pthread_cond_t restart_cond;
  645. extern void thread_reportin(struct thr_info *thr);
  646. extern int restart_wait(unsigned int mstime);
  647. extern int stale_wait(unsigned int mstime, struct work*, bool checkend);
  648. extern void kill_work(void);
  649. extern void app_restart(void);
  650. extern void reinit_device(struct cgpu_info *cgpu);
  651. #ifdef HAVE_ADL
  652. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  653. extern int set_fanspeed(int gpu, int iFanSpeed);
  654. extern int set_vddc(int gpu, float fVddc);
  655. extern int set_engineclock(int gpu, int iEngineClock);
  656. extern int set_memoryclock(int gpu, int iMemoryClock);
  657. #endif
  658. extern void api(int thr_id);
  659. extern struct pool *current_pool(void);
  660. extern int enabled_pools;
  661. extern bool detect_stratum(struct pool *pool, char *url);
  662. extern void print_summary(void);
  663. extern struct pool *add_pool(void);
  664. extern void add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  665. #define MAX_GPUDEVICES 16
  666. #define MIN_INTENSITY -10
  667. #define _MIN_INTENSITY_STR "-10"
  668. #ifdef USE_SCRYPT
  669. #define MAX_INTENSITY 20
  670. #define _MAX_INTENSITY_STR "20"
  671. #else
  672. #define MAX_INTENSITY 14
  673. #define _MAX_INTENSITY_STR "14"
  674. #endif
  675. extern struct list_head scan_devices;
  676. extern bool opt_force_dev_init;
  677. extern int nDevs;
  678. extern int opt_n_threads;
  679. extern int num_processors;
  680. extern int hw_errors;
  681. extern bool use_syslog;
  682. extern bool opt_quiet;
  683. extern struct thr_info *thr_info;
  684. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  685. extern int gpu_threads;
  686. #ifdef USE_SCRYPT
  687. extern bool opt_scrypt;
  688. #else
  689. #define opt_scrypt (0)
  690. #endif
  691. extern double total_secs;
  692. extern int mining_threads;
  693. extern struct cgpu_info *cpus;
  694. extern int total_devices;
  695. extern struct cgpu_info **devices;
  696. extern int total_pools;
  697. extern struct pool **pools;
  698. extern const char *algo_names[];
  699. extern enum sha256_algos opt_algo;
  700. extern struct strategies strategies[];
  701. extern enum pool_strategy pool_strategy;
  702. extern int opt_rotate_period;
  703. extern double total_mhashes_done;
  704. extern unsigned int new_blocks;
  705. extern unsigned int found_blocks;
  706. extern int total_accepted, total_rejected, total_diff1;;
  707. extern int total_getworks, total_stale, total_discarded;
  708. extern uint64_t total_bytes_xfer;
  709. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  710. extern unsigned int local_work;
  711. extern unsigned int total_go, total_ro;
  712. extern const int opt_cutofftemp;
  713. extern int opt_hysteresis;
  714. extern int opt_fail_pause;
  715. extern int opt_log_interval;
  716. extern unsigned long long global_hashrate;
  717. extern char *current_fullhash;
  718. extern uint64_t best_diff;
  719. extern struct timeval block_timeval;
  720. #ifdef HAVE_OPENCL
  721. typedef struct {
  722. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  723. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  724. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  725. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  726. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  727. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  728. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  729. cl_uint W16; cl_uint W17; cl_uint W2;
  730. cl_uint PreVal4; cl_uint T1;
  731. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  732. cl_uint PreVal4addT1; cl_uint T1substate0;
  733. cl_uint PreVal4_2;
  734. cl_uint PreVal0;
  735. cl_uint PreW18;
  736. cl_uint PreW19;
  737. cl_uint PreW31;
  738. cl_uint PreW32;
  739. /* For diakgcn */
  740. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  741. cl_uint zeroA, zeroB;
  742. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  743. #ifdef USE_SCRYPT
  744. struct work *work;
  745. #endif
  746. } dev_blk_ctx;
  747. #else
  748. typedef struct {
  749. uint32_t nonce;
  750. } dev_blk_ctx;
  751. #endif
  752. struct curl_ent {
  753. CURL *curl;
  754. struct list_head node;
  755. struct timeval tv;
  756. };
  757. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  758. * equal disabled */
  759. enum pool_enable {
  760. POOL_DISABLED,
  761. POOL_ENABLED,
  762. POOL_REJECTING,
  763. };
  764. enum pool_protocol {
  765. PLP_NONE,
  766. PLP_GETWORK,
  767. PLP_GETBLOCKTEMPLATE,
  768. };
  769. struct stratum_work {
  770. char *job_id;
  771. char *prev_hash;
  772. char *coinbase1;
  773. char *coinbase2;
  774. char **merkle;
  775. char *bbversion;
  776. char *nbit;
  777. char *ntime;
  778. bool clean;
  779. size_t cb1_len;
  780. size_t cb2_len;
  781. size_t cb_len;
  782. size_t header_len;
  783. int merkles;
  784. double diff;
  785. bool transparency_probed;
  786. time_t transparency_time;
  787. bool opaque;
  788. };
  789. #define RBUFSIZE 8192
  790. #define RECVSIZE (RBUFSIZE - 4)
  791. struct pool {
  792. int pool_no;
  793. int prio;
  794. int accepted, rejected;
  795. int seq_rejects;
  796. int seq_getfails;
  797. int solved;
  798. int diff1;
  799. char diff[8];
  800. double diff_accepted;
  801. double diff_rejected;
  802. double diff_stale;
  803. bool submit_fail;
  804. bool idle;
  805. bool lagging;
  806. bool probed;
  807. enum pool_enable enabled;
  808. bool submit_old;
  809. bool removed;
  810. bool lp_started;
  811. unsigned char work_restart_id;
  812. uint32_t block_id;
  813. enum pool_protocol proto;
  814. char *hdr_path;
  815. char *lp_url;
  816. char *lp_id;
  817. enum pool_protocol lp_proto;
  818. curl_socket_t lp_socket;
  819. unsigned int getwork_requested;
  820. unsigned int stale_shares;
  821. unsigned int discarded_work;
  822. unsigned int getfail_occasions;
  823. unsigned int remotefail_occasions;
  824. struct timeval tv_idle;
  825. double utility;
  826. int last_shares, shares;
  827. char *rpc_url;
  828. char *rpc_userpass;
  829. char *rpc_user, *rpc_pass;
  830. char *rpc_proxy;
  831. pthread_mutex_t pool_lock;
  832. struct thread_q *submit_q;
  833. struct thread_q *getwork_q;
  834. pthread_t longpoll_thread;
  835. pthread_t submit_thread;
  836. pthread_t getwork_thread;
  837. int curls;
  838. pthread_cond_t cr_cond;
  839. struct list_head curlring;
  840. struct submit_work_state *sws_waiting_on_curl;
  841. time_t last_share_time;
  842. double last_share_diff;
  843. uint64_t best_diff;
  844. struct cgminer_stats cgminer_stats;
  845. struct cgminer_pool_stats cgminer_pool_stats;
  846. /* Stratum variables */
  847. char *stratum_url;
  848. char *stratum_port;
  849. CURL *stratum_curl;
  850. SOCKETTYPE sock;
  851. char *sockbuf;
  852. size_t sockbuf_size;
  853. char *sockaddr_url; /* stripped url used for sockaddr */
  854. char *nonce1;
  855. size_t n1_len;
  856. uint32_t nonce2;
  857. int n2size;
  858. bool has_stratum;
  859. bool stratum_active;
  860. time_t last_work_time; /* only set for Stratum right now */
  861. bool stratum_auth;
  862. bool stratum_notify;
  863. struct stratum_work swork;
  864. pthread_t stratum_thread;
  865. pthread_mutex_t stratum_lock;
  866. pthread_mutex_t last_work_lock;
  867. struct work *last_work_copy;
  868. };
  869. #define GETWORK_MODE_TESTPOOL 'T'
  870. #define GETWORK_MODE_POOL 'P'
  871. #define GETWORK_MODE_LP 'L'
  872. #define GETWORK_MODE_BENCHMARK 'B'
  873. #define GETWORK_MODE_STRATUM 'S'
  874. #define GETWORK_MODE_GBT 'G'
  875. struct work {
  876. unsigned char data[128];
  877. unsigned char midstate[32];
  878. unsigned char target[32];
  879. unsigned char hash[32];
  880. int rolls;
  881. dev_blk_ctx blk;
  882. struct thr_info *thr;
  883. int thr_id;
  884. struct pool *pool;
  885. struct timeval tv_staged;
  886. bool mined;
  887. bool clone;
  888. bool cloned;
  889. int rolltime;
  890. bool longpoll;
  891. bool stale;
  892. bool mandatory;
  893. bool block;
  894. bool queued;
  895. bool stratum;
  896. char *job_id;
  897. char *nonce2;
  898. char *ntime;
  899. double sdiff;
  900. unsigned char work_restart_id;
  901. int id;
  902. UT_hash_handle hh;
  903. double work_difficulty;
  904. blktemplate_t *tmpl;
  905. int *tmpl_refcount;
  906. unsigned int dataid;
  907. struct timeval tv_getwork;
  908. struct timeval tv_getwork_reply;
  909. struct timeval tv_cloned;
  910. struct timeval tv_work_start;
  911. struct timeval tv_work_found;
  912. char getwork_mode;
  913. /* Used to queue shares in submit_waiting */
  914. struct list_head list;
  915. };
  916. extern void get_datestamp(char *, struct timeval *);
  917. enum test_nonce2_result {
  918. TNR_GOOD,
  919. TNR_HIGH,
  920. TNR_BAD,
  921. };
  922. extern enum test_nonce2_result _test_nonce2(struct work *, uint32_t nonce, bool checktarget);
  923. #define test_nonce(work, nonce, checktarget) (_test_nonce2(work, nonce, checktarget) == TNR_GOOD)
  924. #define test_nonce2(work, nonce) (_test_nonce2(work, nonce, true))
  925. extern void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  926. extern void tailsprintf(char *f, const char *fmt, ...);
  927. extern void wlogprint(const char *f, ...);
  928. extern int curses_int(const char *query);
  929. extern char *curses_input(const char *query);
  930. extern void kill_work(void);
  931. extern int prioritize_pools(char *param, int *pid);
  932. extern void validate_pool_priorities(void);
  933. extern void switch_pools(struct pool *selected);
  934. extern void remove_pool(struct pool *pool);
  935. extern void write_config(FILE *fcfg);
  936. extern void zero_bestshare(void);
  937. extern void zero_stats(void);
  938. extern void default_save_file(char *filename);
  939. extern bool log_curses_only(int prio, const char *f, va_list ap) FORMAT_SYNTAX_CHECK(printf, 2, 0);
  940. extern void clear_logwin(void);
  941. extern bool pool_tclear(struct pool *pool, bool *var);
  942. extern struct thread_q *tq_new(void);
  943. extern void tq_free(struct thread_q *tq);
  944. extern bool tq_push(struct thread_q *tq, void *data);
  945. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  946. extern void tq_freeze(struct thread_q *tq);
  947. extern void tq_thaw(struct thread_q *tq);
  948. extern bool successful_connect;
  949. extern void adl(void);
  950. extern void clean_work(struct work *work);
  951. extern void free_work(struct work *work);
  952. extern void __copy_work(struct work *work, struct work *base_work);
  953. extern struct work *copy_work(struct work *base_work);
  954. enum api_data_type {
  955. API_ESCAPE,
  956. API_STRING,
  957. API_CONST,
  958. API_INT,
  959. API_UINT,
  960. API_UINT32,
  961. API_UINT64,
  962. API_DOUBLE,
  963. API_ELAPSED,
  964. API_BOOL,
  965. API_TIMEVAL,
  966. API_TIME,
  967. API_MHS,
  968. API_MHTOTAL,
  969. API_TEMP,
  970. API_UTILITY,
  971. API_FREQ,
  972. API_VOLTS,
  973. API_HS,
  974. API_DIFF,
  975. API_JSON,
  976. };
  977. struct api_data {
  978. enum api_data_type type;
  979. char *name;
  980. void *data;
  981. bool data_was_malloc;
  982. struct api_data *prev;
  983. struct api_data *next;
  984. };
  985. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  986. extern struct api_data *api_add_string(struct api_data *root, char *name, const char *data, bool copy_data);
  987. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  988. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  989. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  990. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  991. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  992. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  993. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  994. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  995. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  996. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  997. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  998. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  999. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  1000. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  1001. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  1002. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  1003. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  1004. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  1005. extern struct api_data *api_add_json(struct api_data *root, char *name, json_t *data, bool copy_data);
  1006. #endif /* __MINER_H__ */