miner.h 25 KB

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