miner.h 10.0 KB

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