miner.h 39 KB

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