miner.h 39 KB

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