miner.h 30 KB

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