miner.h 30 KB

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