miner.h 40 KB

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