miner.h 29 KB

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