miner.h 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  1. #ifndef __MINER_H__
  2. #define __MINER_H__
  3. #include "config.h"
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <sys/time.h>
  7. #include <pthread.h>
  8. #include <jansson.h>
  9. #ifdef HAVE_LIBCURL
  10. #include <curl/curl.h>
  11. #else
  12. typedef char CURL;
  13. extern char *curly;
  14. #define curl_easy_init(curl) (curly)
  15. #define curl_easy_cleanup(curl) {}
  16. #define curl_global_cleanup() {}
  17. #define CURL_GLOBAL_ALL 0
  18. #define curl_global_init(X) (0)
  19. #endif
  20. #include <sched.h>
  21. #include "elist.h"
  22. #include "uthash.h"
  23. #include "logging.h"
  24. #include "util.h"
  25. #include <sys/types.h>
  26. #ifndef WIN32
  27. # include <sys/socket.h>
  28. # include <netdb.h>
  29. #endif
  30. #ifdef USE_USBUTILS
  31. #include <semaphore.h>
  32. #endif
  33. #ifdef HAVE_OPENCL
  34. #ifdef __APPLE_CC__
  35. #include <OpenCL/opencl.h>
  36. #else
  37. #include <CL/cl.h>
  38. #endif
  39. #endif /* HAVE_OPENCL */
  40. #ifdef STDC_HEADERS
  41. # include <stdlib.h>
  42. # include <stddef.h>
  43. #else
  44. # ifdef HAVE_STDLIB_H
  45. # include <stdlib.h>
  46. # endif
  47. #endif
  48. #ifdef HAVE_ALLOCA_H
  49. # include <alloca.h>
  50. #elif defined __GNUC__
  51. # ifndef WIN32
  52. # define alloca __builtin_alloca
  53. # else
  54. # include <malloc.h>
  55. # endif
  56. #elif defined _AIX
  57. # define alloca __alloca
  58. #elif defined _MSC_VER
  59. # include <malloc.h>
  60. # define alloca _alloca
  61. #else
  62. # ifndef HAVE_ALLOCA
  63. # ifdef __cplusplus
  64. extern "C"
  65. # endif
  66. void *alloca (size_t);
  67. # endif
  68. #endif
  69. #ifdef __MINGW32__
  70. #include <windows.h>
  71. #include <io.h>
  72. static inline int fsync (int fd)
  73. {
  74. return (FlushFileBuffers ((HANDLE) _get_osfhandle (fd))) ? 0 : -1;
  75. }
  76. #ifndef EWOULDBLOCK
  77. # define EWOULDBLOCK EAGAIN
  78. #endif
  79. #ifndef MSG_DONTWAIT
  80. # define MSG_DONTWAIT 0x1000000
  81. #endif
  82. #endif /* __MINGW32__ */
  83. #if defined (__linux)
  84. #ifndef LINUX
  85. #define LINUX
  86. #endif
  87. #endif
  88. #ifdef WIN32
  89. #ifndef timersub
  90. #define timersub(a, b, result) \
  91. do { \
  92. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  93. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  94. if ((result)->tv_usec < 0) { \
  95. --(result)->tv_sec; \
  96. (result)->tv_usec += 1000000; \
  97. } \
  98. } while (0)
  99. #endif
  100. #ifndef timeradd
  101. # define timeradd(a, b, result) \
  102. do { \
  103. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  104. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  105. if ((result)->tv_usec >= 1000000) \
  106. { \
  107. ++(result)->tv_sec; \
  108. (result)->tv_usec -= 1000000; \
  109. } \
  110. } while (0)
  111. #endif
  112. #endif
  113. #ifdef HAVE_ADL
  114. #include "ADL_SDK/adl_sdk.h"
  115. #endif
  116. #ifdef USE_USBUTILS
  117. #include <libusb.h>
  118. #endif
  119. #ifdef USE_USBUTILS
  120. #include "usbutils.h"
  121. #endif
  122. #if (!defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) \
  123. || (defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)))
  124. #ifndef bswap_16
  125. #define bswap_16 __builtin_bswap16
  126. #define bswap_32 __builtin_bswap32
  127. #define bswap_64 __builtin_bswap64
  128. #endif
  129. #else
  130. #if HAVE_BYTESWAP_H
  131. #include <byteswap.h>
  132. #elif defined(USE_SYS_ENDIAN_H)
  133. #include <sys/endian.h>
  134. #elif defined(__APPLE__)
  135. #include <libkern/OSByteOrder.h>
  136. #define bswap_16 OSSwapInt16
  137. #define bswap_32 OSSwapInt32
  138. #define bswap_64 OSSwapInt64
  139. #else
  140. #define bswap_16(value) \
  141. ((((value) & 0xff) << 8) | ((value) >> 8))
  142. #define bswap_32(value) \
  143. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  144. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  145. #define bswap_64(value) \
  146. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  147. << 32) | \
  148. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  149. #endif
  150. #endif /* !defined(__GLXBYTEORDER_H__) */
  151. /* This assumes htobe32 is a macro in endian.h, and if it doesn't exist, then
  152. * htobe64 also won't exist */
  153. #ifndef htobe32
  154. # if __BYTE_ORDER == __LITTLE_ENDIAN
  155. # define htole16(x) (x)
  156. # define htole32(x) (x)
  157. # define le32toh(x) (x)
  158. # define be32toh(x) bswap_32(x)
  159. # define be64toh(x) bswap_64(x)
  160. # define htobe32(x) bswap_32(x)
  161. # define htobe64(x) bswap_64(x)
  162. # elif __BYTE_ORDER == __BIG_ENDIAN
  163. # define htole16(x) bswap_16(x)
  164. # define htole32(x) bswap_32(x)
  165. # define le32toh(x) bswap_32(x)
  166. # define be32toh(x) (x)
  167. # define be64toh(x) (x)
  168. # define htobe32(x) (x)
  169. # define htobe64(x) (x)
  170. #else
  171. #error UNKNOWN BYTE ORDER
  172. #endif
  173. #endif
  174. #undef unlikely
  175. #undef likely
  176. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  177. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  178. #define likely(expr) (__builtin_expect(!!(expr), 1))
  179. #else
  180. #define unlikely(expr) (expr)
  181. #define likely(expr) (expr)
  182. #endif
  183. #define __maybe_unused __attribute__((unused))
  184. #define uninitialised_var(x) x = x
  185. #if defined(__i386__)
  186. #define WANT_CRYPTOPP_ASM32
  187. #endif
  188. #ifndef ARRAY_SIZE
  189. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  190. #endif
  191. #ifdef MIPSEB
  192. #ifndef roundl
  193. #define roundl(x) (long double)((long long)((x==0)?0.0:((x)+((x)>0)?0.5:-0.5)))
  194. #endif
  195. #endif
  196. /* No semtimedop on apple so ignore timeout till we implement one */
  197. #ifdef __APPLE__
  198. #define semtimedop(SEM, SOPS, VAL, TIMEOUT) semop(SEM, SOPS, VAL)
  199. #endif
  200. #ifndef MIN
  201. #define MIN(x, y) ((x) > (y) ? (y) : (x))
  202. #endif
  203. #ifndef MAX
  204. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  205. #endif
  206. /* Put avalon last to make it the last device it tries to detect to prevent it
  207. * trying to claim same chip but different devices. Adding a device here will
  208. * update all macros in the code that use the *_PARSE_COMMANDS macros for each
  209. * listed driver. */
  210. #define FPGA_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
  211. DRIVER_ADD_COMMAND(bitforce) \
  212. DRIVER_ADD_COMMAND(icarus) \
  213. DRIVER_ADD_COMMAND(modminer)
  214. #define ASIC_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
  215. DRIVER_ADD_COMMAND(bflsc) \
  216. DRIVER_ADD_COMMAND(bitfury) \
  217. DRIVER_ADD_COMMAND(hashfast) \
  218. DRIVER_ADD_COMMAND(klondike) \
  219. DRIVER_ADD_COMMAND(knc) \
  220. DRIVER_ADD_COMMAND(avalon)
  221. #define DRIVER_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
  222. DRIVER_ADD_COMMAND(opencl) \
  223. FPGA_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
  224. ASIC_PARSE_COMMANDS(DRIVER_ADD_COMMAND)
  225. #define DRIVER_ENUM(X) DRIVER_##X,
  226. #define DRIVER_PROTOTYPE(X) struct device_drv X##_drv;
  227. /* Create drv_driver enum from DRIVER_PARSE_COMMANDS macro */
  228. enum drv_driver {
  229. DRIVER_PARSE_COMMANDS(DRIVER_ENUM)
  230. DRIVER_MAX
  231. };
  232. /* Use DRIVER_PARSE_COMMANDS to generate extern device_drv prototypes */
  233. DRIVER_PARSE_COMMANDS(DRIVER_PROTOTYPE)
  234. enum alive {
  235. LIFE_WELL,
  236. LIFE_SICK,
  237. LIFE_DEAD,
  238. LIFE_NOSTART,
  239. LIFE_INIT,
  240. };
  241. enum pool_strategy {
  242. POOL_FAILOVER,
  243. POOL_ROUNDROBIN,
  244. POOL_ROTATE,
  245. POOL_LOADBALANCE,
  246. POOL_BALANCE,
  247. };
  248. #define TOP_STRATEGY (POOL_BALANCE)
  249. struct strategies {
  250. const char *s;
  251. };
  252. struct cgpu_info;
  253. #ifdef HAVE_ADL
  254. struct gpu_adl {
  255. ADLTemperature lpTemperature;
  256. int iAdapterIndex;
  257. int lpAdapterID;
  258. int iBusNumber;
  259. char strAdapterName[256];
  260. ADLPMActivity lpActivity;
  261. ADLODParameters lpOdParameters;
  262. ADLODPerformanceLevels *DefPerfLev;
  263. ADLFanSpeedInfo lpFanSpeedInfo;
  264. ADLFanSpeedValue lpFanSpeedValue;
  265. ADLFanSpeedValue DefFanSpeedValue;
  266. int iEngineClock;
  267. int iMemoryClock;
  268. int iVddc;
  269. int iPercentage;
  270. bool autofan;
  271. bool autoengine;
  272. bool managed; /* Were the values ever changed on this card */
  273. int lastengine;
  274. int lasttemp;
  275. int targetfan;
  276. int targettemp;
  277. int overtemp;
  278. int minspeed;
  279. int maxspeed;
  280. int gpu;
  281. bool has_fanspeed;
  282. struct gpu_adl *twin;
  283. };
  284. #endif
  285. extern void blank_get_statline_before(char *buf, size_t bufsiz, struct cgpu_info __maybe_unused *cgpu);
  286. struct api_data;
  287. struct thr_info;
  288. struct work;
  289. struct device_drv {
  290. enum drv_driver drv_id;
  291. char *dname;
  292. char *name;
  293. // DRV-global functions
  294. void (*drv_detect)(bool);
  295. // Device-specific functions
  296. void (*reinit_device)(struct cgpu_info *);
  297. void (*get_statline_before)(char *, size_t, struct cgpu_info *);
  298. void (*get_statline)(char *, size_t, struct cgpu_info *);
  299. struct api_data *(*get_api_stats)(struct cgpu_info *);
  300. bool (*get_stats)(struct cgpu_info *);
  301. void (*identify_device)(struct cgpu_info *); // e.g. to flash a led
  302. char *(*set_device)(struct cgpu_info *, char *option, char *setting, char *replybuf);
  303. // Thread-specific functions
  304. bool (*thread_prepare)(struct thr_info *);
  305. uint64_t (*can_limit_work)(struct thr_info *);
  306. bool (*thread_init)(struct thr_info *);
  307. bool (*prepare_work)(struct thr_info *, struct work *);
  308. /* Which hash work loop this driver uses. */
  309. void (*hash_work)(struct thr_info *);
  310. /* Two variants depending on whether the device divides work up into
  311. * small pieces or works with whole work items and may or may not have
  312. * a queue of its own. */
  313. int64_t (*scanhash)(struct thr_info *, struct work *, int64_t);
  314. int64_t (*scanwork)(struct thr_info *);
  315. /* Used to extract work from the hash table of queued work and tell
  316. * the main loop that it should not add any further work to the table.
  317. */
  318. bool (*queue_full)(struct cgpu_info *);
  319. /* Tell the driver of a block change */
  320. void (*flush_work)(struct cgpu_info *);
  321. /* Tell the driver of an updated work template for eg. stratum */
  322. void (*update_work)(struct cgpu_info *);
  323. void (*hw_error)(struct thr_info *);
  324. void (*thread_shutdown)(struct thr_info *);
  325. void (*thread_enable)(struct thr_info *);
  326. // Does it need to be free()d?
  327. bool copy;
  328. /* Highest target diff the device supports */
  329. double max_diff;
  330. double working_diff;
  331. };
  332. extern struct device_drv *copy_drv(struct device_drv*);
  333. enum dev_enable {
  334. DEV_ENABLED,
  335. DEV_DISABLED,
  336. DEV_RECOVER,
  337. };
  338. enum cl_kernels {
  339. KL_NONE,
  340. KL_POCLBM,
  341. KL_PHATK,
  342. KL_DIAKGCN,
  343. KL_DIABLO,
  344. KL_SCRYPT,
  345. };
  346. enum dev_reason {
  347. REASON_THREAD_FAIL_INIT,
  348. REASON_THREAD_ZERO_HASH,
  349. REASON_THREAD_FAIL_QUEUE,
  350. REASON_DEV_SICK_IDLE_60,
  351. REASON_DEV_DEAD_IDLE_600,
  352. REASON_DEV_NOSTART,
  353. REASON_DEV_OVER_HEAT,
  354. REASON_DEV_THERMAL_CUTOFF,
  355. REASON_DEV_COMMS_ERROR,
  356. REASON_DEV_THROTTLE,
  357. };
  358. #define REASON_NONE "None"
  359. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  360. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  361. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  362. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  363. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  364. #define REASON_DEV_NOSTART_STR "Device failed to start"
  365. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  366. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  367. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  368. #define REASON_DEV_THROTTLE_STR "Device throttle"
  369. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  370. #define MIN_SEC_UNSET 99999999
  371. struct cgminer_stats {
  372. uint32_t getwork_calls;
  373. struct timeval getwork_wait;
  374. struct timeval getwork_wait_max;
  375. struct timeval getwork_wait_min;
  376. };
  377. // Just the actual network getworks to the pool
  378. struct cgminer_pool_stats {
  379. uint32_t getwork_calls;
  380. uint32_t getwork_attempts;
  381. struct timeval getwork_wait;
  382. struct timeval getwork_wait_max;
  383. struct timeval getwork_wait_min;
  384. double getwork_wait_rolling;
  385. bool hadrolltime;
  386. bool canroll;
  387. bool hadexpire;
  388. uint32_t rolltime;
  389. double min_diff;
  390. double max_diff;
  391. double last_diff;
  392. uint32_t min_diff_count;
  393. uint32_t max_diff_count;
  394. uint64_t times_sent;
  395. uint64_t bytes_sent;
  396. uint64_t net_bytes_sent;
  397. uint64_t times_received;
  398. uint64_t bytes_received;
  399. uint64_t net_bytes_received;
  400. };
  401. struct cgpu_info {
  402. int cgminer_id;
  403. struct device_drv *drv;
  404. int device_id;
  405. char *name;
  406. char *device_path;
  407. void *device_data;
  408. #ifdef USE_USBUTILS
  409. struct cg_usb_device *usbdev;
  410. #endif
  411. #ifdef USE_AVALON
  412. struct work **works;
  413. int work_array;
  414. int queued;
  415. int results;
  416. #endif
  417. #ifdef USE_USBUTILS
  418. struct cg_usb_info usbinfo;
  419. #endif
  420. #ifdef USE_MODMINER
  421. char fpgaid;
  422. unsigned char clock;
  423. pthread_mutex_t *modminer_mutex;
  424. #endif
  425. #ifdef USE_BITFORCE
  426. struct timeval work_start_tv;
  427. unsigned int wait_ms;
  428. unsigned int sleep_ms;
  429. double avg_wait_f;
  430. unsigned int avg_wait_d;
  431. uint32_t nonces;
  432. bool nonce_range;
  433. bool polling;
  434. bool flash_led;
  435. #endif /* USE_BITFORCE */
  436. #if defined(USE_BITFORCE) || defined(USE_BFLSC)
  437. pthread_mutex_t device_mutex;
  438. #endif /* USE_BITFORCE || USE_BFLSC */
  439. enum dev_enable deven;
  440. int accepted;
  441. int rejected;
  442. int hw_errors;
  443. double rolling;
  444. double total_mhashes;
  445. double utility;
  446. enum alive status;
  447. char init[40];
  448. struct timeval last_message_tv;
  449. int threads;
  450. struct thr_info **thr;
  451. int64_t max_hashes;
  452. const char *kname;
  453. #ifdef HAVE_OPENCL
  454. bool mapped;
  455. int virtual_gpu;
  456. int virtual_adl;
  457. int intensity;
  458. bool dynamic;
  459. cl_uint vwidth;
  460. size_t work_size;
  461. enum cl_kernels kernel;
  462. cl_ulong max_alloc;
  463. #ifdef USE_SCRYPT
  464. int opt_lg, lookup_gap;
  465. size_t opt_tc, thread_concurrency;
  466. size_t shaders;
  467. #endif
  468. struct timeval tv_gpustart;
  469. int intervals;
  470. #endif
  471. bool new_work;
  472. float temp;
  473. int cutofftemp;
  474. #ifdef HAVE_ADL
  475. bool has_adl;
  476. struct gpu_adl adl;
  477. int gpu_engine;
  478. int min_engine;
  479. int gpu_fan;
  480. int min_fan;
  481. int gpu_memclock;
  482. int gpu_memdiff;
  483. int gpu_powertune;
  484. float gpu_vddc;
  485. #endif
  486. int diff1;
  487. double diff_accepted;
  488. double diff_rejected;
  489. int last_share_pool;
  490. time_t last_share_pool_time;
  491. double last_share_diff;
  492. time_t last_device_valid_work;
  493. time_t device_last_well;
  494. time_t device_last_not_well;
  495. enum dev_reason device_not_well_reason;
  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 shutdown;
  512. struct timeval dev_start_tv;
  513. };
  514. extern bool add_cgpu(struct cgpu_info*);
  515. struct thread_q {
  516. struct list_head q;
  517. bool frozen;
  518. pthread_mutex_t mutex;
  519. pthread_cond_t cond;
  520. };
  521. struct thr_info {
  522. int id;
  523. int device_thread;
  524. bool primary_thread;
  525. pthread_t pth;
  526. cgsem_t sem;
  527. struct thread_q *q;
  528. struct cgpu_info *cgpu;
  529. void *cgpu_data;
  530. struct timeval last;
  531. struct timeval sick;
  532. bool pause;
  533. bool getwork;
  534. double rolling;
  535. bool work_restart;
  536. bool work_update;
  537. };
  538. struct string_elist {
  539. char *string;
  540. bool free_me;
  541. struct list_head list;
  542. };
  543. static inline void string_elist_add(const char *s, struct list_head *head)
  544. {
  545. struct string_elist *n;
  546. n = calloc(1, sizeof(*n));
  547. n->string = strdup(s);
  548. n->free_me = true;
  549. list_add_tail(&n->list, head);
  550. }
  551. static inline void string_elist_del(struct string_elist *item)
  552. {
  553. if (item->free_me)
  554. free(item->string);
  555. list_del(&item->list);
  556. }
  557. static inline uint32_t swab32(uint32_t v)
  558. {
  559. return bswap_32(v);
  560. }
  561. static inline void swap256(void *dest_p, const void *src_p)
  562. {
  563. uint32_t *dest = dest_p;
  564. const uint32_t *src = src_p;
  565. dest[0] = src[7];
  566. dest[1] = src[6];
  567. dest[2] = src[5];
  568. dest[3] = src[4];
  569. dest[4] = src[3];
  570. dest[5] = src[2];
  571. dest[6] = src[1];
  572. dest[7] = src[0];
  573. }
  574. static inline void swab256(void *dest_p, const void *src_p)
  575. {
  576. uint32_t *dest = dest_p;
  577. const uint32_t *src = src_p;
  578. dest[0] = swab32(src[7]);
  579. dest[1] = swab32(src[6]);
  580. dest[2] = swab32(src[5]);
  581. dest[3] = swab32(src[4]);
  582. dest[4] = swab32(src[3]);
  583. dest[5] = swab32(src[2]);
  584. dest[6] = swab32(src[1]);
  585. dest[7] = swab32(src[0]);
  586. }
  587. static inline void flip32(void *dest_p, const void *src_p)
  588. {
  589. uint32_t *dest = dest_p;
  590. const uint32_t *src = src_p;
  591. int i;
  592. for (i = 0; i < 8; i++)
  593. dest[i] = swab32(src[i]);
  594. }
  595. static inline void flip64(void *dest_p, const void *src_p)
  596. {
  597. uint32_t *dest = dest_p;
  598. const uint32_t *src = src_p;
  599. int i;
  600. for (i = 0; i < 16; i++)
  601. dest[i] = swab32(src[i]);
  602. }
  603. static inline void flip80(void *dest_p, const void *src_p)
  604. {
  605. uint32_t *dest = dest_p;
  606. const uint32_t *src = src_p;
  607. int i;
  608. for (i = 0; i < 20; i++)
  609. dest[i] = swab32(src[i]);
  610. }
  611. static inline void flip128(void *dest_p, const void *src_p)
  612. {
  613. uint32_t *dest = dest_p;
  614. const uint32_t *src = src_p;
  615. int i;
  616. for (i = 0; i < 32; i++)
  617. dest[i] = swab32(src[i]);
  618. }
  619. /* For flipping to the correct endianness if necessary */
  620. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  621. static inline void endian_flip32(void *dest_p, const void *src_p)
  622. {
  623. flip32(dest_p, src_p);
  624. }
  625. static inline void endian_flip128(void *dest_p, const void *src_p)
  626. {
  627. flip128(dest_p, src_p);
  628. }
  629. #else
  630. static inline void
  631. endian_flip32(void __maybe_unused *dest_p, const void __maybe_unused *src_p)
  632. {
  633. }
  634. static inline void
  635. endian_flip128(void __maybe_unused *dest_p, const void __maybe_unused *src_p)
  636. {
  637. }
  638. #endif
  639. extern void _quit(int status);
  640. /*
  641. * Set this to non-zero to enable lock tracking
  642. * Use the API lockstats command to see the locking status on stderr
  643. * i.e. in your log file if you 2> log.log - but not on the screen
  644. * API lockstats is privilidged but will always exist and will return
  645. * success if LOCK_TRACKING is enabled and warning if disabled
  646. * In production code, this should never be enabled since it will slow down all locking
  647. * So, e.g. use it to track down a deadlock - after a reproducable deadlock occurs
  648. * ... Of course if the API code itself deadlocks, it wont help :)
  649. */
  650. #define LOCK_TRACKING 0
  651. #if LOCK_TRACKING
  652. enum cglock_typ {
  653. CGLOCK_MUTEX,
  654. CGLOCK_RW,
  655. CGLOCK_UNKNOWN
  656. };
  657. extern uint64_t api_getlock(void *lock, const char *file, const char *func, const int line);
  658. extern void api_gotlock(uint64_t id, void *lock, const char *file, const char *func, const int line);
  659. extern uint64_t api_trylock(void *lock, const char *file, const char *func, const int line);
  660. extern void api_didlock(uint64_t id, int ret, void *lock, const char *file, const char *func, const int line);
  661. extern void api_gunlock(void *lock, const char *file, const char *func, const int line);
  662. extern void api_initlock(void *lock, enum cglock_typ typ, const char *file, const char *func, const int line);
  663. #define GETLOCK(_lock, _file, _func, _line) uint64_t _id1 = api_getlock((void *)(_lock), _file, _func, _line)
  664. #define GOTLOCK(_lock, _file, _func, _line) api_gotlock(_id1, (void *)(_lock), _file, _func, _line)
  665. #define TRYLOCK(_lock, _file, _func, _line) uint64_t _id2 = api_trylock((void *)(_lock), _file, _func, _line)
  666. #define DIDLOCK(_ret, _lock, _file, _func, _line) api_didlock(_id2, _ret, (void *)(_lock), _file, _func, _line)
  667. #define GUNLOCK(_lock, _file, _func, _line) api_gunlock((void *)(_lock), _file, _func, _line)
  668. #define INITLOCK(_lock, _typ, _file, _func, _line) api_initlock((void *)(_lock), _typ, _file, _func, _line)
  669. #else
  670. #define GETLOCK(_lock, _file, _func, _line)
  671. #define GOTLOCK(_lock, _file, _func, _line)
  672. #define TRYLOCK(_lock, _file, _func, _line)
  673. #define DIDLOCK(_ret, _lock, _file, _func, _line)
  674. #define GUNLOCK(_lock, _file, _func, _line)
  675. #define INITLOCK(_typ, _lock, _file, _func, _line)
  676. #endif
  677. #define mutex_lock(_lock) _mutex_lock(_lock, __FILE__, __func__, __LINE__)
  678. #define mutex_unlock_noyield(_lock) _mutex_unlock_noyield(_lock, __FILE__, __func__, __LINE__)
  679. #define mutex_unlock(_lock) _mutex_unlock(_lock, __FILE__, __func__, __LINE__)
  680. #define mutex_trylock(_lock) _mutex_trylock(_lock, __FILE__, __func__, __LINE__)
  681. #define wr_lock(_lock) _wr_lock(_lock, __FILE__, __func__, __LINE__)
  682. #define rd_lock(_lock) _rd_lock(_lock, __FILE__, __func__, __LINE__)
  683. #define rw_unlock(_lock) _rw_unlock(_lock, __FILE__, __func__, __LINE__)
  684. #define rd_unlock_noyield(_lock) _rd_unlock_noyield(_lock, __FILE__, __func__, __LINE__)
  685. #define wr_unlock_noyield(_lock) _wr_unlock_noyield(_lock, __FILE__, __func__, __LINE__)
  686. #define rd_unlock(_lock) _rd_unlock(_lock, __FILE__, __func__, __LINE__)
  687. #define wr_unlock(_lock) _wr_unlock(_lock, __FILE__, __func__, __LINE__)
  688. #define mutex_init(_lock) _mutex_init(_lock, __FILE__, __func__, __LINE__)
  689. #define rwlock_init(_lock) _rwlock_init(_lock, __FILE__, __func__, __LINE__)
  690. #define cglock_init(_lock) _cglock_init(_lock, __FILE__, __func__, __LINE__)
  691. #define cg_rlock(_lock) _cg_rlock(_lock, __FILE__, __func__, __LINE__)
  692. #define cg_ilock(_lock) _cg_ilock(_lock, __FILE__, __func__, __LINE__)
  693. #define cg_ulock(_lock) _cg_ulock(_lock, __FILE__, __func__, __LINE__)
  694. #define cg_wlock(_lock) _cg_wlock(_lock, __FILE__, __func__, __LINE__)
  695. #define cg_dwlock(_lock) _cg_dwlock(_lock, __FILE__, __func__, __LINE__)
  696. #define cg_dwilock(_lock) _cg_dwilock(_lock, __FILE__, __func__, __LINE__)
  697. #define cg_dlock(_lock) _cg_dlock(_lock, __FILE__, __func__, __LINE__)
  698. #define cg_runlock(_lock) _cg_runlock(_lock, __FILE__, __func__, __LINE__)
  699. #define cg_ruwlock(_lock) _cg_ruwlock(_lock, __FILE__, __func__, __LINE__)
  700. #define cg_wunlock(_lock) _cg_wunlock(_lock, __FILE__, __func__, __LINE__)
  701. static inline void _mutex_lock(pthread_mutex_t *lock, const char *file, const char *func, const int line)
  702. {
  703. GETLOCK(lock, file, func, line);
  704. if (unlikely(pthread_mutex_lock(lock)))
  705. quitfrom(1, file, func, line, "WTF MUTEX ERROR ON LOCK! errno=%d", errno);
  706. GOTLOCK(lock, file, func, line);
  707. }
  708. static inline void _mutex_unlock_noyield(pthread_mutex_t *lock, const char *file, const char *func, const int line)
  709. {
  710. if (unlikely(pthread_mutex_unlock(lock)))
  711. quitfrom(1, file, func, line, "WTF MUTEX ERROR ON UNLOCK! errno=%d", errno);
  712. GUNLOCK(lock, file, func, line);
  713. }
  714. static inline void _mutex_unlock(pthread_mutex_t *lock, const char *file, const char *func, const int line)
  715. {
  716. _mutex_unlock_noyield(lock, file, func, line);
  717. sched_yield();
  718. }
  719. static inline int _mutex_trylock(pthread_mutex_t *lock, __maybe_unused const char *file, __maybe_unused const char *func, __maybe_unused const int line)
  720. {
  721. TRYLOCK(lock, file, func, line);
  722. int ret = pthread_mutex_trylock(lock);
  723. DIDLOCK(ret, lock, file, func, line);
  724. return ret;
  725. }
  726. static inline void _wr_lock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  727. {
  728. GETLOCK(lock, file, func, line);
  729. if (unlikely(pthread_rwlock_wrlock(lock)))
  730. quitfrom(1, file, func, line, "WTF WRLOCK ERROR ON LOCK! errno=%d", errno);
  731. GOTLOCK(lock, file, func, line);
  732. }
  733. static inline void _rd_lock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  734. {
  735. GETLOCK(lock, file, func, line);
  736. if (unlikely(pthread_rwlock_rdlock(lock)))
  737. quitfrom(1, file, func, line, "WTF RDLOCK ERROR ON LOCK! errno=%d", errno);
  738. GOTLOCK(lock, file, func, line);
  739. }
  740. static inline void _rw_unlock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  741. {
  742. if (unlikely(pthread_rwlock_unlock(lock)))
  743. quitfrom(1, file, func, line, "WTF RWLOCK ERROR ON UNLOCK! errno=%d", errno);
  744. GUNLOCK(lock, file, func, line);
  745. }
  746. static inline void _rd_unlock_noyield(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  747. {
  748. _rw_unlock(lock, file, func, line);
  749. }
  750. static inline void _wr_unlock_noyield(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  751. {
  752. _rw_unlock(lock, file, func, line);
  753. }
  754. static inline void _rd_unlock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  755. {
  756. _rw_unlock(lock, file, func, line);
  757. sched_yield();
  758. }
  759. static inline void _wr_unlock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  760. {
  761. _rw_unlock(lock, file, func, line);
  762. sched_yield();
  763. }
  764. static inline void _mutex_init(pthread_mutex_t *lock, const char *file, const char *func, const int line)
  765. {
  766. if (unlikely(pthread_mutex_init(lock, NULL)))
  767. quitfrom(1, file, func, line, "Failed to pthread_mutex_init errno=%d", errno);
  768. INITLOCK(lock, CGLOCK_MUTEX, file, func, line);
  769. }
  770. static inline void mutex_destroy(pthread_mutex_t *lock)
  771. {
  772. /* Ignore return code. This only invalidates the mutex on linux but
  773. * releases resources on windows. */
  774. pthread_mutex_destroy(lock);
  775. }
  776. static inline void _rwlock_init(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  777. {
  778. if (unlikely(pthread_rwlock_init(lock, NULL)))
  779. quitfrom(1, file, func, line, "Failed to pthread_rwlock_init errno=%d", errno);
  780. INITLOCK(lock, CGLOCK_RW, file, func, line);
  781. }
  782. static inline void rwlock_destroy(pthread_rwlock_t *lock)
  783. {
  784. pthread_rwlock_destroy(lock);
  785. }
  786. static inline void _cglock_init(cglock_t *lock, const char *file, const char *func, const int line)
  787. {
  788. _mutex_init(&lock->mutex, file, func, line);
  789. _rwlock_init(&lock->rwlock, file, func, line);
  790. }
  791. static inline void cglock_destroy(cglock_t *lock)
  792. {
  793. rwlock_destroy(&lock->rwlock);
  794. mutex_destroy(&lock->mutex);
  795. }
  796. /* Read lock variant of cglock. Cannot be promoted. */
  797. static inline void _cg_rlock(cglock_t *lock, const char *file, const char *func, const int line)
  798. {
  799. _mutex_lock(&lock->mutex, file, func, line);
  800. _rd_lock(&lock->rwlock, file, func, line);
  801. _mutex_unlock_noyield(&lock->mutex, file, func, line);
  802. }
  803. /* Intermediate variant of cglock - behaves as a read lock but can be promoted
  804. * to a write lock or demoted to read lock. */
  805. static inline void _cg_ilock(cglock_t *lock, const char *file, const char *func, const int line)
  806. {
  807. _mutex_lock(&lock->mutex, file, func, line);
  808. }
  809. /* Upgrade intermediate variant to a write lock */
  810. static inline void _cg_ulock(cglock_t *lock, const char *file, const char *func, const int line)
  811. {
  812. _wr_lock(&lock->rwlock, file, func, line);
  813. }
  814. /* Write lock variant of cglock */
  815. static inline void _cg_wlock(cglock_t *lock, const char *file, const char *func, const int line)
  816. {
  817. _mutex_lock(&lock->mutex, file, func, line);
  818. _wr_lock(&lock->rwlock, file, func, line);
  819. }
  820. /* Downgrade write variant to a read lock */
  821. static inline void _cg_dwlock(cglock_t *lock, const char *file, const char *func, const int line)
  822. {
  823. _wr_unlock_noyield(&lock->rwlock, file, func, line);
  824. _rd_lock(&lock->rwlock, file, func, line);
  825. _mutex_unlock_noyield(&lock->mutex, file, func, line);
  826. }
  827. /* Demote a write variant to an intermediate variant */
  828. static inline void _cg_dwilock(cglock_t *lock, const char *file, const char *func, const int line)
  829. {
  830. _wr_unlock(&lock->rwlock, file, func, line);
  831. }
  832. /* Downgrade intermediate variant to a read lock */
  833. static inline void _cg_dlock(cglock_t *lock, const char *file, const char *func, const int line)
  834. {
  835. _rd_lock(&lock->rwlock, file, func, line);
  836. _mutex_unlock_noyield(&lock->mutex, file, func, line);
  837. }
  838. static inline void _cg_runlock(cglock_t *lock, const char *file, const char *func, const int line)
  839. {
  840. _rd_unlock(&lock->rwlock, file, func, line);
  841. }
  842. /* This drops the read lock and grabs a write lock. It does NOT protect data
  843. * between the two locks! */
  844. static inline void _cg_ruwlock(cglock_t *lock, const char *file, const char *func, const int line)
  845. {
  846. _rd_unlock_noyield(&lock->rwlock, file, func, line);
  847. _cg_wlock(lock, file, func, line);
  848. }
  849. static inline void _cg_wunlock(cglock_t *lock, const char *file, const char *func, const int line)
  850. {
  851. _wr_unlock_noyield(&lock->rwlock, file, func, line);
  852. _mutex_unlock(&lock->mutex, file, func, line);
  853. }
  854. struct pool;
  855. #define API_MCAST_CODE "FTW"
  856. #define API_MCAST_ADDR "224.0.0.75"
  857. extern bool opt_work_update;
  858. extern bool opt_protocol;
  859. extern bool have_longpoll;
  860. extern char *opt_kernel_path;
  861. extern char *opt_socks_proxy;
  862. extern char *cgminer_path;
  863. extern bool opt_fail_only;
  864. extern bool opt_autofan;
  865. extern bool opt_autoengine;
  866. extern bool use_curses;
  867. extern char *opt_api_allow;
  868. extern bool opt_api_mcast;
  869. extern char *opt_api_mcast_addr;
  870. extern char *opt_api_mcast_code;
  871. extern char *opt_api_mcast_des;
  872. extern int opt_api_mcast_port;
  873. extern char *opt_api_groups;
  874. extern char *opt_api_description;
  875. extern int opt_api_port;
  876. extern bool opt_api_listen;
  877. extern bool opt_api_network;
  878. extern bool opt_delaynet;
  879. extern bool opt_restart;
  880. extern bool opt_nogpu;
  881. extern char *opt_icarus_options;
  882. extern char *opt_icarus_timing;
  883. extern bool opt_worktime;
  884. #ifdef USE_AVALON
  885. extern char *opt_avalon_options;
  886. extern char *opt_bitburner_fury_options;
  887. #endif
  888. #ifdef USE_KLONDIKE
  889. extern char *opt_klondike_options;
  890. #endif
  891. #ifdef USE_USBUTILS
  892. extern char *opt_usb_select;
  893. extern int opt_usbdump;
  894. extern bool opt_usb_list_all;
  895. extern cgsem_t usb_resource_sem;
  896. #endif
  897. #ifdef USE_BITFORCE
  898. extern bool opt_bfl_noncerange;
  899. #endif
  900. extern int swork_id;
  901. #if LOCK_TRACKING
  902. extern pthread_mutex_t lockstat_lock;
  903. #endif
  904. extern pthread_rwlock_t netacc_lock;
  905. extern const uint32_t sha256_init_state[];
  906. #ifdef HAVE_LIBCURL
  907. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  908. const char *rpc_req, bool, bool, int *,
  909. struct pool *pool, bool);
  910. #endif
  911. extern const char *proxytype(proxytypes_t proxytype);
  912. extern char *get_proxy(char *url, struct pool *pool);
  913. extern void __bin2hex(char *s, const unsigned char *p, size_t len);
  914. extern char *bin2hex(const unsigned char *p, size_t len);
  915. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  916. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  917. unsigned char *pdata,
  918. unsigned char *phash1, unsigned char *phash,
  919. const unsigned char *ptarget,
  920. uint32_t max_nonce,
  921. uint32_t *last_nonce,
  922. uint32_t nonce);
  923. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  924. extern int opt_queue;
  925. extern int opt_scantime;
  926. extern int opt_expiry;
  927. #ifdef USE_USBUTILS
  928. extern pthread_mutex_t cgusb_lock;
  929. extern pthread_mutex_t cgusbres_lock;
  930. extern cglock_t cgusb_fd_lock;
  931. #endif
  932. extern cglock_t control_lock;
  933. extern pthread_mutex_t hash_lock;
  934. extern pthread_mutex_t console_lock;
  935. extern cglock_t ch_lock;
  936. extern pthread_rwlock_t mining_thr_lock;
  937. extern pthread_rwlock_t devices_lock;
  938. extern pthread_mutex_t restart_lock;
  939. extern pthread_cond_t restart_cond;
  940. extern void clear_stratum_shares(struct pool *pool);
  941. extern void set_target(unsigned char *dest_target, double diff);
  942. extern int restart_wait(struct thr_info *thr, unsigned int mstime);
  943. extern void kill_work(void);
  944. extern void reinit_device(struct cgpu_info *cgpu);
  945. #ifdef HAVE_ADL
  946. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  947. extern int set_fanspeed(int gpu, int iFanSpeed);
  948. extern int set_vddc(int gpu, float fVddc);
  949. extern int set_engineclock(int gpu, int iEngineClock);
  950. extern int set_memoryclock(int gpu, int iMemoryClock);
  951. #endif
  952. extern void api(int thr_id);
  953. extern struct pool *current_pool(void);
  954. extern int enabled_pools;
  955. extern void get_intrange(char *arg, int *val1, int *val2);
  956. extern bool detect_stratum(struct pool *pool, char *url);
  957. extern void print_summary(void);
  958. extern void adjust_quota_gcd(void);
  959. extern struct pool *add_pool(void);
  960. extern bool add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  961. #define MAX_GPUDEVICES 16
  962. #define MAX_DEVICES 4096
  963. #define MIN_SHA_INTENSITY -10
  964. #define MIN_SHA_INTENSITY_STR "-10"
  965. #define MAX_SHA_INTENSITY 14
  966. #define MAX_SHA_INTENSITY_STR "14"
  967. #define MIN_SCRYPT_INTENSITY 8
  968. #define MIN_SCRYPT_INTENSITY_STR "8"
  969. #define MAX_SCRYPT_INTENSITY 20
  970. #define MAX_SCRYPT_INTENSITY_STR "20"
  971. #ifdef USE_SCRYPT
  972. #define MIN_INTENSITY (opt_scrypt ? MIN_SCRYPT_INTENSITY : MIN_SHA_INTENSITY)
  973. #define MIN_INTENSITY_STR (opt_scrypt ? MIN_SCRYPT_INTENSITY_STR : MIN_SHA_INTENSITY_STR)
  974. #define MAX_INTENSITY (opt_scrypt ? MAX_SCRYPT_INTENSITY : MAX_SHA_INTENSITY)
  975. #define MAX_INTENSITY_STR (opt_scrypt ? MAX_SCRYPT_INTENSITY_STR : MAX_SHA_INTENSITY_STR)
  976. #define MAX_GPU_INTENSITY MAX_SCRYPT_INTENSITY
  977. #else
  978. #define MIN_INTENSITY MIN_SHA_INTENSITY
  979. #define MIN_INTENSITY_STR MIN_SHA_INTENSITY_STR
  980. #define MAX_INTENSITY MAX_SHA_INTENSITY
  981. #define MAX_INTENSITY_STR MAX_SHA_INTENSITY_STR
  982. #define MAX_GPU_INTENSITY MAX_SHA_INTENSITY
  983. #endif
  984. extern bool hotplug_mode;
  985. extern int hotplug_time;
  986. extern struct list_head scan_devices;
  987. extern int nDevs;
  988. extern int num_processors;
  989. extern int hw_errors;
  990. extern bool use_syslog;
  991. extern bool opt_quiet;
  992. extern struct thr_info *control_thr;
  993. extern struct thr_info **mining_thr;
  994. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  995. extern int gpu_threads;
  996. #ifdef USE_SCRYPT
  997. extern bool opt_scrypt;
  998. #else
  999. #define opt_scrypt (0)
  1000. #endif
  1001. extern double total_secs;
  1002. extern int mining_threads;
  1003. extern int total_devices;
  1004. extern int zombie_devs;
  1005. extern struct cgpu_info **devices;
  1006. extern int total_pools;
  1007. extern struct pool **pools;
  1008. extern struct strategies strategies[];
  1009. extern enum pool_strategy pool_strategy;
  1010. extern int opt_rotate_period;
  1011. extern double total_rolling;
  1012. extern double total_mhashes_done;
  1013. extern unsigned int new_blocks;
  1014. extern unsigned int found_blocks;
  1015. extern int total_accepted, total_rejected, total_diff1;;
  1016. extern int total_getworks, total_stale, total_discarded;
  1017. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  1018. extern unsigned int local_work;
  1019. extern unsigned int total_go, total_ro;
  1020. extern const int opt_cutofftemp;
  1021. extern int opt_log_interval;
  1022. extern unsigned long long global_hashrate;
  1023. extern char *current_fullhash;
  1024. extern double current_diff;
  1025. extern uint64_t best_diff;
  1026. extern struct timeval block_timeval;
  1027. extern char *workpadding;
  1028. #ifdef HAVE_OPENCL
  1029. typedef struct {
  1030. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  1031. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  1032. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  1033. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  1034. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  1035. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  1036. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  1037. cl_uint W16; cl_uint W17; cl_uint W2;
  1038. cl_uint PreVal4; cl_uint T1;
  1039. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  1040. cl_uint PreVal4addT1; cl_uint T1substate0;
  1041. cl_uint PreVal4_2;
  1042. cl_uint PreVal0;
  1043. cl_uint PreW18;
  1044. cl_uint PreW19;
  1045. cl_uint PreW31;
  1046. cl_uint PreW32;
  1047. /* For diakgcn */
  1048. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  1049. cl_uint zeroA, zeroB;
  1050. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  1051. #ifdef USE_SCRYPT
  1052. struct work *work;
  1053. #endif
  1054. } dev_blk_ctx;
  1055. #else
  1056. typedef struct {
  1057. uint32_t nonce;
  1058. } dev_blk_ctx;
  1059. #endif
  1060. struct curl_ent {
  1061. CURL *curl;
  1062. struct list_head node;
  1063. struct timeval tv;
  1064. };
  1065. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  1066. * equal disabled */
  1067. enum pool_enable {
  1068. POOL_DISABLED,
  1069. POOL_ENABLED,
  1070. POOL_REJECTING,
  1071. };
  1072. struct stratum_work {
  1073. char *job_id;
  1074. char *prev_hash;
  1075. unsigned char **merkle_bin;
  1076. char *bbversion;
  1077. char *nbit;
  1078. char *ntime;
  1079. bool clean;
  1080. size_t cb_len;
  1081. size_t header_len;
  1082. int merkles;
  1083. double diff;
  1084. };
  1085. #define RBUFSIZE 8192
  1086. #define RECVSIZE (RBUFSIZE - 4)
  1087. struct pool {
  1088. int pool_no;
  1089. int prio;
  1090. int accepted, rejected;
  1091. int seq_rejects;
  1092. int seq_getfails;
  1093. int solved;
  1094. int diff1;
  1095. char diff[8];
  1096. int quota;
  1097. int quota_gcd;
  1098. int quota_used;
  1099. double diff_accepted;
  1100. double diff_rejected;
  1101. double diff_stale;
  1102. bool submit_fail;
  1103. bool idle;
  1104. bool lagging;
  1105. bool probed;
  1106. enum pool_enable enabled;
  1107. bool submit_old;
  1108. bool removed;
  1109. bool lp_started;
  1110. char *hdr_path;
  1111. char *lp_url;
  1112. unsigned int getwork_requested;
  1113. unsigned int stale_shares;
  1114. unsigned int discarded_work;
  1115. unsigned int getfail_occasions;
  1116. unsigned int remotefail_occasions;
  1117. struct timeval tv_idle;
  1118. double utility;
  1119. int last_shares, shares;
  1120. char *rpc_req;
  1121. char *rpc_url;
  1122. char *rpc_userpass;
  1123. char *rpc_user, *rpc_pass;
  1124. proxytypes_t rpc_proxytype;
  1125. char *rpc_proxy;
  1126. pthread_mutex_t pool_lock;
  1127. cglock_t data_lock;
  1128. struct thread_q *submit_q;
  1129. struct thread_q *getwork_q;
  1130. pthread_t longpoll_thread;
  1131. pthread_t test_thread;
  1132. bool testing;
  1133. int curls;
  1134. pthread_cond_t cr_cond;
  1135. struct list_head curlring;
  1136. time_t last_share_time;
  1137. double last_share_diff;
  1138. uint64_t best_diff;
  1139. struct cgminer_stats cgminer_stats;
  1140. struct cgminer_pool_stats cgminer_pool_stats;
  1141. /* Stratum variables */
  1142. char *stratum_url;
  1143. char *stratum_port;
  1144. struct addrinfo stratum_hints;
  1145. SOCKETTYPE sock;
  1146. char *sockbuf;
  1147. size_t sockbuf_size;
  1148. char *sockaddr_url; /* stripped url used for sockaddr */
  1149. char *sockaddr_proxy_url;
  1150. char *sockaddr_proxy_port;
  1151. char *nonce1;
  1152. unsigned char *nonce1bin;
  1153. size_t n1_len;
  1154. uint32_t nonce2;
  1155. int n2size;
  1156. char *sessionid;
  1157. bool has_stratum;
  1158. bool stratum_active;
  1159. bool stratum_init;
  1160. bool stratum_notify;
  1161. struct stratum_work swork;
  1162. pthread_t stratum_sthread;
  1163. pthread_t stratum_rthread;
  1164. pthread_mutex_t stratum_lock;
  1165. struct thread_q *stratum_q;
  1166. int sshares; /* stratum shares submitted waiting on response */
  1167. /* GBT variables */
  1168. bool has_gbt;
  1169. cglock_t gbt_lock;
  1170. unsigned char previousblockhash[32];
  1171. unsigned char gbt_target[32];
  1172. char *coinbasetxn;
  1173. char *longpollid;
  1174. char *gbt_workid;
  1175. int gbt_expires;
  1176. uint32_t gbt_version;
  1177. uint32_t curtime;
  1178. uint32_t gbt_bits;
  1179. unsigned char *txn_hashes;
  1180. int gbt_txns;
  1181. int coinbase_len;
  1182. /* Shared by both stratum & GBT */
  1183. unsigned char *coinbase;
  1184. int nonce2_offset;
  1185. unsigned char header_bin[128];
  1186. int merkle_offset;
  1187. struct timeval tv_lastwork;
  1188. };
  1189. #define GETWORK_MODE_TESTPOOL 'T'
  1190. #define GETWORK_MODE_POOL 'P'
  1191. #define GETWORK_MODE_LP 'L'
  1192. #define GETWORK_MODE_BENCHMARK 'B'
  1193. #define GETWORK_MODE_STRATUM 'S'
  1194. #define GETWORK_MODE_GBT 'G'
  1195. struct work {
  1196. unsigned char data[128];
  1197. unsigned char midstate[32];
  1198. unsigned char target[32];
  1199. unsigned char hash[32];
  1200. #ifdef USE_SCRYPT
  1201. unsigned char device_target[32];
  1202. #endif
  1203. double device_diff;
  1204. uint64_t share_diff;
  1205. unsigned char hash2[32];
  1206. int rolls;
  1207. int drv_rolllimit; /* How much the driver can roll ntime */
  1208. dev_blk_ctx blk;
  1209. struct thr_info *thr;
  1210. int thr_id;
  1211. struct pool *pool;
  1212. struct timeval tv_staged;
  1213. bool mined;
  1214. bool clone;
  1215. bool cloned;
  1216. int rolltime;
  1217. bool longpoll;
  1218. bool stale;
  1219. bool mandatory;
  1220. bool block;
  1221. bool stratum;
  1222. char *job_id;
  1223. uint32_t nonce2;
  1224. size_t nonce2_len;
  1225. char *ntime;
  1226. double sdiff;
  1227. char *nonce1;
  1228. bool gbt;
  1229. char *coinbase;
  1230. int gbt_txns;
  1231. unsigned int work_block;
  1232. int id;
  1233. UT_hash_handle hh;
  1234. double work_difficulty;
  1235. // Allow devices to identify work if multiple sub-devices
  1236. int subid;
  1237. // Allow devices to flag work for their own purposes
  1238. bool devflag;
  1239. // Allow devices to timestamp work for their own purposes
  1240. struct timeval tv_stamp;
  1241. struct timeval tv_getwork;
  1242. struct timeval tv_getwork_reply;
  1243. struct timeval tv_cloned;
  1244. struct timeval tv_work_start;
  1245. struct timeval tv_work_found;
  1246. char getwork_mode;
  1247. };
  1248. #ifdef USE_MODMINER
  1249. struct modminer_fpga_state {
  1250. bool work_running;
  1251. struct work running_work;
  1252. struct timeval tv_workstart;
  1253. uint32_t hashes;
  1254. char next_work_cmd[46];
  1255. char fpgaid;
  1256. bool overheated;
  1257. bool new_work;
  1258. uint32_t shares;
  1259. uint32_t shares_last_hw;
  1260. uint32_t hw_errors;
  1261. uint32_t shares_to_good;
  1262. uint32_t timeout_fail;
  1263. uint32_t success_more;
  1264. struct timeval last_changed;
  1265. struct timeval last_nonce;
  1266. struct timeval first_work;
  1267. bool death_stage_one;
  1268. bool tried_two_byte_temp;
  1269. bool one_byte_temp;
  1270. };
  1271. #endif
  1272. #define TAILBUFSIZ 64
  1273. #define tailsprintf(buf, bufsiz, fmt, ...) do { \
  1274. char tmp13[TAILBUFSIZ]; \
  1275. size_t len13, buflen = strlen(buf); \
  1276. snprintf(tmp13, sizeof(tmp13), fmt, ##__VA_ARGS__); \
  1277. len13 = strlen(tmp13); \
  1278. if ((buflen + len13) >= bufsiz) \
  1279. quit(1, "tailsprintf buffer overflow in %s %s line %d", __FILE__, __func__, __LINE__); \
  1280. strcat(buf, tmp13); \
  1281. } while (0)
  1282. extern void get_datestamp(char *, size_t, struct timeval *);
  1283. extern void inc_hw_errors(struct thr_info *thr);
  1284. extern bool test_nonce(struct work *work, uint32_t nonce);
  1285. extern bool test_nonce_diff(struct work *work, uint32_t nonce, double diff);
  1286. extern void submit_tested_work(struct thr_info *thr, struct work *work);
  1287. extern bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  1288. extern bool submit_noffset_nonce(struct thr_info *thr, struct work *work, uint32_t nonce,
  1289. int noffset);
  1290. extern struct work *get_work(struct thr_info *thr, const int thr_id);
  1291. extern struct work *get_queued(struct cgpu_info *cgpu);
  1292. extern struct work *__find_work_bymidstate(struct work *que, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1293. extern struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1294. extern struct work *clone_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1295. extern void __work_completed(struct cgpu_info *cgpu, struct work *work);
  1296. extern void work_completed(struct cgpu_info *cgpu, struct work *work);
  1297. extern struct work *take_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1298. extern void hash_driver_work(struct thr_info *mythr);
  1299. extern void hash_queued_work(struct thr_info *mythr);
  1300. extern void _wlog(const char *str);
  1301. extern void _wlogprint(const char *str);
  1302. extern int curses_int(const char *query);
  1303. extern char *curses_input(const char *query);
  1304. extern void kill_work(void);
  1305. extern void switch_pools(struct pool *selected);
  1306. extern void discard_work(struct work *work);
  1307. extern void remove_pool(struct pool *pool);
  1308. extern void write_config(FILE *fcfg);
  1309. extern void zero_bestshare(void);
  1310. extern void zero_stats(void);
  1311. extern void default_save_file(char *filename);
  1312. extern bool log_curses_only(int prio, const char *datetime, const char *str);
  1313. extern void clear_logwin(void);
  1314. extern void logwin_update(void);
  1315. extern bool pool_tclear(struct pool *pool, bool *var);
  1316. extern struct thread_q *tq_new(void);
  1317. extern void tq_free(struct thread_q *tq);
  1318. extern bool tq_push(struct thread_q *tq, void *data);
  1319. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  1320. extern void tq_freeze(struct thread_q *tq);
  1321. extern void tq_thaw(struct thread_q *tq);
  1322. extern bool successful_connect;
  1323. extern void adl(void);
  1324. extern void app_restart(void);
  1325. extern void clean_work(struct work *work);
  1326. extern void free_work(struct work *work);
  1327. extern struct work *copy_work_noffset(struct work *base_work, int noffset);
  1328. #define copy_work(work_in) copy_work_noffset(work_in, 0)
  1329. extern struct thr_info *get_thread(int thr_id);
  1330. extern struct cgpu_info *get_devices(int id);
  1331. enum api_data_type {
  1332. API_ESCAPE,
  1333. API_STRING,
  1334. API_CONST,
  1335. API_UINT8,
  1336. API_UINT16,
  1337. API_INT,
  1338. API_UINT,
  1339. API_UINT32,
  1340. API_UINT64,
  1341. API_DOUBLE,
  1342. API_ELAPSED,
  1343. API_BOOL,
  1344. API_TIMEVAL,
  1345. API_TIME,
  1346. API_MHS,
  1347. API_MHTOTAL,
  1348. API_TEMP,
  1349. API_UTILITY,
  1350. API_FREQ,
  1351. API_VOLTS,
  1352. API_HS,
  1353. API_DIFF,
  1354. API_PERCENT
  1355. };
  1356. struct api_data {
  1357. enum api_data_type type;
  1358. char *name;
  1359. void *data;
  1360. bool data_was_malloc;
  1361. struct api_data *prev;
  1362. struct api_data *next;
  1363. };
  1364. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  1365. extern struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data);
  1366. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  1367. extern struct api_data *api_add_uint8(struct api_data *root, char *name, uint8_t *data, bool copy_data);
  1368. extern struct api_data *api_add_uint16(struct api_data *root, char *name, uint16_t *data, bool copy_data);
  1369. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  1370. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  1371. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  1372. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  1373. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  1374. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  1375. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  1376. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  1377. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  1378. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  1379. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  1380. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  1381. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  1382. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  1383. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  1384. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  1385. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  1386. extern struct api_data *api_add_percent(struct api_data *root, char *name, double *data, bool copy_data);
  1387. #endif /* __MINER_H__ */