miner.h 45 KB

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