miner.h 44 KB

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