miner.h 36 KB

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