miner.h 35 KB

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