miner.h 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  1. /*
  2. * Copyright 2012-2014 Luke Dashjr
  3. * Copyright 2014 Nate Woolls
  4. * Copyright 2011-2013 Con Kolivas
  5. * Copyright 2012-2013 Andrew Smith
  6. * Copyright 2011 Glenn Francis Murray
  7. * Copyright 2010-2011 Jeff Garzik
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 3 of the License, or (at your option)
  12. * any later version. See COPYING for more details.
  13. */
  14. #ifndef BFG_MINER_H
  15. #define BFG_MINER_H
  16. #include "config.h"
  17. #ifdef WIN32
  18. #include <winsock2.h>
  19. #endif
  20. #include <float.h>
  21. #include <stdbool.h>
  22. #include <stdint.h>
  23. #include <sys/time.h>
  24. #include <pthread.h>
  25. #include <jansson.h>
  26. #include <curl/curl.h>
  27. #include <sched.h>
  28. #ifdef HAVE_INTTYPES_H
  29. #include <inttypes.h>
  30. #endif
  31. #include <blkmaker.h>
  32. #include <blktemplate.h>
  33. #if defined(WORDS_BIGENDIAN) && !defined(__BIG_ENDIAN__)
  34. /* uthash.h depends on __BIG_ENDIAN__ on BE platforms */
  35. #define __BIG_ENDIAN__ 1
  36. #endif
  37. #include <uthash.h>
  38. #include <utlist.h>
  39. #include "logging.h"
  40. #include "util.h"
  41. #ifdef STDC_HEADERS
  42. # include <stdlib.h>
  43. # include <stddef.h>
  44. #else
  45. # ifdef HAVE_STDLIB_H
  46. # include <stdlib.h>
  47. # endif
  48. #endif
  49. #ifdef HAVE_ALLOCA_H
  50. # include <alloca.h>
  51. #elif !defined alloca
  52. # ifdef __GNUC__
  53. # define alloca __builtin_alloca
  54. # elif defined _AIX
  55. # define alloca __alloca
  56. # elif defined _MSC_VER
  57. # include <malloc.h>
  58. # define alloca _alloca
  59. # elif !defined HAVE_ALLOCA
  60. # ifdef __cplusplus
  61. extern "C"
  62. # endif
  63. void *alloca (size_t);
  64. # endif
  65. #endif
  66. #ifdef __MINGW32__
  67. #include <windows.h>
  68. #include <io.h>
  69. static inline int fsync (int fd)
  70. {
  71. return (FlushFileBuffers ((HANDLE) _get_osfhandle (fd))) ? 0 : -1;
  72. }
  73. #ifndef EWOULDBLOCK
  74. # define EWOULDBLOCK EAGAIN
  75. #endif
  76. #ifndef MSG_DONTWAIT
  77. # define MSG_DONTWAIT 0x1000000
  78. #endif
  79. #endif /* __MINGW32__ */
  80. #if defined (__linux)
  81. #ifndef LINUX
  82. #define LINUX
  83. #endif
  84. #endif
  85. #ifdef HAVE_ADL
  86. #include "ADL/adl_sdk.h"
  87. #endif
  88. #ifdef HAVE_LIBUSB
  89. #include <libusb.h>
  90. #endif
  91. #ifdef USE_ZTEX
  92. #include "libztex.h"
  93. #endif
  94. #ifdef USE_BITFURY
  95. #include "libbitfury.h"
  96. #endif
  97. #ifdef HAVE_BYTESWAP_H
  98. #include <byteswap.h>
  99. #endif
  100. #ifdef HAVE_ENDIAN_H
  101. #include <endian.h>
  102. #endif
  103. #ifdef HAVE_SYS_ENDIAN_H
  104. #include <sys/endian.h>
  105. #endif
  106. #ifdef HAVE_LIBKERN_OSBYTEORDER_H
  107. #include <libkern/OSByteOrder.h>
  108. #endif
  109. #ifndef bswap_16
  110. #define bswap_16(value) \
  111. ((((value) & 0xff) << 8) | ((value) >> 8))
  112. #define bswap_32(value) \
  113. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  114. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  115. #define bswap_64(value) \
  116. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  117. << 32) | \
  118. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  119. #endif
  120. /* This assumes htobe32 is a macro and that if it doesn't exist, then the
  121. * also won't exist */
  122. #ifndef htobe32
  123. # ifndef WORDS_BIGENDIAN
  124. # define htole16(x) (x)
  125. # define htole32(x) (x)
  126. # define htole64(x) (x)
  127. # define htobe16(x) bswap_16(x)
  128. # define htobe32(x) bswap_32(x)
  129. # define htobe64(x) bswap_64(x)
  130. # else
  131. # define htole16(x) bswap_16(x)
  132. # define htole32(x) bswap_32(x)
  133. # define htole64(x) bswap_64(x)
  134. # define htobe16(x) (x)
  135. # define htobe32(x) (x)
  136. # define htobe64(x) (x)
  137. # endif
  138. #endif
  139. #ifndef be32toh
  140. # define le16toh(x) htole16(x)
  141. # define le32toh(x) htole32(x)
  142. # define le64toh(x) htole64(x)
  143. # define be16toh(x) htobe16(x)
  144. # define be32toh(x) htobe32(x)
  145. # define be64toh(x) htobe64(x)
  146. #endif
  147. #ifndef max
  148. # define max(a, b) ((a) > (b) ? (a) : (b))
  149. #endif
  150. #undef unlikely
  151. #undef likely
  152. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  153. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  154. #define likely(expr) (__builtin_expect(!!(expr), 1))
  155. #else
  156. #define unlikely(expr) (expr)
  157. #define likely(expr) (expr)
  158. #endif
  159. #ifndef __maybe_unused
  160. #define __maybe_unused __attribute__((unused))
  161. #endif
  162. #define uninitialised_var(x) x = x
  163. #if defined(__i386__)
  164. #define WANT_CRYPTOPP_ASM32
  165. #endif
  166. #ifndef ARRAY_SIZE
  167. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  168. #endif
  169. #ifdef HAVE_CURSES
  170. extern int my_cancellable_getch(void);
  171. # ifdef getch
  172. // getch() is a macro
  173. static int __maybe_unused __real_getch(void) {
  174. return getch();
  175. }
  176. # undef getch
  177. # define getch() my_cancellable_getch()
  178. # else
  179. // getch() is a real function
  180. # define __real_getch getch
  181. # define getch() my_cancellable_getch()
  182. # endif
  183. #endif
  184. enum alive {
  185. LIFE_WELL,
  186. LIFE_SICK,
  187. LIFE_DEAD,
  188. LIFE_NOSTART,
  189. LIFE_INIT,
  190. LIFE_WAIT,
  191. LIFE_INIT2, // Still initializing, but safe to call functions
  192. LIFE_DEAD2, // Totally dead, NOT safe to call functions
  193. LIFE_MIXED, // Only valid in display variables, NOT on devices
  194. };
  195. enum pool_strategy {
  196. POOL_FAILOVER,
  197. POOL_ROUNDROBIN,
  198. POOL_ROTATE,
  199. POOL_LOADBALANCE,
  200. POOL_BALANCE,
  201. };
  202. #define TOP_STRATEGY (POOL_BALANCE)
  203. struct strategies {
  204. const char *s;
  205. };
  206. struct cgpu_info;
  207. #ifdef HAVE_ADL
  208. struct gpu_adl {
  209. ADLTemperature lpTemperature;
  210. int iAdapterIndex;
  211. int lpAdapterID;
  212. int iBusNumber;
  213. char strAdapterName[256];
  214. ADLPMActivity lpActivity;
  215. ADLODParameters lpOdParameters;
  216. ADLODPerformanceLevels *DefPerfLev;
  217. ADLFanSpeedInfo lpFanSpeedInfo;
  218. ADLFanSpeedValue lpFanSpeedValue;
  219. ADLFanSpeedValue DefFanSpeedValue;
  220. int iEngineClock;
  221. int iMemoryClock;
  222. int iVddc;
  223. int iPercentage;
  224. bool autofan;
  225. bool autoengine;
  226. bool managed; /* Were the values ever changed on this card */
  227. int lastengine;
  228. int lasttemp;
  229. int targetfan;
  230. int overtemp;
  231. int minspeed;
  232. int maxspeed;
  233. int gpu;
  234. bool has_fanspeed;
  235. struct gpu_adl *twin;
  236. };
  237. #endif
  238. enum pow_algorithm {
  239. #ifdef USE_KECCAK
  240. POW_KECCAK,
  241. #endif
  242. #ifdef USE_SHA256D
  243. POW_SHA256D,
  244. #endif
  245. #ifdef USE_SCRYPT
  246. POW_SCRYPT,
  247. #endif
  248. POW_ALGORITHM_COUNT,
  249. };
  250. struct api_data;
  251. struct thr_info;
  252. struct work;
  253. struct lowlevel_device_info;
  254. enum bfg_probe_result_flags_values {
  255. BPR_CONTINUE_PROBES = 1<< 0,
  256. BPR_DONT_RESCAN = 1<< 1,
  257. BPR_WRONG_DEVTYPE = BPR_CONTINUE_PROBES | BPR_DONT_RESCAN,
  258. };
  259. extern unsigned *_bfg_probe_result_flags();
  260. #define bfg_probe_result_flags (*_bfg_probe_result_flags())
  261. struct mining_algorithm;
  262. struct device_drv {
  263. const char *dname;
  264. const char *name;
  265. int8_t probe_priority;
  266. bool lowl_probe_by_name_only;
  267. // DRV-global functions
  268. void (*drv_init)();
  269. // drv_min_nonce_diff's proc may be NULL
  270. // drv_min_nonce_diff should return negative if algorithm is not supported
  271. float (*drv_min_nonce_diff)(struct cgpu_info *proc, const struct mining_algorithm *);
  272. void (*drv_detect)();
  273. bool (*lowl_match)(const struct lowlevel_device_info *);
  274. bool (*lowl_probe)(const struct lowlevel_device_info *);
  275. // Processor-specific functions
  276. void (*watchdog)(struct cgpu_info *, const struct timeval *tv_now);
  277. void (*reinit_device)(struct cgpu_info *);
  278. bool (*override_statline_temp2)(char *buf, size_t bufsz, struct cgpu_info *, bool per_processor);
  279. struct api_data* (*get_api_extra_device_detail)(struct cgpu_info *);
  280. struct api_data* (*get_api_extra_device_status)(struct cgpu_info *);
  281. struct api_data *(*get_api_stats)(struct cgpu_info *);
  282. bool (*get_stats)(struct cgpu_info *);
  283. bool (*identify_device)(struct cgpu_info *); // e.g. to flash a led
  284. char *(*set_device)(struct cgpu_info *, char *option, char *setting, char *replybuf);
  285. void (*proc_wlogprint_status)(struct cgpu_info *);
  286. void (*proc_tui_wlogprint_choices)(struct cgpu_info *);
  287. const char *(*proc_tui_handle_choice)(struct cgpu_info *, int input);
  288. void (*zero_stats)(struct cgpu_info *);
  289. // Thread-specific functions
  290. bool (*thread_prepare)(struct thr_info *);
  291. void (*minerloop)(struct thr_info *);
  292. uint64_t (*can_limit_work)(struct thr_info *);
  293. bool (*thread_init)(struct thr_info *);
  294. bool (*prepare_work)(struct thr_info *, struct work *);
  295. int64_t (*scanhash)(struct thr_info *, struct work *, int64_t);
  296. int64_t (*scanwork)(struct thr_info *);
  297. /* Used to extract work from the hash table of queued work and tell
  298. * the main loop that it should not add any further work to the table.
  299. */
  300. bool (*queue_full)(struct cgpu_info *);
  301. void (*flush_work)(struct cgpu_info *);
  302. void (*hw_error)(struct thr_info *);
  303. void (*thread_shutdown)(struct thr_info *);
  304. void (*thread_disable)(struct thr_info *);
  305. void (*thread_enable)(struct thr_info *);
  306. // Can be used per-thread or per-processor (only with minerloop async or queue!)
  307. void (*poll)(struct thr_info *);
  308. // === Implemented by minerloop_async ===
  309. bool (*job_prepare)(struct thr_info*, struct work*, uint64_t);
  310. void (*job_start)(struct thr_info*);
  311. void (*job_get_results)(struct thr_info*, struct work*);
  312. int64_t (*job_process_results)(struct thr_info*, struct work*, bool stopping);
  313. // === Implemented by minerloop_queue ===
  314. bool (*queue_append)(struct thr_info *, struct work *);
  315. void (*queue_flush)(struct thr_info *);
  316. };
  317. enum dev_enable {
  318. DEV_ENABLED,
  319. DEV_DISABLED, // Disabled by user
  320. DEV_RECOVER, // Disabled by temperature cutoff in watchdog
  321. DEV_RECOVER_ERR, // Disabled by communications error
  322. DEV_RECOVER_DRV, // Disabled by driver
  323. };
  324. enum cl_kernels {
  325. KL_NONE,
  326. #ifdef USE_SHA256D
  327. KL_POCLBM,
  328. KL_PHATK,
  329. KL_DIAKGCN,
  330. KL_DIABLO,
  331. #endif
  332. #ifdef USE_OPENCL_FULLHEADER
  333. KL_FULLHEADER,
  334. #endif
  335. #ifdef USE_SCRYPT
  336. KL_SCRYPT,
  337. #endif
  338. OPENCL_KERNEL_INTERFACE_COUNT,
  339. };
  340. enum dev_reason {
  341. REASON_THREAD_FAIL_INIT,
  342. REASON_THREAD_ZERO_HASH,
  343. REASON_THREAD_FAIL_QUEUE,
  344. REASON_DEV_SICK_IDLE_60,
  345. REASON_DEV_DEAD_IDLE_600,
  346. REASON_DEV_NOSTART,
  347. REASON_DEV_OVER_HEAT,
  348. REASON_DEV_THERMAL_CUTOFF,
  349. REASON_DEV_COMMS_ERROR,
  350. REASON_DEV_THROTTLE,
  351. };
  352. #define REASON_NONE "None"
  353. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  354. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  355. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  356. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  357. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  358. #define REASON_DEV_NOSTART_STR "Device failed to start"
  359. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  360. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  361. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  362. #define REASON_DEV_THROTTLE_STR "Device throttle"
  363. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  364. #define MIN_SEC_UNSET 99999999
  365. enum {
  366. MSG_NOPOOL = 8,
  367. MSG_MISPID = 25,
  368. MSG_INVPID = 26,
  369. MSG_DUPPID = 74,
  370. MSG_POOLPRIO = 73,
  371. };
  372. struct cgminer_stats {
  373. struct timeval start_tv;
  374. uint32_t getwork_calls;
  375. struct timeval getwork_wait;
  376. struct timeval getwork_wait_max;
  377. struct timeval getwork_wait_min;
  378. struct timeval _get_start;
  379. };
  380. // Just the actual network getworks to the pool
  381. struct cgminer_pool_stats {
  382. uint32_t getwork_calls;
  383. uint32_t getwork_attempts;
  384. struct timeval getwork_wait;
  385. struct timeval getwork_wait_max;
  386. struct timeval getwork_wait_min;
  387. double getwork_wait_rolling;
  388. bool hadrolltime;
  389. bool canroll;
  390. bool hadexpire;
  391. uint32_t rolltime;
  392. double min_diff;
  393. double max_diff;
  394. double last_diff;
  395. uint32_t min_diff_count;
  396. uint32_t max_diff_count;
  397. uint64_t times_sent;
  398. uint64_t bytes_sent;
  399. uint64_t net_bytes_sent;
  400. uint64_t times_received;
  401. uint64_t bytes_received;
  402. uint64_t net_bytes_received;
  403. };
  404. #define PRIprepr "-6s"
  405. #define PRIpreprv "s"
  406. #define ALLOC_H2B_NOUNIT 6
  407. #define ALLOC_H2B_SHORT 7
  408. #define ALLOC_H2B_SPACED 8
  409. #define ALLOC_H2B_SHORTV 7
  410. struct cgpu_info {
  411. int cgminer_id;
  412. int device_line_id;
  413. struct device_drv *drv;
  414. const struct bfg_set_device_definition *set_device_funcs;
  415. int device_id;
  416. char *dev_repr;
  417. char *dev_repr_ns;
  418. const char *name;
  419. int procs;
  420. int proc_id;
  421. char proc_repr[9];
  422. char proc_repr_ns[9];
  423. struct cgpu_info *device;
  424. struct cgpu_info *next_proc;
  425. int extra_work_queue;
  426. const char *device_path;
  427. void *device_data;
  428. const char *dev_manufacturer;
  429. const char *dev_product;
  430. const char *dev_serial;
  431. union {
  432. #ifdef USE_ZTEX
  433. struct libztex_device *device_ztex;
  434. #endif
  435. int device_fd;
  436. #ifdef USE_X6500
  437. struct ft232r_device_handle *device_ft232r;
  438. #endif
  439. };
  440. #ifdef USE_AVALON
  441. struct work **works;
  442. int work_array;
  443. int queued;
  444. int results;
  445. #endif
  446. #ifdef USE_BITFORCE
  447. struct timeval work_start_tv;
  448. unsigned int wait_ms;
  449. unsigned int sleep_ms;
  450. double avg_wait_f;
  451. unsigned int avg_wait_d;
  452. uint32_t nonces;
  453. bool polling;
  454. #endif
  455. #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_TWINFURY)
  456. bool flash_led;
  457. #endif
  458. pthread_mutex_t device_mutex;
  459. pthread_cond_t device_cond;
  460. enum dev_enable deven;
  461. bool already_set_defaults;
  462. int accepted;
  463. int rejected;
  464. int stale;
  465. double bad_diff1;
  466. int hw_errors;
  467. double rolling;
  468. double total_mhashes;
  469. double utility;
  470. double utility_diff1;
  471. enum alive status;
  472. char init[40];
  473. struct timeval last_message_tv;
  474. int threads;
  475. struct thr_info **thr;
  476. int64_t max_hashes;
  477. const char *kname;
  478. float temp;
  479. int cutofftemp;
  480. int targettemp;
  481. bool targettemp_user;
  482. double diff1;
  483. double diff_accepted;
  484. double diff_rejected;
  485. double diff_stale;
  486. int last_share_pool;
  487. time_t last_share_pool_time;
  488. double last_share_diff;
  489. time_t last_device_valid_work;
  490. time_t device_last_well;
  491. time_t device_last_not_well;
  492. struct timeval tv_device_last_not_well;
  493. enum dev_reason device_not_well_reason;
  494. float reinit_backoff;
  495. int thread_fail_init_count;
  496. int thread_zero_hash_count;
  497. int thread_fail_queue_count;
  498. int dev_sick_idle_60_count;
  499. int dev_dead_idle_600_count;
  500. int dev_nostart_count;
  501. int dev_over_heat_count; // It's a warning but worth knowing
  502. int dev_thermal_cutoff_count;
  503. int dev_comms_error_count;
  504. int dev_throttle_count;
  505. struct cgminer_stats cgminer_stats;
  506. pthread_rwlock_t qlock;
  507. struct work *queued_work;
  508. struct work *unqueued_work;
  509. unsigned int queued_count;
  510. bool disable_watchdog;
  511. bool shutdown;
  512. };
  513. extern void renumber_cgpu(struct cgpu_info *);
  514. extern bool add_cgpu(struct cgpu_info*);
  515. struct tq_ent;
  516. struct thread_q {
  517. struct tq_ent *q;
  518. bool frozen;
  519. pthread_mutex_t mutex;
  520. pthread_cond_t cond;
  521. };
  522. enum thr_busy_state {
  523. TBS_IDLE,
  524. TBS_GETTING_RESULTS,
  525. TBS_STARTING_JOB,
  526. };
  527. struct thr_info {
  528. int id;
  529. int device_thread;
  530. bool primary_thread;
  531. bool has_pth;
  532. pthread_t pth;
  533. struct thread_q *q;
  534. struct cgpu_info *cgpu;
  535. void *cgpu_data;
  536. struct timeval last;
  537. struct timeval sick;
  538. bool scanhash_working;
  539. uint64_t hashes_done;
  540. struct timeval tv_hashes_done;
  541. struct timeval tv_lastupdate;
  542. struct timeval _tv_last_hashes_done_call;
  543. bool pause;
  544. time_t getwork;
  545. double rolling;
  546. // Used by minerloop_async
  547. struct work *prev_work;
  548. struct work *work;
  549. struct work *next_work;
  550. enum thr_busy_state busy_state;
  551. bool _mt_disable_called;
  552. struct timeval tv_morework;
  553. struct work *results_work;
  554. bool _job_transition_in_progress;
  555. bool _proceed_with_new_job;
  556. struct timeval tv_results_jobstart;
  557. struct timeval tv_jobstart;
  558. struct timeval tv_poll;
  559. struct timeval tv_watchdog;
  560. notifier_t notifier;
  561. bool starting_next_work;
  562. uint32_t _max_nonce;
  563. notifier_t mutex_request;
  564. // Used by minerloop_queue
  565. struct work *work_list;
  566. bool queue_full;
  567. bool work_restart;
  568. notifier_t work_restart_notifier;
  569. };
  570. struct string_elist {
  571. char *string;
  572. bool free_me;
  573. struct string_elist *prev;
  574. struct string_elist *next;
  575. };
  576. static inline void string_elist_add(const char *s, struct string_elist **head)
  577. {
  578. struct string_elist *n;
  579. n = calloc(1, sizeof(*n));
  580. n->string = strdup(s);
  581. n->free_me = true;
  582. DL_APPEND(*head, n);
  583. }
  584. static inline void string_elist_del(struct string_elist **head, struct string_elist *item)
  585. {
  586. if (item->free_me)
  587. free(item->string);
  588. DL_DELETE(*head, item);
  589. free(item);
  590. }
  591. struct bfg_loaded_configfile {
  592. char *filename;
  593. int fileconf_load;
  594. struct bfg_loaded_configfile *next;
  595. };
  596. extern struct bfg_loaded_configfile *bfg_loaded_configfiles;
  597. static inline uint32_t swab32(uint32_t v)
  598. {
  599. return bswap_32(v);
  600. }
  601. static inline void swap256(void *dest_p, const void *src_p)
  602. {
  603. uint32_t *dest = dest_p;
  604. const uint32_t *src = src_p;
  605. dest[0] = src[7];
  606. dest[1] = src[6];
  607. dest[2] = src[5];
  608. dest[3] = src[4];
  609. dest[4] = src[3];
  610. dest[5] = src[2];
  611. dest[6] = src[1];
  612. dest[7] = src[0];
  613. }
  614. static inline void swap32yes(void*out, const void*in, size_t sz) {
  615. size_t swapcounter = 0;
  616. for (swapcounter = 0; swapcounter < sz; ++swapcounter)
  617. (((uint32_t*)out)[swapcounter]) = swab32(((uint32_t*)in)[swapcounter]);
  618. }
  619. #define LOCAL_swap32(type, var, sz) \
  620. type __swapped_ ## var[sz * 4 / sizeof(type)]; \
  621. swap32yes(__swapped_ ## var, var, sz); \
  622. var = __swapped_ ## var; \
  623. // end
  624. #ifdef WORDS_BIGENDIAN
  625. # define swap32tobe(out, in, sz) ((out == in) ? (void)0 : (void)memmove(out, in, (sz)*4))
  626. # define LOCAL_swap32be(type, var, sz) ;
  627. # define swap32tole(out, in, sz) swap32yes(out, in, sz)
  628. # define LOCAL_swap32le(type, var, sz) LOCAL_swap32(type, var, sz)
  629. #else
  630. # define swap32tobe(out, in, sz) swap32yes(out, in, sz)
  631. # define LOCAL_swap32be(type, var, sz) LOCAL_swap32(type, var, sz)
  632. # define swap32tole(out, in, sz) ((out == in) ? (void)0 : (void)memmove(out, in, (sz)*4))
  633. # define LOCAL_swap32le(type, var, sz) ;
  634. #endif
  635. static inline void swab256(void *dest_p, const void *src_p)
  636. {
  637. uint32_t *dest = dest_p;
  638. const uint32_t *src = src_p;
  639. dest[0] = swab32(src[7]);
  640. dest[1] = swab32(src[6]);
  641. dest[2] = swab32(src[5]);
  642. dest[3] = swab32(src[4]);
  643. dest[4] = swab32(src[3]);
  644. dest[5] = swab32(src[2]);
  645. dest[6] = swab32(src[1]);
  646. dest[7] = swab32(src[0]);
  647. }
  648. static inline
  649. void bswap_96p(void * const dest_p, const void * const src_p)
  650. {
  651. uint32_t * const dest = dest_p;
  652. const uint32_t * const src = src_p;
  653. dest[0] = bswap_32(src[2]);
  654. dest[1] = bswap_32(src[1]);
  655. dest[2] = bswap_32(src[0]);
  656. }
  657. static inline
  658. void bswap_32mult(void * const dest_p, const void * const src_p, const size_t sz)
  659. {
  660. const uint32_t *s = src_p;
  661. const uint32_t *s_end = &s[sz];
  662. uint32_t *d = dest_p;
  663. d = &d[sz - 1];
  664. for ( ; s < s_end; ++s, --d)
  665. *d = bswap_32(*s);
  666. }
  667. #define flip12(dest_p, src_p) swap32yes(dest_p, src_p, 12 / 4)
  668. #define flip32(dest_p, src_p) swap32yes(dest_p, src_p, 32 / 4)
  669. #define WATCHDOG_INTERVAL 2
  670. extern void bfg_watchdog(struct cgpu_info *, struct timeval *tvp_now);
  671. extern void _quit(int status);
  672. static inline void mutex_lock(pthread_mutex_t *lock)
  673. {
  674. if (unlikely(pthread_mutex_lock(lock)))
  675. quit(1, "WTF MUTEX ERROR ON LOCK!");
  676. }
  677. static inline void mutex_unlock_noyield(pthread_mutex_t *lock)
  678. {
  679. if (unlikely(pthread_mutex_unlock(lock)))
  680. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  681. }
  682. static inline void mutex_unlock(pthread_mutex_t *lock)
  683. {
  684. mutex_unlock_noyield(lock);
  685. sched_yield();
  686. }
  687. static inline int mutex_trylock(pthread_mutex_t *lock)
  688. {
  689. return pthread_mutex_trylock(lock);
  690. }
  691. static inline void wr_lock(pthread_rwlock_t *lock)
  692. {
  693. if (unlikely(pthread_rwlock_wrlock(lock)))
  694. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  695. }
  696. static inline void rd_lock(pthread_rwlock_t *lock)
  697. {
  698. if (unlikely(pthread_rwlock_rdlock(lock)))
  699. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  700. }
  701. static inline void rw_unlock(pthread_rwlock_t *lock)
  702. {
  703. if (unlikely(pthread_rwlock_unlock(lock)))
  704. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  705. }
  706. static inline void rd_unlock_noyield(pthread_rwlock_t *lock)
  707. {
  708. rw_unlock(lock);
  709. }
  710. static inline void wr_unlock_noyield(pthread_rwlock_t *lock)
  711. {
  712. rw_unlock(lock);
  713. }
  714. static inline void rd_unlock(pthread_rwlock_t *lock)
  715. {
  716. rw_unlock(lock);
  717. sched_yield();
  718. }
  719. static inline void wr_unlock(pthread_rwlock_t *lock)
  720. {
  721. rw_unlock(lock);
  722. sched_yield();
  723. }
  724. static inline void mutex_init(pthread_mutex_t *lock)
  725. {
  726. if (unlikely(pthread_mutex_init(lock, NULL)))
  727. quit(1, "Failed to pthread_mutex_init");
  728. }
  729. static inline void mutex_destroy(pthread_mutex_t *lock)
  730. {
  731. /* Ignore return code. This only invalidates the mutex on linux but
  732. * releases resources on windows. */
  733. pthread_mutex_destroy(lock);
  734. }
  735. static inline void rwlock_init(pthread_rwlock_t *lock)
  736. {
  737. if (unlikely(pthread_rwlock_init(lock, NULL)))
  738. quit(1, "Failed to pthread_rwlock_init");
  739. }
  740. /* cgminer locks, a write biased variant of rwlocks */
  741. struct cglock {
  742. pthread_mutex_t mutex;
  743. pthread_rwlock_t rwlock;
  744. };
  745. typedef struct cglock cglock_t;
  746. static inline void rwlock_destroy(pthread_rwlock_t *lock)
  747. {
  748. pthread_rwlock_destroy(lock);
  749. }
  750. static inline void cglock_init(cglock_t *lock)
  751. {
  752. mutex_init(&lock->mutex);
  753. rwlock_init(&lock->rwlock);
  754. }
  755. static inline void cglock_destroy(cglock_t *lock)
  756. {
  757. rwlock_destroy(&lock->rwlock);
  758. mutex_destroy(&lock->mutex);
  759. }
  760. /* Read lock variant of cglock. Cannot be promoted. */
  761. static inline void cg_rlock(cglock_t *lock)
  762. {
  763. mutex_lock(&lock->mutex);
  764. rd_lock(&lock->rwlock);
  765. mutex_unlock_noyield(&lock->mutex);
  766. }
  767. /* Intermediate variant of cglock - behaves as a read lock but can be promoted
  768. * to a write lock or demoted to read lock. */
  769. static inline void cg_ilock(cglock_t *lock)
  770. {
  771. mutex_lock(&lock->mutex);
  772. }
  773. /* Upgrade intermediate variant to a write lock */
  774. static inline void cg_ulock(cglock_t *lock)
  775. {
  776. wr_lock(&lock->rwlock);
  777. }
  778. /* Write lock variant of cglock */
  779. static inline void cg_wlock(cglock_t *lock)
  780. {
  781. mutex_lock(&lock->mutex);
  782. wr_lock(&lock->rwlock);
  783. }
  784. /* Downgrade write variant to a read lock */
  785. static inline void cg_dwlock(cglock_t *lock)
  786. {
  787. wr_unlock_noyield(&lock->rwlock);
  788. rd_lock(&lock->rwlock);
  789. mutex_unlock_noyield(&lock->mutex);
  790. }
  791. /* Demote a write variant to an intermediate variant */
  792. static inline void cg_dwilock(cglock_t *lock)
  793. {
  794. wr_unlock(&lock->rwlock);
  795. }
  796. /* Downgrade intermediate variant to a read lock */
  797. static inline void cg_dlock(cglock_t *lock)
  798. {
  799. rd_lock(&lock->rwlock);
  800. mutex_unlock(&lock->mutex);
  801. }
  802. static inline void cg_runlock(cglock_t *lock)
  803. {
  804. rd_unlock(&lock->rwlock);
  805. }
  806. static inline void cg_wunlock(cglock_t *lock)
  807. {
  808. wr_unlock_noyield(&lock->rwlock);
  809. mutex_unlock(&lock->mutex);
  810. }
  811. struct pool;
  812. #define API_MCAST_CODE "FTW"
  813. #define API_MCAST_ADDR "224.0.0.75"
  814. extern bool opt_protocol;
  815. extern bool opt_dev_protocol;
  816. extern char *opt_coinbase_sig;
  817. extern char *request_target_str;
  818. extern int opt_skip_checks;
  819. extern char *opt_kernel_path;
  820. extern char *opt_socks_proxy;
  821. extern char *cmd_idle, *cmd_sick, *cmd_dead;
  822. extern char *cgminer_path;
  823. extern bool opt_fail_only;
  824. extern bool opt_autofan;
  825. extern bool opt_autoengine;
  826. extern bool use_curses;
  827. extern int last_logstatusline_len;
  828. #ifdef HAVE_LIBUSB
  829. extern bool have_libusb;
  830. #endif
  831. extern int httpsrv_port;
  832. extern int stratumsrv_port;
  833. extern char *opt_api_allow;
  834. extern bool opt_api_mcast;
  835. extern char *opt_api_mcast_addr;
  836. extern char *opt_api_mcast_code;
  837. extern char *opt_api_mcast_des;
  838. extern int opt_api_mcast_port;
  839. extern char *opt_api_groups;
  840. extern char *opt_api_description;
  841. extern int opt_api_port;
  842. extern bool opt_api_listen;
  843. extern bool opt_api_network;
  844. extern bool opt_delaynet;
  845. extern time_t last_getwork;
  846. extern bool opt_disable_client_reconnect;
  847. extern bool opt_restart;
  848. extern char *opt_icarus_options;
  849. extern char *opt_icarus_timing;
  850. extern bool opt_worktime;
  851. #ifdef USE_AVALON
  852. extern char *opt_avalon_options;
  853. #endif
  854. #ifdef USE_KLONDIKE
  855. extern char *opt_klondike_options;
  856. #endif
  857. #ifdef USE_BITFORCE
  858. extern bool opt_bfl_noncerange;
  859. #endif
  860. extern int swork_id;
  861. extern pthread_rwlock_t netacc_lock;
  862. extern const uint32_t sha256_init_state[];
  863. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  864. const char *rpc_req, bool, bool, int *,
  865. struct pool *pool, bool);
  866. extern bool our_curl_supports_proxy_uris();
  867. extern void bin2hex(char *out, const void *in, size_t len);
  868. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  869. extern int opt_queue;
  870. extern int opt_scantime;
  871. extern int opt_expiry;
  872. extern cglock_t control_lock;
  873. extern pthread_mutex_t stats_lock;
  874. extern pthread_mutex_t hash_lock;
  875. extern pthread_mutex_t console_lock;
  876. extern cglock_t ch_lock;
  877. extern pthread_rwlock_t mining_thr_lock;
  878. extern pthread_rwlock_t devices_lock;
  879. extern bool _bfg_console_cancel_disabled;
  880. extern int _bfg_console_prev_cancelstate;
  881. static inline
  882. void bfg_console_lock(void)
  883. {
  884. int prev_cancelstate;
  885. bool cancel_disabled = !pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &prev_cancelstate);
  886. mutex_lock(&console_lock);
  887. _bfg_console_cancel_disabled = cancel_disabled;
  888. if (cancel_disabled)
  889. _bfg_console_prev_cancelstate = prev_cancelstate;
  890. }
  891. static inline
  892. void bfg_console_unlock(void)
  893. {
  894. int prev_cancelstate;
  895. bool cancel_disabled = _bfg_console_cancel_disabled;
  896. if (cancel_disabled)
  897. {
  898. prev_cancelstate = _bfg_console_prev_cancelstate;
  899. mutex_unlock(&console_lock);
  900. pthread_setcancelstate(prev_cancelstate, &prev_cancelstate);
  901. }
  902. else
  903. mutex_unlock(&console_lock);
  904. }
  905. extern void thread_reportin(struct thr_info *thr);
  906. extern void thread_reportout(struct thr_info *);
  907. extern void clear_stratum_shares(struct pool *pool);
  908. extern void hashmeter2(struct thr_info *);
  909. extern bool stale_work(struct work *, bool share);
  910. extern bool stale_work_future(struct work *, bool share, unsigned long ustime);
  911. extern void blkhashstr(char *out, const unsigned char *hash);
  912. static const float minimum_pdiff = max(FLT_MIN, 1./0x100000000);
  913. extern void set_target_to_pdiff(void *dest_target, double pdiff);
  914. #define bdiff_to_pdiff(n) (n * 1.0000152587)
  915. extern void set_target_to_bdiff(void *dest_target, double bdiff);
  916. #define pdiff_to_bdiff(n) (n * 0.9999847412109375)
  917. extern double target_diff(const unsigned char *target);
  918. extern void kill_work(void);
  919. extern void app_restart(void);
  920. extern void __thr_being_msg(int prio, struct thr_info *, const char *);
  921. extern void mt_enable(struct thr_info *thr);
  922. extern void proc_enable(struct cgpu_info *);
  923. extern void reinit_device(struct cgpu_info *cgpu);
  924. extern void cgpu_set_defaults(struct cgpu_info *);
  925. extern void drv_set_defaults(const struct device_drv *, const void *, void *userp, const char *devpath, const char *serial, int mode);
  926. #ifdef HAVE_ADL
  927. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  928. extern int set_fanspeed(int gpu, int iFanSpeed);
  929. extern int set_vddc(int gpu, float fVddc);
  930. extern int set_engineclock(int gpu, int iEngineClock);
  931. extern int set_memoryclock(int gpu, int iMemoryClock);
  932. #endif
  933. extern void api(int thr_id);
  934. extern struct pool *current_pool(void);
  935. extern int enabled_pools;
  936. extern bool get_intrange(const char *arg, int *val1, int *val2);
  937. extern bool detect_stratum(struct pool *pool, char *url);
  938. extern void print_summary(void);
  939. extern struct mining_algorithm *mining_algorithm_by_alias(const char *alias);
  940. extern struct mining_goal_info *get_mining_goal(const char *name);
  941. extern void goal_set_malgo(struct mining_goal_info *, struct mining_algorithm *);
  942. extern void mining_goal_reset(struct mining_goal_info * const goal);
  943. extern void adjust_quota_gcd(void);
  944. extern struct pool *add_pool2(struct mining_goal_info *);
  945. #define add_pool() add_pool2(get_mining_goal("default"))
  946. extern bool add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  947. #define MAX_GPUDEVICES 16
  948. #define MAX_DEVICES 4096
  949. struct block_info {
  950. uint32_t block_id;
  951. uint8_t prevblkhash[0x20];
  952. unsigned block_seen_order; // new_blocks when this block was first seen; was 'block_no'
  953. uint32_t height;
  954. time_t first_seen_time;
  955. UT_hash_handle hh;
  956. };
  957. struct blockchain_info {
  958. struct block_info *blocks;
  959. struct block_info *currentblk;
  960. uint64_t currentblk_subsidy; // only valid when height is known! (and assumes Bitcoin)
  961. char currentblk_first_seen_time_str[0x20]; // was global blocktime
  962. };
  963. struct _clState;
  964. struct cgpu_info;
  965. struct mining_algorithm;
  966. struct mining_algorithm {
  967. const char *name;
  968. const char *aliases;
  969. enum pow_algorithm algo;
  970. uint8_t ui_skip_hash_bytes;
  971. uint8_t worktime_skip_prevblk_u32;
  972. float reasonable_low_nonce_diff;
  973. void (*hash_data_f)(void *digest, const void *data);
  974. int goal_refs;
  975. int staged;
  976. int base_queue;
  977. struct mining_algorithm *next;
  978. #ifdef USE_OPENCL
  979. bool opencl_nodefault;
  980. float (*opencl_oclthreads_to_intensity)(unsigned long oclthreads);
  981. unsigned long (*opencl_intensity_to_oclthreads)(float intensity);
  982. unsigned long opencl_min_oclthreads;
  983. unsigned long opencl_max_oclthreads;
  984. float opencl_min_nonce_diff;
  985. char *(*opencl_get_default_kernel_file)(const struct mining_algorithm *, struct cgpu_info *, struct _clState *);
  986. #endif
  987. };
  988. struct mining_goal_info {
  989. unsigned id;
  990. char *name;
  991. bool is_default;
  992. struct blockchain_info *blkchain;
  993. bytes_t *generation_script; // was opt_coinbase_script
  994. struct mining_algorithm *malgo;
  995. double current_diff;
  996. char current_diff_str[ALLOC_H2B_SHORTV]; // was global block_diff
  997. char net_hashrate[ALLOC_H2B_SHORT];
  998. char *current_goal_detail;
  999. double diff_accepted;
  1000. bool have_longpoll;
  1001. UT_hash_handle hh;
  1002. };
  1003. extern struct string_elist *scan_devices;
  1004. extern bool opt_force_dev_init;
  1005. extern int nDevs;
  1006. extern int opt_n_threads;
  1007. extern int num_processors;
  1008. extern int hw_errors;
  1009. extern bool use_syslog;
  1010. extern bool opt_quiet;
  1011. extern struct thr_info *control_thr;
  1012. extern struct thr_info **mining_thr;
  1013. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  1014. extern double total_secs;
  1015. extern int mining_threads;
  1016. extern struct cgpu_info *cpus;
  1017. extern int total_devices;
  1018. extern struct cgpu_info **devices;
  1019. extern int total_devices_new;
  1020. extern struct cgpu_info **devices_new;
  1021. extern int total_pools;
  1022. extern struct pool **pools;
  1023. extern const char *algo_names[];
  1024. extern enum sha256_algos opt_algo;
  1025. extern struct strategies strategies[];
  1026. extern enum pool_strategy pool_strategy;
  1027. extern int opt_rotate_period;
  1028. extern double total_rolling;
  1029. extern double total_mhashes_done;
  1030. extern unsigned int new_blocks;
  1031. extern unsigned int found_blocks;
  1032. extern int total_accepted, total_rejected;
  1033. extern int total_getworks, total_stale, total_discarded;
  1034. extern uint64_t total_bytes_rcvd, total_bytes_sent;
  1035. #define total_bytes_xfer (total_bytes_rcvd + total_bytes_sent)
  1036. extern double total_diff1, total_bad_diff1;
  1037. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  1038. extern unsigned int local_work;
  1039. extern unsigned int total_go, total_ro;
  1040. extern const int opt_cutofftemp;
  1041. extern int opt_hysteresis;
  1042. extern int opt_fail_pause;
  1043. extern int opt_log_interval;
  1044. extern unsigned long long global_hashrate;
  1045. extern unsigned unittest_failures;
  1046. extern double best_diff;
  1047. extern struct mining_algorithm *mining_algorithms;
  1048. extern struct mining_goal_info *mining_goals;
  1049. struct curl_ent {
  1050. CURL *curl;
  1051. struct curl_ent *next;
  1052. struct timeval tv;
  1053. };
  1054. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  1055. * equal disabled */
  1056. enum pool_enable {
  1057. POOL_DISABLED,
  1058. POOL_ENABLED,
  1059. POOL_REJECTING,
  1060. POOL_MISBEHAVING,
  1061. };
  1062. enum pool_protocol {
  1063. PLP_NONE,
  1064. PLP_GETWORK,
  1065. PLP_GETBLOCKTEMPLATE,
  1066. };
  1067. struct bfg_tmpl_ref {
  1068. blktemplate_t *tmpl;
  1069. int refcount;
  1070. pthread_mutex_t mutex;
  1071. };
  1072. struct ntime_roll_limits {
  1073. uint32_t min;
  1074. uint32_t max;
  1075. struct timeval tv_ref;
  1076. int16_t minoff;
  1077. int16_t maxoff;
  1078. };
  1079. struct stratum_work {
  1080. // Used only as a session id for resuming
  1081. char *nonce1;
  1082. struct bfg_tmpl_ref *tr;
  1083. char *job_id;
  1084. bool clean;
  1085. bytes_t coinbase;
  1086. size_t nonce2_offset;
  1087. int n2size;
  1088. int merkles;
  1089. bytes_t merkle_bin;
  1090. uint8_t header1[36];
  1091. uint8_t diffbits[4];
  1092. uint32_t ntime;
  1093. struct timeval tv_received;
  1094. struct ntime_roll_limits ntime_roll_limits;
  1095. struct timeval tv_expire;
  1096. uint8_t target[32];
  1097. bool transparency_probed;
  1098. struct timeval tv_transparency;
  1099. bool opaque;
  1100. cglock_t *data_lock_p;
  1101. struct pool *pool;
  1102. unsigned char work_restart_id;
  1103. };
  1104. #define RBUFSIZE 8192
  1105. #define RECVSIZE (RBUFSIZE - 4)
  1106. /*
  1107. * Build an hash table in case there are lots
  1108. * of addresses to check against
  1109. */
  1110. struct bytes_hashtbl {
  1111. bytes_t b;
  1112. UT_hash_handle hh;
  1113. };
  1114. struct coinbase_param {
  1115. bool testnet;
  1116. struct bytes_hashtbl *scripts;
  1117. int64_t total;
  1118. float perc;
  1119. };
  1120. struct pool {
  1121. int pool_no;
  1122. int prio;
  1123. int accepted, rejected;
  1124. int seq_rejects;
  1125. int seq_getfails;
  1126. int solved;
  1127. double diff1;
  1128. char diff[ALLOC_H2B_SHORTV];
  1129. int quota;
  1130. int quota_gcd;
  1131. int quota_used;
  1132. int works;
  1133. double diff_accepted;
  1134. double diff_rejected;
  1135. double diff_stale;
  1136. bool submit_fail;
  1137. bool idle;
  1138. bool lagging;
  1139. bool probed;
  1140. int force_rollntime;
  1141. enum pool_enable enabled;
  1142. bool failover_only; // NOTE: Ignored by failover and loadbalance strategies (use priority and quota respectively)
  1143. bool submit_old;
  1144. bool removed;
  1145. bool lp_started;
  1146. unsigned char work_restart_id;
  1147. time_t work_restart_time;
  1148. char work_restart_timestamp[11];
  1149. uint32_t block_id;
  1150. struct mining_goal_info *goal;
  1151. enum pool_protocol proto;
  1152. char *hdr_path;
  1153. char *lp_url;
  1154. char *lp_id;
  1155. enum pool_protocol lp_proto;
  1156. curl_socket_t lp_socket;
  1157. bool lp_active;
  1158. unsigned int getwork_requested;
  1159. unsigned int stale_shares;
  1160. unsigned int discarded_work;
  1161. unsigned int getfail_occasions;
  1162. unsigned int remotefail_occasions;
  1163. struct timeval tv_idle;
  1164. double utility;
  1165. int last_shares, shares;
  1166. char *rpc_url;
  1167. char *rpc_userpass;
  1168. char *rpc_user, *rpc_pass;
  1169. char *rpc_proxy;
  1170. pthread_mutex_t pool_lock;
  1171. pthread_mutex_t pool_test_lock;
  1172. cglock_t data_lock;
  1173. struct thread_q *submit_q;
  1174. struct thread_q *getwork_q;
  1175. pthread_t longpoll_thread;
  1176. pthread_t test_thread;
  1177. bool testing;
  1178. int curls;
  1179. pthread_cond_t cr_cond;
  1180. struct curl_ent *curllist;
  1181. struct submit_work_state *sws_waiting_on_curl;
  1182. time_t last_work_time;
  1183. struct timeval tv_last_work_time;
  1184. time_t last_share_time;
  1185. double last_share_diff;
  1186. double best_diff;
  1187. struct cgminer_stats cgminer_stats;
  1188. struct cgminer_pool_stats cgminer_pool_stats;
  1189. /* Stratum variables */
  1190. char *stratum_url;
  1191. char *stratum_port;
  1192. CURL *stratum_curl;
  1193. char curl_err_str[CURL_ERROR_SIZE];
  1194. SOCKETTYPE sock;
  1195. char *sockbuf;
  1196. size_t sockbuf_size;
  1197. char *sockaddr_url; /* stripped url used for sockaddr */
  1198. size_t n1_len;
  1199. uint64_t nonce2;
  1200. int nonce2sz;
  1201. #ifdef WORDS_BIGENDIAN
  1202. int nonce2off;
  1203. #endif
  1204. char *sessionid;
  1205. bool has_stratum;
  1206. bool stratum_active;
  1207. bool stratum_init;
  1208. bool stratum_notify;
  1209. struct stratum_work swork;
  1210. char *goalname;
  1211. char *next_goalname;
  1212. struct mining_algorithm *next_goal_malgo;
  1213. uint8_t next_target[0x20];
  1214. char *next_nonce1;
  1215. int next_n2size;
  1216. pthread_t stratum_thread;
  1217. pthread_mutex_t stratum_lock;
  1218. char *admin_msg;
  1219. /* param for coinbase check */
  1220. struct coinbase_param cb_param;
  1221. pthread_mutex_t last_work_lock;
  1222. struct work *last_work_copy;
  1223. };
  1224. #define GETWORK_MODE_TESTPOOL 'T'
  1225. #define GETWORK_MODE_POOL 'P'
  1226. #define GETWORK_MODE_LP 'L'
  1227. #define GETWORK_MODE_BENCHMARK 'B'
  1228. #define GETWORK_MODE_STRATUM 'S'
  1229. #define GETWORK_MODE_GBT 'G'
  1230. typedef unsigned work_device_id_t;
  1231. #define PRIwdi "04x"
  1232. struct work {
  1233. unsigned char data[128];
  1234. unsigned char midstate[32];
  1235. unsigned char target[32];
  1236. unsigned char hash[32];
  1237. double share_diff;
  1238. int rolls;
  1239. struct ntime_roll_limits ntime_roll_limits;
  1240. struct {
  1241. uint32_t nonce;
  1242. } blk;
  1243. struct thr_info *thr;
  1244. int thr_id;
  1245. struct pool *pool;
  1246. struct timeval tv_staged;
  1247. bool mined;
  1248. bool clone;
  1249. bool cloned;
  1250. int rolltime;
  1251. bool longpoll;
  1252. bool stale;
  1253. bool mandatory;
  1254. bool spare;
  1255. bool block;
  1256. bool stratum;
  1257. char *job_id;
  1258. bytes_t nonce2;
  1259. char *nonce1;
  1260. unsigned char work_restart_id;
  1261. int id;
  1262. work_device_id_t device_id;
  1263. UT_hash_handle hh;
  1264. // Please don't use this if it's at all possible, I'd like to get rid of it eventually.
  1265. void *device_data;
  1266. void *(*device_data_dup_func)(struct work *);
  1267. void (*device_data_free_func)(struct work *);
  1268. double work_difficulty;
  1269. float nonce_diff;
  1270. // Allow devices to identify work if multiple sub-devices
  1271. // DEPRECATED: New code should be using multiple processors instead
  1272. int subid;
  1273. // Allow devices to timestamp work for their own purposes
  1274. struct timeval tv_stamp;
  1275. struct bfg_tmpl_ref *tr;
  1276. unsigned int dataid;
  1277. bool do_foreign_submit;
  1278. struct timeval tv_getwork;
  1279. time_t ts_getwork;
  1280. struct timeval tv_getwork_reply;
  1281. struct timeval tv_cloned;
  1282. struct timeval tv_work_start;
  1283. struct timeval tv_work_found;
  1284. char getwork_mode;
  1285. /* Used to queue shares in submit_waiting */
  1286. struct work *prev;
  1287. struct work *next;
  1288. };
  1289. extern void get_datestamp(char *, size_t, time_t);
  1290. #define get_now_datestamp(buf, bufsz) get_datestamp(buf, bufsz, INVALID_TIMESTAMP)
  1291. extern void get_benchmark_work(struct work *, bool use_swork);
  1292. extern void stratum_work_cpy(struct stratum_work *dst, const struct stratum_work *src);
  1293. extern void stratum_work_clean(struct stratum_work *);
  1294. extern bool pool_has_usable_swork(const struct pool *);
  1295. extern void gen_stratum_work2(struct work *, struct stratum_work *);
  1296. extern void inc_hw_errors3(struct thr_info *thr, const struct work *work, const uint32_t *bad_nonce_p, float nonce_diff);
  1297. static inline
  1298. void inc_hw_errors2(struct thr_info * const thr, const struct work * const work, const uint32_t *bad_nonce_p)
  1299. {
  1300. inc_hw_errors3(thr, work, bad_nonce_p, work ? work->nonce_diff : 1.);
  1301. }
  1302. #define UNKNOWN_NONCE ((uint32_t*)inc_hw_errors2)
  1303. static inline
  1304. void inc_hw_errors(struct thr_info * const thr, const struct work * const work, const uint32_t bad_nonce)
  1305. {
  1306. inc_hw_errors2(thr, work, work ? &bad_nonce : NULL);
  1307. }
  1308. #define inc_hw_errors_only(thr) inc_hw_errors(thr, NULL, 0)
  1309. enum test_nonce2_result {
  1310. TNR_GOOD = 1,
  1311. TNR_HIGH = 0,
  1312. TNR_BAD = -1,
  1313. };
  1314. extern enum test_nonce2_result _test_nonce2(struct work *, uint32_t nonce, bool checktarget);
  1315. #define test_nonce(work, nonce, checktarget) (_test_nonce2(work, nonce, checktarget) == TNR_GOOD)
  1316. #define test_nonce2(work, nonce) (_test_nonce2(work, nonce, true))
  1317. extern bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  1318. extern bool submit_noffset_nonce(struct thr_info *thr, struct work *work, uint32_t nonce,
  1319. int noffset);
  1320. extern void __add_queued(struct cgpu_info *cgpu, struct work *work);
  1321. extern struct work *get_queued(struct cgpu_info *cgpu);
  1322. extern void add_queued(struct cgpu_info *cgpu, struct work *work);
  1323. extern struct work *get_queue_work(struct thr_info *thr, struct cgpu_info *cgpu, int thr_id);
  1324. extern struct work *__find_work_bymidstate(struct work *que, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1325. extern struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1326. extern struct work *clone_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1327. extern void __work_completed(struct cgpu_info *cgpu, struct work *work);
  1328. extern int age_queued_work(struct cgpu_info *cgpu, double secs);
  1329. extern void work_completed(struct cgpu_info *cgpu, struct work *work);
  1330. extern struct work *take_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1331. extern void flush_queue(struct cgpu_info *cgpu);
  1332. extern bool abandon_work(struct work *, struct timeval *work_runtime, uint64_t hashes);
  1333. extern void hash_queued_work(struct thr_info *mythr);
  1334. extern void get_statline3(char *buf, size_t bufsz, struct cgpu_info *, bool for_curses, bool opt_show_procs);
  1335. extern void tailsprintf(char *buf, size_t bufsz, const char *fmt, ...) FORMAT_SYNTAX_CHECK(printf, 3, 4);
  1336. extern void _wlog(const char *str);
  1337. extern void _wlogprint(const char *str);
  1338. extern int curses_int(const char *query);
  1339. extern char *curses_input(const char *query);
  1340. extern bool drv_ready(struct cgpu_info *);
  1341. extern double stats_elapsed(struct cgminer_stats *);
  1342. #define cgpu_runtime(cgpu) stats_elapsed(&((cgpu)->cgminer_stats))
  1343. extern double cgpu_utility(struct cgpu_info *);
  1344. extern void kill_work(void);
  1345. extern int prioritize_pools(char *param, int *pid);
  1346. extern void validate_pool_priorities(void);
  1347. extern void enable_pool(struct pool *);
  1348. extern void manual_enable_pool(struct pool *);
  1349. extern void disable_pool(struct pool *, enum pool_enable);
  1350. extern void switch_pools(struct pool *selected);
  1351. extern void remove_pool(struct pool *pool);
  1352. extern void write_config(FILE *fcfg);
  1353. extern void zero_bestshare(void);
  1354. extern void zero_stats(void);
  1355. extern void default_save_file(char *filename);
  1356. extern bool _log_curses_only(int prio, const char *datetime, const char *str);
  1357. extern void clear_logwin(void);
  1358. extern void logwin_update(void);
  1359. extern bool pool_tclear(struct pool *pool, bool *var);
  1360. extern bool pool_may_redirect_to(struct pool *, const char *uri);
  1361. extern void pool_check_coinbase(struct pool *, const uint8_t *cbtxn, size_t cbtxnsz);
  1362. extern struct thread_q *tq_new(void);
  1363. extern void tq_free(struct thread_q *tq);
  1364. extern bool tq_push(struct thread_q *tq, void *data);
  1365. extern void *tq_pop(struct thread_q *);
  1366. extern void tq_freeze(struct thread_q *tq);
  1367. extern void tq_thaw(struct thread_q *tq);
  1368. extern bool successful_connect;
  1369. extern void adl(void);
  1370. extern void tmpl_decref(struct bfg_tmpl_ref *);
  1371. extern void clean_work(struct work *work);
  1372. extern void free_work(struct work *work);
  1373. extern void __copy_work(struct work *work, const struct work *base_work);
  1374. extern struct work *copy_work_noffset(const struct work *base_work, int noffset);
  1375. #define copy_work(work_in) copy_work_noffset(work_in, 0)
  1376. extern double share_diff(const struct work *);
  1377. extern const char *bfg_workpadding_bin;
  1378. extern void set_simple_ntime_roll_limit(struct ntime_roll_limits *, uint32_t ntime_base, int ntime_roll, const struct timeval *tvp_ref);
  1379. extern void work_set_simple_ntime_roll_limit(struct work *, int ntime_roll, const struct timeval *tvp_ref);
  1380. extern int work_ntime_range(struct work *, const struct timeval *tvp_earliest, const struct timeval *tvp_latest, int desired_roll);
  1381. static inline
  1382. struct mining_algorithm *work_mining_algorithm(const struct work * const work)
  1383. {
  1384. const struct pool * const pool = work->pool;
  1385. const struct mining_goal_info * const goal = pool->goal;
  1386. struct mining_algorithm * const malgo = goal->malgo;
  1387. return malgo;
  1388. }
  1389. extern void work_hash(struct work *);
  1390. #define NTIME_DATA_OFFSET 0x44
  1391. static inline
  1392. uint32_t work_get_ntime(const struct work * const work)
  1393. {
  1394. return upk_u32be(work->data, 0x44);
  1395. }
  1396. static inline
  1397. void work_set_ntime(struct work * const work, const uint32_t ntime)
  1398. {
  1399. pk_u32be(work->data, 0x44, ntime);
  1400. }
  1401. extern char *devpath_to_devid(const char *);
  1402. extern struct thr_info *get_thread(int thr_id);
  1403. extern struct cgpu_info *get_devices(int id);
  1404. extern int create_new_cgpus(void (*addfunc)(void*), void *arg);
  1405. extern int scan_serial(const char *);
  1406. extern bool check_coinbase(const uint8_t *, size_t, const struct coinbase_param *cb_param);
  1407. enum api_data_type {
  1408. API_ESCAPE,
  1409. API_STRING,
  1410. API_CONST,
  1411. API_UINT8,
  1412. API_INT16,
  1413. API_UINT16,
  1414. API_INT,
  1415. API_UINT,
  1416. API_UINT32,
  1417. API_UINT64,
  1418. API_DOUBLE,
  1419. API_ELAPSED,
  1420. API_BOOL,
  1421. API_TIMEVAL,
  1422. API_TIME,
  1423. API_MHS,
  1424. API_MHTOTAL,
  1425. API_TEMP,
  1426. API_UTILITY,
  1427. API_FREQ,
  1428. API_VOLTS,
  1429. API_HS,
  1430. API_DIFF,
  1431. API_JSON,
  1432. API_PERCENT
  1433. };
  1434. struct api_data {
  1435. enum api_data_type type;
  1436. char *name;
  1437. void *data;
  1438. bool data_was_malloc;
  1439. struct api_data *prev;
  1440. struct api_data *next;
  1441. };
  1442. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  1443. extern struct api_data *api_add_string(struct api_data *root, char *name, const char *data, bool copy_data);
  1444. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  1445. extern struct api_data *api_add_uint8(struct api_data *root, char *name, uint8_t *data, bool copy_data);
  1446. extern struct api_data *api_add_int16(struct api_data *root, char *name, uint16_t *data, bool copy_data);
  1447. extern struct api_data *api_add_uint16(struct api_data *root, char *name, uint16_t *data, bool copy_data);
  1448. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  1449. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  1450. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  1451. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  1452. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  1453. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  1454. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  1455. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  1456. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  1457. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  1458. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  1459. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  1460. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  1461. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  1462. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  1463. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  1464. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  1465. extern struct api_data *api_add_json(struct api_data *root, char *name, json_t *data, bool copy_data);
  1466. #endif /* __MINER_H__ */