miner.h 24 KB

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