miner.h 27 KB

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