miner.h 23 KB

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