miner.h 38 KB

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