miner.h 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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. struct pool {
  754. int pool_no;
  755. int prio;
  756. int accepted, rejected;
  757. int seq_rejects;
  758. int seq_getfails;
  759. int solved;
  760. int diff1;
  761. double diff_accepted;
  762. double diff_rejected;
  763. double diff_stale;
  764. int queued;
  765. int staged;
  766. bool submit_fail;
  767. bool idle;
  768. bool lagging;
  769. bool probed;
  770. enum pool_enable enabled;
  771. bool submit_old;
  772. bool removed;
  773. bool lp_started;
  774. unsigned char work_restart_id;
  775. uint32_t block_id;
  776. enum pool_protocol proto;
  777. char *hdr_path;
  778. char *lp_url;
  779. char *lp_id;
  780. enum pool_protocol lp_proto;
  781. curl_socket_t lp_socket;
  782. unsigned int getwork_requested;
  783. unsigned int stale_shares;
  784. unsigned int discarded_work;
  785. unsigned int getfail_occasions;
  786. unsigned int remotefail_occasions;
  787. struct timeval tv_idle;
  788. double utility;
  789. int last_shares, shares;
  790. char *rpc_url;
  791. char *rpc_userpass;
  792. char *rpc_user, *rpc_pass;
  793. char *rpc_proxy;
  794. pthread_mutex_t pool_lock;
  795. struct thread_q *submit_q;
  796. struct thread_q *getwork_q;
  797. pthread_t longpoll_thread;
  798. pthread_t submit_thread;
  799. pthread_t getwork_thread;
  800. int curls;
  801. pthread_cond_t cr_cond;
  802. struct list_head curlring;
  803. time_t last_share_time;
  804. double last_share_diff;
  805. struct cgminer_stats cgminer_stats;
  806. struct cgminer_pool_stats cgminer_pool_stats;
  807. /* Stratum variables */
  808. char *stratum_url;
  809. SOCKETTYPE sock;
  810. struct sockaddr_in *server, client;
  811. char *sockaddr_url; /* stripped url used for sockaddr */
  812. char *nonce1;
  813. uint32_t nonce2;
  814. int n2size;
  815. bool has_stratum;
  816. bool stratum_active;
  817. bool stratum_auth;
  818. struct stratum_work swork;
  819. pthread_t stratum_thread;
  820. };
  821. #define GETWORK_MODE_TESTPOOL 'T'
  822. #define GETWORK_MODE_POOL 'P'
  823. #define GETWORK_MODE_LP 'L'
  824. #define GETWORK_MODE_BENCHMARK 'B'
  825. #define GETWORK_MODE_STRATUM 'S'
  826. struct work {
  827. unsigned char data[128];
  828. unsigned char hash1[64];
  829. unsigned char midstate[32];
  830. unsigned char target[32];
  831. unsigned char hash[32];
  832. int rolls;
  833. uint32_t output[1];
  834. uint32_t valid;
  835. dev_blk_ctx blk;
  836. struct thr_info *thr;
  837. int thr_id;
  838. struct pool *pool;
  839. struct timeval tv_staged;
  840. bool mined;
  841. bool clone;
  842. bool cloned;
  843. int rolltime;
  844. bool longpoll;
  845. bool stale;
  846. bool mandatory;
  847. bool block;
  848. bool queued;
  849. bool stratum;
  850. /* These are arbitrary lengths as it is too hard to keep track of
  851. * dynamically allocated ram in work structs */
  852. char job_id[64];
  853. char nonce2[64];
  854. char ntime[16];
  855. int sdiff;
  856. unsigned char work_restart_id;
  857. int id;
  858. UT_hash_handle hh;
  859. double work_difficulty;
  860. blktemplate_t *tmpl;
  861. int *tmpl_refcount;
  862. unsigned int dataid;
  863. struct timeval tv_getwork;
  864. struct timeval tv_getwork_reply;
  865. struct timeval tv_cloned;
  866. struct timeval tv_work_start;
  867. struct timeval tv_work_found;
  868. char getwork_mode;
  869. };
  870. extern void get_datestamp(char *, struct timeval *);
  871. enum test_nonce2_result {
  872. TNR_GOOD,
  873. TNR_HIGH,
  874. TNR_BAD,
  875. };
  876. extern enum test_nonce2_result _test_nonce2(struct work *, uint32_t nonce, bool checktarget);
  877. #define test_nonce(work, nonce, checktarget) (_test_nonce2(work, nonce, checktarget) == TNR_GOOD)
  878. #define test_nonce2(work, nonce) (_test_nonce2(work, nonce, true))
  879. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  880. extern void tailsprintf(char *f, const char *fmt, ...);
  881. extern void wlogprint(const char *f, ...);
  882. extern int curses_int(const char *query);
  883. extern char *curses_input(const char *query);
  884. extern void kill_work(void);
  885. extern void switch_pools(struct pool *selected);
  886. extern void remove_pool(struct pool *pool);
  887. extern void write_config(FILE *fcfg);
  888. extern void default_save_file(char *filename);
  889. extern bool log_curses_only(int prio, const char *f, va_list ap);
  890. extern void clear_logwin(void);
  891. extern bool pool_tclear(struct pool *pool, bool *var);
  892. extern struct thread_q *tq_new(void);
  893. extern void tq_free(struct thread_q *tq);
  894. extern bool tq_push(struct thread_q *tq, void *data);
  895. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  896. extern void tq_freeze(struct thread_q *tq);
  897. extern void tq_thaw(struct thread_q *tq);
  898. extern bool successful_connect;
  899. extern void adl(void);
  900. enum api_data_type {
  901. API_ESCAPE,
  902. API_STRING,
  903. API_CONST,
  904. API_INT,
  905. API_UINT,
  906. API_UINT32,
  907. API_UINT64,
  908. API_DOUBLE,
  909. API_ELAPSED,
  910. API_BOOL,
  911. API_TIMEVAL,
  912. API_TIME,
  913. API_MHS,
  914. API_MHTOTAL,
  915. API_TEMP,
  916. API_UTILITY,
  917. API_FREQ,
  918. API_VOLTS,
  919. API_HS,
  920. API_DIFF,
  921. API_JSON,
  922. };
  923. struct api_data {
  924. enum api_data_type type;
  925. char *name;
  926. void *data;
  927. bool data_was_malloc;
  928. struct api_data *prev;
  929. struct api_data *next;
  930. };
  931. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  932. extern struct api_data *api_add_string(struct api_data *root, char *name, const char *data, bool copy_data);
  933. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  934. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  935. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  936. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  937. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  938. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  939. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  940. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  941. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  942. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  943. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  944. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  945. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  946. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  947. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  948. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  949. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  950. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  951. extern struct api_data *api_add_json(struct api_data *root, char *name, json_t *data, bool copy_data);
  952. #endif /* __MINER_H__ */