miner.h 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. #include <curl/curl.h>
  10. #include <blkmaker.h>
  11. #include <blktemplate.h>
  12. #include "elist.h"
  13. #include "uthash.h"
  14. #include "logging.h"
  15. #ifdef HAVE_OPENCL
  16. #include "CL/cl.h"
  17. #endif /* HAVE_OPENCL */
  18. #ifdef STDC_HEADERS
  19. # include <stdlib.h>
  20. # include <stddef.h>
  21. #else
  22. # ifdef HAVE_STDLIB_H
  23. # include <stdlib.h>
  24. # endif
  25. #endif
  26. #ifdef HAVE_ALLOCA_H
  27. # include <alloca.h>
  28. #elif defined __GNUC__
  29. # ifndef WIN32
  30. # define alloca __builtin_alloca
  31. # else
  32. # include <malloc.h>
  33. # endif
  34. #elif defined _AIX
  35. # define alloca __alloca
  36. #elif defined _MSC_VER
  37. # include <malloc.h>
  38. # define alloca _alloca
  39. #else
  40. # ifndef HAVE_ALLOCA
  41. # ifdef __cplusplus
  42. extern "C"
  43. # endif
  44. void *alloca (size_t);
  45. # endif
  46. #endif
  47. #if defined (__linux)
  48. #ifndef LINUX
  49. #define LINUX
  50. #endif
  51. #endif
  52. #ifdef WIN32
  53. #ifndef timersub
  54. #define timersub(a, b, result) \
  55. do { \
  56. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  57. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  58. if ((result)->tv_usec < 0) { \
  59. --(result)->tv_sec; \
  60. (result)->tv_usec += 1000000; \
  61. } \
  62. } while (0)
  63. #endif
  64. #ifndef timeradd
  65. # define timeradd(a, b, result) \
  66. do { \
  67. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  68. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  69. if ((result)->tv_usec >= 1000000) \
  70. { \
  71. ++(result)->tv_sec; \
  72. (result)->tv_usec -= 1000000; \
  73. } \
  74. } while (0)
  75. #endif
  76. #endif
  77. #ifdef HAVE_ADL
  78. #include "ADL/adl_sdk.h"
  79. #endif
  80. #ifdef HAVE_LIBUSB
  81. #include <libusb-1.0/libusb.h>
  82. #endif
  83. #ifdef USE_ZTEX
  84. #include "libztex.h"
  85. #endif
  86. #if !defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  87. #define bswap_16 __builtin_bswap16
  88. #define bswap_32 __builtin_bswap32
  89. #define bswap_64 __builtin_bswap64
  90. #else
  91. #if HAVE_BYTESWAP_H
  92. #include <byteswap.h>
  93. #elif defined(USE_SYS_ENDIAN_H)
  94. #include <sys/endian.h>
  95. #elif defined(__APPLE__)
  96. #include <libkern/OSByteOrder.h>
  97. #define bswap_16 OSSwapInt16
  98. #define bswap_32 OSSwapInt32
  99. #define bswap_64 OSSwapInt64
  100. #else
  101. #define bswap_16(value) \
  102. ((((value) & 0xff) << 8) | ((value) >> 8))
  103. #define bswap_32(value) \
  104. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  105. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  106. #define bswap_64(value) \
  107. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  108. << 32) | \
  109. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  110. #endif
  111. #endif /* !defined(__GLXBYTEORDER_H__) */
  112. #ifdef WIN32
  113. #ifndef __LITTLE_ENDIAN
  114. #define __LITTLE_ENDIAN 1234
  115. #define __BIG_ENDIAN 4321
  116. #endif
  117. #ifndef __BYTE_ORDER
  118. #define __BYTE_ORDER __LITTLE_ENDIAN
  119. #endif
  120. #else
  121. #include <endian.h>
  122. #endif
  123. /* This assumes htobe32 is a macro in endian.h */
  124. #ifndef htobe32
  125. # if __BYTE_ORDER == __LITTLE_ENDIAN
  126. # define be32toh(x) bswap_32(x)
  127. # define htobe32(x) bswap_32(x)
  128. # define le32toh(x) (x)
  129. # define htole32(x) (x)
  130. # elif __BYTE_ORDER == __BIG_ENDIAN
  131. # define be32toh(x) (x)
  132. # define htobe32(x) (x)
  133. # define le32toh(x) bswap_32(x)
  134. # define htole32(x) bswap_32(x)
  135. #else
  136. #error UNKNOWN BYTE ORDER
  137. #endif
  138. #endif
  139. #ifndef max
  140. # define max(a, b) ((a) > (b) ? (a) : (b))
  141. #endif
  142. #undef unlikely
  143. #undef likely
  144. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  145. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  146. #define likely(expr) (__builtin_expect(!!(expr), 1))
  147. #else
  148. #define unlikely(expr) (expr)
  149. #define likely(expr) (expr)
  150. #endif
  151. #define __maybe_unused __attribute__((unused))
  152. #define uninitialised_var(x) x = x
  153. #if defined(__i386__)
  154. #define WANT_CRYPTOPP_ASM32
  155. #endif
  156. #ifndef ARRAY_SIZE
  157. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  158. #endif
  159. #ifdef HAVE_CURSES
  160. extern int my_cancellable_getch(void);
  161. # ifdef getch
  162. // getch() is a macro
  163. static int __maybe_unused __real_getch(void) {
  164. return getch();
  165. }
  166. # undef getch
  167. # define getch() my_cancellable_getch()
  168. # else
  169. // getch() is a real function
  170. # define __real_getch getch
  171. # define getch() my_cancellable_getch()
  172. # endif
  173. #endif
  174. enum alive {
  175. LIFE_WELL,
  176. LIFE_SICK,
  177. LIFE_DEAD,
  178. LIFE_NOSTART,
  179. LIFE_INIT,
  180. LIFE_WAIT,
  181. };
  182. enum pool_strategy {
  183. POOL_FAILOVER,
  184. POOL_ROUNDROBIN,
  185. POOL_ROTATE,
  186. POOL_LOADBALANCE,
  187. POOL_BALANCE,
  188. };
  189. #define TOP_STRATEGY (POOL_BALANCE)
  190. struct strategies {
  191. const char *s;
  192. };
  193. struct cgpu_info;
  194. #ifdef HAVE_ADL
  195. struct gpu_adl {
  196. ADLTemperature lpTemperature;
  197. int iAdapterIndex;
  198. int lpAdapterID;
  199. int iBusNumber;
  200. char strAdapterName[256];
  201. ADLPMActivity lpActivity;
  202. ADLODParameters lpOdParameters;
  203. ADLODPerformanceLevels *DefPerfLev;
  204. ADLFanSpeedInfo lpFanSpeedInfo;
  205. ADLFanSpeedValue lpFanSpeedValue;
  206. ADLFanSpeedValue DefFanSpeedValue;
  207. int iEngineClock;
  208. int iMemoryClock;
  209. int iVddc;
  210. int iPercentage;
  211. bool autofan;
  212. bool autoengine;
  213. bool managed; /* Were the values ever changed on this card */
  214. int lastengine;
  215. int lasttemp;
  216. int targetfan;
  217. int targettemp;
  218. int overtemp;
  219. int minspeed;
  220. int maxspeed;
  221. int gpu;
  222. bool has_fanspeed;
  223. struct gpu_adl *twin;
  224. };
  225. #endif
  226. struct api_data;
  227. struct thr_info;
  228. struct work;
  229. struct device_api {
  230. const char*dname;
  231. const char*name;
  232. // API-global functions
  233. void (*api_detect)();
  234. // Device-specific functions
  235. void (*reinit_device)(struct cgpu_info*);
  236. void (*get_statline_before)(char*, struct cgpu_info*);
  237. void (*get_statline)(char*, struct cgpu_info*);
  238. struct api_data* (*get_api_extra_device_detail)(struct cgpu_info*);
  239. struct api_data* (*get_api_extra_device_status)(struct cgpu_info*);
  240. struct api_data *(*get_api_stats)(struct cgpu_info*);
  241. bool (*get_stats)(struct cgpu_info*);
  242. void (*identify_device)(struct cgpu_info*); // e.g. to flash a led
  243. // Thread-specific functions
  244. bool (*thread_prepare)(struct thr_info*);
  245. uint64_t (*can_limit_work)(struct thr_info*);
  246. bool (*thread_init)(struct thr_info*);
  247. void (*free_work)(struct thr_info*, struct work*);
  248. bool (*prepare_work)(struct thr_info*, struct work*);
  249. int64_t (*scanhash)(struct thr_info*, struct work*, int64_t);
  250. void (*thread_shutdown)(struct thr_info*);
  251. void (*thread_enable)(struct thr_info*);
  252. };
  253. enum dev_enable {
  254. DEV_ENABLED,
  255. DEV_DISABLED,
  256. DEV_RECOVER,
  257. };
  258. enum cl_kernels {
  259. KL_NONE,
  260. KL_POCLBM,
  261. KL_PHATK,
  262. KL_DIAKGCN,
  263. KL_DIABLO,
  264. KL_SCRYPT,
  265. };
  266. enum dev_reason {
  267. REASON_THREAD_FAIL_INIT,
  268. REASON_THREAD_ZERO_HASH,
  269. REASON_THREAD_FAIL_QUEUE,
  270. REASON_DEV_SICK_IDLE_60,
  271. REASON_DEV_DEAD_IDLE_600,
  272. REASON_DEV_NOSTART,
  273. REASON_DEV_OVER_HEAT,
  274. REASON_DEV_THERMAL_CUTOFF,
  275. REASON_DEV_COMMS_ERROR,
  276. REASON_DEV_THROTTLE,
  277. };
  278. #define REASON_NONE "None"
  279. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  280. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  281. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  282. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  283. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  284. #define REASON_DEV_NOSTART_STR "Device failed to start"
  285. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  286. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  287. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  288. #define REASON_DEV_THROTTLE_STR "Device throttle"
  289. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  290. #define MIN_SEC_UNSET 99999999
  291. struct cgminer_stats {
  292. uint32_t getwork_calls;
  293. struct timeval getwork_wait;
  294. struct timeval getwork_wait_max;
  295. struct timeval getwork_wait_min;
  296. };
  297. // Just the actual network getworks to the pool
  298. struct cgminer_pool_stats {
  299. uint32_t getwork_calls;
  300. uint32_t getwork_attempts;
  301. struct timeval getwork_wait;
  302. struct timeval getwork_wait_max;
  303. struct timeval getwork_wait_min;
  304. double getwork_wait_rolling;
  305. bool hadrolltime;
  306. bool canroll;
  307. bool hadexpire;
  308. uint32_t rolltime;
  309. };
  310. struct cgpu_info {
  311. int cgminer_id;
  312. const struct device_api *api;
  313. const char *devtype;
  314. int device_id;
  315. const char *name;
  316. const char *device_path;
  317. FILE *device_file;
  318. union {
  319. #ifdef USE_ZTEX
  320. struct libztex_device *device_ztex;
  321. #endif
  322. int device_fd;
  323. };
  324. #ifdef USE_BITFORCE
  325. struct timeval work_start_tv;
  326. unsigned int wait_ms;
  327. unsigned int sleep_ms;
  328. double avg_wait_f;
  329. unsigned int avg_wait_d;
  330. uint32_t nonces;
  331. bool nonce_range;
  332. bool polling;
  333. bool flash_led;
  334. #endif
  335. void *cgpu_data;
  336. pthread_mutex_t device_mutex;
  337. enum dev_enable deven;
  338. int accepted;
  339. double accepted_weighed;
  340. int rejected;
  341. int hw_errors;
  342. double rolling;
  343. double total_mhashes;
  344. double utility;
  345. double utility_diff1;
  346. enum alive status;
  347. char init[40];
  348. struct timeval last_message_tv;
  349. int threads;
  350. struct thr_info **thr;
  351. int64_t max_hashes;
  352. const char *kname;
  353. #ifdef HAVE_OPENCL
  354. bool mapped;
  355. int virtual_gpu;
  356. int virtual_adl;
  357. int intensity;
  358. bool dynamic;
  359. cl_uint vwidth;
  360. size_t work_size;
  361. enum cl_kernels kernel;
  362. cl_ulong max_alloc;
  363. #ifdef USE_SCRYPT
  364. int opt_lg, lookup_gap;
  365. int opt_tc, thread_concurrency;
  366. int shaders;
  367. #endif
  368. struct timeval tv_gpustart;;
  369. struct timeval tv_gpuend;
  370. double gpu_us_average;
  371. #endif
  372. bool new_work;
  373. float temp;
  374. int cutofftemp;
  375. #ifdef HAVE_ADL
  376. bool has_adl;
  377. struct gpu_adl adl;
  378. int gpu_engine;
  379. int min_engine;
  380. int gpu_fan;
  381. int min_fan;
  382. int gpu_memclock;
  383. int gpu_memdiff;
  384. int gpu_powertune;
  385. float gpu_vddc;
  386. #endif
  387. int diff1;
  388. int last_share_pool;
  389. time_t last_share_pool_time;
  390. time_t device_last_well;
  391. time_t device_last_not_well;
  392. enum dev_reason device_not_well_reason;
  393. int thread_fail_init_count;
  394. int thread_zero_hash_count;
  395. int thread_fail_queue_count;
  396. int dev_sick_idle_60_count;
  397. int dev_dead_idle_600_count;
  398. int dev_nostart_count;
  399. int dev_over_heat_count; // It's a warning but worth knowing
  400. int dev_thermal_cutoff_count;
  401. int dev_comms_error_count;
  402. int dev_throttle_count;
  403. struct cgminer_stats cgminer_stats;
  404. };
  405. extern void renumber_cgpu(struct cgpu_info *);
  406. extern bool add_cgpu(struct cgpu_info*);
  407. struct thread_q {
  408. struct list_head q;
  409. bool frozen;
  410. pthread_mutex_t mutex;
  411. pthread_cond_t cond;
  412. };
  413. struct thr_info {
  414. int id;
  415. int device_thread;
  416. bool primary_thread;
  417. pthread_t pth;
  418. struct thread_q *q;
  419. struct cgpu_info *cgpu;
  420. void *cgpu_data;
  421. struct timeval last;
  422. struct timeval sick;
  423. bool pause;
  424. time_t getwork;
  425. double rolling;
  426. bool work_restart;
  427. int work_restart_fd;
  428. int _work_restart_fd_w;
  429. };
  430. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  431. extern void thr_info_cancel(struct thr_info *thr);
  432. extern void thr_info_freeze(struct thr_info *thr);
  433. extern void nmsleep(unsigned int msecs);
  434. extern void rename_thr(const char* name);
  435. extern double tdiff(struct timeval *end, struct timeval *start);
  436. struct string_elist {
  437. char *string;
  438. bool free_me;
  439. struct list_head list;
  440. };
  441. static inline void string_elist_add(const char *s, struct list_head *head)
  442. {
  443. struct string_elist *n;
  444. n = calloc(1, sizeof(*n));
  445. n->string = strdup(s);
  446. n->free_me = true;
  447. list_add_tail(&n->list, head);
  448. }
  449. static inline void string_elist_del(struct string_elist *item)
  450. {
  451. if (item->free_me)
  452. free(item->string);
  453. list_del(&item->list);
  454. free(item);
  455. }
  456. static inline uint32_t swab32(uint32_t v)
  457. {
  458. return bswap_32(v);
  459. }
  460. static inline void swap256(void *dest_p, const void *src_p)
  461. {
  462. uint32_t *dest = dest_p;
  463. const uint32_t *src = src_p;
  464. dest[0] = src[7];
  465. dest[1] = src[6];
  466. dest[2] = src[5];
  467. dest[3] = src[4];
  468. dest[4] = src[3];
  469. dest[5] = src[2];
  470. dest[6] = src[1];
  471. dest[7] = src[0];
  472. }
  473. static inline void swap32yes(void*out, const void*in, size_t sz) {
  474. size_t swapcounter = 0;
  475. for (swapcounter = 0; swapcounter < sz; ++swapcounter)
  476. (((uint32_t*)out)[swapcounter]) = swab32(((uint32_t*)in)[swapcounter]);
  477. }
  478. #ifdef __BIG_ENDIAN__
  479. # define swap32tobe(out, in, sz) (void)0
  480. # define swap32tole(out, in, sz) swap32yes(out, in, sz)
  481. #else
  482. # define swap32tobe(out, in, sz) swap32yes(out, in, sz)
  483. # define swap32tole(out, in, sz) (void)0
  484. #endif
  485. extern void quit(int status, const char *format, ...);
  486. static inline void mutex_lock(pthread_mutex_t *lock)
  487. {
  488. if (unlikely(pthread_mutex_lock(lock)))
  489. quit(1, "WTF MUTEX ERROR ON LOCK!");
  490. }
  491. static inline void mutex_unlock(pthread_mutex_t *lock)
  492. {
  493. if (unlikely(pthread_mutex_unlock(lock)))
  494. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  495. }
  496. static inline int mutex_trylock(pthread_mutex_t *lock)
  497. {
  498. return pthread_mutex_trylock(lock);
  499. }
  500. static inline void wr_lock(pthread_rwlock_t *lock)
  501. {
  502. if (unlikely(pthread_rwlock_wrlock(lock)))
  503. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  504. }
  505. static inline void rd_lock(pthread_rwlock_t *lock)
  506. {
  507. if (unlikely(pthread_rwlock_rdlock(lock)))
  508. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  509. }
  510. static inline void rw_unlock(pthread_rwlock_t *lock)
  511. {
  512. if (unlikely(pthread_rwlock_unlock(lock)))
  513. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  514. }
  515. static inline void rd_unlock(pthread_rwlock_t *lock)
  516. {
  517. rw_unlock(lock);
  518. }
  519. static inline void wr_unlock(pthread_rwlock_t *lock)
  520. {
  521. rw_unlock(lock);
  522. }
  523. static inline void mutex_init(pthread_mutex_t *lock)
  524. {
  525. if (unlikely(pthread_mutex_init(lock, NULL)))
  526. quit(1, "Failed to pthread_mutex_init");
  527. }
  528. static inline void rwlock_init(pthread_rwlock_t *lock)
  529. {
  530. if (unlikely(pthread_rwlock_init(lock, NULL)))
  531. quit(1, "Failed to pthread_rwlock_init");
  532. }
  533. struct pool;
  534. extern bool opt_protocol;
  535. extern bool have_longpoll;
  536. extern char *opt_kernel_path;
  537. extern char *opt_socks_proxy;
  538. extern char *cgminer_path;
  539. extern bool opt_fail_only;
  540. extern bool opt_autofan;
  541. extern bool opt_autoengine;
  542. extern bool use_curses;
  543. extern char *opt_api_allow;
  544. extern char *opt_api_groups;
  545. extern char *opt_api_description;
  546. extern int opt_api_port;
  547. extern bool opt_api_listen;
  548. extern bool opt_api_network;
  549. extern bool opt_delaynet;
  550. extern bool opt_restart;
  551. extern char *opt_icarus_options;
  552. extern char *opt_icarus_timing;
  553. extern bool opt_worktime;
  554. #ifdef USE_BITFORCE
  555. extern bool opt_bfl_noncerange;
  556. #endif
  557. extern pthread_rwlock_t netacc_lock;
  558. extern const uint32_t sha256_init_state[];
  559. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  560. const char *rpc_req, bool, bool, int *,
  561. struct pool *pool, bool);
  562. extern bool our_curl_supports_proxy_uris();
  563. extern char *bin2hex(const unsigned char *p, size_t len);
  564. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  565. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  566. unsigned char *pdata,
  567. unsigned char *phash1, unsigned char *phash,
  568. const unsigned char *ptarget,
  569. uint32_t max_nonce,
  570. uint32_t *last_nonce,
  571. uint32_t nonce);
  572. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  573. extern int opt_queue;
  574. extern int opt_scantime;
  575. extern int opt_expiry;
  576. extern pthread_mutex_t console_lock;
  577. extern pthread_mutex_t ch_lock;
  578. extern pthread_mutex_t restart_lock;
  579. extern pthread_cond_t restart_cond;
  580. extern void thread_reportin(struct thr_info *thr);
  581. extern int restart_wait(unsigned int mstime);
  582. extern int stale_wait(unsigned int mstime, struct work*, bool checkend);
  583. extern void kill_work(void);
  584. extern void reinit_device(struct cgpu_info *cgpu);
  585. #ifdef HAVE_ADL
  586. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  587. extern int set_fanspeed(int gpu, int iFanSpeed);
  588. extern int set_vddc(int gpu, float fVddc);
  589. extern int set_engineclock(int gpu, int iEngineClock);
  590. extern int set_memoryclock(int gpu, int iMemoryClock);
  591. #endif
  592. extern void api(int thr_id);
  593. extern struct pool *current_pool(void);
  594. extern int enabled_pools;
  595. extern void add_pool_details5(bool live, char *url, char *user, char *pass, char *proxy);
  596. extern void add_pool_details(bool live, char *url, char *user, char *pass);
  597. #define MAX_GPUDEVICES 16
  598. #define MIN_INTENSITY -10
  599. #define _MIN_INTENSITY_STR "-10"
  600. #ifdef USE_SCRYPT
  601. #define MAX_INTENSITY 20
  602. #define _MAX_INTENSITY_STR "20"
  603. #else
  604. #define MAX_INTENSITY 14
  605. #define _MAX_INTENSITY_STR "14"
  606. #endif
  607. extern struct list_head scan_devices;
  608. extern int nDevs;
  609. extern int opt_n_threads;
  610. extern int num_processors;
  611. extern int hw_errors;
  612. extern bool use_syslog;
  613. extern bool opt_quiet;
  614. extern struct thr_info *thr_info;
  615. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  616. extern int gpu_threads;
  617. #ifdef USE_SCRYPT
  618. extern bool opt_scrypt;
  619. #else
  620. #define opt_scrypt (0)
  621. #endif
  622. extern double total_secs;
  623. extern int mining_threads;
  624. extern struct cgpu_info *cpus;
  625. extern int total_devices;
  626. extern struct cgpu_info **devices;
  627. extern int total_pools;
  628. extern struct pool **pools;
  629. extern const char *algo_names[];
  630. extern enum sha256_algos opt_algo;
  631. extern struct strategies strategies[];
  632. extern enum pool_strategy pool_strategy;
  633. extern int opt_rotate_period;
  634. extern double total_mhashes_done;
  635. extern unsigned int new_blocks;
  636. extern unsigned int found_blocks;
  637. extern int total_accepted, total_rejected, total_diff1;;
  638. extern int total_getworks, total_stale, total_discarded;
  639. extern unsigned int local_work;
  640. extern unsigned int total_go, total_ro;
  641. extern const int opt_cutofftemp;
  642. extern int opt_fail_pause;
  643. extern int opt_log_interval;
  644. extern unsigned long long global_hashrate;
  645. extern char *current_fullhash;
  646. extern struct timeval block_timeval;
  647. #ifdef HAVE_OPENCL
  648. typedef struct {
  649. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  650. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  651. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  652. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  653. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  654. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  655. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  656. cl_uint W16; cl_uint W17; cl_uint W2;
  657. cl_uint PreVal4; cl_uint T1;
  658. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  659. cl_uint PreVal4addT1; cl_uint T1substate0;
  660. cl_uint PreVal4_2;
  661. cl_uint PreVal0;
  662. cl_uint PreW18;
  663. cl_uint PreW19;
  664. cl_uint PreW31;
  665. cl_uint PreW32;
  666. /* For diakgcn */
  667. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  668. cl_uint zeroA, zeroB;
  669. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  670. #ifdef USE_SCRYPT
  671. struct work *work;
  672. #endif
  673. } dev_blk_ctx;
  674. #else
  675. typedef struct {
  676. uint32_t nonce;
  677. } dev_blk_ctx;
  678. #endif
  679. struct curl_ent {
  680. CURL *curl;
  681. struct list_head node;
  682. struct timeval tv;
  683. };
  684. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  685. * equal disabled */
  686. enum pool_enable {
  687. POOL_DISABLED,
  688. POOL_ENABLED,
  689. POOL_REJECTING,
  690. };
  691. enum pool_protocol {
  692. PLP_NONE,
  693. PLP_GETWORK,
  694. PLP_GETBLOCKTEMPLATE,
  695. };
  696. struct pool {
  697. int pool_no;
  698. int prio;
  699. int accepted, rejected;
  700. int seq_rejects;
  701. int solved;
  702. int diff1;
  703. int queued;
  704. int staged;
  705. bool submit_fail;
  706. bool idle;
  707. bool lagging;
  708. bool probed;
  709. enum pool_enable enabled;
  710. bool submit_old;
  711. bool removed;
  712. bool lp_started;
  713. unsigned char work_restart_id;
  714. uint32_t block_id;
  715. enum pool_protocol proto;
  716. char *hdr_path;
  717. char *lp_url;
  718. char *lp_id;
  719. enum pool_protocol lp_proto;
  720. curl_socket_t lp_socket;
  721. unsigned int getwork_requested;
  722. unsigned int stale_shares;
  723. unsigned int discarded_work;
  724. unsigned int getfail_occasions;
  725. unsigned int remotefail_occasions;
  726. struct timeval tv_idle;
  727. double utility;
  728. int last_shares, shares;
  729. char *rpc_url;
  730. char *rpc_userpass;
  731. char *rpc_user, *rpc_pass;
  732. char *rpc_proxy;
  733. pthread_mutex_t pool_lock;
  734. struct thread_q *submit_q;
  735. struct thread_q *getwork_q;
  736. pthread_t longpoll_thread;
  737. pthread_t submit_thread;
  738. pthread_t getwork_thread;
  739. int curls;
  740. pthread_cond_t cr_cond;
  741. struct list_head curlring;
  742. time_t last_share_time;
  743. struct cgminer_stats cgminer_stats;
  744. struct cgminer_pool_stats cgminer_pool_stats;
  745. };
  746. #define GETWORK_MODE_TESTPOOL 'T'
  747. #define GETWORK_MODE_POOL 'P'
  748. #define GETWORK_MODE_LP 'L'
  749. #define GETWORK_MODE_BENCHMARK 'B'
  750. struct work {
  751. unsigned char data[128];
  752. unsigned char hash1[64];
  753. unsigned char midstate[32];
  754. unsigned char target[32];
  755. unsigned char hash[32];
  756. int rolls;
  757. uint32_t output[1];
  758. uint32_t valid;
  759. dev_blk_ctx blk;
  760. struct thr_info *thr;
  761. int thr_id;
  762. struct pool *pool;
  763. struct timeval tv_staged;
  764. bool mined;
  765. bool clone;
  766. bool cloned;
  767. int rolltime;
  768. bool longpoll;
  769. bool stale;
  770. bool mandatory;
  771. bool block;
  772. bool queued;
  773. unsigned char work_restart_id;
  774. int id;
  775. UT_hash_handle hh;
  776. float difficulty;
  777. blktemplate_t *tmpl;
  778. int *tmpl_refcount;
  779. unsigned int dataid;
  780. struct timeval tv_getwork;
  781. struct timeval tv_getwork_reply;
  782. struct timeval tv_cloned;
  783. struct timeval tv_work_start;
  784. struct timeval tv_work_found;
  785. char getwork_mode;
  786. };
  787. extern void get_datestamp(char *, struct timeval *);
  788. extern bool test_nonce(struct work *work, uint32_t nonce, bool checktarget);
  789. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  790. extern void tailsprintf(char *f, const char *fmt, ...);
  791. extern void wlogprint(const char *f, ...);
  792. extern int curses_int(const char *query);
  793. extern char *curses_input(const char *query);
  794. extern void kill_work(void);
  795. extern void switch_pools(struct pool *selected);
  796. extern void remove_pool(struct pool *pool);
  797. extern void write_config(FILE *fcfg);
  798. extern void default_save_file(char *filename);
  799. extern bool log_curses_only(int prio, const char *f, va_list ap);
  800. extern void clear_logwin(void);
  801. extern bool pool_tclear(struct pool *pool, bool *var);
  802. extern struct thread_q *tq_new(void);
  803. extern void tq_free(struct thread_q *tq);
  804. extern bool tq_push(struct thread_q *tq, void *data);
  805. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  806. extern void tq_freeze(struct thread_q *tq);
  807. extern void tq_thaw(struct thread_q *tq);
  808. extern bool successful_connect;
  809. extern void adl(void);
  810. enum api_data_type {
  811. API_ESCAPE,
  812. API_STRING,
  813. API_CONST,
  814. API_INT,
  815. API_UINT,
  816. API_UINT32,
  817. API_UINT64,
  818. API_DOUBLE,
  819. API_ELAPSED,
  820. API_BOOL,
  821. API_TIMEVAL,
  822. API_TIME,
  823. API_MHS,
  824. API_MHTOTAL,
  825. API_TEMP,
  826. API_UTILITY,
  827. API_FREQ,
  828. API_VOLTS,
  829. API_HS,
  830. API_JSON,
  831. };
  832. struct api_data {
  833. enum api_data_type type;
  834. char *name;
  835. void *data;
  836. bool data_was_malloc;
  837. struct api_data *prev;
  838. struct api_data *next;
  839. };
  840. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  841. extern struct api_data *api_add_string(struct api_data *root, char *name, const char *data, bool copy_data);
  842. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  843. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  844. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  845. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  846. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  847. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  848. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  849. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  850. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  851. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  852. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  853. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  854. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  855. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  856. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  857. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  858. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  859. extern struct api_data *api_add_json(struct api_data *root, char *name, json_t *data, bool copy_data);
  860. #endif /* __MINER_H__ */