miner.h 29 KB

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