miner.h 43 KB

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