miner.h 42 KB

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