miner.h 28 KB

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