miner.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. #ifndef __MINER_H__
  2. #define __MINER_H__
  3. #include "config.h"
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <sys/time.h>
  7. #include <pthread.h>
  8. #include <jansson.h>
  9. #include <curl/curl.h>
  10. #include "elist.h"
  11. #include "uthash.h"
  12. #include "logging.h"
  13. #include "util.h"
  14. #ifdef HAVE_OPENCL
  15. #ifdef __APPLE_CC__
  16. #include <OpenCL/opencl.h>
  17. #else
  18. #include <CL/cl.h>
  19. #endif
  20. #endif /* HAVE_OPENCL */
  21. #ifdef STDC_HEADERS
  22. # include <stdlib.h>
  23. # include <stddef.h>
  24. #else
  25. # ifdef HAVE_STDLIB_H
  26. # include <stdlib.h>
  27. # endif
  28. #endif
  29. #ifdef HAVE_ALLOCA_H
  30. # include <alloca.h>
  31. #elif defined __GNUC__
  32. # ifndef WIN32
  33. # define alloca __builtin_alloca
  34. # else
  35. # include <malloc.h>
  36. # endif
  37. #elif defined _AIX
  38. # define alloca __alloca
  39. #elif defined _MSC_VER
  40. # include <malloc.h>
  41. # define alloca _alloca
  42. #else
  43. # ifndef HAVE_ALLOCA
  44. # ifdef __cplusplus
  45. extern "C"
  46. # endif
  47. void *alloca (size_t);
  48. # endif
  49. #endif
  50. #ifdef __MINGW32__
  51. #include <windows.h>
  52. #include <io.h>
  53. static inline int fsync (int fd)
  54. {
  55. return (FlushFileBuffers ((HANDLE) _get_osfhandle (fd))) ? 0 : -1;
  56. }
  57. #ifndef EWOULDBLOCK
  58. # define EWOULDBLOCK EAGAIN
  59. #endif
  60. #ifndef MSG_DONTWAIT
  61. # define MSG_DONTWAIT 0x1000000
  62. #endif
  63. #endif /* __MINGW32__ */
  64. #if defined (__linux)
  65. #ifndef LINUX
  66. #define LINUX
  67. #endif
  68. #endif
  69. #ifdef WIN32
  70. #ifndef timersub
  71. #define timersub(a, b, result) \
  72. do { \
  73. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  74. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  75. if ((result)->tv_usec < 0) { \
  76. --(result)->tv_sec; \
  77. (result)->tv_usec += 1000000; \
  78. } \
  79. } while (0)
  80. #endif
  81. #ifndef timeradd
  82. # define timeradd(a, b, result) \
  83. do { \
  84. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  85. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  86. if ((result)->tv_usec >= 1000000) \
  87. { \
  88. ++(result)->tv_sec; \
  89. (result)->tv_usec -= 1000000; \
  90. } \
  91. } while (0)
  92. #endif
  93. #endif
  94. #ifdef HAVE_ADL
  95. #include "ADL_SDK/adl_sdk.h"
  96. #endif
  97. #ifdef HAVE_LIBUSB
  98. #include <libusb.h>
  99. #endif
  100. #ifdef USE_ZTEX
  101. #include "libztex.h"
  102. #endif
  103. #ifdef USE_USBUTILS
  104. #include "usbutils.h"
  105. #endif
  106. #if (!defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) \
  107. || (defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)))
  108. #ifndef bswap_16
  109. #define bswap_16 __builtin_bswap16
  110. #define bswap_32 __builtin_bswap32
  111. #define bswap_64 __builtin_bswap64
  112. #endif
  113. #else
  114. #if HAVE_BYTESWAP_H
  115. #include <byteswap.h>
  116. #elif defined(USE_SYS_ENDIAN_H)
  117. #include <sys/endian.h>
  118. #elif defined(__APPLE__)
  119. #include <libkern/OSByteOrder.h>
  120. #define bswap_16 OSSwapInt16
  121. #define bswap_32 OSSwapInt32
  122. #define bswap_64 OSSwapInt64
  123. #else
  124. #define bswap_16(value) \
  125. ((((value) & 0xff) << 8) | ((value) >> 8))
  126. #define bswap_32(value) \
  127. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  128. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  129. #define bswap_64(value) \
  130. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  131. << 32) | \
  132. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  133. #endif
  134. #endif /* !defined(__GLXBYTEORDER_H__) */
  135. /* This assumes htobe32 is a macro in endian.h, and if it doesn't exist, then
  136. * htobe64 also won't exist */
  137. #ifndef htobe32
  138. # if __BYTE_ORDER == __LITTLE_ENDIAN
  139. # define htole16(x) (x)
  140. # define htole32(x) (x)
  141. # define le32toh(x) (x)
  142. # define be32toh(x) bswap_32(x)
  143. # define be64toh(x) bswap_64(x)
  144. # define htobe32(x) bswap_32(x)
  145. # define htobe64(x) bswap_64(x)
  146. # elif __BYTE_ORDER == __BIG_ENDIAN
  147. # define htole16(x) bswap_16(x)
  148. # define htole32(x) bswap_32(x)
  149. # define le32toh(x) bswap_32(x)
  150. # define be32toh(x) (x)
  151. # define be64toh(x) (x)
  152. # define htobe32(x) (x)
  153. # define htobe64(x) (x)
  154. #else
  155. #error UNKNOWN BYTE ORDER
  156. #endif
  157. #endif
  158. #undef unlikely
  159. #undef likely
  160. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  161. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  162. #define likely(expr) (__builtin_expect(!!(expr), 1))
  163. #else
  164. #define unlikely(expr) (expr)
  165. #define likely(expr) (expr)
  166. #endif
  167. #define __maybe_unused __attribute__((unused))
  168. #define uninitialised_var(x) x = x
  169. #if defined(__i386__)
  170. #define WANT_CRYPTOPP_ASM32
  171. #endif
  172. #ifndef ARRAY_SIZE
  173. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  174. #endif
  175. #ifdef MIPSEB
  176. #ifndef roundl
  177. #define roundl(x) (long double)((long long)((x==0)?0.0:((x)+((x)>0)?0.5:-0.5)))
  178. #endif
  179. #endif
  180. #define MIN(x, y) ((x) > (y) ? (y) : (x))
  181. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  182. enum drv_driver {
  183. DRIVER_OPENCL = 0,
  184. DRIVER_ICARUS,
  185. DRIVER_BITFORCE,
  186. DRIVER_MODMINER,
  187. DRIVER_ZTEX,
  188. DRIVER_CPU,
  189. DRIVER_BFLSC,
  190. DRIVER_AVALON,
  191. DRIVER_MAX
  192. };
  193. enum alive {
  194. LIFE_WELL,
  195. LIFE_SICK,
  196. LIFE_DEAD,
  197. LIFE_NOSTART,
  198. LIFE_INIT,
  199. };
  200. enum pool_strategy {
  201. POOL_FAILOVER,
  202. POOL_ROUNDROBIN,
  203. POOL_ROTATE,
  204. POOL_LOADBALANCE,
  205. POOL_BALANCE,
  206. };
  207. #define TOP_STRATEGY (POOL_BALANCE)
  208. struct strategies {
  209. const char *s;
  210. };
  211. struct cgpu_info;
  212. #ifdef HAVE_ADL
  213. struct gpu_adl {
  214. ADLTemperature lpTemperature;
  215. int iAdapterIndex;
  216. int lpAdapterID;
  217. int iBusNumber;
  218. char strAdapterName[256];
  219. ADLPMActivity lpActivity;
  220. ADLODParameters lpOdParameters;
  221. ADLODPerformanceLevels *DefPerfLev;
  222. ADLFanSpeedInfo lpFanSpeedInfo;
  223. ADLFanSpeedValue lpFanSpeedValue;
  224. ADLFanSpeedValue DefFanSpeedValue;
  225. int iEngineClock;
  226. int iMemoryClock;
  227. int iVddc;
  228. int iPercentage;
  229. bool autofan;
  230. bool autoengine;
  231. bool managed; /* Were the values ever changed on this card */
  232. int lastengine;
  233. int lasttemp;
  234. int targetfan;
  235. int targettemp;
  236. int overtemp;
  237. int minspeed;
  238. int maxspeed;
  239. int gpu;
  240. bool has_fanspeed;
  241. struct gpu_adl *twin;
  242. };
  243. #endif
  244. extern void blank_get_statline_before(char *buf, struct cgpu_info __maybe_unused *cgpu);
  245. struct api_data;
  246. struct thr_info;
  247. struct work;
  248. struct device_drv {
  249. enum drv_driver drv_id;
  250. char *dname;
  251. char *name;
  252. // DRV-global functions
  253. void (*drv_detect)();
  254. // Device-specific functions
  255. void (*reinit_device)(struct cgpu_info *);
  256. void (*get_statline_before)(char *, struct cgpu_info *);
  257. void (*get_statline)(char *, struct cgpu_info *);
  258. struct api_data *(*get_api_stats)(struct cgpu_info *);
  259. bool (*get_stats)(struct cgpu_info *);
  260. void (*identify_device)(struct cgpu_info *); // e.g. to flash a led
  261. char *(*set_device)(struct cgpu_info *, char *option, char *setting, char *replybuf);
  262. // Thread-specific functions
  263. bool (*thread_prepare)(struct thr_info *);
  264. uint64_t (*can_limit_work)(struct thr_info *);
  265. bool (*thread_init)(struct thr_info *);
  266. bool (*prepare_work)(struct thr_info *, struct work *);
  267. /* Which hash work loop this driver uses. */
  268. void (*hash_work)(struct thr_info *);
  269. /* Two variants depending on whether the device divides work up into
  270. * small pieces or works with whole work items and may or may not have
  271. * a queue of its own. */
  272. int64_t (*scanhash)(struct thr_info *, struct work *, int64_t);
  273. int64_t (*scanwork)(struct thr_info *);
  274. /* Used to extract work from the hash table of queued work and tell
  275. * the main loop that it should not add any further work to the table.
  276. */
  277. bool (*queue_full)(struct cgpu_info *);
  278. void (*flush_work)(struct cgpu_info *);
  279. void (*hw_error)(struct thr_info *);
  280. void (*thread_shutdown)(struct thr_info *);
  281. void (*thread_enable)(struct thr_info *);
  282. // Does it need to be free()d?
  283. bool copy;
  284. /* Highest target diff the device supports */
  285. double max_diff;
  286. };
  287. extern struct device_drv *copy_drv(struct device_drv*);
  288. enum dev_enable {
  289. DEV_ENABLED,
  290. DEV_DISABLED,
  291. DEV_RECOVER,
  292. };
  293. enum cl_kernels {
  294. KL_NONE,
  295. KL_POCLBM,
  296. KL_PHATK,
  297. KL_DIAKGCN,
  298. KL_DIABLO,
  299. KL_SCRYPT,
  300. };
  301. enum dev_reason {
  302. REASON_THREAD_FAIL_INIT,
  303. REASON_THREAD_ZERO_HASH,
  304. REASON_THREAD_FAIL_QUEUE,
  305. REASON_DEV_SICK_IDLE_60,
  306. REASON_DEV_DEAD_IDLE_600,
  307. REASON_DEV_NOSTART,
  308. REASON_DEV_OVER_HEAT,
  309. REASON_DEV_THERMAL_CUTOFF,
  310. REASON_DEV_COMMS_ERROR,
  311. REASON_DEV_THROTTLE,
  312. };
  313. #define REASON_NONE "None"
  314. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  315. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  316. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  317. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  318. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  319. #define REASON_DEV_NOSTART_STR "Device failed to start"
  320. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  321. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  322. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  323. #define REASON_DEV_THROTTLE_STR "Device throttle"
  324. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  325. #define MIN_SEC_UNSET 99999999
  326. struct cgminer_stats {
  327. uint32_t getwork_calls;
  328. struct timeval getwork_wait;
  329. struct timeval getwork_wait_max;
  330. struct timeval getwork_wait_min;
  331. };
  332. // Just the actual network getworks to the pool
  333. struct cgminer_pool_stats {
  334. uint32_t getwork_calls;
  335. uint32_t getwork_attempts;
  336. struct timeval getwork_wait;
  337. struct timeval getwork_wait_max;
  338. struct timeval getwork_wait_min;
  339. double getwork_wait_rolling;
  340. bool hadrolltime;
  341. bool canroll;
  342. bool hadexpire;
  343. uint32_t rolltime;
  344. double min_diff;
  345. double max_diff;
  346. double last_diff;
  347. uint32_t min_diff_count;
  348. uint32_t max_diff_count;
  349. uint64_t times_sent;
  350. uint64_t bytes_sent;
  351. uint64_t net_bytes_sent;
  352. uint64_t times_received;
  353. uint64_t bytes_received;
  354. uint64_t net_bytes_received;
  355. };
  356. struct cgpu_info {
  357. int cgminer_id;
  358. struct device_drv *drv;
  359. int device_id;
  360. char *name;
  361. char *device_path;
  362. FILE *device_file;
  363. union {
  364. #ifdef USE_ZTEX
  365. struct libztex_device *device_ztex;
  366. #endif
  367. #ifdef USE_USBUTILS
  368. struct cg_usb_device *usbdev;
  369. #endif
  370. #if defined(USE_ICARUS) || defined(USE_AVALON)
  371. int device_fd;
  372. #endif
  373. };
  374. #ifdef USE_AVALON
  375. struct work **works;
  376. int work_array;
  377. int queued;
  378. int results;
  379. #endif
  380. #ifdef USE_USBUTILS
  381. struct cg_usb_info usbinfo;
  382. #endif
  383. #ifdef USE_MODMINER
  384. char fpgaid;
  385. unsigned char clock;
  386. pthread_mutex_t *modminer_mutex;
  387. #endif
  388. #ifdef USE_BITFORCE
  389. struct timeval work_start_tv;
  390. unsigned int wait_ms;
  391. unsigned int sleep_ms;
  392. double avg_wait_f;
  393. unsigned int avg_wait_d;
  394. uint32_t nonces;
  395. bool nonce_range;
  396. bool polling;
  397. bool flash_led;
  398. pthread_mutex_t device_mutex;
  399. #endif
  400. enum dev_enable deven;
  401. int accepted;
  402. int rejected;
  403. int hw_errors;
  404. double rolling;
  405. double total_mhashes;
  406. double utility;
  407. enum alive status;
  408. char init[40];
  409. struct timeval last_message_tv;
  410. int threads;
  411. struct thr_info **thr;
  412. int64_t max_hashes;
  413. const char *kname;
  414. #ifdef HAVE_OPENCL
  415. bool mapped;
  416. int virtual_gpu;
  417. int virtual_adl;
  418. int intensity;
  419. bool dynamic;
  420. cl_uint vwidth;
  421. size_t work_size;
  422. enum cl_kernels kernel;
  423. cl_ulong max_alloc;
  424. #ifdef USE_SCRYPT
  425. int opt_lg, lookup_gap;
  426. size_t opt_tc, thread_concurrency;
  427. size_t shaders;
  428. #endif
  429. struct timeval tv_gpustart;
  430. int intervals;
  431. #endif
  432. bool new_work;
  433. float temp;
  434. int cutofftemp;
  435. #ifdef HAVE_ADL
  436. bool has_adl;
  437. struct gpu_adl adl;
  438. int gpu_engine;
  439. int min_engine;
  440. int gpu_fan;
  441. int min_fan;
  442. int gpu_memclock;
  443. int gpu_memdiff;
  444. int gpu_powertune;
  445. float gpu_vddc;
  446. #endif
  447. int diff1;
  448. double diff_accepted;
  449. double diff_rejected;
  450. int last_share_pool;
  451. time_t last_share_pool_time;
  452. double last_share_diff;
  453. time_t last_device_valid_work;
  454. time_t device_last_well;
  455. time_t device_last_not_well;
  456. enum dev_reason device_not_well_reason;
  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. pthread_rwlock_t qlock;
  469. struct work *queued_work;
  470. unsigned int queued_count;
  471. };
  472. extern bool add_cgpu(struct cgpu_info*);
  473. struct thread_q {
  474. struct list_head q;
  475. bool frozen;
  476. pthread_mutex_t mutex;
  477. pthread_cond_t cond;
  478. };
  479. struct thr_info {
  480. int id;
  481. int device_thread;
  482. bool primary_thread;
  483. pthread_t pth;
  484. struct thread_q *q;
  485. struct cgpu_info *cgpu;
  486. void *cgpu_data;
  487. struct timeval last;
  488. struct timeval sick;
  489. bool pause;
  490. bool getwork;
  491. double rolling;
  492. bool work_restart;
  493. };
  494. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  495. extern void thr_info_cancel(struct thr_info *thr);
  496. extern void thr_info_freeze(struct thr_info *thr);
  497. extern void nmsleep(unsigned int msecs);
  498. extern double us_tdiff(struct timeval *end, struct timeval *start);
  499. extern double tdiff(struct timeval *end, struct timeval *start);
  500. struct string_elist {
  501. char *string;
  502. bool free_me;
  503. struct list_head list;
  504. };
  505. static inline void string_elist_add(const char *s, struct list_head *head)
  506. {
  507. struct string_elist *n;
  508. n = calloc(1, sizeof(*n));
  509. n->string = strdup(s);
  510. n->free_me = true;
  511. list_add_tail(&n->list, head);
  512. }
  513. static inline void string_elist_del(struct string_elist *item)
  514. {
  515. if (item->free_me)
  516. free(item->string);
  517. list_del(&item->list);
  518. }
  519. static inline uint32_t swab32(uint32_t v)
  520. {
  521. return bswap_32(v);
  522. }
  523. static inline void swap256(void *dest_p, const void *src_p)
  524. {
  525. uint32_t *dest = dest_p;
  526. const uint32_t *src = src_p;
  527. dest[0] = src[7];
  528. dest[1] = src[6];
  529. dest[2] = src[5];
  530. dest[3] = src[4];
  531. dest[4] = src[3];
  532. dest[5] = src[2];
  533. dest[6] = src[1];
  534. dest[7] = src[0];
  535. }
  536. static inline void swab256(void *dest_p, const void *src_p)
  537. {
  538. uint32_t *dest = dest_p;
  539. const uint32_t *src = src_p;
  540. dest[0] = swab32(src[7]);
  541. dest[1] = swab32(src[6]);
  542. dest[2] = swab32(src[5]);
  543. dest[3] = swab32(src[4]);
  544. dest[4] = swab32(src[3]);
  545. dest[5] = swab32(src[2]);
  546. dest[6] = swab32(src[1]);
  547. dest[7] = swab32(src[0]);
  548. }
  549. static inline void flip32(void *dest_p, const void *src_p)
  550. {
  551. uint32_t *dest = dest_p;
  552. const uint32_t *src = src_p;
  553. int i;
  554. for (i = 0; i < 8; i++)
  555. dest[i] = swab32(src[i]);
  556. }
  557. static inline void flip80(void *dest_p, const void *src_p)
  558. {
  559. uint32_t *dest = dest_p;
  560. const uint32_t *src = src_p;
  561. int i;
  562. for (i = 0; i < 20; i++)
  563. dest[i] = swab32(src[i]);
  564. }
  565. extern void quit(int status, const char *format, ...);
  566. static inline void mutex_lock(pthread_mutex_t *lock)
  567. {
  568. if (unlikely(pthread_mutex_lock(lock)))
  569. quit(1, "WTF MUTEX ERROR ON LOCK!");
  570. }
  571. static inline void mutex_unlock(pthread_mutex_t *lock)
  572. {
  573. if (unlikely(pthread_mutex_unlock(lock)))
  574. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  575. }
  576. static inline int mutex_trylock(pthread_mutex_t *lock)
  577. {
  578. return pthread_mutex_trylock(lock);
  579. }
  580. static inline void wr_lock(pthread_rwlock_t *lock)
  581. {
  582. if (unlikely(pthread_rwlock_wrlock(lock)))
  583. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  584. }
  585. static inline void rd_lock(pthread_rwlock_t *lock)
  586. {
  587. if (unlikely(pthread_rwlock_rdlock(lock)))
  588. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  589. }
  590. static inline void rw_unlock(pthread_rwlock_t *lock)
  591. {
  592. if (unlikely(pthread_rwlock_unlock(lock)))
  593. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  594. }
  595. static inline void rd_unlock(pthread_rwlock_t *lock)
  596. {
  597. rw_unlock(lock);
  598. }
  599. static inline void wr_unlock(pthread_rwlock_t *lock)
  600. {
  601. rw_unlock(lock);
  602. }
  603. static inline void mutex_init(pthread_mutex_t *lock)
  604. {
  605. if (unlikely(pthread_mutex_init(lock, NULL)))
  606. quit(1, "Failed to pthread_mutex_init");
  607. }
  608. static inline void rwlock_init(pthread_rwlock_t *lock)
  609. {
  610. if (unlikely(pthread_rwlock_init(lock, NULL)))
  611. quit(1, "Failed to pthread_rwlock_init");
  612. }
  613. /* cgminer locks, a write biased variant of rwlocks */
  614. struct cglock {
  615. pthread_mutex_t mutex;
  616. pthread_rwlock_t rwlock;
  617. };
  618. typedef struct cglock cglock_t;
  619. static inline void cglock_init(cglock_t *lock)
  620. {
  621. mutex_init(&lock->mutex);
  622. rwlock_init(&lock->rwlock);
  623. }
  624. /* Read lock variant of cglock */
  625. static inline void cg_rlock(cglock_t *lock)
  626. {
  627. mutex_lock(&lock->mutex);
  628. rd_lock(&lock->rwlock);
  629. mutex_unlock(&lock->mutex);
  630. }
  631. /* Intermediate variant of cglock */
  632. static inline void cg_ilock(cglock_t *lock)
  633. {
  634. mutex_lock(&lock->mutex);
  635. }
  636. /* Upgrade intermediate variant to a write lock */
  637. static inline void cg_ulock(cglock_t *lock)
  638. {
  639. wr_lock(&lock->rwlock);
  640. }
  641. /* Write lock variant of cglock */
  642. static inline void cg_wlock(cglock_t *lock)
  643. {
  644. mutex_lock(&lock->mutex);
  645. wr_lock(&lock->rwlock);
  646. }
  647. /* Downgrade intermediate variant to a read lock */
  648. static inline void cg_dlock(cglock_t *lock)
  649. {
  650. rd_lock(&lock->rwlock);
  651. mutex_unlock(&lock->mutex);
  652. }
  653. static inline void cg_runlock(cglock_t *lock)
  654. {
  655. rd_unlock(&lock->rwlock);
  656. }
  657. static inline void cg_wunlock(cglock_t *lock)
  658. {
  659. wr_unlock(&lock->rwlock);
  660. mutex_unlock(&lock->mutex);
  661. }
  662. struct pool;
  663. extern bool opt_protocol;
  664. extern bool have_longpoll;
  665. extern char *opt_kernel_path;
  666. extern char *opt_socks_proxy;
  667. extern char *cgminer_path;
  668. extern bool opt_fail_only;
  669. extern bool opt_autofan;
  670. extern bool opt_autoengine;
  671. extern bool use_curses;
  672. extern char *opt_api_allow;
  673. extern char *opt_api_groups;
  674. extern char *opt_api_description;
  675. extern int opt_api_port;
  676. extern bool opt_api_listen;
  677. extern bool opt_api_network;
  678. extern bool opt_delaynet;
  679. extern bool opt_restart;
  680. extern char *opt_icarus_options;
  681. extern char *opt_icarus_timing;
  682. extern bool opt_worktime;
  683. #ifdef USE_AVALON
  684. extern char *opt_avalon_options;
  685. #endif
  686. #ifdef USE_USBUTILS
  687. extern char *opt_usb_select;
  688. extern int opt_usbdump;
  689. extern bool opt_usb_list_all;
  690. #endif
  691. #ifdef USE_BITFORCE
  692. extern bool opt_bfl_noncerange;
  693. #endif
  694. extern bool ping;
  695. extern int swork_id;
  696. extern pthread_rwlock_t netacc_lock;
  697. extern const uint32_t sha256_init_state[];
  698. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  699. const char *rpc_req, bool, bool, int *,
  700. struct pool *pool, bool);
  701. extern const char *proxytype(curl_proxytype proxytype);
  702. extern char *get_proxy(char *url, struct pool *pool);
  703. extern char *bin2hex(const unsigned char *p, size_t len);
  704. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  705. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  706. unsigned char *pdata,
  707. unsigned char *phash1, unsigned char *phash,
  708. const unsigned char *ptarget,
  709. uint32_t max_nonce,
  710. uint32_t *last_nonce,
  711. uint32_t nonce);
  712. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  713. extern int opt_queue;
  714. extern int opt_scantime;
  715. extern int opt_expiry;
  716. #ifdef USE_USBUTILS
  717. extern pthread_mutex_t cgusb_lock;
  718. #endif
  719. extern cglock_t control_lock;
  720. extern pthread_mutex_t hash_lock;
  721. extern pthread_mutex_t console_lock;
  722. extern cglock_t ch_lock;
  723. extern pthread_rwlock_t mining_thr_lock;
  724. extern pthread_rwlock_t devices_lock;
  725. extern pthread_mutex_t restart_lock;
  726. extern pthread_cond_t restart_cond;
  727. extern void thread_reportin(struct thr_info *thr);
  728. extern void clear_stratum_shares(struct pool *pool);
  729. extern int restart_wait(unsigned int mstime);
  730. extern void kill_work(void);
  731. extern void reinit_device(struct cgpu_info *cgpu);
  732. #ifdef HAVE_ADL
  733. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  734. extern int set_fanspeed(int gpu, int iFanSpeed);
  735. extern int set_vddc(int gpu, float fVddc);
  736. extern int set_engineclock(int gpu, int iEngineClock);
  737. extern int set_memoryclock(int gpu, int iMemoryClock);
  738. #endif
  739. extern void api(int thr_id);
  740. extern struct pool *current_pool(void);
  741. extern int enabled_pools;
  742. extern bool detect_stratum(struct pool *pool, char *url);
  743. extern void print_summary(void);
  744. extern struct pool *add_pool(void);
  745. extern bool add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  746. #define MAX_GPUDEVICES 16
  747. #define MIN_INTENSITY -10
  748. #define _MIN_INTENSITY_STR "-10"
  749. #ifdef USE_SCRYPT
  750. #define MAX_INTENSITY 20
  751. #define _MAX_INTENSITY_STR "20"
  752. #else
  753. #define MAX_INTENSITY 14
  754. #define _MAX_INTENSITY_STR "14"
  755. #endif
  756. extern bool hotplug_mode;
  757. extern int hotplug_time;
  758. extern struct list_head scan_devices;
  759. extern int nDevs;
  760. extern int opt_n_threads;
  761. extern int num_processors;
  762. extern int hw_errors;
  763. extern bool use_syslog;
  764. extern bool opt_quiet;
  765. extern struct thr_info *control_thr;
  766. extern struct thr_info **mining_thr;
  767. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  768. extern int gpu_threads;
  769. #ifdef USE_SCRYPT
  770. extern bool opt_scrypt;
  771. #else
  772. #define opt_scrypt (0)
  773. #endif
  774. extern double total_secs;
  775. extern int mining_threads;
  776. extern struct cgpu_info *cpus;
  777. extern int total_devices;
  778. extern struct cgpu_info **devices;
  779. extern int total_pools;
  780. extern struct pool **pools;
  781. extern const char *algo_names[];
  782. extern enum sha256_algos opt_algo;
  783. extern struct strategies strategies[];
  784. extern enum pool_strategy pool_strategy;
  785. extern int opt_rotate_period;
  786. extern double total_mhashes_done;
  787. extern unsigned int new_blocks;
  788. extern unsigned int found_blocks;
  789. extern int total_accepted, total_rejected, total_diff1;;
  790. extern int total_getworks, total_stale, total_discarded;
  791. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  792. extern unsigned int local_work;
  793. extern unsigned int total_go, total_ro;
  794. extern const int opt_cutofftemp;
  795. extern int opt_log_interval;
  796. extern unsigned long long global_hashrate;
  797. extern char *current_fullhash;
  798. extern double current_diff;
  799. extern uint64_t best_diff;
  800. extern struct timeval block_timeval;
  801. #ifdef HAVE_OPENCL
  802. typedef struct {
  803. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  804. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  805. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  806. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  807. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  808. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  809. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  810. cl_uint W16; cl_uint W17; cl_uint W2;
  811. cl_uint PreVal4; cl_uint T1;
  812. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  813. cl_uint PreVal4addT1; cl_uint T1substate0;
  814. cl_uint PreVal4_2;
  815. cl_uint PreVal0;
  816. cl_uint PreW18;
  817. cl_uint PreW19;
  818. cl_uint PreW31;
  819. cl_uint PreW32;
  820. /* For diakgcn */
  821. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  822. cl_uint zeroA, zeroB;
  823. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  824. #ifdef USE_SCRYPT
  825. struct work *work;
  826. #endif
  827. } dev_blk_ctx;
  828. #else
  829. typedef struct {
  830. uint32_t nonce;
  831. } dev_blk_ctx;
  832. #endif
  833. struct curl_ent {
  834. CURL *curl;
  835. struct list_head node;
  836. struct timeval tv;
  837. };
  838. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  839. * equal disabled */
  840. enum pool_enable {
  841. POOL_DISABLED,
  842. POOL_ENABLED,
  843. POOL_REJECTING,
  844. };
  845. struct stratum_work {
  846. char *job_id;
  847. char *prev_hash;
  848. char *coinbase1;
  849. char *coinbase2;
  850. char **merkle;
  851. char *bbversion;
  852. char *nbit;
  853. char *ntime;
  854. bool clean;
  855. size_t cb1_len;
  856. size_t cb2_len;
  857. size_t cb_len;
  858. size_t header_len;
  859. int merkles;
  860. double diff;
  861. };
  862. #define RBUFSIZE 8192
  863. #define RECVSIZE (RBUFSIZE - 4)
  864. struct pool {
  865. int pool_no;
  866. int prio;
  867. int accepted, rejected;
  868. int seq_rejects;
  869. int seq_getfails;
  870. int solved;
  871. int diff1;
  872. char diff[8];
  873. double diff_accepted;
  874. double diff_rejected;
  875. double diff_stale;
  876. bool submit_fail;
  877. bool idle;
  878. bool lagging;
  879. bool probed;
  880. enum pool_enable enabled;
  881. bool submit_old;
  882. bool removed;
  883. bool lp_started;
  884. char *hdr_path;
  885. char *lp_url;
  886. unsigned int getwork_requested;
  887. unsigned int stale_shares;
  888. unsigned int discarded_work;
  889. unsigned int getfail_occasions;
  890. unsigned int remotefail_occasions;
  891. struct timeval tv_idle;
  892. double utility;
  893. int last_shares, shares;
  894. char *rpc_req;
  895. char *rpc_url;
  896. char *rpc_userpass;
  897. char *rpc_user, *rpc_pass;
  898. curl_proxytype rpc_proxytype;
  899. char *rpc_proxy;
  900. pthread_mutex_t pool_lock;
  901. cglock_t data_lock;
  902. struct thread_q *submit_q;
  903. struct thread_q *getwork_q;
  904. pthread_t longpoll_thread;
  905. pthread_t test_thread;
  906. bool testing;
  907. int curls;
  908. pthread_cond_t cr_cond;
  909. struct list_head curlring;
  910. time_t last_share_time;
  911. double last_share_diff;
  912. uint64_t best_diff;
  913. struct cgminer_stats cgminer_stats;
  914. struct cgminer_pool_stats cgminer_pool_stats;
  915. /* Stratum variables */
  916. char *stratum_url;
  917. char *stratum_port;
  918. CURL *stratum_curl;
  919. SOCKETTYPE sock;
  920. char *sockbuf;
  921. size_t sockbuf_size;
  922. char *sockaddr_url; /* stripped url used for sockaddr */
  923. char *nonce1;
  924. size_t n1_len;
  925. uint32_t nonce2;
  926. int n2size;
  927. char *sessionid;
  928. bool has_stratum;
  929. bool stratum_active;
  930. bool stratum_init;
  931. bool stratum_notify;
  932. struct stratum_work swork;
  933. pthread_t stratum_thread;
  934. pthread_mutex_t stratum_lock;
  935. int sshares; /* stratum shares submitted waiting on response */
  936. /* GBT variables */
  937. bool has_gbt;
  938. cglock_t gbt_lock;
  939. unsigned char previousblockhash[32];
  940. unsigned char gbt_target[32];
  941. char *coinbasetxn;
  942. char *longpollid;
  943. char *gbt_workid;
  944. int gbt_expires;
  945. uint32_t gbt_version;
  946. uint32_t curtime;
  947. uint32_t gbt_bits;
  948. unsigned char *gbt_coinbase;
  949. unsigned char *txn_hashes;
  950. int gbt_txns;
  951. int coinbase_len;
  952. struct timeval tv_lastwork;
  953. };
  954. #define GETWORK_MODE_TESTPOOL 'T'
  955. #define GETWORK_MODE_POOL 'P'
  956. #define GETWORK_MODE_LP 'L'
  957. #define GETWORK_MODE_BENCHMARK 'B'
  958. #define GETWORK_MODE_STRATUM 'S'
  959. #define GETWORK_MODE_GBT 'G'
  960. struct work {
  961. unsigned char data[128];
  962. unsigned char midstate[32];
  963. unsigned char target[32];
  964. unsigned char hash[32];
  965. double device_diff;
  966. uint64_t share_diff;
  967. int rolls;
  968. dev_blk_ctx blk;
  969. struct thr_info *thr;
  970. int thr_id;
  971. struct pool *pool;
  972. struct timeval tv_staged;
  973. bool mined;
  974. bool clone;
  975. bool cloned;
  976. int rolltime;
  977. bool longpoll;
  978. bool stale;
  979. bool mandatory;
  980. bool block;
  981. bool queued;
  982. bool stratum;
  983. char *job_id;
  984. char *nonce2;
  985. char *ntime;
  986. double sdiff;
  987. char *nonce1;
  988. bool gbt;
  989. char *gbt_coinbase;
  990. int gbt_txns;
  991. unsigned int work_block;
  992. int id;
  993. UT_hash_handle hh;
  994. double work_difficulty;
  995. // Allow devices to identify work if multiple sub-devices
  996. int subid;
  997. // Allow devices to flag work for their own purposes
  998. bool devflag;
  999. struct timeval tv_getwork;
  1000. struct timeval tv_getwork_reply;
  1001. struct timeval tv_cloned;
  1002. struct timeval tv_work_start;
  1003. struct timeval tv_work_found;
  1004. char getwork_mode;
  1005. };
  1006. #ifdef USE_MODMINER
  1007. struct modminer_fpga_state {
  1008. bool work_running;
  1009. struct work running_work;
  1010. struct timeval tv_workstart;
  1011. uint32_t hashes;
  1012. char next_work_cmd[46];
  1013. char fpgaid;
  1014. bool overheated;
  1015. bool new_work;
  1016. uint32_t shares;
  1017. uint32_t shares_last_hw;
  1018. uint32_t hw_errors;
  1019. uint32_t shares_to_good;
  1020. uint32_t timeout_fail;
  1021. uint32_t success_more;
  1022. struct timeval last_changed;
  1023. struct timeval last_nonce;
  1024. struct timeval first_work;
  1025. bool death_stage_one;
  1026. bool tried_two_byte_temp;
  1027. bool one_byte_temp;
  1028. };
  1029. #endif
  1030. extern void get_datestamp(char *, struct timeval *);
  1031. extern void inc_hw_errors(struct thr_info *thr);
  1032. extern void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  1033. extern struct work *get_queued(struct cgpu_info *cgpu);
  1034. extern struct work *__find_work_bymidstate(struct work *que, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1035. extern struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1036. extern void work_completed(struct cgpu_info *cgpu, struct work *work);
  1037. extern void hash_queued_work(struct thr_info *mythr);
  1038. extern void tailsprintf(char *f, const char *fmt, ...);
  1039. extern void wlogprint(const char *f, ...);
  1040. extern int curses_int(const char *query);
  1041. extern char *curses_input(const char *query);
  1042. extern void kill_work(void);
  1043. extern void switch_pools(struct pool *selected);
  1044. extern void discard_work(struct work *work);
  1045. extern void remove_pool(struct pool *pool);
  1046. extern void write_config(FILE *fcfg);
  1047. extern void zero_bestshare(void);
  1048. extern void zero_stats(void);
  1049. extern void default_save_file(char *filename);
  1050. extern bool log_curses_only(int prio, const char *f, va_list ap);
  1051. extern void clear_logwin(void);
  1052. extern bool pool_tclear(struct pool *pool, bool *var);
  1053. extern struct thread_q *tq_new(void);
  1054. extern void tq_free(struct thread_q *tq);
  1055. extern bool tq_push(struct thread_q *tq, void *data);
  1056. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  1057. extern void tq_freeze(struct thread_q *tq);
  1058. extern void tq_thaw(struct thread_q *tq);
  1059. extern bool successful_connect;
  1060. extern void adl(void);
  1061. extern void app_restart(void);
  1062. extern void clean_work(struct work *work);
  1063. extern void free_work(struct work *work);
  1064. extern void __copy_work(struct work *work, struct work *base_work);
  1065. extern struct work *copy_work(struct work *base_work);
  1066. extern struct thr_info *get_thread(int thr_id);
  1067. extern struct cgpu_info *get_devices(int id);
  1068. enum api_data_type {
  1069. API_ESCAPE,
  1070. API_STRING,
  1071. API_CONST,
  1072. API_INT,
  1073. API_UINT,
  1074. API_UINT32,
  1075. API_UINT64,
  1076. API_DOUBLE,
  1077. API_ELAPSED,
  1078. API_BOOL,
  1079. API_TIMEVAL,
  1080. API_TIME,
  1081. API_MHS,
  1082. API_MHTOTAL,
  1083. API_TEMP,
  1084. API_UTILITY,
  1085. API_FREQ,
  1086. API_VOLTS,
  1087. API_HS,
  1088. API_DIFF
  1089. };
  1090. struct api_data {
  1091. enum api_data_type type;
  1092. char *name;
  1093. void *data;
  1094. bool data_was_malloc;
  1095. struct api_data *prev;
  1096. struct api_data *next;
  1097. };
  1098. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  1099. extern struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data);
  1100. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  1101. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  1102. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  1103. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  1104. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  1105. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  1106. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  1107. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  1108. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  1109. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  1110. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  1111. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  1112. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  1113. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  1114. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  1115. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  1116. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  1117. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  1118. #endif /* __MINER_H__ */