miner.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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 (*thread_shutdown)(struct thr_info*);
  234. void (*thread_enable)(struct thr_info*);
  235. };
  236. enum dev_enable {
  237. DEV_ENABLED,
  238. DEV_DISABLED,
  239. DEV_RECOVER,
  240. };
  241. enum cl_kernels {
  242. KL_NONE,
  243. KL_POCLBM,
  244. KL_PHATK,
  245. KL_DIAKGCN,
  246. KL_DIABLO,
  247. KL_SCRYPT,
  248. };
  249. enum dev_reason {
  250. REASON_THREAD_FAIL_INIT,
  251. REASON_THREAD_ZERO_HASH,
  252. REASON_THREAD_FAIL_QUEUE,
  253. REASON_DEV_SICK_IDLE_60,
  254. REASON_DEV_DEAD_IDLE_600,
  255. REASON_DEV_NOSTART,
  256. REASON_DEV_OVER_HEAT,
  257. REASON_DEV_THERMAL_CUTOFF,
  258. REASON_DEV_COMMS_ERROR,
  259. REASON_DEV_THROTTLE,
  260. };
  261. #define REASON_NONE "None"
  262. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  263. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  264. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  265. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  266. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  267. #define REASON_DEV_NOSTART_STR "Device failed to start"
  268. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  269. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  270. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  271. #define REASON_DEV_THROTTLE_STR "Device throttle"
  272. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  273. #define MIN_SEC_UNSET 99999999
  274. struct cgminer_stats {
  275. uint32_t getwork_calls;
  276. struct timeval getwork_wait;
  277. struct timeval getwork_wait_max;
  278. struct timeval getwork_wait_min;
  279. };
  280. // Just the actual network getworks to the pool
  281. struct cgminer_pool_stats {
  282. uint32_t getwork_calls;
  283. uint32_t getwork_attempts;
  284. struct timeval getwork_wait;
  285. struct timeval getwork_wait_max;
  286. struct timeval getwork_wait_min;
  287. double getwork_wait_rolling;
  288. bool hadrolltime;
  289. bool canroll;
  290. bool hadexpire;
  291. uint32_t rolltime;
  292. double min_diff;
  293. double max_diff;
  294. double last_diff;
  295. uint32_t min_diff_count;
  296. uint32_t max_diff_count;
  297. };
  298. struct cgpu_info {
  299. int cgminer_id;
  300. struct device_api *api;
  301. int device_id;
  302. char *name;
  303. char *device_path;
  304. FILE *device_file;
  305. union {
  306. #ifdef USE_ZTEX
  307. struct libztex_device *device_ztex;
  308. #endif
  309. int device_fd;
  310. };
  311. #ifdef USE_BITFORCE
  312. struct timeval work_start_tv;
  313. unsigned int wait_ms;
  314. unsigned int sleep_ms;
  315. double avg_wait_f;
  316. unsigned int avg_wait_d;
  317. uint32_t nonces;
  318. bool nonce_range;
  319. bool polling;
  320. bool flash_led;
  321. #endif
  322. pthread_mutex_t device_mutex;
  323. enum dev_enable deven;
  324. int accepted;
  325. int rejected;
  326. int hw_errors;
  327. double rolling;
  328. double total_mhashes;
  329. double utility;
  330. enum alive status;
  331. char init[40];
  332. struct timeval last_message_tv;
  333. int threads;
  334. struct thr_info **thr;
  335. int64_t max_hashes;
  336. const char *kname;
  337. #ifdef HAVE_OPENCL
  338. bool mapped;
  339. int virtual_gpu;
  340. int virtual_adl;
  341. int intensity;
  342. bool dynamic;
  343. cl_uint vwidth;
  344. size_t work_size;
  345. enum cl_kernels kernel;
  346. cl_ulong max_alloc;
  347. #ifdef USE_SCRYPT
  348. int opt_lg, lookup_gap;
  349. size_t opt_tc, thread_concurrency;
  350. size_t shaders;
  351. #endif
  352. struct timeval tv_gpustart;
  353. int intervals;
  354. #endif
  355. bool new_work;
  356. float temp;
  357. int cutofftemp;
  358. #ifdef HAVE_ADL
  359. bool has_adl;
  360. struct gpu_adl adl;
  361. int gpu_engine;
  362. int min_engine;
  363. int gpu_fan;
  364. int min_fan;
  365. int gpu_memclock;
  366. int gpu_memdiff;
  367. int gpu_powertune;
  368. float gpu_vddc;
  369. #endif
  370. int diff1;
  371. double diff_accepted;
  372. double diff_rejected;
  373. int last_share_pool;
  374. time_t last_share_pool_time;
  375. double last_share_diff;
  376. time_t device_last_well;
  377. time_t device_last_not_well;
  378. enum dev_reason device_not_well_reason;
  379. int thread_fail_init_count;
  380. int thread_zero_hash_count;
  381. int thread_fail_queue_count;
  382. int dev_sick_idle_60_count;
  383. int dev_dead_idle_600_count;
  384. int dev_nostart_count;
  385. int dev_over_heat_count; // It's a warning but worth knowing
  386. int dev_thermal_cutoff_count;
  387. int dev_comms_error_count;
  388. int dev_throttle_count;
  389. struct cgminer_stats cgminer_stats;
  390. };
  391. extern bool add_cgpu(struct cgpu_info*);
  392. struct thread_q {
  393. struct list_head q;
  394. bool frozen;
  395. pthread_mutex_t mutex;
  396. pthread_cond_t cond;
  397. };
  398. struct thr_info {
  399. int id;
  400. int device_thread;
  401. bool primary_thread;
  402. pthread_t pth;
  403. struct thread_q *q;
  404. struct cgpu_info *cgpu;
  405. void *cgpu_data;
  406. struct timeval last;
  407. struct timeval sick;
  408. bool pause;
  409. bool getwork;
  410. double rolling;
  411. bool work_restart;
  412. };
  413. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  414. extern void thr_info_cancel(struct thr_info *thr);
  415. extern void thr_info_freeze(struct thr_info *thr);
  416. extern void nmsleep(unsigned int msecs);
  417. extern double us_tdiff(struct timeval *end, struct timeval *start);
  418. extern double tdiff(struct timeval *end, struct timeval *start);
  419. struct string_elist {
  420. char *string;
  421. bool free_me;
  422. struct list_head list;
  423. };
  424. static inline void string_elist_add(const char *s, struct list_head *head)
  425. {
  426. struct string_elist *n;
  427. n = calloc(1, sizeof(*n));
  428. n->string = strdup(s);
  429. n->free_me = true;
  430. list_add_tail(&n->list, head);
  431. }
  432. static inline void string_elist_del(struct string_elist *item)
  433. {
  434. if (item->free_me)
  435. free(item->string);
  436. list_del(&item->list);
  437. }
  438. static inline uint32_t swab32(uint32_t v)
  439. {
  440. return bswap_32(v);
  441. }
  442. static inline void swap256(void *dest_p, const void *src_p)
  443. {
  444. uint32_t *dest = dest_p;
  445. const uint32_t *src = src_p;
  446. dest[0] = src[7];
  447. dest[1] = src[6];
  448. dest[2] = src[5];
  449. dest[3] = src[4];
  450. dest[4] = src[3];
  451. dest[5] = src[2];
  452. dest[6] = src[1];
  453. dest[7] = src[0];
  454. }
  455. static inline void swab256(void *dest_p, const void *src_p)
  456. {
  457. uint32_t *dest = dest_p;
  458. const uint32_t *src = src_p;
  459. dest[0] = swab32(src[7]);
  460. dest[1] = swab32(src[6]);
  461. dest[2] = swab32(src[5]);
  462. dest[3] = swab32(src[4]);
  463. dest[4] = swab32(src[3]);
  464. dest[5] = swab32(src[2]);
  465. dest[6] = swab32(src[1]);
  466. dest[7] = swab32(src[0]);
  467. }
  468. extern void quit(int status, const char *format, ...);
  469. static inline void mutex_lock(pthread_mutex_t *lock)
  470. {
  471. if (unlikely(pthread_mutex_lock(lock)))
  472. quit(1, "WTF MUTEX ERROR ON LOCK!");
  473. }
  474. static inline void mutex_unlock(pthread_mutex_t *lock)
  475. {
  476. if (unlikely(pthread_mutex_unlock(lock)))
  477. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  478. }
  479. static inline int mutex_trylock(pthread_mutex_t *lock)
  480. {
  481. return pthread_mutex_trylock(lock);
  482. }
  483. static inline void wr_lock(pthread_rwlock_t *lock)
  484. {
  485. if (unlikely(pthread_rwlock_wrlock(lock)))
  486. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  487. }
  488. static inline void rd_lock(pthread_rwlock_t *lock)
  489. {
  490. if (unlikely(pthread_rwlock_rdlock(lock)))
  491. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  492. }
  493. static inline void rw_unlock(pthread_rwlock_t *lock)
  494. {
  495. if (unlikely(pthread_rwlock_unlock(lock)))
  496. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  497. }
  498. static inline void rd_unlock(pthread_rwlock_t *lock)
  499. {
  500. rw_unlock(lock);
  501. }
  502. static inline void wr_unlock(pthread_rwlock_t *lock)
  503. {
  504. rw_unlock(lock);
  505. }
  506. static inline void mutex_init(pthread_mutex_t *lock)
  507. {
  508. if (unlikely(pthread_mutex_init(lock, NULL)))
  509. quit(1, "Failed to pthread_mutex_init");
  510. }
  511. static inline void rwlock_init(pthread_rwlock_t *lock)
  512. {
  513. if (unlikely(pthread_rwlock_init(lock, NULL)))
  514. quit(1, "Failed to pthread_rwlock_init");
  515. }
  516. struct pool;
  517. extern bool opt_protocol;
  518. extern bool have_longpoll;
  519. extern char *opt_kernel_path;
  520. extern char *opt_socks_proxy;
  521. extern char *cgminer_path;
  522. extern bool opt_fail_only;
  523. extern bool opt_autofan;
  524. extern bool opt_autoengine;
  525. extern bool use_curses;
  526. extern char *opt_api_allow;
  527. extern char *opt_api_groups;
  528. extern char *opt_api_description;
  529. extern int opt_api_port;
  530. extern bool opt_api_listen;
  531. extern bool opt_api_network;
  532. extern bool opt_delaynet;
  533. extern bool opt_restart;
  534. extern char *opt_icarus_options;
  535. extern char *opt_icarus_timing;
  536. extern bool opt_worktime;
  537. #ifdef USE_BITFORCE
  538. extern bool opt_bfl_noncerange;
  539. #endif
  540. extern int swork_id;
  541. extern pthread_rwlock_t netacc_lock;
  542. extern const uint32_t sha256_init_state[];
  543. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  544. const char *rpc_req, bool, bool, int *,
  545. struct pool *pool, bool);
  546. extern const char *proxytype(curl_proxytype proxytype);
  547. extern char *get_proxy(char *url, struct pool *pool);
  548. extern char *bin2hex(const unsigned char *p, size_t len);
  549. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  550. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  551. unsigned char *pdata,
  552. unsigned char *phash1, unsigned char *phash,
  553. const unsigned char *ptarget,
  554. uint32_t max_nonce,
  555. uint32_t *last_nonce,
  556. uint32_t nonce);
  557. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  558. extern int opt_queue;
  559. extern int opt_scantime;
  560. extern int opt_expiry;
  561. extern pthread_mutex_t console_lock;
  562. extern pthread_mutex_t ch_lock;
  563. extern pthread_mutex_t restart_lock;
  564. extern pthread_cond_t restart_cond;
  565. extern void thread_reportin(struct thr_info *thr);
  566. extern int restart_wait(unsigned int mstime);
  567. extern void kill_work(void);
  568. extern void reinit_device(struct cgpu_info *cgpu);
  569. #ifdef HAVE_ADL
  570. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  571. extern int set_fanspeed(int gpu, int iFanSpeed);
  572. extern int set_vddc(int gpu, float fVddc);
  573. extern int set_engineclock(int gpu, int iEngineClock);
  574. extern int set_memoryclock(int gpu, int iMemoryClock);
  575. #endif
  576. extern void api(int thr_id);
  577. extern struct pool *current_pool(void);
  578. extern int enabled_pools;
  579. extern bool detect_stratum(struct pool *pool, char *url);
  580. extern struct pool *add_pool(void);
  581. extern void add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  582. #define MAX_GPUDEVICES 16
  583. #define MIN_INTENSITY -10
  584. #define _MIN_INTENSITY_STR "-10"
  585. #ifdef USE_SCRYPT
  586. #define MAX_INTENSITY 20
  587. #define _MAX_INTENSITY_STR "20"
  588. #else
  589. #define MAX_INTENSITY 14
  590. #define _MAX_INTENSITY_STR "14"
  591. #endif
  592. extern struct list_head scan_devices;
  593. extern int nDevs;
  594. extern int opt_n_threads;
  595. extern int num_processors;
  596. extern int hw_errors;
  597. extern bool use_syslog;
  598. extern bool opt_quiet;
  599. extern struct thr_info *thr_info;
  600. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  601. extern int gpu_threads;
  602. #ifdef USE_SCRYPT
  603. extern bool opt_scrypt;
  604. #else
  605. #define opt_scrypt (0)
  606. #endif
  607. extern double total_secs;
  608. extern int mining_threads;
  609. extern struct cgpu_info *cpus;
  610. extern int total_devices;
  611. extern struct cgpu_info **devices;
  612. extern int total_pools;
  613. extern struct pool **pools;
  614. extern const char *algo_names[];
  615. extern enum sha256_algos opt_algo;
  616. extern struct strategies strategies[];
  617. extern enum pool_strategy pool_strategy;
  618. extern int opt_rotate_period;
  619. extern double total_mhashes_done;
  620. extern unsigned int new_blocks;
  621. extern unsigned int found_blocks;
  622. extern int total_accepted, total_rejected, total_diff1;;
  623. extern int total_getworks, total_stale, total_discarded;
  624. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  625. extern unsigned int local_work;
  626. extern unsigned int total_go, total_ro;
  627. extern const int opt_cutofftemp;
  628. extern int opt_log_interval;
  629. extern unsigned long long global_hashrate;
  630. extern char *current_fullhash;
  631. extern struct timeval block_timeval;
  632. #ifdef HAVE_OPENCL
  633. typedef struct {
  634. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  635. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  636. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  637. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  638. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  639. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  640. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  641. cl_uint W16; cl_uint W17; cl_uint W2;
  642. cl_uint PreVal4; cl_uint T1;
  643. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  644. cl_uint PreVal4addT1; cl_uint T1substate0;
  645. cl_uint PreVal4_2;
  646. cl_uint PreVal0;
  647. cl_uint PreW18;
  648. cl_uint PreW19;
  649. cl_uint PreW31;
  650. cl_uint PreW32;
  651. /* For diakgcn */
  652. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  653. cl_uint zeroA, zeroB;
  654. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  655. #ifdef USE_SCRYPT
  656. struct work *work;
  657. #endif
  658. } dev_blk_ctx;
  659. #else
  660. typedef struct {
  661. uint32_t nonce;
  662. } dev_blk_ctx;
  663. #endif
  664. struct curl_ent {
  665. CURL *curl;
  666. struct list_head node;
  667. struct timeval tv;
  668. };
  669. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  670. * equal disabled */
  671. enum pool_enable {
  672. POOL_DISABLED,
  673. POOL_ENABLED,
  674. POOL_REJECTING,
  675. };
  676. struct stratum_work {
  677. char *job_id;
  678. char *prev_hash;
  679. char *coinbase1;
  680. char *coinbase2;
  681. char **merkle;
  682. char *bbversion;
  683. char *nbit;
  684. char *ntime;
  685. bool clean;
  686. int merkles;
  687. int diff;
  688. };
  689. #define RECVSIZE 8191
  690. #define RBUFSIZE (RECVSIZE + 1)
  691. struct pool {
  692. int pool_no;
  693. int prio;
  694. int accepted, rejected;
  695. int seq_rejects;
  696. int seq_getfails;
  697. int solved;
  698. int diff1;
  699. double diff_accepted;
  700. double diff_rejected;
  701. double diff_stale;
  702. int queued;
  703. int staged;
  704. bool submit_fail;
  705. bool idle;
  706. bool lagging;
  707. bool probed;
  708. enum pool_enable enabled;
  709. bool submit_old;
  710. bool removed;
  711. bool lp_started;
  712. char *hdr_path;
  713. char *lp_url;
  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. curl_proxytype rpc_proxytype;
  726. char *rpc_proxy;
  727. pthread_mutex_t pool_lock;
  728. struct thread_q *submit_q;
  729. struct thread_q *getwork_q;
  730. pthread_t longpoll_thread;
  731. pthread_t submit_thread;
  732. pthread_t getwork_thread;
  733. int curls;
  734. pthread_cond_t cr_cond;
  735. struct list_head curlring;
  736. time_t last_share_time;
  737. double last_share_diff;
  738. struct cgminer_stats cgminer_stats;
  739. struct cgminer_pool_stats cgminer_pool_stats;
  740. /* Stratum variables */
  741. char *stratum_url;
  742. char *stratum_port;
  743. CURL *stratum_curl;
  744. SOCKETTYPE sock;
  745. char sockbuf[RBUFSIZE];
  746. struct sockaddr_in *server, client;
  747. char *sockaddr_url; /* stripped url used for sockaddr */
  748. char *nonce1;
  749. uint32_t nonce2;
  750. int n2size;
  751. bool has_stratum;
  752. bool stratum_active;
  753. bool stratum_auth;
  754. struct stratum_work swork;
  755. pthread_t stratum_thread;
  756. pthread_mutex_t stratum_lock;
  757. };
  758. #define GETWORK_MODE_TESTPOOL 'T'
  759. #define GETWORK_MODE_POOL 'P'
  760. #define GETWORK_MODE_LP 'L'
  761. #define GETWORK_MODE_BENCHMARK 'B'
  762. #define GETWORK_MODE_STRATUM 'S'
  763. struct work {
  764. unsigned char data[128];
  765. unsigned char hash1[64];
  766. unsigned char midstate[32];
  767. unsigned char target[32];
  768. unsigned char hash[32];
  769. uint32_t outputhash;
  770. int rolls;
  771. dev_blk_ctx blk;
  772. struct thr_info *thr;
  773. int thr_id;
  774. struct pool *pool;
  775. struct timeval tv_staged;
  776. bool mined;
  777. bool clone;
  778. bool cloned;
  779. int rolltime;
  780. bool longpoll;
  781. bool stale;
  782. bool mandatory;
  783. bool block;
  784. bool queued;
  785. bool stratum;
  786. /* These are arbitrary lengths as it is too hard to keep track of
  787. * dynamically allocated ram in work structs */
  788. char job_id[64];
  789. char nonce2[64];
  790. char ntime[16];
  791. int sdiff;
  792. unsigned int work_block;
  793. int id;
  794. UT_hash_handle hh;
  795. double work_difficulty;
  796. struct timeval tv_getwork;
  797. struct timeval tv_getwork_reply;
  798. struct timeval tv_cloned;
  799. struct timeval tv_work_start;
  800. struct timeval tv_work_found;
  801. char getwork_mode;
  802. };
  803. #ifdef USE_MODMINER
  804. struct modminer_fpga_state {
  805. bool work_running;
  806. struct work running_work;
  807. struct timeval tv_workstart;
  808. uint32_t hashes;
  809. char next_work_cmd[46];
  810. unsigned char clock;
  811. int no_nonce_counter;
  812. int good_share_counter;
  813. time_t last_cutoff_reduced;
  814. unsigned char temp;
  815. };
  816. #endif
  817. extern void get_datestamp(char *, struct timeval *);
  818. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  819. extern void tailsprintf(char *f, const char *fmt, ...);
  820. extern void wlogprint(const char *f, ...);
  821. extern int curses_int(const char *query);
  822. extern char *curses_input(const char *query);
  823. extern void kill_work(void);
  824. extern void switch_pools(struct pool *selected);
  825. extern void remove_pool(struct pool *pool);
  826. extern void write_config(FILE *fcfg);
  827. extern void default_save_file(char *filename);
  828. extern bool log_curses_only(int prio, const char *f, va_list ap);
  829. extern void clear_logwin(void);
  830. extern bool pool_tclear(struct pool *pool, bool *var);
  831. extern struct thread_q *tq_new(void);
  832. extern void tq_free(struct thread_q *tq);
  833. extern bool tq_push(struct thread_q *tq, void *data);
  834. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  835. extern void tq_freeze(struct thread_q *tq);
  836. extern void tq_thaw(struct thread_q *tq);
  837. extern bool successful_connect;
  838. extern void adl(void);
  839. extern void app_restart(void);
  840. enum api_data_type {
  841. API_ESCAPE,
  842. API_STRING,
  843. API_CONST,
  844. API_INT,
  845. API_UINT,
  846. API_UINT32,
  847. API_UINT64,
  848. API_DOUBLE,
  849. API_ELAPSED,
  850. API_BOOL,
  851. API_TIMEVAL,
  852. API_TIME,
  853. API_MHS,
  854. API_MHTOTAL,
  855. API_TEMP,
  856. API_UTILITY,
  857. API_FREQ,
  858. API_VOLTS,
  859. API_HS,
  860. API_DIFF
  861. };
  862. struct api_data {
  863. enum api_data_type type;
  864. char *name;
  865. void *data;
  866. bool data_was_malloc;
  867. struct api_data *prev;
  868. struct api_data *next;
  869. };
  870. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  871. extern struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data);
  872. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  873. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  874. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  875. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  876. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  877. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  878. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  879. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  880. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  881. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  882. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  883. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  884. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  885. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  886. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  887. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  888. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  889. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  890. #endif /* __MINER_H__ */