miner.h 40 KB

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