miner.h 35 KB

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