miner.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  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, // Disabled by user
  294. DEV_RECOVER, // Disabled by temperature cutoff in watchdog
  295. DEV_RECOVER_ERR, // Disabled by communications error
  296. DEV_RECOVER_DRV, // Disabled by driver
  297. };
  298. enum cl_kernels {
  299. KL_NONE,
  300. KL_POCLBM,
  301. KL_PHATK,
  302. KL_DIAKGCN,
  303. KL_DIABLO,
  304. KL_SCRYPT,
  305. };
  306. enum dev_reason {
  307. REASON_THREAD_FAIL_INIT,
  308. REASON_THREAD_ZERO_HASH,
  309. REASON_THREAD_FAIL_QUEUE,
  310. REASON_DEV_SICK_IDLE_60,
  311. REASON_DEV_DEAD_IDLE_600,
  312. REASON_DEV_NOSTART,
  313. REASON_DEV_OVER_HEAT,
  314. REASON_DEV_THERMAL_CUTOFF,
  315. REASON_DEV_COMMS_ERROR,
  316. REASON_DEV_THROTTLE,
  317. };
  318. #define REASON_NONE "None"
  319. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  320. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  321. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  322. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  323. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  324. #define REASON_DEV_NOSTART_STR "Device failed to start"
  325. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  326. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  327. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  328. #define REASON_DEV_THROTTLE_STR "Device throttle"
  329. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  330. #define MIN_SEC_UNSET 99999999
  331. enum {
  332. MSG_NOPOOL = 8,
  333. MSG_MISPID = 25,
  334. MSG_INVPID = 26,
  335. MSG_DUPPID = 74,
  336. MSG_POOLPRIO = 73,
  337. };
  338. struct cgminer_stats {
  339. struct timeval start_tv;
  340. uint32_t getwork_calls;
  341. struct timeval getwork_wait;
  342. struct timeval getwork_wait_max;
  343. struct timeval getwork_wait_min;
  344. struct timeval _get_start;
  345. };
  346. // Just the actual network getworks to the pool
  347. struct cgminer_pool_stats {
  348. uint32_t getwork_calls;
  349. uint32_t getwork_attempts;
  350. struct timeval getwork_wait;
  351. struct timeval getwork_wait_max;
  352. struct timeval getwork_wait_min;
  353. double getwork_wait_rolling;
  354. bool hadrolltime;
  355. bool canroll;
  356. bool hadexpire;
  357. uint32_t rolltime;
  358. double min_diff;
  359. double max_diff;
  360. double last_diff;
  361. uint32_t min_diff_count;
  362. uint32_t max_diff_count;
  363. uint64_t times_sent;
  364. uint64_t bytes_sent;
  365. uint64_t net_bytes_sent;
  366. uint64_t times_received;
  367. uint64_t bytes_received;
  368. uint64_t net_bytes_received;
  369. };
  370. #define PRIprepr "-6s"
  371. #define PRIpreprv "s"
  372. struct cgpu_info {
  373. int cgminer_id;
  374. int device_line_id;
  375. struct device_drv *drv;
  376. const char *devtype;
  377. int device_id;
  378. char *dev_repr;
  379. char *dev_repr_ns;
  380. const char *name;
  381. int procs;
  382. int proc_id;
  383. char proc_repr[8];
  384. char proc_repr_ns[8];
  385. struct cgpu_info *device;
  386. struct cgpu_info *next_proc;
  387. const char *device_path;
  388. void *device_data;
  389. const char *dev_manufacturer;
  390. const char *dev_product;
  391. const char *dev_serial;
  392. union {
  393. #ifdef USE_ZTEX
  394. struct libztex_device *device_ztex;
  395. #endif
  396. int device_fd;
  397. #ifdef USE_X6500
  398. struct ft232r_device_handle *device_ft232r;
  399. #endif
  400. };
  401. #ifdef USE_AVALON
  402. struct work **works;
  403. int work_array;
  404. int queued;
  405. int results;
  406. #endif
  407. #ifdef USE_BITFORCE
  408. struct timeval work_start_tv;
  409. unsigned int wait_ms;
  410. unsigned int sleep_ms;
  411. double avg_wait_f;
  412. unsigned int avg_wait_d;
  413. uint32_t nonces;
  414. bool polling;
  415. #endif
  416. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  417. bool flash_led;
  418. #endif
  419. pthread_mutex_t device_mutex;
  420. pthread_cond_t device_cond;
  421. enum dev_enable deven;
  422. bool already_set_defaults;
  423. int accepted;
  424. int rejected;
  425. int stale;
  426. int bad_nonces;
  427. int hw_errors;
  428. double rolling;
  429. double total_mhashes;
  430. double utility;
  431. double utility_diff1;
  432. enum alive status;
  433. char init[40];
  434. struct timeval last_message_tv;
  435. int threads;
  436. struct thr_info **thr;
  437. int64_t max_hashes;
  438. const char *kname;
  439. #ifdef HAVE_OPENCL
  440. bool mapped;
  441. int virtual_gpu;
  442. int virtual_adl;
  443. int intensity;
  444. bool dynamic;
  445. cl_uint vwidth;
  446. size_t work_size;
  447. enum cl_kernels kernel;
  448. cl_ulong max_alloc;
  449. #ifdef USE_SCRYPT
  450. int opt_lg, lookup_gap;
  451. size_t opt_tc, thread_concurrency;
  452. size_t shaders;
  453. #endif
  454. struct timeval tv_gpustart;
  455. int intervals;
  456. #endif
  457. float temp;
  458. int cutofftemp;
  459. uint8_t cutofftemp_default;
  460. int targettemp;
  461. uint8_t targettemp_default;
  462. #ifdef HAVE_ADL
  463. bool has_adl;
  464. struct gpu_adl adl;
  465. int gpu_engine;
  466. int min_engine;
  467. int gpu_fan;
  468. int min_fan;
  469. int gpu_memclock;
  470. int gpu_memdiff;
  471. int gpu_powertune;
  472. float gpu_vddc;
  473. #endif
  474. int diff1;
  475. double diff_accepted;
  476. double diff_rejected;
  477. double diff_stale;
  478. int last_share_pool;
  479. time_t last_share_pool_time;
  480. double last_share_diff;
  481. time_t last_device_valid_work;
  482. time_t device_last_well;
  483. time_t device_last_not_well;
  484. struct timeval tv_device_last_not_well;
  485. enum dev_reason device_not_well_reason;
  486. float reinit_backoff;
  487. int thread_fail_init_count;
  488. int thread_zero_hash_count;
  489. int thread_fail_queue_count;
  490. int dev_sick_idle_60_count;
  491. int dev_dead_idle_600_count;
  492. int dev_nostart_count;
  493. int dev_over_heat_count; // It's a warning but worth knowing
  494. int dev_thermal_cutoff_count;
  495. int dev_comms_error_count;
  496. int dev_throttle_count;
  497. struct cgminer_stats cgminer_stats;
  498. pthread_rwlock_t qlock;
  499. struct work *queued_work;
  500. unsigned int queued_count;
  501. bool disable_watchdog;
  502. bool shutdown;
  503. };
  504. extern void renumber_cgpu(struct cgpu_info *);
  505. extern bool add_cgpu(struct cgpu_info*);
  506. struct tq_ent;
  507. struct thread_q {
  508. struct tq_ent *q;
  509. bool frozen;
  510. pthread_mutex_t mutex;
  511. pthread_cond_t cond;
  512. };
  513. enum thr_busy_state {
  514. TBS_IDLE,
  515. TBS_GETTING_RESULTS,
  516. TBS_STARTING_JOB,
  517. };
  518. struct thr_info {
  519. int id;
  520. int device_thread;
  521. bool primary_thread;
  522. bool has_pth;
  523. pthread_t pth;
  524. struct thread_q *q;
  525. struct cgpu_info *cgpu;
  526. void *cgpu_data;
  527. struct timeval last;
  528. struct timeval sick;
  529. bool scanhash_working;
  530. uint64_t hashes_done;
  531. struct timeval tv_hashes_done;
  532. struct timeval tv_lastupdate;
  533. struct timeval _tv_last_hashes_done_call;
  534. bool pause;
  535. time_t getwork;
  536. double rolling;
  537. // Used by minerloop_async
  538. struct work *prev_work;
  539. struct work *work;
  540. struct work *next_work;
  541. enum thr_busy_state busy_state;
  542. bool _mt_disable_called;
  543. struct timeval tv_morework;
  544. struct work *results_work;
  545. bool _job_transition_in_progress;
  546. bool _proceed_with_new_job;
  547. struct timeval tv_results_jobstart;
  548. struct timeval tv_jobstart;
  549. struct timeval tv_poll;
  550. struct timeval tv_watchdog;
  551. notifier_t notifier;
  552. bool starting_next_work;
  553. uint32_t _max_nonce;
  554. notifier_t mutex_request;
  555. // Used by minerloop_queue
  556. struct work *work_list;
  557. bool queue_full;
  558. bool work_restart;
  559. notifier_t work_restart_notifier;
  560. };
  561. struct string_elist {
  562. char *string;
  563. bool free_me;
  564. struct string_elist *prev;
  565. struct string_elist *next;
  566. };
  567. static inline void string_elist_add(const char *s, struct string_elist **head)
  568. {
  569. struct string_elist *n;
  570. n = calloc(1, sizeof(*n));
  571. n->string = strdup(s);
  572. n->free_me = true;
  573. DL_APPEND(*head, n);
  574. }
  575. static inline void string_elist_del(struct string_elist **head, struct string_elist *item)
  576. {
  577. if (item->free_me)
  578. free(item->string);
  579. DL_DELETE(*head, item);
  580. free(item);
  581. }
  582. static inline uint32_t swab32(uint32_t v)
  583. {
  584. return bswap_32(v);
  585. }
  586. static inline void swap256(void *dest_p, const void *src_p)
  587. {
  588. uint32_t *dest = dest_p;
  589. const uint32_t *src = src_p;
  590. dest[0] = src[7];
  591. dest[1] = src[6];
  592. dest[2] = src[5];
  593. dest[3] = src[4];
  594. dest[4] = src[3];
  595. dest[5] = src[2];
  596. dest[6] = src[1];
  597. dest[7] = src[0];
  598. }
  599. static inline void swap32yes(void*out, const void*in, size_t sz) {
  600. size_t swapcounter = 0;
  601. for (swapcounter = 0; swapcounter < sz; ++swapcounter)
  602. (((uint32_t*)out)[swapcounter]) = swab32(((uint32_t*)in)[swapcounter]);
  603. }
  604. #define LOCAL_swap32(type, var, sz) \
  605. type __swapped_ ## var[sz * 4 / sizeof(type)]; \
  606. swap32yes(__swapped_ ## var, var, sz); \
  607. var = __swapped_ ## var; \
  608. // end
  609. #ifdef WORDS_BIGENDIAN
  610. # define swap32tobe(out, in, sz) ((out == in) ? (void)0 : memmove(out, in, sz))
  611. # define LOCAL_swap32be(type, var, sz) ;
  612. # define swap32tole(out, in, sz) swap32yes(out, in, sz)
  613. # define LOCAL_swap32le(type, var, sz) LOCAL_swap32(type, var, sz)
  614. #else
  615. # define swap32tobe(out, in, sz) swap32yes(out, in, sz)
  616. # define LOCAL_swap32be(type, var, sz) LOCAL_swap32(type, var, sz)
  617. # define swap32tole(out, in, sz) ((out == in) ? (void)0 : memmove(out, in, sz))
  618. # define LOCAL_swap32le(type, var, sz) ;
  619. #endif
  620. static inline void swab256(void *dest_p, const void *src_p)
  621. {
  622. uint32_t *dest = dest_p;
  623. const uint32_t *src = src_p;
  624. dest[0] = swab32(src[7]);
  625. dest[1] = swab32(src[6]);
  626. dest[2] = swab32(src[5]);
  627. dest[3] = swab32(src[4]);
  628. dest[4] = swab32(src[3]);
  629. dest[5] = swab32(src[2]);
  630. dest[6] = swab32(src[1]);
  631. dest[7] = swab32(src[0]);
  632. }
  633. #define flip32(dest_p, src_p) swap32yes(dest_p, src_p, 32 / 4)
  634. #define WATCHDOG_INTERVAL 2
  635. extern void bfg_watchdog(struct cgpu_info *, struct timeval *tvp_now);
  636. extern void _quit(int status);
  637. static inline void mutex_lock(pthread_mutex_t *lock)
  638. {
  639. if (unlikely(pthread_mutex_lock(lock)))
  640. quit(1, "WTF MUTEX ERROR ON LOCK!");
  641. }
  642. static inline void mutex_unlock_noyield(pthread_mutex_t *lock)
  643. {
  644. if (unlikely(pthread_mutex_unlock(lock)))
  645. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  646. }
  647. static inline void mutex_unlock(pthread_mutex_t *lock)
  648. {
  649. mutex_unlock_noyield(lock);
  650. sched_yield();
  651. }
  652. static inline int mutex_trylock(pthread_mutex_t *lock)
  653. {
  654. return pthread_mutex_trylock(lock);
  655. }
  656. static inline void wr_lock(pthread_rwlock_t *lock)
  657. {
  658. if (unlikely(pthread_rwlock_wrlock(lock)))
  659. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  660. }
  661. static inline void rd_lock(pthread_rwlock_t *lock)
  662. {
  663. if (unlikely(pthread_rwlock_rdlock(lock)))
  664. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  665. }
  666. static inline void rw_unlock(pthread_rwlock_t *lock)
  667. {
  668. if (unlikely(pthread_rwlock_unlock(lock)))
  669. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  670. }
  671. static inline void rd_unlock_noyield(pthread_rwlock_t *lock)
  672. {
  673. rw_unlock(lock);
  674. }
  675. static inline void wr_unlock_noyield(pthread_rwlock_t *lock)
  676. {
  677. rw_unlock(lock);
  678. }
  679. static inline void rd_unlock(pthread_rwlock_t *lock)
  680. {
  681. rw_unlock(lock);
  682. sched_yield();
  683. }
  684. static inline void wr_unlock(pthread_rwlock_t *lock)
  685. {
  686. rw_unlock(lock);
  687. sched_yield();
  688. }
  689. static inline void mutex_init(pthread_mutex_t *lock)
  690. {
  691. if (unlikely(pthread_mutex_init(lock, NULL)))
  692. quit(1, "Failed to pthread_mutex_init");
  693. }
  694. static inline void rwlock_init(pthread_rwlock_t *lock)
  695. {
  696. if (unlikely(pthread_rwlock_init(lock, NULL)))
  697. quit(1, "Failed to pthread_rwlock_init");
  698. }
  699. /* cgminer locks, a write biased variant of rwlocks */
  700. struct cglock {
  701. pthread_mutex_t mutex;
  702. pthread_rwlock_t rwlock;
  703. };
  704. typedef struct cglock cglock_t;
  705. static inline void cglock_init(cglock_t *lock)
  706. {
  707. mutex_init(&lock->mutex);
  708. rwlock_init(&lock->rwlock);
  709. }
  710. /* Read lock variant of cglock. Cannot be promoted. */
  711. static inline void cg_rlock(cglock_t *lock)
  712. {
  713. mutex_lock(&lock->mutex);
  714. rd_lock(&lock->rwlock);
  715. mutex_unlock_noyield(&lock->mutex);
  716. }
  717. /* Intermediate variant of cglock - behaves as a read lock but can be promoted
  718. * to a write lock or demoted to read lock. */
  719. static inline void cg_ilock(cglock_t *lock)
  720. {
  721. mutex_lock(&lock->mutex);
  722. }
  723. /* Upgrade intermediate variant to a write lock */
  724. static inline void cg_ulock(cglock_t *lock)
  725. {
  726. wr_lock(&lock->rwlock);
  727. }
  728. /* Write lock variant of cglock */
  729. static inline void cg_wlock(cglock_t *lock)
  730. {
  731. mutex_lock(&lock->mutex);
  732. wr_lock(&lock->rwlock);
  733. }
  734. /* Downgrade write variant to a read lock */
  735. static inline void cg_dwlock(cglock_t *lock)
  736. {
  737. wr_unlock_noyield(&lock->rwlock);
  738. rd_lock(&lock->rwlock);
  739. mutex_unlock_noyield(&lock->mutex);
  740. }
  741. /* Demote a write variant to an intermediate variant */
  742. static inline void cg_dwilock(cglock_t *lock)
  743. {
  744. wr_unlock(&lock->rwlock);
  745. }
  746. /* Downgrade intermediate variant to a read lock */
  747. static inline void cg_dlock(cglock_t *lock)
  748. {
  749. rd_lock(&lock->rwlock);
  750. mutex_unlock(&lock->mutex);
  751. }
  752. static inline void cg_runlock(cglock_t *lock)
  753. {
  754. rd_unlock(&lock->rwlock);
  755. }
  756. static inline void cg_wunlock(cglock_t *lock)
  757. {
  758. wr_unlock_noyield(&lock->rwlock);
  759. mutex_unlock(&lock->mutex);
  760. }
  761. struct pool;
  762. #define API_MCAST_CODE "FTW"
  763. #define API_MCAST_ADDR "224.0.0.75"
  764. extern bool opt_protocol;
  765. extern bool opt_dev_protocol;
  766. extern char *opt_coinbase_sig;
  767. extern char *request_target_str;
  768. extern bool have_longpoll;
  769. extern int opt_skip_checks;
  770. extern char *opt_kernel_path;
  771. extern char *opt_socks_proxy;
  772. extern char *cmd_idle, *cmd_sick, *cmd_dead;
  773. extern char *cgminer_path;
  774. extern bool opt_fail_only;
  775. extern bool opt_autofan;
  776. extern bool opt_autoengine;
  777. extern bool use_curses;
  778. #ifdef HAVE_LIBUSB
  779. extern bool have_libusb;
  780. #endif
  781. extern int httpsrv_port;
  782. extern int stratumsrv_port;
  783. extern char *opt_api_allow;
  784. extern bool opt_api_mcast;
  785. extern char *opt_api_mcast_addr;
  786. extern char *opt_api_mcast_code;
  787. extern char *opt_api_mcast_des;
  788. extern int opt_api_mcast_port;
  789. extern char *opt_api_groups;
  790. extern char *opt_api_description;
  791. extern int opt_api_port;
  792. extern bool opt_api_listen;
  793. extern bool opt_api_network;
  794. extern bool opt_delaynet;
  795. extern bool opt_restart;
  796. extern char *opt_icarus_options;
  797. extern char *opt_icarus_timing;
  798. extern bool opt_worktime;
  799. #ifdef USE_AVALON
  800. extern char *opt_avalon_options;
  801. #endif
  802. #ifdef USE_BITFORCE
  803. extern bool opt_bfl_noncerange;
  804. #endif
  805. extern int swork_id;
  806. extern pthread_rwlock_t netacc_lock;
  807. extern const uint32_t sha256_init_state[];
  808. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  809. const char *rpc_req, bool, bool, int *,
  810. struct pool *pool, bool);
  811. extern bool our_curl_supports_proxy_uris();
  812. extern void bin2hex(char *out, const void *in, size_t len);
  813. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  814. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  815. unsigned char *pdata,
  816. unsigned char *phash1, unsigned char *phash,
  817. const unsigned char *ptarget,
  818. uint32_t max_nonce,
  819. uint32_t *last_nonce,
  820. uint32_t nonce);
  821. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  822. extern int opt_queue;
  823. extern int opt_scantime;
  824. extern int opt_expiry;
  825. extern cglock_t control_lock;
  826. extern pthread_mutex_t stats_lock;
  827. extern pthread_mutex_t hash_lock;
  828. extern pthread_mutex_t console_lock;
  829. extern cglock_t ch_lock;
  830. extern pthread_rwlock_t mining_thr_lock;
  831. extern pthread_rwlock_t devices_lock;
  832. extern bool _bfg_console_cancel_disabled;
  833. extern int _bfg_console_prev_cancelstate;
  834. static inline
  835. void bfg_console_lock(void)
  836. {
  837. _bfg_console_cancel_disabled = !pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &_bfg_console_prev_cancelstate);
  838. mutex_lock(&console_lock);
  839. }
  840. static inline
  841. void bfg_console_unlock(void)
  842. {
  843. mutex_unlock(&console_lock);
  844. if (_bfg_console_cancel_disabled)
  845. pthread_setcancelstate(_bfg_console_prev_cancelstate, &_bfg_console_prev_cancelstate);
  846. }
  847. extern void thread_reportin(struct thr_info *thr);
  848. extern void thread_reportout(struct thr_info *);
  849. extern void clear_stratum_shares(struct pool *pool);
  850. extern void hashmeter2(struct thr_info *);
  851. extern bool stale_work(struct work *, bool share);
  852. extern bool stale_work_future(struct work *, bool share, unsigned long ustime);
  853. extern void set_target(unsigned char *dest_target, double diff);
  854. extern void kill_work(void);
  855. extern void app_restart(void);
  856. extern void __thr_being_msg(int prio, struct thr_info *, const char *);
  857. extern void mt_enable(struct thr_info *thr);
  858. extern void proc_enable(struct cgpu_info *);
  859. extern void reinit_device(struct cgpu_info *cgpu);
  860. extern void cgpu_set_defaults(struct cgpu_info *);
  861. #ifdef HAVE_ADL
  862. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  863. extern int set_fanspeed(int gpu, int iFanSpeed);
  864. extern int set_vddc(int gpu, float fVddc);
  865. extern int set_engineclock(int gpu, int iEngineClock);
  866. extern int set_memoryclock(int gpu, int iMemoryClock);
  867. #endif
  868. extern void api(int thr_id);
  869. extern struct pool *current_pool(void);
  870. extern int enabled_pools;
  871. extern bool get_intrange(const char *arg, int *val1, int *val2);
  872. extern bool detect_stratum(struct pool *pool, char *url);
  873. extern void print_summary(void);
  874. extern void adjust_quota_gcd(void);
  875. extern struct pool *add_pool(void);
  876. extern bool add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  877. #define MAX_GPUDEVICES 16
  878. #define MAX_DEVICES 4096
  879. #define MIN_SHA_INTENSITY -10
  880. #define MIN_SHA_INTENSITY_STR "-10"
  881. #define MAX_SHA_INTENSITY 14
  882. #define MAX_SHA_INTENSITY_STR "14"
  883. #define MIN_SCRYPT_INTENSITY 8
  884. #define MIN_SCRYPT_INTENSITY_STR "8"
  885. #define MAX_SCRYPT_INTENSITY 20
  886. #define MAX_SCRYPT_INTENSITY_STR "20"
  887. #ifdef USE_SCRYPT
  888. #define MIN_INTENSITY (opt_scrypt ? MIN_SCRYPT_INTENSITY : MIN_SHA_INTENSITY)
  889. #define MIN_INTENSITY_STR (opt_scrypt ? MIN_SCRYPT_INTENSITY_STR : MIN_SHA_INTENSITY_STR)
  890. #define MAX_INTENSITY (opt_scrypt ? MAX_SCRYPT_INTENSITY : MAX_SHA_INTENSITY)
  891. #define MAX_INTENSITY_STR (opt_scrypt ? MAX_SCRYPT_INTENSITY_STR : MAX_SHA_INTENSITY_STR)
  892. #define MAX_GPU_INTENSITY MAX_SCRYPT_INTENSITY
  893. #else
  894. #define MIN_INTENSITY MIN_SHA_INTENSITY
  895. #define MIN_INTENSITY_STR MIN_SHA_INTENSITY_STR
  896. #define MAX_INTENSITY MAX_SHA_INTENSITY
  897. #define MAX_INTENSITY_STR MAX_SHA_INTENSITY_STR
  898. #define MAX_GPU_INTENSITY MAX_SHA_INTENSITY
  899. #endif
  900. extern struct string_elist *scan_devices;
  901. extern bool opt_force_dev_init;
  902. extern int nDevs;
  903. extern int opt_n_threads;
  904. extern int num_processors;
  905. extern int hw_errors;
  906. extern bool use_syslog;
  907. extern bool opt_quiet;
  908. extern struct thr_info *control_thr;
  909. extern struct thr_info **mining_thr;
  910. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  911. extern int gpu_threads;
  912. #ifdef USE_SCRYPT
  913. extern bool opt_scrypt;
  914. #else
  915. #define opt_scrypt (0)
  916. #endif
  917. extern double total_secs;
  918. extern int mining_threads;
  919. extern struct cgpu_info *cpus;
  920. extern int total_devices;
  921. extern struct cgpu_info **devices;
  922. extern int total_devices_new;
  923. extern struct cgpu_info **devices_new;
  924. extern int total_pools;
  925. extern struct pool **pools;
  926. extern const char *algo_names[];
  927. extern enum sha256_algos opt_algo;
  928. extern struct strategies strategies[];
  929. extern enum pool_strategy pool_strategy;
  930. extern int opt_rotate_period;
  931. extern double total_rolling;
  932. extern double total_mhashes_done;
  933. extern unsigned int new_blocks;
  934. extern unsigned int found_blocks;
  935. extern int total_accepted, total_rejected, total_diff1;;
  936. extern int total_bad_nonces;
  937. extern int total_getworks, total_stale, total_discarded;
  938. extern uint64_t total_bytes_rcvd, total_bytes_sent;
  939. #define total_bytes_xfer (total_bytes_rcvd + total_bytes_sent)
  940. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  941. extern unsigned int local_work;
  942. extern unsigned int total_go, total_ro;
  943. extern const int opt_cutofftemp;
  944. extern int opt_hysteresis;
  945. extern int opt_fail_pause;
  946. extern int opt_log_interval;
  947. extern unsigned long long global_hashrate;
  948. extern char *current_fullhash;
  949. extern double current_diff;
  950. extern uint64_t best_diff;
  951. extern time_t block_time;
  952. #ifdef HAVE_OPENCL
  953. typedef struct {
  954. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  955. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  956. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  957. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  958. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  959. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  960. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  961. cl_uint W16; cl_uint W17; cl_uint W2;
  962. cl_uint PreVal4; cl_uint T1;
  963. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  964. cl_uint PreVal4addT1; cl_uint T1substate0;
  965. cl_uint PreVal4_2;
  966. cl_uint PreVal0;
  967. cl_uint PreW18;
  968. cl_uint PreW19;
  969. cl_uint PreW31;
  970. cl_uint PreW32;
  971. /* For diakgcn */
  972. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  973. cl_uint zeroA, zeroB;
  974. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  975. #ifdef USE_SCRYPT
  976. struct work *work;
  977. #endif
  978. } dev_blk_ctx;
  979. #else
  980. typedef struct {
  981. uint32_t nonce;
  982. } dev_blk_ctx;
  983. #endif
  984. struct curl_ent {
  985. CURL *curl;
  986. struct curl_ent *next;
  987. struct timeval tv;
  988. };
  989. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  990. * equal disabled */
  991. enum pool_enable {
  992. POOL_DISABLED,
  993. POOL_ENABLED,
  994. POOL_REJECTING,
  995. };
  996. enum pool_protocol {
  997. PLP_NONE,
  998. PLP_GETWORK,
  999. PLP_GETBLOCKTEMPLATE,
  1000. };
  1001. struct stratum_work {
  1002. char *job_id;
  1003. bool clean;
  1004. bytes_t coinbase;
  1005. size_t nonce2_offset;
  1006. int merkles;
  1007. bytes_t merkle_bin;
  1008. uint8_t header1[36];
  1009. uint8_t diffbits[4];
  1010. uint32_t ntime;
  1011. struct timeval tv_received;
  1012. double diff;
  1013. bool transparency_probed;
  1014. struct timeval tv_transparency;
  1015. bool opaque;
  1016. cglock_t *data_lock_p;
  1017. };
  1018. #define RBUFSIZE 8192
  1019. #define RECVSIZE (RBUFSIZE - 4)
  1020. struct pool {
  1021. int pool_no;
  1022. int prio;
  1023. int accepted, rejected;
  1024. int seq_rejects;
  1025. int seq_getfails;
  1026. int solved;
  1027. int diff1;
  1028. char diff[8];
  1029. int quota;
  1030. int quota_gcd;
  1031. int quota_used;
  1032. double diff_accepted;
  1033. double diff_rejected;
  1034. double diff_stale;
  1035. bool submit_fail;
  1036. bool idle;
  1037. bool lagging;
  1038. bool probed;
  1039. int force_rollntime;
  1040. enum pool_enable enabled;
  1041. bool submit_old;
  1042. bool removed;
  1043. bool lp_started;
  1044. unsigned char work_restart_id;
  1045. uint32_t block_id;
  1046. enum pool_protocol proto;
  1047. char *hdr_path;
  1048. char *lp_url;
  1049. char *lp_id;
  1050. enum pool_protocol lp_proto;
  1051. curl_socket_t lp_socket;
  1052. unsigned int getwork_requested;
  1053. unsigned int stale_shares;
  1054. unsigned int discarded_work;
  1055. unsigned int getfail_occasions;
  1056. unsigned int remotefail_occasions;
  1057. struct timeval tv_idle;
  1058. double utility;
  1059. int last_shares, shares;
  1060. char *rpc_url;
  1061. char *rpc_userpass;
  1062. char *rpc_user, *rpc_pass;
  1063. char *rpc_proxy;
  1064. pthread_mutex_t pool_lock;
  1065. cglock_t data_lock;
  1066. struct thread_q *submit_q;
  1067. struct thread_q *getwork_q;
  1068. pthread_t longpoll_thread;
  1069. pthread_t test_thread;
  1070. bool testing;
  1071. int curls;
  1072. pthread_cond_t cr_cond;
  1073. struct curl_ent *curllist;
  1074. struct submit_work_state *sws_waiting_on_curl;
  1075. time_t last_work_time;
  1076. struct timeval tv_last_work_time;
  1077. time_t last_share_time;
  1078. double last_share_diff;
  1079. uint64_t best_diff;
  1080. struct cgminer_stats cgminer_stats;
  1081. struct cgminer_pool_stats cgminer_pool_stats;
  1082. /* Stratum variables */
  1083. char *stratum_url;
  1084. char *stratum_port;
  1085. CURL *stratum_curl;
  1086. SOCKETTYPE sock;
  1087. char *sockbuf;
  1088. size_t sockbuf_size;
  1089. char *sockaddr_url; /* stripped url used for sockaddr */
  1090. char *nonce1;
  1091. size_t n1_len;
  1092. uint32_t nonce2;
  1093. int nonce2sz;
  1094. #ifdef WORDS_BIGENDIAN
  1095. int nonce2off;
  1096. #endif
  1097. int n2size;
  1098. char *sessionid;
  1099. bool has_stratum;
  1100. bool stratum_active;
  1101. bool stratum_init;
  1102. bool stratum_notify;
  1103. struct stratum_work swork;
  1104. pthread_t stratum_thread;
  1105. pthread_mutex_t stratum_lock;
  1106. char *admin_msg;
  1107. pthread_mutex_t last_work_lock;
  1108. struct work *last_work_copy;
  1109. };
  1110. #define GETWORK_MODE_TESTPOOL 'T'
  1111. #define GETWORK_MODE_POOL 'P'
  1112. #define GETWORK_MODE_LP 'L'
  1113. #define GETWORK_MODE_BENCHMARK 'B'
  1114. #define GETWORK_MODE_STRATUM 'S'
  1115. #define GETWORK_MODE_GBT 'G'
  1116. struct work {
  1117. unsigned char data[128];
  1118. unsigned char midstate[32];
  1119. unsigned char target[32];
  1120. unsigned char hash[32];
  1121. uint64_t share_diff;
  1122. int rolls;
  1123. dev_blk_ctx blk;
  1124. struct thr_info *thr;
  1125. int thr_id;
  1126. struct pool *pool;
  1127. struct timeval tv_staged;
  1128. bool mined;
  1129. bool clone;
  1130. bool cloned;
  1131. int rolltime;
  1132. bool longpoll;
  1133. bool stale;
  1134. bool mandatory;
  1135. bool block;
  1136. bool queued;
  1137. bool stratum;
  1138. char *job_id;
  1139. bytes_t nonce2;
  1140. double sdiff;
  1141. char *nonce1;
  1142. unsigned char work_restart_id;
  1143. int id;
  1144. int device_id;
  1145. UT_hash_handle hh;
  1146. double work_difficulty;
  1147. // Allow devices to identify work if multiple sub-devices
  1148. // DEPRECATED: New code should be using multiple processors instead
  1149. unsigned char subid;
  1150. blktemplate_t *tmpl;
  1151. int *tmpl_refcount;
  1152. unsigned int dataid;
  1153. bool do_foreign_submit;
  1154. struct timeval tv_getwork;
  1155. time_t ts_getwork;
  1156. struct timeval tv_getwork_reply;
  1157. struct timeval tv_cloned;
  1158. struct timeval tv_work_start;
  1159. struct timeval tv_work_found;
  1160. char getwork_mode;
  1161. /* Used to queue shares in submit_waiting */
  1162. struct work *prev;
  1163. struct work *next;
  1164. };
  1165. extern void get_datestamp(char *, size_t, time_t);
  1166. #define get_now_datestamp(buf, bufsz) get_datestamp(buf, bufsz, INVALID_TIMESTAMP)
  1167. extern void stratum_work_cpy(struct stratum_work *dst, const struct stratum_work *src);
  1168. extern void stratum_work_clean(struct stratum_work *);
  1169. extern void gen_stratum_work2(struct work *, struct stratum_work *, const char *nonce1);
  1170. extern void inc_hw_errors2(struct thr_info *thr, const struct work *work, const uint32_t *bad_nonce_p);
  1171. extern void inc_hw_errors(struct thr_info *, const struct work *, const uint32_t bad_nonce);
  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 struct work *get_queued(struct cgpu_info *cgpu);
  1183. extern struct work *__find_work_bymidstate(struct work *que, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1184. extern struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1185. extern struct work *clone_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1186. extern void work_completed(struct cgpu_info *cgpu, struct work *work);
  1187. extern struct work *take_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1188. extern bool abandon_work(struct work *, struct timeval *work_runtime, uint64_t hashes);
  1189. extern void hash_queued_work(struct thr_info *mythr);
  1190. extern void get_statline3(char *buf, size_t bufsz, struct cgpu_info *, bool for_curses, bool opt_show_procs);
  1191. extern void tailsprintf(char *buf, size_t bufsz, const char *fmt, ...) FORMAT_SYNTAX_CHECK(printf, 3, 4);
  1192. extern void _wlog(const char *str);
  1193. extern void _wlogprint(const char *str);
  1194. extern int curses_int(const char *query);
  1195. extern char *curses_input(const char *query);
  1196. extern bool drv_ready(struct cgpu_info *);
  1197. extern double stats_elapsed(struct cgminer_stats *);
  1198. #define cgpu_runtime(cgpu) stats_elapsed(&((cgpu)->cgminer_stats))
  1199. extern double cgpu_utility(struct cgpu_info *);
  1200. extern void kill_work(void);
  1201. extern int prioritize_pools(char *param, int *pid);
  1202. extern void validate_pool_priorities(void);
  1203. extern void switch_pools(struct pool *selected);
  1204. extern void remove_pool(struct pool *pool);
  1205. extern void write_config(FILE *fcfg);
  1206. extern void zero_bestshare(void);
  1207. extern void zero_stats(void);
  1208. extern void default_save_file(char *filename);
  1209. extern bool _log_curses_only(int prio, const char *datetime, const char *str);
  1210. extern void clear_logwin(void);
  1211. extern void logwin_update(void);
  1212. extern bool pool_tclear(struct pool *pool, bool *var);
  1213. extern struct thread_q *tq_new(void);
  1214. extern void tq_free(struct thread_q *tq);
  1215. extern bool tq_push(struct thread_q *tq, void *data);
  1216. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  1217. extern void tq_freeze(struct thread_q *tq);
  1218. extern void tq_thaw(struct thread_q *tq);
  1219. extern bool successful_connect;
  1220. extern void adl(void);
  1221. extern void clean_work(struct work *work);
  1222. extern void free_work(struct work *work);
  1223. extern void __copy_work(struct work *work, const struct work *base_work);
  1224. extern struct work *copy_work(const struct work *base_work);
  1225. extern struct thr_info *get_thread(int thr_id);
  1226. extern struct cgpu_info *get_devices(int id);
  1227. extern int create_new_cgpus(void (*addfunc)(void*), void *arg);
  1228. extern int scan_serial(const char *);
  1229. enum api_data_type {
  1230. API_ESCAPE,
  1231. API_STRING,
  1232. API_CONST,
  1233. API_INT,
  1234. API_UINT,
  1235. API_UINT32,
  1236. API_UINT64,
  1237. API_DOUBLE,
  1238. API_ELAPSED,
  1239. API_BOOL,
  1240. API_TIMEVAL,
  1241. API_TIME,
  1242. API_MHS,
  1243. API_MHTOTAL,
  1244. API_TEMP,
  1245. API_UTILITY,
  1246. API_FREQ,
  1247. API_VOLTS,
  1248. API_HS,
  1249. API_DIFF,
  1250. API_JSON,
  1251. API_PERCENT
  1252. };
  1253. struct api_data {
  1254. enum api_data_type type;
  1255. char *name;
  1256. void *data;
  1257. bool data_was_malloc;
  1258. struct api_data *prev;
  1259. struct api_data *next;
  1260. };
  1261. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  1262. extern struct api_data *api_add_string(struct api_data *root, char *name, const char *data, bool copy_data);
  1263. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  1264. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  1265. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  1266. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  1267. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  1268. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  1269. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  1270. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  1271. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  1272. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  1273. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  1274. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  1275. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  1276. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  1277. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  1278. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  1279. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  1280. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  1281. extern struct api_data *api_add_json(struct api_data *root, char *name, json_t *data, bool copy_data);
  1282. #endif /* __MINER_H__ */