miner.h 24 KB

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