miner.h 13 KB

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