miner.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. #ifdef HAVE_OPENCL
  11. #ifdef __APPLE_CC__
  12. #include <OpenCL/opencl.h>
  13. #else
  14. #include <CL/cl.h>
  15. #endif
  16. #endif /* HAVE_OPENCL */
  17. #ifdef STDC_HEADERS
  18. # include <stdlib.h>
  19. # include <stddef.h>
  20. #else
  21. # ifdef HAVE_STDLIB_H
  22. # include <stdlib.h>
  23. # endif
  24. #endif
  25. #ifdef HAVE_ALLOCA_H
  26. # include <alloca.h>
  27. #elif defined __GNUC__
  28. # ifndef WIN32
  29. # define alloca __builtin_alloca
  30. # else
  31. # include <malloc.h>
  32. # endif
  33. #elif defined _AIX
  34. # define alloca __alloca
  35. #elif defined _MSC_VER
  36. # include <malloc.h>
  37. # define alloca _alloca
  38. #else
  39. # ifndef HAVE_ALLOCA
  40. # ifdef __cplusplus
  41. extern "C"
  42. # endif
  43. void *alloca (size_t);
  44. # endif
  45. #endif
  46. #ifdef __SSE2__
  47. #define WANT_SSE2_4WAY 1
  48. #endif
  49. #if defined(__i386__) || defined(__x86_64__)
  50. #define WANT_VIA_PADLOCK 1
  51. #endif
  52. #if defined(__x86_64__) && defined(HAS_YASM)
  53. #define WANT_X8664_SSE2 1
  54. #endif
  55. #if defined(__x86_64__) && defined(HAS_YASM)
  56. #define WANT_X8664_SSE4 1
  57. #endif
  58. #if !defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  59. #define WANT_BUILTIN_BSWAP
  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_INFO,
  101. LOG_DEBUG,
  102. };
  103. #endif
  104. #undef unlikely
  105. #undef likely
  106. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  107. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  108. #define likely(expr) (__builtin_expect(!!(expr), 1))
  109. #else
  110. #define unlikely(expr) (expr)
  111. #define likely(expr) (expr)
  112. #endif
  113. #if defined(__i386__)
  114. #define WANT_CRYPTOPP_ASM32
  115. #endif
  116. #ifndef ARRAY_SIZE
  117. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  118. #endif
  119. enum alive {
  120. LIFE_WELL,
  121. LIFE_SICK,
  122. LIFE_DEAD,
  123. };
  124. struct cgpu_info {
  125. int is_gpu;
  126. int cpu_gpu;
  127. int accepted;
  128. int rejected;
  129. int hw_errors;
  130. double rolling;
  131. double total_mhashes;
  132. unsigned int getworks;
  133. double efficiency;
  134. double utility;
  135. enum alive status;
  136. char init[40];
  137. struct timeval last_message_tv;
  138. };
  139. struct thr_info {
  140. int id;
  141. pthread_t *pth;
  142. struct thread_q *q;
  143. struct cgpu_info *cgpu;
  144. struct timeval last;
  145. bool getwork;
  146. double rolling;
  147. };
  148. extern inline int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  149. static inline uint32_t swab32(uint32_t v)
  150. {
  151. #ifdef WANT_BUILTIN_BSWAP
  152. return __builtin_bswap32(v);
  153. #else
  154. return bswap_32(v);
  155. #endif
  156. }
  157. static inline void swap256(void *dest_p, const void *src_p)
  158. {
  159. uint32_t *dest = dest_p;
  160. const uint32_t *src = src_p;
  161. dest[0] = src[7];
  162. dest[1] = src[6];
  163. dest[2] = src[5];
  164. dest[3] = src[4];
  165. dest[4] = src[3];
  166. dest[5] = src[2];
  167. dest[6] = src[1];
  168. dest[7] = src[0];
  169. }
  170. extern void quit(int status, const char *format, ...);
  171. static inline void mutex_lock(pthread_mutex_t *lock)
  172. {
  173. if (unlikely(pthread_mutex_lock(lock)))
  174. quit(1, "WTF MUTEX ERROR ON LOCK!");
  175. }
  176. static inline void mutex_unlock(pthread_mutex_t *lock)
  177. {
  178. if (unlikely(pthread_mutex_unlock(lock)))
  179. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  180. }
  181. struct pool;
  182. extern bool opt_debug;
  183. extern bool opt_protocol;
  184. extern bool opt_log_output;
  185. extern const uint32_t sha256_init_state[];
  186. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  187. const char *rpc_req, bool, bool, bool *,
  188. struct pool *pool);
  189. extern char *bin2hex(const unsigned char *p, size_t len);
  190. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  191. extern unsigned int ScanHash_4WaySSE2(int, const unsigned char *pmidstate,
  192. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  193. const unsigned char *ptarget,
  194. uint32_t max_nonce, unsigned long *nHashesDone, uint32_t nonce);
  195. extern unsigned int scanhash_sse2_amd64(int, const unsigned char *pmidstate,
  196. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  197. const unsigned char *ptarget,
  198. uint32_t max_nonce, unsigned long *nHashesDone);
  199. extern bool scanhash_via(int, unsigned char *data_inout,
  200. const unsigned char *target,
  201. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  202. extern bool scanhash_c(int, const unsigned char *midstate, unsigned char *data,
  203. unsigned char *hash1, unsigned char *hash,
  204. const unsigned char *target,
  205. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  206. extern bool scanhash_cryptopp(int, const unsigned char *midstate,unsigned char *data,
  207. unsigned char *hash1, unsigned char *hash,
  208. const unsigned char *target,
  209. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  210. extern bool scanhash_asm32(int, const unsigned char *midstate,unsigned char *data,
  211. unsigned char *hash1, unsigned char *hash,
  212. const unsigned char *target,
  213. uint32_t max_nonce, unsigned long *hashes_done, uint32_t nonce);
  214. extern int scanhash_sse2_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  215. unsigned char *phash1, unsigned char *phash,
  216. const unsigned char *ptarget,
  217. uint32_t max_nonce, unsigned long *nHashesDone,
  218. uint32_t nonce);
  219. extern int scanhash_sse4_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  220. unsigned char *phash1, unsigned char *phash,
  221. const unsigned char *ptarget,
  222. uint32_t max_nonce, unsigned long *nHashesDone,
  223. uint32_t nonce);
  224. extern int
  225. timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);
  226. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  227. extern int opt_scantime;
  228. struct thread_q;
  229. struct work_restart {
  230. volatile unsigned long restart;
  231. char padding[128 - sizeof(unsigned long)];
  232. };
  233. extern int hw_errors;
  234. extern bool use_syslog;
  235. extern struct thr_info *thr_info;
  236. extern int longpoll_thr_id;
  237. extern struct work_restart *work_restart;
  238. extern pthread_mutex_t control_lock;
  239. #ifdef HAVE_OPENCL
  240. typedef struct {
  241. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  242. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  243. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  244. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  245. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  246. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  247. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  248. cl_uint W16; cl_uint W17; cl_uint W2;
  249. cl_uint PreVal4; cl_uint T1;
  250. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  251. cl_uint PreVal4addT1; cl_uint T1substate0;
  252. cl_uint PreVal4_2;
  253. cl_uint PreVal0;
  254. cl_uint PreW18;
  255. cl_uint PreW19;
  256. cl_uint PreW31;
  257. cl_uint PreW32;
  258. } dev_blk_ctx;
  259. #else
  260. typedef struct {
  261. uint32_t nonce;
  262. } dev_blk_ctx;
  263. #endif
  264. struct pool {
  265. int pool_no;
  266. int prio;
  267. int accepted, rejected;
  268. bool submit_fail;
  269. bool idle;
  270. bool probed;
  271. bool enabled;
  272. char *hdr_path;
  273. unsigned int getwork_requested;
  274. unsigned int stale_shares;
  275. unsigned int discarded_work;
  276. unsigned int localgen_occasions;
  277. unsigned int remotefail_occasions;
  278. struct timeval tv_idle;
  279. char *rpc_url;
  280. char *rpc_userpass;
  281. char *rpc_user, *rpc_pass;
  282. pthread_mutex_t pool_lock;
  283. };
  284. struct work {
  285. unsigned char data[128];
  286. unsigned char hash1[64];
  287. unsigned char midstate[32];
  288. unsigned char target[32];
  289. unsigned char hash[32];
  290. int rolls;
  291. uint32_t output[1];
  292. uint32_t valid;
  293. dev_blk_ctx blk;
  294. struct thr_info *thr;
  295. int thr_id;
  296. struct pool *pool;
  297. struct timeval tv_staged;
  298. bool mined;
  299. bool clone;
  300. bool cloned;
  301. bool rolltime;
  302. };
  303. enum cl_kernel {
  304. KL_NONE,
  305. KL_POCLBM,
  306. KL_PHATK,
  307. };
  308. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  309. extern void kill_work(void);
  310. extern void log_curses(int prio, const char *f, va_list ap);
  311. extern void vapplog(int prio, const char *fmt, va_list ap);
  312. extern void applog(int prio, const char *fmt, ...);
  313. extern struct thread_q *tq_new(void);
  314. extern void tq_free(struct thread_q *tq);
  315. extern bool tq_push_head(struct thread_q *tq, void *data);
  316. extern bool tq_push(struct thread_q *tq, void *data);
  317. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  318. extern void tq_freeze(struct thread_q *tq);
  319. extern void tq_thaw(struct thread_q *tq);
  320. extern bool test_and_set(bool *var);
  321. extern bool test_and_clear(bool *var);
  322. extern bool successful_connect;
  323. extern enum cl_kernel chosen_kernel;
  324. #endif /* __MINER_H__ */