miner.h 37 KB

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