miner.h 30 KB

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