miner.h 29 KB

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