miner.h 38 KB

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