miner.h 22 KB

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