miner.h 44 KB

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