miner.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. #ifdef __SSE2__
  57. #define WANT_SSE2_4WAY 1
  58. #endif
  59. #if defined(__i386__) && defined(HAS_YASM) && defined(__SSE2__)
  60. #define WANT_X8632_SSE2 1
  61. #endif
  62. #if defined(__i386__) || defined(__x86_64__)
  63. #define WANT_VIA_PADLOCK 1
  64. #endif
  65. #if defined(__x86_64__) && defined(HAS_YASM)
  66. #define WANT_X8664_SSE2 1
  67. #endif
  68. #if defined(__x86_64__) && defined(HAS_YASM)
  69. #define WANT_X8664_SSE4 1
  70. #endif
  71. #if !defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  72. #define WANT_BUILTIN_BSWAP
  73. #else
  74. #if HAVE_BYTESWAP_H
  75. #include <byteswap.h>
  76. #elif defined(USE_SYS_ENDIAN_H)
  77. #include <sys/endian.h>
  78. #elif defined(__APPLE__)
  79. #include <libkern/OSByteOrder.h>
  80. #define bswap_16 OSSwapInt16
  81. #define bswap_32 OSSwapInt32
  82. #define bswap_64 OSSwapInt64
  83. #else
  84. #define bswap_16(value) \
  85. ((((value) & 0xff) << 8) | ((value) >> 8))
  86. #define bswap_32(value) \
  87. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  88. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  89. #define bswap_64(value) \
  90. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  91. << 32) | \
  92. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  93. #endif
  94. #endif /* !defined(__GLXBYTEORDER_H__) */
  95. /* This assumes htobe32 is a macro in endian.h */
  96. #ifndef htobe32
  97. # if __BYTE_ORDER == __LITTLE_ENDIAN
  98. # define be32toh(x) bswap_32(x)
  99. # define htobe32(x) bswap_32(x)
  100. # elif __BYTE_ORDER == __BIG_ENDIAN
  101. # define be32toh(x) (x)
  102. # define htobe32(x) (x)
  103. #else
  104. #error UNKNOWN BYTE ORDER
  105. #endif
  106. #endif
  107. #ifdef HAVE_SYSLOG_H
  108. #include <syslog.h>
  109. #else
  110. enum {
  111. LOG_ERR,
  112. LOG_WARNING,
  113. LOG_INFO,
  114. LOG_DEBUG,
  115. };
  116. #endif
  117. #undef unlikely
  118. #undef likely
  119. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  120. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  121. #define likely(expr) (__builtin_expect(!!(expr), 1))
  122. #else
  123. #define unlikely(expr) (expr)
  124. #define likely(expr) (expr)
  125. #endif
  126. #if defined(__i386__)
  127. #define WANT_CRYPTOPP_ASM32
  128. #endif
  129. #ifndef ARRAY_SIZE
  130. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  131. #endif
  132. enum alive {
  133. LIFE_WELL,
  134. LIFE_SICK,
  135. LIFE_DEAD,
  136. };
  137. #ifdef HAVE_ADL
  138. struct gpu_adl {
  139. ADLTemperature lpTemperature;
  140. int iAdapterIndex;
  141. int lpAdapterID;
  142. int lpStatus;
  143. ADLPMActivity lpActivity;
  144. ADLODParameters lpOdParameters;
  145. ADLODPerformanceLevels *DefPerfLev;
  146. ADLFanSpeedInfo lpFanSpeedInfo;
  147. ADLFanSpeedValue lpFanSpeedValue;
  148. ADLFanSpeedValue DefFanSpeedValue;
  149. int iEngineClock;
  150. int iMemoryClock;
  151. int iVddc;
  152. bool autofan;
  153. bool autoengine;
  154. int targettemp;
  155. int overtemp;
  156. int minspeed;
  157. int maxspeed;
  158. };
  159. #endif
  160. struct cgpu_info {
  161. int is_gpu;
  162. int cpu_gpu;
  163. int accepted;
  164. int rejected;
  165. int hw_errors;
  166. double rolling;
  167. double total_mhashes;
  168. unsigned int getworks;
  169. double efficiency;
  170. double utility;
  171. enum alive status;
  172. char init[40];
  173. struct timeval last_message_tv;
  174. #ifdef HAVE_ADL
  175. bool has_adl;
  176. struct gpu_adl adl;
  177. #endif
  178. };
  179. struct thread_q {
  180. struct list_head q;
  181. bool frozen;
  182. pthread_mutex_t mutex;
  183. pthread_cond_t cond;
  184. };
  185. struct thr_info {
  186. int id;
  187. pthread_t *pth;
  188. struct thread_q *q;
  189. struct cgpu_info *cgpu;
  190. struct timeval last;
  191. struct timeval sick;
  192. bool pause;
  193. bool getwork;
  194. double rolling;
  195. };
  196. extern inline int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  197. static inline uint32_t swab32(uint32_t v)
  198. {
  199. #ifdef WANT_BUILTIN_BSWAP
  200. return __builtin_bswap32(v);
  201. #else
  202. return bswap_32(v);
  203. #endif
  204. }
  205. static inline void swap256(void *dest_p, const void *src_p)
  206. {
  207. uint32_t *dest = dest_p;
  208. const uint32_t *src = src_p;
  209. dest[0] = src[7];
  210. dest[1] = src[6];
  211. dest[2] = src[5];
  212. dest[3] = src[4];
  213. dest[4] = src[3];
  214. dest[5] = src[2];
  215. dest[6] = src[1];
  216. dest[7] = src[0];
  217. }
  218. extern void quit(int status, const char *format, ...);
  219. static inline void mutex_lock(pthread_mutex_t *lock)
  220. {
  221. if (unlikely(pthread_mutex_lock(lock)))
  222. quit(1, "WTF MUTEX ERROR ON LOCK!");
  223. }
  224. static inline void mutex_unlock(pthread_mutex_t *lock)
  225. {
  226. if (unlikely(pthread_mutex_unlock(lock)))
  227. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  228. }
  229. static inline void wr_lock(pthread_rwlock_t *lock)
  230. {
  231. if (unlikely(pthread_rwlock_wrlock(lock)))
  232. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  233. }
  234. static inline void rd_lock(pthread_rwlock_t *lock)
  235. {
  236. if (unlikely(pthread_rwlock_rdlock(lock)))
  237. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  238. }
  239. static inline void rw_unlock(pthread_rwlock_t *lock)
  240. {
  241. if (unlikely(pthread_rwlock_unlock(lock)))
  242. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  243. }
  244. static inline void rd_unlock(pthread_rwlock_t *lock)
  245. {
  246. rw_unlock(lock);
  247. }
  248. static inline void wr_unlock(pthread_rwlock_t *lock)
  249. {
  250. rw_unlock(lock);
  251. }
  252. struct pool;
  253. extern bool opt_debug;
  254. extern bool opt_protocol;
  255. extern bool opt_log_output;
  256. extern char *opt_kernel_path;
  257. extern bool opt_autofan;
  258. extern bool opt_autoengine;
  259. extern const uint32_t sha256_init_state[];
  260. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  261. const char *rpc_req, bool, bool, bool *,
  262. struct pool *pool);
  263. extern char *bin2hex(const unsigned char *p, size_t len);
  264. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  265. extern unsigned int ScanHash_4WaySSE2(int, const unsigned char *pmidstate,
  266. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  267. const unsigned char *ptarget,
  268. uint32_t max_nonce, unsigned long *nHashesDone, uint32_t nonce);
  269. extern unsigned int scanhash_sse2_amd64(int, const unsigned char *pmidstate,
  270. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  271. const unsigned char *ptarget,
  272. uint32_t max_nonce, unsigned long *nHashesDone);
  273. extern bool scanhash_via(int, unsigned char *data_inout,
  274. const unsigned char *target,
  275. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  276. extern bool scanhash_c(int, const unsigned char *midstate, unsigned char *data,
  277. unsigned char *hash1, unsigned char *hash,
  278. const unsigned char *target,
  279. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  280. extern bool scanhash_cryptopp(int, const unsigned char *midstate,unsigned char *data,
  281. unsigned char *hash1, unsigned char *hash,
  282. const unsigned char *target,
  283. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  284. extern bool scanhash_asm32(int, const unsigned char *midstate,unsigned char *data,
  285. unsigned char *hash1, unsigned char *hash,
  286. const unsigned char *target,
  287. uint32_t max_nonce, unsigned long *hashes_done, uint32_t nonce);
  288. extern int scanhash_sse2_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  289. unsigned char *phash1, unsigned char *phash,
  290. const unsigned char *ptarget,
  291. uint32_t max_nonce, unsigned long *nHashesDone,
  292. uint32_t nonce);
  293. extern int scanhash_sse4_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  294. unsigned char *phash1, unsigned char *phash,
  295. const unsigned char *ptarget,
  296. uint32_t max_nonce, unsigned long *nHashesDone,
  297. uint32_t nonce);
  298. extern int scanhash_sse2_32(int, const unsigned char *pmidstate, unsigned char *pdata,
  299. unsigned char *phash1, unsigned char *phash,
  300. const unsigned char *ptarget,
  301. uint32_t max_nonce, unsigned long *nHashesDone,
  302. uint32_t nonce);
  303. extern int
  304. timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);
  305. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  306. extern int opt_scantime;
  307. struct work_restart {
  308. volatile unsigned long restart;
  309. char padding[128 - sizeof(unsigned long)];
  310. };
  311. extern int hw_errors;
  312. extern bool use_syslog;
  313. extern struct thr_info *thr_info;
  314. extern int longpoll_thr_id;
  315. extern struct work_restart *work_restart;
  316. extern struct cgpu_info *gpus;
  317. #ifdef HAVE_OPENCL
  318. typedef struct {
  319. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  320. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  321. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  322. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  323. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  324. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  325. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  326. cl_uint W16; cl_uint W17; cl_uint W2;
  327. cl_uint PreVal4; cl_uint T1;
  328. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  329. cl_uint PreVal4addT1; cl_uint T1substate0;
  330. cl_uint PreVal4_2;
  331. cl_uint PreVal0;
  332. cl_uint PreW18;
  333. cl_uint PreW19;
  334. cl_uint PreW31;
  335. cl_uint PreW32;
  336. } dev_blk_ctx;
  337. #else
  338. typedef struct {
  339. uint32_t nonce;
  340. } dev_blk_ctx;
  341. #endif
  342. struct pool {
  343. int pool_no;
  344. int prio;
  345. int accepted, rejected;
  346. bool submit_fail;
  347. bool idle;
  348. bool lagging;
  349. bool probed;
  350. bool enabled;
  351. char *hdr_path;
  352. unsigned int getwork_requested;
  353. unsigned int stale_shares;
  354. unsigned int discarded_work;
  355. unsigned int getfail_occasions;
  356. unsigned int remotefail_occasions;
  357. struct timeval tv_idle;
  358. char *rpc_url;
  359. char *rpc_userpass;
  360. char *rpc_user, *rpc_pass;
  361. pthread_mutex_t pool_lock;
  362. };
  363. struct work {
  364. unsigned char data[128];
  365. unsigned char hash1[64];
  366. unsigned char midstate[32];
  367. unsigned char target[32];
  368. unsigned char hash[32];
  369. int rolls;
  370. uint32_t output[1];
  371. uint32_t valid;
  372. dev_blk_ctx blk;
  373. struct thr_info *thr;
  374. int thr_id;
  375. struct pool *pool;
  376. struct timeval tv_staged;
  377. bool mined;
  378. bool clone;
  379. bool cloned;
  380. bool rolltime;
  381. int id;
  382. UT_hash_handle hh;
  383. };
  384. enum cl_kernel {
  385. KL_NONE,
  386. KL_POCLBM,
  387. KL_PHATK,
  388. };
  389. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  390. extern void wlogprint(const char *f, ...);
  391. extern int curses_int(const char *query);
  392. extern char *curses_input(const char *query);
  393. extern void kill_work(void);
  394. extern void log_curses(int prio, const char *f, va_list ap);
  395. extern void vapplog(int prio, const char *fmt, va_list ap);
  396. extern void applog(int prio, const char *fmt, ...);
  397. extern struct thread_q *tq_new(void);
  398. extern void tq_free(struct thread_q *tq);
  399. extern bool tq_push(struct thread_q *tq, void *data);
  400. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  401. extern void tq_freeze(struct thread_q *tq);
  402. extern void tq_thaw(struct thread_q *tq);
  403. extern bool successful_connect;
  404. extern enum cl_kernel chosen_kernel;
  405. extern void adl(void);
  406. #endif /* __MINER_H__ */