miner.h 41 KB

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