miner.h 30 KB

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