miner.h 26 KB

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