miner.h 45 KB

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