miner.h 36 KB

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