miner.h 26 KB

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