miner.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. #ifndef __MINER_H__
  2. #define __MINER_H__
  3. #include "config.h"
  4. #ifdef WIN32
  5. #include <winsock2.h>
  6. #endif
  7. #include <stdbool.h>
  8. #include <stdint.h>
  9. #include <sys/time.h>
  10. #include <pthread.h>
  11. #include <jansson.h>
  12. #include <curl/curl.h>
  13. #include <blkmaker.h>
  14. #include <blktemplate.h>
  15. #if defined(WORDS_BIGENDIAN) && !defined(__BIG_ENDIAN__)
  16. /* uthash.h depends on __BIG_ENDIAN__ on BE platforms */
  17. #define __BIG_ENDIAN__ 1
  18. #endif
  19. #include "elist.h"
  20. #include "uthash.h"
  21. #include "logging.h"
  22. #include "util.h"
  23. #ifdef HAVE_OPENCL
  24. #include "CL/cl.h"
  25. #endif /* HAVE_OPENCL */
  26. #ifdef STDC_HEADERS
  27. # include <stdlib.h>
  28. # include <stddef.h>
  29. #else
  30. # ifdef HAVE_STDLIB_H
  31. # include <stdlib.h>
  32. # endif
  33. #endif
  34. #ifdef HAVE_ALLOCA_H
  35. # include <alloca.h>
  36. #elif defined __GNUC__
  37. # ifndef WIN32
  38. # define alloca __builtin_alloca
  39. # else
  40. # include <malloc.h>
  41. # endif
  42. #elif defined _AIX
  43. # define alloca __alloca
  44. #elif defined _MSC_VER
  45. # include <malloc.h>
  46. # define alloca _alloca
  47. #else
  48. # ifndef HAVE_ALLOCA
  49. # ifdef __cplusplus
  50. extern "C"
  51. # endif
  52. void *alloca (size_t);
  53. # endif
  54. #endif
  55. #ifdef __MINGW32__
  56. #include <windows.h>
  57. #include <io.h>
  58. static inline int fsync (int fd)
  59. {
  60. return (FlushFileBuffers ((HANDLE) _get_osfhandle (fd))) ? 0 : -1;
  61. }
  62. #ifndef EWOULDBLOCK
  63. # define EWOULDBLOCK EAGAIN
  64. #endif
  65. #ifndef MSG_DONTWAIT
  66. # define MSG_DONTWAIT 0x1000000
  67. #endif
  68. #endif /* __MINGW32__ */
  69. #if defined (__linux)
  70. #ifndef LINUX
  71. #define LINUX
  72. #endif
  73. #endif
  74. #ifdef HAVE_ADL
  75. #include "ADL/adl_sdk.h"
  76. #endif
  77. #ifdef HAVE_LIBUSB
  78. #include <libusb.h>
  79. #endif
  80. #ifdef USE_ZTEX
  81. #include "libztex.h"
  82. #endif
  83. #ifdef HAVE_BYTESWAP_H
  84. #include <byteswap.h>
  85. #endif
  86. #ifdef HAVE_ENDIAN_H
  87. #include <endian.h>
  88. #endif
  89. #ifdef HAVE_SYS_ENDIAN_H
  90. #include <sys/endian.h>
  91. #endif
  92. #ifdef HAVE_LIBKERN_OSBYTEORDER_H
  93. #include <libkern/OSByteOrder.h>
  94. #endif
  95. #ifndef bswap_16
  96. #define bswap_16(value) \
  97. ((((value) & 0xff) << 8) | ((value) >> 8))
  98. #define bswap_32(value) \
  99. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  100. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  101. #define bswap_64(value) \
  102. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  103. << 32) | \
  104. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  105. #endif
  106. /* This assumes htobe32 is a macro and that if it doesn't exist, then the
  107. * also won't exist */
  108. #ifndef htobe32
  109. # ifndef WORDS_BIGENDIAN
  110. # define htole16(x) (x)
  111. # define htole32(x) (x)
  112. # define htole64(x) (x)
  113. # define htobe32(x) bswap_32(x)
  114. # define htobe64(x) bswap_64(x)
  115. # else
  116. # define htole16(x) bswap_16(x)
  117. # define htole32(x) bswap_32(x)
  118. # define htole64(x) bswap_64(x)
  119. # define htobe32(x) (x)
  120. # define htobe64(x) (x)
  121. # endif
  122. #endif
  123. #ifndef be32toh
  124. # define le32toh(x) htole32(x)
  125. # define le64toh(x) htole64(x)
  126. # define be32toh(x) htobe32(x)
  127. # define be64toh(x) htobe64(x)
  128. #endif
  129. #ifndef max
  130. # define max(a, b) ((a) > (b) ? (a) : (b))
  131. #endif
  132. #undef unlikely
  133. #undef likely
  134. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  135. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  136. #define likely(expr) (__builtin_expect(!!(expr), 1))
  137. #else
  138. #define unlikely(expr) (expr)
  139. #define likely(expr) (expr)
  140. #endif
  141. #ifndef __maybe_unused
  142. #define __maybe_unused __attribute__((unused))
  143. #endif
  144. #define uninitialised_var(x) x = x
  145. #if defined(__i386__)
  146. #define WANT_CRYPTOPP_ASM32
  147. #endif
  148. #ifndef ARRAY_SIZE
  149. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  150. #endif
  151. #ifdef HAVE_CURSES
  152. extern int my_cancellable_getch(void);
  153. # ifdef getch
  154. // getch() is a macro
  155. static int __maybe_unused __real_getch(void) {
  156. return getch();
  157. }
  158. # undef getch
  159. # define getch() my_cancellable_getch()
  160. # else
  161. // getch() is a real function
  162. # define __real_getch getch
  163. # define getch() my_cancellable_getch()
  164. # endif
  165. #endif
  166. enum alive {
  167. LIFE_WELL,
  168. LIFE_SICK,
  169. LIFE_DEAD,
  170. LIFE_NOSTART,
  171. LIFE_INIT,
  172. LIFE_WAIT,
  173. };
  174. enum pool_strategy {
  175. POOL_FAILOVER,
  176. POOL_ROUNDROBIN,
  177. POOL_ROTATE,
  178. POOL_LOADBALANCE,
  179. POOL_BALANCE,
  180. };
  181. #define TOP_STRATEGY (POOL_BALANCE)
  182. struct strategies {
  183. const char *s;
  184. };
  185. struct cgpu_info;
  186. #ifdef HAVE_ADL
  187. struct gpu_adl {
  188. ADLTemperature lpTemperature;
  189. int iAdapterIndex;
  190. int lpAdapterID;
  191. int iBusNumber;
  192. char strAdapterName[256];
  193. ADLPMActivity lpActivity;
  194. ADLODParameters lpOdParameters;
  195. ADLODPerformanceLevels *DefPerfLev;
  196. ADLFanSpeedInfo lpFanSpeedInfo;
  197. ADLFanSpeedValue lpFanSpeedValue;
  198. ADLFanSpeedValue DefFanSpeedValue;
  199. int iEngineClock;
  200. int iMemoryClock;
  201. int iVddc;
  202. int iPercentage;
  203. bool autofan;
  204. bool autoengine;
  205. bool managed; /* Were the values ever changed on this card */
  206. int lastengine;
  207. int lasttemp;
  208. int targetfan;
  209. int overtemp;
  210. int minspeed;
  211. int maxspeed;
  212. int gpu;
  213. bool has_fanspeed;
  214. struct gpu_adl *twin;
  215. };
  216. #endif
  217. struct api_data;
  218. struct thr_info;
  219. struct work;
  220. struct device_api {
  221. const char *dname;
  222. const char *name;
  223. // API-global functions
  224. void (*api_detect)();
  225. // Device-specific functions
  226. void (*get_dev_statline_before)(char *, struct cgpu_info *);
  227. void (*get_dev_statline_after)(char *, struct cgpu_info *);
  228. // Processor-specific functions
  229. void (*reinit_device)(struct cgpu_info *);
  230. void (*get_statline_before)(char *, struct cgpu_info *);
  231. void (*get_statline)(char *, struct cgpu_info *);
  232. struct api_data* (*get_api_extra_device_detail)(struct cgpu_info *);
  233. struct api_data* (*get_api_extra_device_status)(struct cgpu_info *);
  234. struct api_data *(*get_api_stats)(struct cgpu_info *);
  235. bool (*get_stats)(struct cgpu_info *);
  236. bool (*identify_device)(struct cgpu_info *); // e.g. to flash a led
  237. char *(*set_device)(struct cgpu_info *, char *option, char *setting, char *replybuf);
  238. // Thread-specific functions
  239. bool (*thread_prepare)(struct thr_info *);
  240. void (*minerloop)(struct thr_info *);
  241. uint64_t (*can_limit_work)(struct thr_info *);
  242. bool (*thread_init)(struct thr_info *);
  243. bool (*prepare_work)(struct thr_info *, struct work *);
  244. #ifdef USE_AVALON
  245. int64_t (*scanhash_queue)(struct thr_info *, struct work **, int64_t);
  246. #endif
  247. int64_t (*scanhash)(struct thr_info *, struct work *, int64_t);
  248. void (*hw_error)(struct thr_info *);
  249. void (*thread_shutdown)(struct thr_info *);
  250. void (*thread_enable)(struct thr_info *);
  251. // Can be used per-thread or per-processor (only with minerloop async or queue!)
  252. void (*poll)(struct thr_info *);
  253. // === Implemented by minerloop_async ===
  254. bool (*job_prepare)(struct thr_info*, struct work*, uint64_t);
  255. void (*job_start)(struct thr_info*);
  256. void (*job_get_results)(struct thr_info*, struct work*);
  257. int64_t (*job_process_results)(struct thr_info*, struct work*, bool stopping);
  258. // === Implemented by minerloop_queue ===
  259. bool (*queue_append)(struct thr_info *, struct work *);
  260. void (*queue_flush)(struct thr_info *);
  261. };
  262. enum dev_enable {
  263. DEV_ENABLED,
  264. DEV_DISABLED,
  265. DEV_RECOVER,
  266. DEV_RECOVER_ERR,
  267. };
  268. enum cl_kernels {
  269. KL_NONE,
  270. KL_POCLBM,
  271. KL_PHATK,
  272. KL_DIAKGCN,
  273. KL_DIABLO,
  274. KL_SCRYPT,
  275. };
  276. enum dev_reason {
  277. REASON_THREAD_FAIL_INIT,
  278. REASON_THREAD_ZERO_HASH,
  279. REASON_THREAD_FAIL_QUEUE,
  280. REASON_DEV_SICK_IDLE_60,
  281. REASON_DEV_DEAD_IDLE_600,
  282. REASON_DEV_NOSTART,
  283. REASON_DEV_OVER_HEAT,
  284. REASON_DEV_THERMAL_CUTOFF,
  285. REASON_DEV_COMMS_ERROR,
  286. REASON_DEV_THROTTLE,
  287. };
  288. #define REASON_NONE "None"
  289. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  290. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  291. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  292. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  293. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  294. #define REASON_DEV_NOSTART_STR "Device failed to start"
  295. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  296. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  297. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  298. #define REASON_DEV_THROTTLE_STR "Device throttle"
  299. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  300. #define MIN_SEC_UNSET 99999999
  301. enum {
  302. MSG_NOPOOL = 8,
  303. MSG_MISPID = 25,
  304. MSG_INVPID = 26,
  305. MSG_DUPPID = 74,
  306. MSG_POOLPRIO = 73,
  307. };
  308. struct cgminer_stats {
  309. uint32_t getwork_calls;
  310. struct timeval getwork_wait;
  311. struct timeval getwork_wait_max;
  312. struct timeval getwork_wait_min;
  313. struct timeval _get_start;
  314. };
  315. // Just the actual network getworks to the pool
  316. struct cgminer_pool_stats {
  317. uint32_t getwork_calls;
  318. uint32_t getwork_attempts;
  319. struct timeval getwork_wait;
  320. struct timeval getwork_wait_max;
  321. struct timeval getwork_wait_min;
  322. double getwork_wait_rolling;
  323. bool hadrolltime;
  324. bool canroll;
  325. bool hadexpire;
  326. uint32_t rolltime;
  327. double min_diff;
  328. double max_diff;
  329. double last_diff;
  330. uint32_t min_diff_count;
  331. uint32_t max_diff_count;
  332. uint64_t times_sent;
  333. uint64_t bytes_sent;
  334. uint64_t net_bytes_sent;
  335. uint64_t times_received;
  336. uint64_t bytes_received;
  337. uint64_t net_bytes_received;
  338. };
  339. #define PRIprepr "-6s"
  340. #define PRIpreprv "s"
  341. struct cgpu_info {
  342. int cgminer_id;
  343. int device_line_id;
  344. const struct device_api *api;
  345. const char *devtype;
  346. int device_id;
  347. char *dev_repr;
  348. char *dev_repr_ns;
  349. const char *name;
  350. int procs;
  351. int proc_id;
  352. char proc_repr[8];
  353. char proc_repr_ns[8];
  354. struct cgpu_info *device;
  355. struct cgpu_info *next_proc;
  356. const char *device_path;
  357. FILE *device_file;
  358. union {
  359. #ifdef USE_ZTEX
  360. struct libztex_device *device_ztex;
  361. #endif
  362. int device_fd;
  363. #ifdef USE_X6500
  364. struct ft232r_device_handle *device_ft232r;
  365. #endif
  366. };
  367. #ifdef USE_BITFORCE
  368. struct timeval work_start_tv;
  369. unsigned int wait_ms;
  370. unsigned int sleep_ms;
  371. double avg_wait_f;
  372. unsigned int avg_wait_d;
  373. uint32_t nonces;
  374. bool polling;
  375. #endif
  376. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  377. bool flash_led;
  378. #endif
  379. void *cgpu_data;
  380. pthread_mutex_t device_mutex;
  381. pthread_cond_t device_cond;
  382. enum dev_enable deven;
  383. int accepted;
  384. int rejected;
  385. int hw_errors;
  386. double rolling;
  387. double total_mhashes;
  388. double utility;
  389. double utility_diff1;
  390. enum alive status;
  391. char init[40];
  392. struct timeval last_message_tv;
  393. int threads;
  394. struct thr_info **thr;
  395. int64_t max_hashes;
  396. const char *kname;
  397. #ifdef HAVE_OPENCL
  398. bool mapped;
  399. int virtual_gpu;
  400. int virtual_adl;
  401. int intensity;
  402. bool dynamic;
  403. cl_uint vwidth;
  404. size_t work_size;
  405. enum cl_kernels kernel;
  406. cl_ulong max_alloc;
  407. #ifdef USE_SCRYPT
  408. int opt_lg, lookup_gap;
  409. size_t opt_tc, thread_concurrency;
  410. size_t shaders;
  411. #endif
  412. struct timeval tv_gpustart;
  413. int intervals;
  414. #endif
  415. float temp;
  416. int cutofftemp;
  417. int targettemp;
  418. #ifdef HAVE_ADL
  419. bool has_adl;
  420. struct gpu_adl adl;
  421. int gpu_engine;
  422. int min_engine;
  423. int gpu_fan;
  424. int min_fan;
  425. int gpu_memclock;
  426. int gpu_memdiff;
  427. int gpu_powertune;
  428. float gpu_vddc;
  429. #endif
  430. int diff1;
  431. double diff_accepted;
  432. double diff_rejected;
  433. int last_share_pool;
  434. time_t last_share_pool_time;
  435. double last_share_diff;
  436. time_t device_last_well;
  437. time_t device_last_not_well;
  438. enum dev_reason device_not_well_reason;
  439. float reinit_backoff;
  440. int thread_fail_init_count;
  441. int thread_zero_hash_count;
  442. int thread_fail_queue_count;
  443. int dev_sick_idle_60_count;
  444. int dev_dead_idle_600_count;
  445. int dev_nostart_count;
  446. int dev_over_heat_count; // It's a warning but worth knowing
  447. int dev_thermal_cutoff_count;
  448. int dev_comms_error_count;
  449. int dev_throttle_count;
  450. struct cgminer_stats cgminer_stats;
  451. };
  452. extern void renumber_cgpu(struct cgpu_info *);
  453. extern bool add_cgpu(struct cgpu_info*);
  454. struct thread_q {
  455. struct list_head q;
  456. bool frozen;
  457. pthread_mutex_t mutex;
  458. pthread_cond_t cond;
  459. };
  460. enum thr_busy_state {
  461. TBS_IDLE,
  462. TBS_GETTING_RESULTS,
  463. TBS_STARTING_JOB,
  464. };
  465. struct thr_info {
  466. int id;
  467. int device_thread;
  468. bool primary_thread;
  469. pthread_t pth;
  470. struct thread_q *q;
  471. struct cgpu_info *cgpu;
  472. void *cgpu_data;
  473. struct timeval last;
  474. struct timeval sick;
  475. bool scanhash_working;
  476. uint64_t hashes_done;
  477. struct timeval tv_hashes_done;
  478. struct timeval tv_lastupdate;
  479. bool pause;
  480. time_t getwork;
  481. double rolling;
  482. // Used by minerloop_async
  483. struct work *prev_work;
  484. struct work *work;
  485. struct work *next_work;
  486. enum thr_busy_state busy_state;
  487. struct timeval tv_morework;
  488. struct work *results_work;
  489. bool _job_transition_in_progress;
  490. bool _proceed_with_new_job;
  491. struct timeval tv_results_jobstart;
  492. struct timeval tv_jobstart;
  493. struct timeval tv_poll;
  494. notifier_t notifier;
  495. bool starting_next_work;
  496. uint32_t _max_nonce;
  497. notifier_t mutex_request;
  498. // Used by minerloop_queue
  499. struct list_head work_list;
  500. bool queue_full;
  501. bool _last_sbr_state;
  502. bool work_restart;
  503. notifier_t work_restart_notifier;
  504. };
  505. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  506. extern void thr_info_cancel(struct thr_info *thr);
  507. extern void thr_info_freeze(struct thr_info *thr);
  508. extern void nmsleep(unsigned int msecs);
  509. extern double us_tdiff(struct timeval *end, struct timeval *start);
  510. extern double tdiff(struct timeval *end, struct timeval *start);
  511. struct string_elist {
  512. char *string;
  513. bool free_me;
  514. struct list_head list;
  515. };
  516. static inline void string_elist_add(const char *s, struct list_head *head)
  517. {
  518. struct string_elist *n;
  519. n = calloc(1, sizeof(*n));
  520. n->string = strdup(s);
  521. n->free_me = true;
  522. list_add_tail(&n->list, head);
  523. }
  524. static inline void string_elist_del(struct string_elist *item)
  525. {
  526. if (item->free_me)
  527. free(item->string);
  528. list_del(&item->list);
  529. free(item);
  530. }
  531. static inline uint32_t swab32(uint32_t v)
  532. {
  533. return bswap_32(v);
  534. }
  535. static inline void swap256(void *dest_p, const void *src_p)
  536. {
  537. uint32_t *dest = dest_p;
  538. const uint32_t *src = src_p;
  539. dest[0] = src[7];
  540. dest[1] = src[6];
  541. dest[2] = src[5];
  542. dest[3] = src[4];
  543. dest[4] = src[3];
  544. dest[5] = src[2];
  545. dest[6] = src[1];
  546. dest[7] = src[0];
  547. }
  548. static inline void swap32yes(void*out, const void*in, size_t sz) {
  549. size_t swapcounter = 0;
  550. for (swapcounter = 0; swapcounter < sz; ++swapcounter)
  551. (((uint32_t*)out)[swapcounter]) = swab32(((uint32_t*)in)[swapcounter]);
  552. }
  553. #define LOCAL_swap32(type, var, sz) \
  554. type __swapped_ ## var[sz * 4 / sizeof(type)]; \
  555. swap32yes(__swapped_ ## var, var, sz); \
  556. var = __swapped_ ## var; \
  557. // end
  558. #ifdef WORDS_BIGENDIAN
  559. # define swap32tobe(out, in, sz) ((out == in) ? (void)0 : memmove(out, in, sz))
  560. # define LOCAL_swap32be(type, var, sz) ;
  561. # define swap32tole(out, in, sz) swap32yes(out, in, sz)
  562. # define LOCAL_swap32le(type, var, sz) LOCAL_swap32(type, var, sz)
  563. #else
  564. # define swap32tobe(out, in, sz) swap32yes(out, in, sz)
  565. # define LOCAL_swap32be(type, var, sz) LOCAL_swap32(type, var, sz)
  566. # define swap32tole(out, in, sz) ((out == in) ? (void)0 : memmove(out, in, sz))
  567. # define LOCAL_swap32le(type, var, sz) ;
  568. #endif
  569. static inline void swab256(void *dest_p, const void *src_p)
  570. {
  571. uint32_t *dest = dest_p;
  572. const uint32_t *src = src_p;
  573. dest[0] = swab32(src[7]);
  574. dest[1] = swab32(src[6]);
  575. dest[2] = swab32(src[5]);
  576. dest[3] = swab32(src[4]);
  577. dest[4] = swab32(src[3]);
  578. dest[5] = swab32(src[2]);
  579. dest[6] = swab32(src[1]);
  580. dest[7] = swab32(src[0]);
  581. }
  582. #define flip32(dest_p, src_p) swap32yes(dest_p, src_p, 32 / 4)
  583. extern void quit(int status, const char *format, ...) NORETURN FORMAT_SYNTAX_CHECK(printf, 2, 3);
  584. static inline void mutex_lock(pthread_mutex_t *lock)
  585. {
  586. if (unlikely(pthread_mutex_lock(lock)))
  587. quit(1, "WTF MUTEX ERROR ON LOCK!");
  588. }
  589. static inline void mutex_unlock(pthread_mutex_t *lock)
  590. {
  591. if (unlikely(pthread_mutex_unlock(lock)))
  592. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  593. }
  594. static inline int mutex_trylock(pthread_mutex_t *lock)
  595. {
  596. return pthread_mutex_trylock(lock);
  597. }
  598. static inline void wr_lock(pthread_rwlock_t *lock)
  599. {
  600. if (unlikely(pthread_rwlock_wrlock(lock)))
  601. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  602. }
  603. static inline void rd_lock(pthread_rwlock_t *lock)
  604. {
  605. if (unlikely(pthread_rwlock_rdlock(lock)))
  606. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  607. }
  608. static inline void rw_unlock(pthread_rwlock_t *lock)
  609. {
  610. if (unlikely(pthread_rwlock_unlock(lock)))
  611. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  612. }
  613. static inline void rd_unlock(pthread_rwlock_t *lock)
  614. {
  615. rw_unlock(lock);
  616. }
  617. static inline void wr_unlock(pthread_rwlock_t *lock)
  618. {
  619. rw_unlock(lock);
  620. }
  621. static inline void mutex_init(pthread_mutex_t *lock)
  622. {
  623. if (unlikely(pthread_mutex_init(lock, NULL)))
  624. quit(1, "Failed to pthread_mutex_init");
  625. }
  626. static inline void rwlock_init(pthread_rwlock_t *lock)
  627. {
  628. if (unlikely(pthread_rwlock_init(lock, NULL)))
  629. quit(1, "Failed to pthread_rwlock_init");
  630. }
  631. struct pool;
  632. extern bool opt_protocol;
  633. extern char *opt_coinbase_sig;
  634. extern bool have_longpoll;
  635. extern int opt_skip_checks;
  636. extern char *opt_kernel_path;
  637. extern char *opt_socks_proxy;
  638. extern char *cgminer_path;
  639. extern bool opt_fail_only;
  640. extern bool opt_autofan;
  641. extern bool opt_autoengine;
  642. extern bool use_curses;
  643. extern char *opt_api_allow;
  644. extern char *opt_api_groups;
  645. extern char *opt_api_description;
  646. extern int opt_api_port;
  647. extern bool opt_api_listen;
  648. extern bool opt_api_network;
  649. extern bool opt_delaynet;
  650. extern bool opt_restart;
  651. extern char *opt_icarus_options;
  652. extern char *opt_icarus_timing;
  653. extern bool opt_worktime;
  654. extern char *opt_avalon_options;
  655. #ifdef USE_BITFORCE
  656. extern bool opt_bfl_noncerange;
  657. #endif
  658. extern int swork_id;
  659. extern pthread_rwlock_t netacc_lock;
  660. extern const uint32_t sha256_init_state[];
  661. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  662. const char *rpc_req, bool, bool, int *,
  663. struct pool *pool, bool);
  664. extern bool our_curl_supports_proxy_uris();
  665. extern char *bin2hex(const unsigned char *p, size_t len);
  666. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  667. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  668. unsigned char *pdata,
  669. unsigned char *phash1, unsigned char *phash,
  670. const unsigned char *ptarget,
  671. uint32_t max_nonce,
  672. uint32_t *last_nonce,
  673. uint32_t nonce);
  674. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  675. extern int opt_queue;
  676. extern int opt_scantime;
  677. extern int opt_expiry;
  678. extern pthread_mutex_t hash_lock;
  679. extern pthread_mutex_t console_lock;
  680. extern pthread_mutex_t ch_lock;
  681. extern void thread_reportin(struct thr_info *thr);
  682. extern void thread_reportout(struct thr_info *);
  683. extern void clear_stratum_shares(struct pool *pool);
  684. extern void hashmeter2(struct thr_info *);
  685. extern bool stale_work(struct work *, bool share);
  686. extern bool stale_work_future(struct work *, bool share, unsigned long ustime);
  687. extern void kill_work(void);
  688. extern void app_restart(void);
  689. extern void mt_enable(struct thr_info *thr);
  690. extern void proc_enable(struct cgpu_info *);
  691. extern void reinit_device(struct cgpu_info *cgpu);
  692. #ifdef HAVE_ADL
  693. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  694. extern int set_fanspeed(int gpu, int iFanSpeed);
  695. extern int set_vddc(int gpu, float fVddc);
  696. extern int set_engineclock(int gpu, int iEngineClock);
  697. extern int set_memoryclock(int gpu, int iMemoryClock);
  698. #endif
  699. extern void api(int thr_id);
  700. extern struct pool *current_pool(void);
  701. extern int enabled_pools;
  702. extern bool detect_stratum(struct pool *pool, char *url);
  703. extern void print_summary(void);
  704. extern struct pool *add_pool(void);
  705. extern void add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  706. #define MAX_GPUDEVICES 16
  707. #define MIN_INTENSITY -10
  708. #define _MIN_INTENSITY_STR "-10"
  709. #ifdef USE_SCRYPT
  710. #define MAX_INTENSITY 20
  711. #define _MAX_INTENSITY_STR "20"
  712. #else
  713. #define MAX_INTENSITY 14
  714. #define _MAX_INTENSITY_STR "14"
  715. #endif
  716. extern struct list_head scan_devices;
  717. extern bool opt_force_dev_init;
  718. extern int nDevs;
  719. extern int opt_n_threads;
  720. extern int num_processors;
  721. extern int hw_errors;
  722. extern bool use_syslog;
  723. extern bool opt_quiet;
  724. extern struct thr_info *thr_info;
  725. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  726. extern int gpu_threads;
  727. #ifdef USE_SCRYPT
  728. extern bool opt_scrypt;
  729. #else
  730. #define opt_scrypt (0)
  731. #endif
  732. extern double total_secs;
  733. extern int mining_threads;
  734. extern struct cgpu_info *cpus;
  735. extern int total_devices;
  736. extern struct cgpu_info **devices;
  737. extern int total_pools;
  738. extern struct pool **pools;
  739. extern const char *algo_names[];
  740. extern enum sha256_algos opt_algo;
  741. extern struct strategies strategies[];
  742. extern enum pool_strategy pool_strategy;
  743. extern int opt_rotate_period;
  744. extern double total_mhashes_done;
  745. extern unsigned int new_blocks;
  746. extern unsigned int found_blocks;
  747. extern int total_accepted, total_rejected, total_diff1;;
  748. extern int total_getworks, total_stale, total_discarded;
  749. extern uint64_t total_bytes_xfer;
  750. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  751. extern unsigned int local_work;
  752. extern unsigned int total_go, total_ro;
  753. extern const int opt_cutofftemp;
  754. extern int opt_hysteresis;
  755. extern int opt_fail_pause;
  756. extern int opt_log_interval;
  757. extern unsigned long long global_hashrate;
  758. extern char *current_fullhash;
  759. extern double current_diff;
  760. extern uint64_t best_diff;
  761. extern struct timeval block_timeval;
  762. #ifdef HAVE_OPENCL
  763. typedef struct {
  764. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  765. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  766. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  767. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  768. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  769. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  770. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  771. cl_uint W16; cl_uint W17; cl_uint W2;
  772. cl_uint PreVal4; cl_uint T1;
  773. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  774. cl_uint PreVal4addT1; cl_uint T1substate0;
  775. cl_uint PreVal4_2;
  776. cl_uint PreVal0;
  777. cl_uint PreW18;
  778. cl_uint PreW19;
  779. cl_uint PreW31;
  780. cl_uint PreW32;
  781. /* For diakgcn */
  782. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  783. cl_uint zeroA, zeroB;
  784. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  785. #ifdef USE_SCRYPT
  786. struct work *work;
  787. #endif
  788. } dev_blk_ctx;
  789. #else
  790. typedef struct {
  791. uint32_t nonce;
  792. } dev_blk_ctx;
  793. #endif
  794. struct curl_ent {
  795. CURL *curl;
  796. struct list_head node;
  797. struct timeval tv;
  798. };
  799. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  800. * equal disabled */
  801. enum pool_enable {
  802. POOL_DISABLED,
  803. POOL_ENABLED,
  804. POOL_REJECTING,
  805. };
  806. enum pool_protocol {
  807. PLP_NONE,
  808. PLP_GETWORK,
  809. PLP_GETBLOCKTEMPLATE,
  810. };
  811. struct stratum_work {
  812. char *job_id;
  813. char *prev_hash;
  814. char *coinbase1;
  815. char *coinbase2;
  816. char **merkle;
  817. char *bbversion;
  818. char *nbit;
  819. char *ntime;
  820. bool clean;
  821. size_t cb1_len;
  822. size_t cb2_len;
  823. size_t cb_len;
  824. size_t header_len;
  825. int merkles;
  826. double diff;
  827. bool transparency_probed;
  828. time_t transparency_time;
  829. bool opaque;
  830. };
  831. #define RBUFSIZE 8192
  832. #define RECVSIZE (RBUFSIZE - 4)
  833. struct pool {
  834. int pool_no;
  835. int prio;
  836. int accepted, rejected;
  837. int seq_rejects;
  838. int seq_getfails;
  839. int solved;
  840. int diff1;
  841. char diff[8];
  842. double diff_accepted;
  843. double diff_rejected;
  844. double diff_stale;
  845. bool submit_fail;
  846. bool idle;
  847. bool lagging;
  848. bool probed;
  849. int force_rollntime;
  850. enum pool_enable enabled;
  851. bool submit_old;
  852. bool removed;
  853. bool lp_started;
  854. unsigned char work_restart_id;
  855. uint32_t block_id;
  856. enum pool_protocol proto;
  857. char *hdr_path;
  858. char *lp_url;
  859. char *lp_id;
  860. enum pool_protocol lp_proto;
  861. curl_socket_t lp_socket;
  862. unsigned int getwork_requested;
  863. unsigned int stale_shares;
  864. unsigned int discarded_work;
  865. unsigned int getfail_occasions;
  866. unsigned int remotefail_occasions;
  867. struct timeval tv_idle;
  868. double utility;
  869. int last_shares, shares;
  870. char *rpc_url;
  871. char *rpc_userpass;
  872. char *rpc_user, *rpc_pass;
  873. char *rpc_proxy;
  874. pthread_mutex_t pool_lock;
  875. struct thread_q *submit_q;
  876. struct thread_q *getwork_q;
  877. pthread_t longpoll_thread;
  878. pthread_t submit_thread;
  879. pthread_t getwork_thread;
  880. int curls;
  881. pthread_cond_t cr_cond;
  882. struct list_head curlring;
  883. struct submit_work_state *sws_waiting_on_curl;
  884. time_t last_share_time;
  885. double last_share_diff;
  886. uint64_t best_diff;
  887. struct cgminer_stats cgminer_stats;
  888. struct cgminer_pool_stats cgminer_pool_stats;
  889. /* Stratum variables */
  890. char *stratum_url;
  891. char *stratum_port;
  892. CURL *stratum_curl;
  893. SOCKETTYPE sock;
  894. char *sockbuf;
  895. size_t sockbuf_size;
  896. char *sockaddr_url; /* stripped url used for sockaddr */
  897. char *nonce1;
  898. size_t n1_len;
  899. uint32_t nonce2;
  900. int n2size;
  901. char *sessionid;
  902. bool has_stratum;
  903. bool stratum_active;
  904. time_t last_work_time; /* only set for Stratum right now */
  905. bool stratum_auth;
  906. bool stratum_notify;
  907. struct stratum_work swork;
  908. pthread_t stratum_thread;
  909. pthread_mutex_t stratum_lock;
  910. char *admin_msg;
  911. pthread_mutex_t last_work_lock;
  912. struct work *last_work_copy;
  913. };
  914. #define GETWORK_MODE_TESTPOOL 'T'
  915. #define GETWORK_MODE_POOL 'P'
  916. #define GETWORK_MODE_LP 'L'
  917. #define GETWORK_MODE_BENCHMARK 'B'
  918. #define GETWORK_MODE_STRATUM 'S'
  919. #define GETWORK_MODE_GBT 'G'
  920. struct work {
  921. unsigned char data[128];
  922. unsigned char midstate[32];
  923. unsigned char target[32];
  924. unsigned char hash[32];
  925. int rolls;
  926. dev_blk_ctx blk;
  927. struct thr_info *thr;
  928. int thr_id;
  929. struct pool *pool;
  930. struct timeval tv_staged;
  931. bool mined;
  932. bool clone;
  933. bool cloned;
  934. int rolltime;
  935. bool longpoll;
  936. bool stale;
  937. bool mandatory;
  938. bool block;
  939. bool queued;
  940. bool stratum;
  941. char *job_id;
  942. char *nonce2;
  943. char *ntime;
  944. double sdiff;
  945. char *nonce1;
  946. unsigned char work_restart_id;
  947. int id;
  948. UT_hash_handle hh;
  949. double work_difficulty;
  950. blktemplate_t *tmpl;
  951. int *tmpl_refcount;
  952. unsigned int dataid;
  953. bool do_foreign_submit;
  954. struct timeval tv_getwork;
  955. struct timeval tv_getwork_reply;
  956. struct timeval tv_cloned;
  957. struct timeval tv_work_start;
  958. struct timeval tv_work_found;
  959. char getwork_mode;
  960. /* Used to queue shares in submit_waiting */
  961. struct list_head list;
  962. };
  963. extern void get_datestamp(char *, struct timeval *);
  964. enum test_nonce2_result {
  965. TNR_GOOD,
  966. TNR_HIGH,
  967. TNR_BAD,
  968. };
  969. extern enum test_nonce2_result _test_nonce2(struct work *, uint32_t nonce, bool checktarget);
  970. #define test_nonce(work, nonce, checktarget) (_test_nonce2(work, nonce, checktarget) == TNR_GOOD)
  971. #define test_nonce2(work, nonce) (_test_nonce2(work, nonce, true))
  972. extern void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  973. extern bool abandon_work(struct work *, struct timeval *work_runtime, uint64_t hashes);
  974. extern void tailsprintf(char *f, const char *fmt, ...);
  975. extern void wlogprint(const char *f, ...);
  976. extern int curses_int(const char *query);
  977. extern char *curses_input(const char *query);
  978. extern void kill_work(void);
  979. extern int prioritize_pools(char *param, int *pid);
  980. extern void validate_pool_priorities(void);
  981. extern void switch_pools(struct pool *selected);
  982. extern void remove_pool(struct pool *pool);
  983. extern void write_config(FILE *fcfg);
  984. extern void zero_bestshare(void);
  985. extern void zero_stats(void);
  986. extern void default_save_file(char *filename);
  987. extern bool log_curses_only(int prio, const char *f, va_list ap) FORMAT_SYNTAX_CHECK(printf, 2, 0);
  988. extern void clear_logwin(void);
  989. extern bool pool_tclear(struct pool *pool, bool *var);
  990. extern struct thread_q *tq_new(void);
  991. extern void tq_free(struct thread_q *tq);
  992. extern bool tq_push(struct thread_q *tq, void *data);
  993. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  994. extern void tq_freeze(struct thread_q *tq);
  995. extern void tq_thaw(struct thread_q *tq);
  996. extern bool successful_connect;
  997. extern void adl(void);
  998. extern void clean_work(struct work *work);
  999. extern void free_work(struct work *work);
  1000. extern void __copy_work(struct work *work, const struct work *base_work);
  1001. extern struct work *copy_work(const struct work *base_work);
  1002. enum api_data_type {
  1003. API_ESCAPE,
  1004. API_STRING,
  1005. API_CONST,
  1006. API_INT,
  1007. API_UINT,
  1008. API_UINT32,
  1009. API_UINT64,
  1010. API_DOUBLE,
  1011. API_ELAPSED,
  1012. API_BOOL,
  1013. API_TIMEVAL,
  1014. API_TIME,
  1015. API_MHS,
  1016. API_MHTOTAL,
  1017. API_TEMP,
  1018. API_UTILITY,
  1019. API_FREQ,
  1020. API_VOLTS,
  1021. API_HS,
  1022. API_DIFF,
  1023. API_JSON,
  1024. };
  1025. struct api_data {
  1026. enum api_data_type type;
  1027. char *name;
  1028. void *data;
  1029. bool data_was_malloc;
  1030. struct api_data *prev;
  1031. struct api_data *next;
  1032. };
  1033. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  1034. extern struct api_data *api_add_string(struct api_data *root, char *name, const char *data, bool copy_data);
  1035. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  1036. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  1037. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  1038. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  1039. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  1040. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  1041. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  1042. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  1043. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  1044. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  1045. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  1046. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  1047. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  1048. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  1049. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  1050. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  1051. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  1052. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  1053. extern struct api_data *api_add_json(struct api_data *root, char *name, json_t *data, bool copy_data);
  1054. #endif /* __MINER_H__ */