miner.h 22 KB

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