miner.h 43 KB

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