miner.h 23 KB

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