miner.h 22 KB

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