miner.h 30 KB

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