miner.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. #ifndef __MINER_H__
  2. #define __MINER_H__
  3. #include "config.h"
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <sys/time.h>
  7. #include <pthread.h>
  8. #include <jansson.h>
  9. #include <curl/curl.h>
  10. #include "elist.h"
  11. #include "uthash.h"
  12. #include "logging.h"
  13. #ifdef HAVE_OPENCL
  14. #ifdef __APPLE_CC__
  15. #include <OpenCL/opencl.h>
  16. #else
  17. #include <CL/cl.h>
  18. #endif
  19. #endif /* HAVE_OPENCL */
  20. #ifdef STDC_HEADERS
  21. # include <stdlib.h>
  22. # include <stddef.h>
  23. #else
  24. # ifdef HAVE_STDLIB_H
  25. # include <stdlib.h>
  26. # endif
  27. #endif
  28. #ifdef HAVE_ALLOCA_H
  29. # include <alloca.h>
  30. #elif defined __GNUC__
  31. # ifndef WIN32
  32. # define alloca __builtin_alloca
  33. # else
  34. # include <malloc.h>
  35. # endif
  36. #elif defined _AIX
  37. # define alloca __alloca
  38. #elif defined _MSC_VER
  39. # include <malloc.h>
  40. # define alloca _alloca
  41. #else
  42. # ifndef HAVE_ALLOCA
  43. # ifdef __cplusplus
  44. extern "C"
  45. # endif
  46. void *alloca (size_t);
  47. # endif
  48. #endif
  49. #if defined (__linux)
  50. #ifndef LINUX
  51. #define LINUX
  52. #endif
  53. #endif
  54. #ifdef HAVE_ADL
  55. #include "ADL_SDK/adl_sdk.h"
  56. #endif
  57. #if !defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  58. #define bswap_16 __builtin_bswap16
  59. #define bswap_32 __builtin_bswap32
  60. #define bswap_64 __builtin_bswap64
  61. #else
  62. #if HAVE_BYTESWAP_H
  63. #include <byteswap.h>
  64. #elif defined(USE_SYS_ENDIAN_H)
  65. #include <sys/endian.h>
  66. #elif defined(__APPLE__)
  67. #include <libkern/OSByteOrder.h>
  68. #define bswap_16 OSSwapInt16
  69. #define bswap_32 OSSwapInt32
  70. #define bswap_64 OSSwapInt64
  71. #else
  72. #define bswap_16(value) \
  73. ((((value) & 0xff) << 8) | ((value) >> 8))
  74. #define bswap_32(value) \
  75. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  76. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  77. #define bswap_64(value) \
  78. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  79. << 32) | \
  80. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  81. #endif
  82. #endif /* !defined(__GLXBYTEORDER_H__) */
  83. /* This assumes htobe32 is a macro in endian.h */
  84. #ifndef htobe32
  85. # if __BYTE_ORDER == __LITTLE_ENDIAN
  86. # define be32toh(x) bswap_32(x)
  87. # define htobe32(x) bswap_32(x)
  88. # elif __BYTE_ORDER == __BIG_ENDIAN
  89. # define be32toh(x) (x)
  90. # define htobe32(x) (x)
  91. #else
  92. #error UNKNOWN BYTE ORDER
  93. #endif
  94. #endif
  95. #undef unlikely
  96. #undef likely
  97. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  98. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  99. #define likely(expr) (__builtin_expect(!!(expr), 1))
  100. #else
  101. #define unlikely(expr) (expr)
  102. #define likely(expr) (expr)
  103. #endif
  104. #define __maybe_unused __attribute__((unused))
  105. #if defined(__i386__)
  106. #define WANT_CRYPTOPP_ASM32
  107. #endif
  108. #ifndef ARRAY_SIZE
  109. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  110. #endif
  111. enum alive {
  112. LIFE_WELL,
  113. LIFE_SICK,
  114. LIFE_DEAD,
  115. LIFE_NOSTART
  116. };
  117. enum pool_strategy {
  118. POOL_FAILOVER,
  119. POOL_ROUNDROBIN,
  120. POOL_ROTATE,
  121. POOL_LOADBALANCE,
  122. };
  123. #define TOP_STRATEGY (POOL_LOADBALANCE)
  124. struct strategies {
  125. const char *s;
  126. };
  127. struct cgpu_info;
  128. #ifdef HAVE_ADL
  129. struct gpu_adl {
  130. ADLTemperature lpTemperature;
  131. int iAdapterIndex;
  132. int lpAdapterID;
  133. int iBusNumber;
  134. char strAdapterName[256];
  135. ADLPMActivity lpActivity;
  136. ADLODParameters lpOdParameters;
  137. ADLODPerformanceLevels *DefPerfLev;
  138. ADLFanSpeedInfo lpFanSpeedInfo;
  139. ADLFanSpeedValue lpFanSpeedValue;
  140. ADLFanSpeedValue DefFanSpeedValue;
  141. int iEngineClock;
  142. int iMemoryClock;
  143. int iVddc;
  144. int iPercentage;
  145. bool autofan;
  146. bool autoengine;
  147. bool managed; /* Were the values ever changed on this card */
  148. int lastengine;
  149. int lasttemp;
  150. int targetfan;
  151. int targettemp;
  152. int overtemp;
  153. int minspeed;
  154. int maxspeed;
  155. int gpu;
  156. bool has_fanspeed;
  157. struct gpu_adl *twin;
  158. };
  159. #endif
  160. struct thr_info;
  161. struct work;
  162. struct device_api {
  163. char*dname;
  164. char*name;
  165. // API-global functions
  166. void (*api_detect)();
  167. // Device-specific functions
  168. void (*reinit_device)(struct cgpu_info*);
  169. void (*get_statline_before)(char*, struct cgpu_info*);
  170. void (*get_statline)(char*, struct cgpu_info*);
  171. json_t* (*get_extra_device_detail)(struct cgpu_info*);
  172. json_t* (*get_extra_device_status)(struct cgpu_info*);
  173. // Thread-specific functions
  174. bool (*thread_prepare)(struct thr_info*);
  175. uint64_t (*can_limit_work)(struct thr_info*);
  176. bool (*thread_init)(struct thr_info*);
  177. void (*free_work)(struct thr_info*, struct work*);
  178. bool (*prepare_work)(struct thr_info*, struct work*);
  179. uint64_t (*scanhash)(struct thr_info*, struct work*, uint64_t);
  180. void (*thread_shutdown)(struct thr_info*);
  181. };
  182. enum dev_enable {
  183. DEV_ENABLED,
  184. DEV_DISABLED,
  185. DEV_RECOVER,
  186. };
  187. enum cl_kernels {
  188. KL_NONE,
  189. KL_POCLBM,
  190. KL_PHATK,
  191. KL_DIAKGCN,
  192. KL_DIABLO,
  193. };
  194. enum dev_reason {
  195. REASON_THREAD_FAIL_INIT,
  196. REASON_THREAD_ZERO_HASH,
  197. REASON_THREAD_FAIL_QUEUE,
  198. REASON_DEV_SICK_IDLE_60,
  199. REASON_DEV_DEAD_IDLE_600,
  200. REASON_DEV_NOSTART,
  201. REASON_DEV_OVER_HEAT,
  202. REASON_DEV_THERMAL_CUTOFF,
  203. };
  204. #define REASON_NONE "None"
  205. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  206. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  207. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  208. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  209. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  210. #define REASON_DEV_NOSTART_STR "Device failed to start"
  211. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  212. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  213. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  214. struct cgpu_info {
  215. int cgminer_id;
  216. struct device_api *api;
  217. int device_id;
  218. char *name;
  219. char *device_path;
  220. FILE *device_file;
  221. int device_fd;
  222. enum dev_enable deven;
  223. int accepted;
  224. int rejected;
  225. int hw_errors;
  226. double rolling;
  227. double total_mhashes;
  228. double utility;
  229. enum alive status;
  230. char init[40];
  231. struct timeval last_message_tv;
  232. int threads;
  233. struct thr_info *thread;
  234. unsigned int max_hashes;
  235. int virtual_gpu;
  236. int intensity;
  237. bool dynamic;
  238. char *kname;
  239. #ifdef HAVE_OPENCL
  240. cl_uint vwidth;
  241. size_t work_size;
  242. enum cl_kernels kernel;
  243. #endif
  244. float temp;
  245. int cutofftemp;
  246. #ifdef HAVE_ADL
  247. bool has_adl;
  248. struct gpu_adl adl;
  249. int gpu_engine;
  250. int min_engine;
  251. int gpu_fan;
  252. int min_fan;
  253. int gpu_memclock;
  254. int gpu_memdiff;
  255. int gpu_powertune;
  256. float gpu_vddc;
  257. #endif
  258. int last_share_pool;
  259. time_t last_share_pool_time;
  260. time_t device_last_well;
  261. time_t device_last_not_well;
  262. enum dev_reason device_not_well_reason;
  263. int thread_fail_init_count;
  264. int thread_zero_hash_count;
  265. int thread_fail_queue_count;
  266. int dev_sick_idle_60_count;
  267. int dev_dead_idle_600_count;
  268. int dev_nostart_count;
  269. int dev_over_heat_count; // It's a warning but worth knowing
  270. int dev_thermal_cutoff_count;
  271. };
  272. extern bool add_cgpu(struct cgpu_info*);
  273. struct thread_q {
  274. struct list_head q;
  275. bool frozen;
  276. pthread_mutex_t mutex;
  277. pthread_cond_t cond;
  278. };
  279. struct thr_info {
  280. int id;
  281. int device_thread;
  282. pthread_t pth;
  283. struct thread_q *q;
  284. struct cgpu_info *cgpu;
  285. void *cgpu_data;
  286. struct timeval last;
  287. struct timeval sick;
  288. bool pause;
  289. bool getwork;
  290. double rolling;
  291. };
  292. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  293. extern void thr_info_cancel(struct thr_info *thr);
  294. extern void thr_info_freeze(struct thr_info *thr);
  295. struct string_elist {
  296. char *string;
  297. bool free_me;
  298. struct list_head list;
  299. };
  300. static inline void string_elist_add(const char *s, struct list_head *head)
  301. {
  302. struct string_elist *n;
  303. n = calloc(1, sizeof(*n));
  304. n->string = strdup(s);
  305. n->free_me = true;
  306. list_add_tail(&n->list, head);
  307. }
  308. static inline void string_elist_del(struct string_elist *item)
  309. {
  310. if (item->free_me)
  311. free(item->string);
  312. list_del(&item->list);
  313. }
  314. static inline uint32_t swab32(uint32_t v)
  315. {
  316. return bswap_32(v);
  317. }
  318. static inline void swap256(void *dest_p, const void *src_p)
  319. {
  320. uint32_t *dest = dest_p;
  321. const uint32_t *src = src_p;
  322. dest[0] = src[7];
  323. dest[1] = src[6];
  324. dest[2] = src[5];
  325. dest[3] = src[4];
  326. dest[4] = src[3];
  327. dest[5] = src[2];
  328. dest[6] = src[1];
  329. dest[7] = src[0];
  330. }
  331. extern void quit(int status, const char *format, ...);
  332. static inline void mutex_lock(pthread_mutex_t *lock)
  333. {
  334. if (unlikely(pthread_mutex_lock(lock)))
  335. quit(1, "WTF MUTEX ERROR ON LOCK!");
  336. }
  337. static inline void mutex_unlock(pthread_mutex_t *lock)
  338. {
  339. if (unlikely(pthread_mutex_unlock(lock)))
  340. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  341. }
  342. static inline void wr_lock(pthread_rwlock_t *lock)
  343. {
  344. if (unlikely(pthread_rwlock_wrlock(lock)))
  345. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  346. }
  347. static inline void rd_lock(pthread_rwlock_t *lock)
  348. {
  349. if (unlikely(pthread_rwlock_rdlock(lock)))
  350. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  351. }
  352. static inline void rw_unlock(pthread_rwlock_t *lock)
  353. {
  354. if (unlikely(pthread_rwlock_unlock(lock)))
  355. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  356. }
  357. static inline void rd_unlock(pthread_rwlock_t *lock)
  358. {
  359. rw_unlock(lock);
  360. }
  361. static inline void wr_unlock(pthread_rwlock_t *lock)
  362. {
  363. rw_unlock(lock);
  364. }
  365. static inline void mutex_init(pthread_mutex_t *lock)
  366. {
  367. if (unlikely(pthread_mutex_init(lock, NULL)))
  368. quit(1, "Failed to pthread_mutex_init");
  369. }
  370. static inline void rwlock_init(pthread_rwlock_t *lock)
  371. {
  372. if (unlikely(pthread_rwlock_init(lock, NULL)))
  373. quit(1, "Failed to pthread_rwlock_init");
  374. }
  375. struct pool;
  376. extern bool opt_protocol;
  377. extern char *opt_kernel_path;
  378. extern char *opt_socks_proxy;
  379. extern char *cgminer_path;
  380. extern bool opt_autofan;
  381. extern bool opt_autoengine;
  382. extern bool use_curses;
  383. extern char *opt_api_allow;
  384. extern char *opt_api_description;
  385. extern int opt_api_port;
  386. extern bool opt_api_listen;
  387. extern bool opt_api_network;
  388. extern bool opt_delaynet;
  389. extern bool opt_restart;
  390. extern pthread_rwlock_t netacc_lock;
  391. extern const uint32_t sha256_init_state[];
  392. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  393. const char *rpc_req, bool, bool, bool *,
  394. struct pool *pool, bool);
  395. extern char *bin2hex(const unsigned char *p, size_t len);
  396. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  397. typedef bool (*sha256_func)(int thr_id, const unsigned char *pmidstate,
  398. unsigned char *pdata,
  399. unsigned char *phash1, unsigned char *phash,
  400. const unsigned char *ptarget,
  401. uint32_t max_nonce,
  402. uint32_t *last_nonce,
  403. uint32_t nonce);
  404. extern int
  405. timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);
  406. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  407. extern int opt_scantime;
  408. struct work_restart {
  409. volatile unsigned long restart;
  410. char padding[128 - sizeof(unsigned long)];
  411. };
  412. extern void thread_reportin(struct thr_info *thr);
  413. extern void kill_work(void);
  414. extern void reinit_device(struct cgpu_info *cgpu);
  415. #ifdef HAVE_ADL
  416. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  417. extern int set_fanspeed(int gpu, int iFanSpeed);
  418. extern int set_vddc(int gpu, float fVddc);
  419. extern int set_engineclock(int gpu, int iEngineClock);
  420. extern int set_memoryclock(int gpu, int iMemoryClock);
  421. #endif
  422. extern void api(int thr_id);
  423. extern struct pool *current_pool(void);
  424. extern int active_pools(void);
  425. extern int add_pool_details(bool live, char *url, char *user, char *pass);
  426. #define ADD_POOL_MAXIMUM 1
  427. #define ADD_POOL_OK 0
  428. #define MAX_GPUDEVICES 16
  429. #define MAX_DEVICES 64
  430. #define MAX_POOLS (32)
  431. #define MIN_INTENSITY -10
  432. #define _MIN_INTENSITY_STR "-10"
  433. #define MAX_INTENSITY 14
  434. #define _MAX_INTENSITY_STR "14"
  435. extern struct list_head scan_devices;
  436. extern int nDevs;
  437. extern int opt_n_threads;
  438. extern int num_processors;
  439. extern int hw_errors;
  440. extern bool use_syslog;
  441. extern struct thr_info *thr_info;
  442. extern int longpoll_thr_id;
  443. extern struct work_restart *work_restart;
  444. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  445. extern int gpu_threads;
  446. extern double total_secs;
  447. extern int mining_threads;
  448. extern struct cgpu_info *cpus;
  449. extern int total_devices;
  450. extern struct cgpu_info *devices[];
  451. extern int total_pools;
  452. extern struct pool *pools[MAX_POOLS];
  453. extern const char *algo_names[];
  454. extern enum sha256_algos opt_algo;
  455. extern struct strategies strategies[];
  456. extern enum pool_strategy pool_strategy;
  457. extern int opt_rotate_period;
  458. extern double total_mhashes_done;
  459. extern unsigned int new_blocks;
  460. extern unsigned int found_blocks;
  461. extern int total_accepted, total_rejected;
  462. extern int total_getworks, total_stale, total_discarded;
  463. extern unsigned int local_work;
  464. extern unsigned int total_go, total_ro;
  465. extern const int opt_cutofftemp;
  466. extern int opt_log_interval;
  467. #ifdef HAVE_OPENCL
  468. typedef struct {
  469. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  470. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  471. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  472. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  473. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  474. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  475. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  476. cl_uint W16; cl_uint W17; cl_uint W2;
  477. cl_uint PreVal4; cl_uint T1;
  478. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  479. cl_uint PreVal4addT1; cl_uint T1substate0;
  480. cl_uint PreVal4_2;
  481. cl_uint PreVal0;
  482. cl_uint PreW18;
  483. cl_uint PreW19;
  484. cl_uint PreW31;
  485. cl_uint PreW32;
  486. /* For diakgcn */
  487. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  488. cl_uint zeroA, zeroB;
  489. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  490. } dev_blk_ctx;
  491. #else
  492. typedef struct {
  493. uint32_t nonce;
  494. } dev_blk_ctx;
  495. #endif
  496. struct pool {
  497. int pool_no;
  498. int prio;
  499. int accepted, rejected;
  500. bool submit_fail;
  501. bool idle;
  502. bool lagging;
  503. bool probed;
  504. bool enabled;
  505. bool submit_old;
  506. char *hdr_path;
  507. char *lp_url;
  508. bool lp_sent;
  509. bool is_lp;
  510. unsigned int getwork_requested;
  511. unsigned int stale_shares;
  512. unsigned int discarded_work;
  513. unsigned int getfail_occasions;
  514. unsigned int remotefail_occasions;
  515. struct timeval tv_idle;
  516. char *rpc_url;
  517. char *rpc_userpass;
  518. char *rpc_user, *rpc_pass;
  519. pthread_mutex_t pool_lock;
  520. };
  521. struct work {
  522. unsigned char data[128];
  523. unsigned char hash1[64];
  524. unsigned char midstate[32];
  525. unsigned char target[32];
  526. unsigned char hash[32];
  527. int rolls;
  528. uint32_t output[1];
  529. uint32_t valid;
  530. dev_blk_ctx blk;
  531. struct thr_info *thr;
  532. int thr_id;
  533. struct pool *pool;
  534. struct timeval tv_staged;
  535. bool mined;
  536. bool clone;
  537. bool cloned;
  538. bool rolltime;
  539. bool longpoll;
  540. unsigned int work_block;
  541. int id;
  542. UT_hash_handle hh;
  543. };
  544. extern void get_datestamp(char *, struct timeval *);
  545. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  546. extern void tailsprintf(char *f, const char *fmt, ...);
  547. extern void wlogprint(const char *f, ...);
  548. extern int curses_int(const char *query);
  549. extern char *curses_input(const char *query);
  550. extern void kill_work(void);
  551. extern void switch_pools(struct pool *selected);
  552. extern void remove_pool(struct pool *pool);
  553. extern void write_config(FILE *fcfg);
  554. extern void log_curses(int prio, const char *f, va_list ap);
  555. extern void clear_logwin(void);
  556. extern bool pool_tclear(struct pool *pool, bool *var);
  557. extern struct thread_q *tq_new(void);
  558. extern void tq_free(struct thread_q *tq);
  559. extern bool tq_push(struct thread_q *tq, void *data);
  560. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  561. extern void tq_freeze(struct thread_q *tq);
  562. extern void tq_thaw(struct thread_q *tq);
  563. extern bool successful_connect;
  564. extern void adl(void);
  565. extern void app_restart(void);
  566. #endif /* __MINER_H__ */