miner.h 8.2 KB

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