miner.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. #ifdef __SSE2__
  49. #define WANT_SSE2_4WAY 1
  50. #endif
  51. #if defined(__i386__) || defined(__x86_64__)
  52. #define WANT_VIA_PADLOCK 1
  53. #endif
  54. #if defined(__x86_64__) && defined(HAS_YASM)
  55. #define WANT_X8664_SSE2 1
  56. #endif
  57. #if defined(__x86_64__) && defined(HAS_YASM)
  58. #define WANT_X8664_SSE4 1
  59. #endif
  60. #if !defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  61. #define WANT_BUILTIN_BSWAP
  62. #else
  63. #if HAVE_BYTESWAP_H
  64. #include <byteswap.h>
  65. #elif defined(USE_SYS_ENDIAN_H)
  66. #include <sys/endian.h>
  67. #elif defined(__APPLE__)
  68. #include <libkern/OSByteOrder.h>
  69. #define bswap_16 OSSwapInt16
  70. #define bswap_32 OSSwapInt32
  71. #define bswap_64 OSSwapInt64
  72. #else
  73. #define bswap_16(value) \
  74. ((((value) & 0xff) << 8) | ((value) >> 8))
  75. #define bswap_32(value) \
  76. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  77. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  78. #define bswap_64(value) \
  79. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  80. << 32) | \
  81. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  82. #endif
  83. #endif /* !defined(__GLXBYTEORDER_H__) */
  84. /* This assumes htobe32 is a macro in endian.h */
  85. #ifndef htobe32
  86. # if __BYTE_ORDER == __LITTLE_ENDIAN
  87. # define be32toh(x) bswap_32(x)
  88. # define htobe32(x) bswap_32(x)
  89. # elif __BYTE_ORDER == __BIG_ENDIAN
  90. # define be32toh(x) (x)
  91. # define htobe32(x) (x)
  92. #else
  93. #error UNKNOWN BYTE ORDER
  94. #endif
  95. #endif
  96. #ifdef HAVE_SYSLOG_H
  97. #include <syslog.h>
  98. #else
  99. enum {
  100. LOG_ERR,
  101. LOG_WARNING,
  102. LOG_INFO,
  103. LOG_DEBUG,
  104. };
  105. #endif
  106. #undef unlikely
  107. #undef likely
  108. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  109. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  110. #define likely(expr) (__builtin_expect(!!(expr), 1))
  111. #else
  112. #define unlikely(expr) (expr)
  113. #define likely(expr) (expr)
  114. #endif
  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. };
  126. struct cgpu_info {
  127. int is_gpu;
  128. int cpu_gpu;
  129. int accepted;
  130. int rejected;
  131. int hw_errors;
  132. double rolling;
  133. double total_mhashes;
  134. unsigned int getworks;
  135. double efficiency;
  136. double utility;
  137. enum alive status;
  138. char init[40];
  139. struct timeval last_message_tv;
  140. };
  141. struct thread_q {
  142. struct list_head q;
  143. bool frozen;
  144. pthread_mutex_t mutex;
  145. pthread_cond_t cond;
  146. };
  147. struct thr_info {
  148. int id;
  149. pthread_t *pth;
  150. struct thread_q *q;
  151. struct cgpu_info *cgpu;
  152. struct timeval last;
  153. struct timeval sick;
  154. bool getwork;
  155. double rolling;
  156. };
  157. extern inline int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  158. static inline uint32_t swab32(uint32_t v)
  159. {
  160. #ifdef WANT_BUILTIN_BSWAP
  161. return __builtin_bswap32(v);
  162. #else
  163. return bswap_32(v);
  164. #endif
  165. }
  166. static inline void swap256(void *dest_p, const void *src_p)
  167. {
  168. uint32_t *dest = dest_p;
  169. const uint32_t *src = src_p;
  170. dest[0] = src[7];
  171. dest[1] = src[6];
  172. dest[2] = src[5];
  173. dest[3] = src[4];
  174. dest[4] = src[3];
  175. dest[5] = src[2];
  176. dest[6] = src[1];
  177. dest[7] = src[0];
  178. }
  179. extern void quit(int status, const char *format, ...);
  180. static inline void mutex_lock(pthread_mutex_t *lock)
  181. {
  182. if (unlikely(pthread_mutex_lock(lock)))
  183. quit(1, "WTF MUTEX ERROR ON LOCK!");
  184. }
  185. static inline void mutex_unlock(pthread_mutex_t *lock)
  186. {
  187. if (unlikely(pthread_mutex_unlock(lock)))
  188. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  189. }
  190. struct pool;
  191. extern bool opt_debug;
  192. extern bool opt_protocol;
  193. extern bool opt_log_output;
  194. extern const uint32_t sha256_init_state[];
  195. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  196. const char *rpc_req, bool, bool, bool *,
  197. struct pool *pool);
  198. extern char *bin2hex(const unsigned char *p, size_t len);
  199. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  200. extern unsigned int ScanHash_4WaySSE2(int, const unsigned char *pmidstate,
  201. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  202. const unsigned char *ptarget,
  203. uint32_t max_nonce, unsigned long *nHashesDone, uint32_t nonce);
  204. extern unsigned int scanhash_sse2_amd64(int, const unsigned char *pmidstate,
  205. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  206. const unsigned char *ptarget,
  207. uint32_t max_nonce, unsigned long *nHashesDone);
  208. extern bool scanhash_via(int, unsigned char *data_inout,
  209. const unsigned char *target,
  210. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  211. extern bool scanhash_c(int, const unsigned char *midstate, unsigned char *data,
  212. unsigned char *hash1, unsigned char *hash,
  213. const unsigned char *target,
  214. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  215. extern bool scanhash_cryptopp(int, const unsigned char *midstate,unsigned char *data,
  216. unsigned char *hash1, unsigned char *hash,
  217. const unsigned char *target,
  218. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  219. extern bool scanhash_asm32(int, const unsigned char *midstate,unsigned char *data,
  220. unsigned char *hash1, unsigned char *hash,
  221. const unsigned char *target,
  222. uint32_t max_nonce, unsigned long *hashes_done, uint32_t nonce);
  223. extern int scanhash_sse2_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  224. unsigned char *phash1, unsigned char *phash,
  225. const unsigned char *ptarget,
  226. uint32_t max_nonce, unsigned long *nHashesDone,
  227. uint32_t nonce);
  228. extern int scanhash_sse4_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  229. unsigned char *phash1, unsigned char *phash,
  230. const unsigned char *ptarget,
  231. uint32_t max_nonce, unsigned long *nHashesDone,
  232. uint32_t nonce);
  233. extern int
  234. timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);
  235. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  236. extern int opt_scantime;
  237. struct work_restart {
  238. volatile unsigned long restart;
  239. char padding[128 - sizeof(unsigned long)];
  240. };
  241. extern int hw_errors;
  242. extern bool use_syslog;
  243. extern struct thr_info *thr_info;
  244. extern int longpoll_thr_id;
  245. extern struct work_restart *work_restart;
  246. extern pthread_mutex_t control_lock;
  247. #ifdef HAVE_OPENCL
  248. typedef struct {
  249. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  250. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  251. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  252. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  253. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  254. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  255. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  256. cl_uint W16; cl_uint W17; cl_uint W2;
  257. cl_uint PreVal4; cl_uint T1;
  258. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  259. cl_uint PreVal4addT1; cl_uint T1substate0;
  260. cl_uint PreVal4_2;
  261. cl_uint PreVal0;
  262. cl_uint PreW18;
  263. cl_uint PreW19;
  264. cl_uint PreW31;
  265. cl_uint PreW32;
  266. } dev_blk_ctx;
  267. #else
  268. typedef struct {
  269. uint32_t nonce;
  270. } dev_blk_ctx;
  271. #endif
  272. struct pool {
  273. int pool_no;
  274. int prio;
  275. int accepted, rejected;
  276. bool submit_fail;
  277. bool idle;
  278. bool lagging;
  279. bool probed;
  280. bool enabled;
  281. char *hdr_path;
  282. unsigned int getwork_requested;
  283. unsigned int stale_shares;
  284. unsigned int discarded_work;
  285. unsigned int localgen_occasions;
  286. unsigned int remotefail_occasions;
  287. struct timeval tv_idle;
  288. char *rpc_url;
  289. char *rpc_userpass;
  290. char *rpc_user, *rpc_pass;
  291. pthread_mutex_t pool_lock;
  292. };
  293. struct work {
  294. unsigned char data[128];
  295. unsigned char hash1[64];
  296. unsigned char midstate[32];
  297. unsigned char target[32];
  298. unsigned char hash[32];
  299. int rolls;
  300. uint32_t output[1];
  301. uint32_t valid;
  302. dev_blk_ctx blk;
  303. struct thr_info *thr;
  304. int thr_id;
  305. struct pool *pool;
  306. struct timeval tv_staged;
  307. bool mined;
  308. bool clone;
  309. bool cloned;
  310. bool rolltime;
  311. int id;
  312. UT_hash_handle hh;
  313. };
  314. enum cl_kernel {
  315. KL_NONE,
  316. KL_POCLBM,
  317. KL_PHATK,
  318. };
  319. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  320. extern void kill_work(void);
  321. extern void log_curses(int prio, const char *f, va_list ap);
  322. extern void vapplog(int prio, const char *fmt, va_list ap);
  323. extern void applog(int prio, const char *fmt, ...);
  324. extern struct thread_q *tq_new(void);
  325. extern void tq_free(struct thread_q *tq);
  326. extern bool tq_push(struct thread_q *tq, void *data);
  327. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  328. extern void tq_freeze(struct thread_q *tq);
  329. extern void tq_thaw(struct thread_q *tq);
  330. extern bool test_and_set(bool *var);
  331. extern bool test_and_clear(bool *var);
  332. extern bool successful_connect;
  333. extern enum cl_kernel chosen_kernel;
  334. #endif /* __MINER_H__ */