miner.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. struct cgpu_info {
  120. int is_gpu;
  121. int cpu_gpu;
  122. int accepted;
  123. int rejected;
  124. int hw_errors;
  125. double rolling;
  126. double total_mhashes;
  127. unsigned int getworks;
  128. double efficiency;
  129. double utility;
  130. bool alive;
  131. };
  132. struct thr_info {
  133. int id;
  134. pthread_t *pth;
  135. struct thread_q *q;
  136. struct cgpu_info *cgpu;
  137. struct timeval last;
  138. bool getwork;
  139. double rolling;
  140. };
  141. extern inline int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  142. static inline uint32_t swab32(uint32_t v)
  143. {
  144. #ifdef WANT_BUILTIN_BSWAP
  145. return __builtin_bswap32(v);
  146. #else
  147. return bswap_32(v);
  148. #endif
  149. }
  150. static inline void swap256(void *dest_p, const void *src_p)
  151. {
  152. uint32_t *dest = dest_p;
  153. const uint32_t *src = src_p;
  154. dest[0] = src[7];
  155. dest[1] = src[6];
  156. dest[2] = src[5];
  157. dest[3] = src[4];
  158. dest[4] = src[3];
  159. dest[5] = src[2];
  160. dest[6] = src[1];
  161. dest[7] = src[0];
  162. }
  163. extern void quit(int status, const char *format, ...);
  164. static inline void mutex_lock(pthread_mutex_t *lock)
  165. {
  166. if (unlikely(pthread_mutex_lock(lock)))
  167. quit(1, "WTF MUTEX ERROR ON LOCK!");
  168. }
  169. static inline void mutex_unlock(pthread_mutex_t *lock)
  170. {
  171. if (unlikely(pthread_mutex_unlock(lock)))
  172. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  173. }
  174. struct pool;
  175. extern bool opt_debug;
  176. extern bool opt_protocol;
  177. extern bool opt_log_output;
  178. extern const uint32_t sha256_init_state[];
  179. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  180. const char *rpc_req, bool, bool, struct pool *pool);
  181. extern char *bin2hex(const unsigned char *p, size_t len);
  182. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  183. extern unsigned int ScanHash_4WaySSE2(int, const unsigned char *pmidstate,
  184. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  185. const unsigned char *ptarget,
  186. uint32_t max_nonce, unsigned long *nHashesDone, uint32_t nonce);
  187. extern unsigned int scanhash_sse2_amd64(int, const unsigned char *pmidstate,
  188. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  189. const unsigned char *ptarget,
  190. uint32_t max_nonce, unsigned long *nHashesDone);
  191. extern bool scanhash_via(int, unsigned char *data_inout,
  192. const unsigned char *target,
  193. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  194. extern bool scanhash_c(int, const unsigned char *midstate, unsigned char *data,
  195. unsigned char *hash1, unsigned char *hash,
  196. const unsigned char *target,
  197. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  198. extern bool scanhash_cryptopp(int, const unsigned char *midstate,unsigned char *data,
  199. unsigned char *hash1, unsigned char *hash,
  200. const unsigned char *target,
  201. uint32_t max_nonce, unsigned long *hashes_done, uint32_t n);
  202. extern bool scanhash_asm32(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 nonce);
  206. extern int scanhash_sse2_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  207. unsigned char *phash1, unsigned char *phash,
  208. const unsigned char *ptarget,
  209. uint32_t max_nonce, unsigned long *nHashesDone,
  210. uint32_t nonce);
  211. extern int scanhash_sse4_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  212. unsigned char *phash1, unsigned char *phash,
  213. const unsigned char *ptarget,
  214. uint32_t max_nonce, unsigned long *nHashesDone,
  215. uint32_t nonce);
  216. extern int
  217. timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);
  218. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  219. extern int opt_scantime;
  220. extern bool want_longpoll;
  221. extern bool have_longpoll;
  222. struct thread_q;
  223. struct work_restart {
  224. volatile unsigned long restart;
  225. char padding[128 - sizeof(unsigned long)];
  226. };
  227. extern int hw_errors;
  228. extern bool use_syslog;
  229. extern struct thr_info *thr_info;
  230. extern int longpoll_thr_id;
  231. extern struct work_restart *work_restart;
  232. extern pthread_mutex_t control_lock;
  233. #ifdef HAVE_OPENCL
  234. typedef struct {
  235. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  236. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  237. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  238. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  239. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  240. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  241. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  242. cl_uint W16; cl_uint W17; cl_uint W2;
  243. cl_uint PreVal4; cl_uint T1;
  244. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  245. cl_uint PreVal4addT1; cl_uint T1substate0;
  246. } dev_blk_ctx;
  247. #else
  248. typedef struct {
  249. uint32_t nonce;
  250. } dev_blk_ctx;
  251. #endif
  252. struct pool {
  253. int pool_no;
  254. int prio;
  255. int accepted, rejected;
  256. bool submit_fail;
  257. bool idle;
  258. bool has_rolltime;
  259. bool probed;
  260. bool enabled;
  261. unsigned int getwork_requested;
  262. unsigned int stale_shares;
  263. unsigned int discarded_work;
  264. unsigned int localgen_occasions;
  265. unsigned int remotefail_occasions;
  266. struct timeval tv_idle;
  267. char *rpc_url;
  268. char *rpc_userpass;
  269. char *rpc_user, *rpc_pass;
  270. pthread_mutex_t pool_lock;
  271. };
  272. struct work {
  273. unsigned char data[128];
  274. unsigned char hash1[64];
  275. unsigned char midstate[32];
  276. unsigned char target[32];
  277. unsigned char hash[32];
  278. int rolls;
  279. uint32_t output[1];
  280. uint32_t valid;
  281. dev_blk_ctx blk;
  282. int thr_id;
  283. struct pool *pool;
  284. struct timeval tv_staged;
  285. bool clone;
  286. bool cloned;
  287. };
  288. enum cl_kernel {
  289. KL_NONE,
  290. KL_POCLBM,
  291. KL_PHATK,
  292. };
  293. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  294. extern void kill_work(void);
  295. extern void log_curses(int prio, const char *f, va_list ap);
  296. extern void vapplog(int prio, const char *fmt, va_list ap);
  297. extern void applog(int prio, const char *fmt, ...);
  298. extern struct thread_q *tq_new(void);
  299. extern void tq_free(struct thread_q *tq);
  300. extern bool tq_push_head(struct thread_q *tq, void *data);
  301. extern bool tq_push(struct thread_q *tq, void *data);
  302. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  303. extern void tq_freeze(struct thread_q *tq);
  304. extern void tq_thaw(struct thread_q *tq);
  305. extern bool test_and_set(bool *var);
  306. extern bool test_and_clear(bool *var);
  307. extern bool successful_connect;
  308. extern enum cl_kernel chosen_kernel;
  309. #endif /* __MINER_H__ */