miner.h 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  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. LIFE_INIT2, // Still initializing, but safe to call functions
  189. };
  190. enum pool_strategy {
  191. POOL_FAILOVER,
  192. POOL_ROUNDROBIN,
  193. POOL_ROTATE,
  194. POOL_LOADBALANCE,
  195. POOL_BALANCE,
  196. };
  197. #define TOP_STRATEGY (POOL_BALANCE)
  198. struct strategies {
  199. const char *s;
  200. };
  201. struct cgpu_info;
  202. #ifdef HAVE_ADL
  203. struct gpu_adl {
  204. ADLTemperature lpTemperature;
  205. int iAdapterIndex;
  206. int lpAdapterID;
  207. int iBusNumber;
  208. char strAdapterName[256];
  209. ADLPMActivity lpActivity;
  210. ADLODParameters lpOdParameters;
  211. ADLODPerformanceLevels *DefPerfLev;
  212. ADLFanSpeedInfo lpFanSpeedInfo;
  213. ADLFanSpeedValue lpFanSpeedValue;
  214. ADLFanSpeedValue DefFanSpeedValue;
  215. int iEngineClock;
  216. int iMemoryClock;
  217. int iVddc;
  218. int iPercentage;
  219. bool autofan;
  220. bool autoengine;
  221. bool managed; /* Were the values ever changed on this card */
  222. int lastengine;
  223. int lasttemp;
  224. int targetfan;
  225. int overtemp;
  226. int minspeed;
  227. int maxspeed;
  228. int gpu;
  229. bool has_fanspeed;
  230. struct gpu_adl *twin;
  231. };
  232. #endif
  233. struct api_data;
  234. struct thr_info;
  235. struct work;
  236. struct device_api {
  237. const char *dname;
  238. const char *name;
  239. // API-global functions
  240. void (*api_detect)();
  241. // Device-specific functions
  242. void (*get_dev_statline_before)(char *, struct cgpu_info *);
  243. void (*get_dev_statline_after)(char *, struct cgpu_info *);
  244. // Processor-specific functions
  245. void (*reinit_device)(struct cgpu_info *);
  246. void (*get_statline_before)(char *, struct cgpu_info *);
  247. void (*get_statline)(char *, struct cgpu_info *);
  248. struct api_data* (*get_api_extra_device_detail)(struct cgpu_info *);
  249. struct api_data* (*get_api_extra_device_status)(struct cgpu_info *);
  250. struct api_data *(*get_api_stats)(struct cgpu_info *);
  251. bool (*get_stats)(struct cgpu_info *);
  252. bool (*identify_device)(struct cgpu_info *); // e.g. to flash a led
  253. char *(*set_device)(struct cgpu_info *, char *option, char *setting, char *replybuf);
  254. // Thread-specific functions
  255. bool (*thread_prepare)(struct thr_info *);
  256. void (*minerloop)(struct thr_info *);
  257. uint64_t (*can_limit_work)(struct thr_info *);
  258. bool (*thread_init)(struct thr_info *);
  259. bool (*prepare_work)(struct thr_info *, struct work *);
  260. #ifdef USE_AVALON
  261. int64_t (*scanhash_queue)(struct thr_info *, struct work **, int64_t);
  262. #endif
  263. int64_t (*scanhash)(struct thr_info *, struct work *, int64_t);
  264. void (*hw_error)(struct thr_info *);
  265. void (*thread_shutdown)(struct thr_info *);
  266. void (*thread_enable)(struct thr_info *);
  267. // Can be used per-thread or per-processor (only with minerloop async or queue!)
  268. void (*poll)(struct thr_info *);
  269. // === Implemented by minerloop_async ===
  270. bool (*job_prepare)(struct thr_info*, struct work*, uint64_t);
  271. void (*job_start)(struct thr_info*);
  272. void (*job_get_results)(struct thr_info*, struct work*);
  273. int64_t (*job_process_results)(struct thr_info*, struct work*, bool stopping);
  274. // === Implemented by minerloop_queue ===
  275. bool (*queue_append)(struct thr_info *, struct work *);
  276. void (*queue_flush)(struct thr_info *);
  277. };
  278. enum dev_enable {
  279. DEV_ENABLED,
  280. DEV_DISABLED,
  281. DEV_RECOVER,
  282. DEV_RECOVER_ERR,
  283. };
  284. enum cl_kernels {
  285. KL_NONE,
  286. KL_POCLBM,
  287. KL_PHATK,
  288. KL_DIAKGCN,
  289. KL_DIABLO,
  290. KL_SCRYPT,
  291. };
  292. enum dev_reason {
  293. REASON_THREAD_FAIL_INIT,
  294. REASON_THREAD_ZERO_HASH,
  295. REASON_THREAD_FAIL_QUEUE,
  296. REASON_DEV_SICK_IDLE_60,
  297. REASON_DEV_DEAD_IDLE_600,
  298. REASON_DEV_NOSTART,
  299. REASON_DEV_OVER_HEAT,
  300. REASON_DEV_THERMAL_CUTOFF,
  301. REASON_DEV_COMMS_ERROR,
  302. REASON_DEV_THROTTLE,
  303. };
  304. #define REASON_NONE "None"
  305. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  306. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  307. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  308. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  309. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  310. #define REASON_DEV_NOSTART_STR "Device failed to start"
  311. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  312. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  313. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  314. #define REASON_DEV_THROTTLE_STR "Device throttle"
  315. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  316. #define MIN_SEC_UNSET 99999999
  317. enum {
  318. MSG_NOPOOL = 8,
  319. MSG_MISPID = 25,
  320. MSG_INVPID = 26,
  321. MSG_DUPPID = 74,
  322. MSG_POOLPRIO = 73,
  323. };
  324. struct cgminer_stats {
  325. struct timeval start_tv;
  326. uint32_t getwork_calls;
  327. struct timeval getwork_wait;
  328. struct timeval getwork_wait_max;
  329. struct timeval getwork_wait_min;
  330. struct timeval _get_start;
  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. #define PRIprepr "-6s"
  357. #define PRIpreprv "s"
  358. struct cgpu_info {
  359. int cgminer_id;
  360. int device_line_id;
  361. const struct device_api *api;
  362. const char *devtype;
  363. int device_id;
  364. char *dev_repr;
  365. char *dev_repr_ns;
  366. const char *name;
  367. int procs;
  368. int proc_id;
  369. char proc_repr[8];
  370. char proc_repr_ns[8];
  371. struct cgpu_info *device;
  372. struct cgpu_info *next_proc;
  373. const char *device_path;
  374. FILE *device_file;
  375. union {
  376. #ifdef USE_ZTEX
  377. struct libztex_device *device_ztex;
  378. #endif
  379. int device_fd;
  380. #ifdef USE_X6500
  381. struct ft232r_device_handle *device_ft232r;
  382. #endif
  383. };
  384. #ifdef USE_BITFORCE
  385. struct timeval work_start_tv;
  386. unsigned int wait_ms;
  387. unsigned int sleep_ms;
  388. double avg_wait_f;
  389. unsigned int avg_wait_d;
  390. uint32_t nonces;
  391. bool polling;
  392. #endif
  393. #if defined(USE_BITFORCE) || defined(USE_ICARUS)
  394. bool flash_led;
  395. #endif
  396. void *cgpu_data;
  397. pthread_mutex_t device_mutex;
  398. pthread_cond_t device_cond;
  399. enum dev_enable deven;
  400. int accepted;
  401. int rejected;
  402. int hw_errors;
  403. double rolling;
  404. double total_mhashes;
  405. double utility;
  406. double utility_diff1;
  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. float temp;
  433. int cutofftemp;
  434. int targettemp;
  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. float reinit_backoff;
  458. int thread_fail_init_count;
  459. int thread_zero_hash_count;
  460. int thread_fail_queue_count;
  461. int dev_sick_idle_60_count;
  462. int dev_dead_idle_600_count;
  463. int dev_nostart_count;
  464. int dev_over_heat_count; // It's a warning but worth knowing
  465. int dev_thermal_cutoff_count;
  466. int dev_comms_error_count;
  467. int dev_throttle_count;
  468. struct cgminer_stats cgminer_stats;
  469. };
  470. extern void renumber_cgpu(struct cgpu_info *);
  471. extern bool add_cgpu(struct cgpu_info*);
  472. struct thread_q {
  473. struct list_head q;
  474. bool frozen;
  475. pthread_mutex_t mutex;
  476. pthread_cond_t cond;
  477. };
  478. enum thr_busy_state {
  479. TBS_IDLE,
  480. TBS_GETTING_RESULTS,
  481. TBS_STARTING_JOB,
  482. };
  483. struct thr_info {
  484. int id;
  485. int device_thread;
  486. bool primary_thread;
  487. pthread_t pth;
  488. struct thread_q *q;
  489. struct cgpu_info *cgpu;
  490. void *cgpu_data;
  491. struct timeval last;
  492. struct timeval sick;
  493. bool scanhash_working;
  494. uint64_t hashes_done;
  495. struct timeval tv_hashes_done;
  496. struct timeval tv_lastupdate;
  497. bool pause;
  498. time_t getwork;
  499. double rolling;
  500. // Used by minerloop_async
  501. struct work *prev_work;
  502. struct work *work;
  503. struct work *next_work;
  504. enum thr_busy_state busy_state;
  505. struct timeval tv_morework;
  506. struct work *results_work;
  507. bool _job_transition_in_progress;
  508. bool _proceed_with_new_job;
  509. struct timeval tv_results_jobstart;
  510. struct timeval tv_jobstart;
  511. struct timeval tv_poll;
  512. notifier_t notifier;
  513. bool starting_next_work;
  514. uint32_t _max_nonce;
  515. notifier_t mutex_request;
  516. // Used by minerloop_queue
  517. struct list_head work_list;
  518. bool queue_full;
  519. bool _last_sbr_state;
  520. bool work_restart;
  521. notifier_t work_restart_notifier;
  522. };
  523. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  524. extern void thr_info_cancel(struct thr_info *thr);
  525. extern void thr_info_freeze(struct thr_info *thr);
  526. extern void nmsleep(unsigned int msecs);
  527. extern double us_tdiff(struct timeval *end, struct timeval *start);
  528. extern double tdiff(struct timeval *end, struct timeval *start);
  529. struct string_elist {
  530. char *string;
  531. bool free_me;
  532. struct list_head list;
  533. };
  534. static inline void string_elist_add(const char *s, struct list_head *head)
  535. {
  536. struct string_elist *n;
  537. n = calloc(1, sizeof(*n));
  538. n->string = strdup(s);
  539. n->free_me = true;
  540. list_add_tail(&n->list, head);
  541. }
  542. static inline void string_elist_del(struct string_elist *item)
  543. {
  544. if (item->free_me)
  545. free(item->string);
  546. list_del(&item->list);
  547. free(item);
  548. }
  549. static inline uint32_t swab32(uint32_t v)
  550. {
  551. return bswap_32(v);
  552. }
  553. static inline void swap256(void *dest_p, const void *src_p)
  554. {
  555. uint32_t *dest = dest_p;
  556. const uint32_t *src = src_p;
  557. dest[0] = src[7];
  558. dest[1] = src[6];
  559. dest[2] = src[5];
  560. dest[3] = src[4];
  561. dest[4] = src[3];
  562. dest[5] = src[2];
  563. dest[6] = src[1];
  564. dest[7] = src[0];
  565. }
  566. static inline void swap32yes(void*out, const void*in, size_t sz) {
  567. size_t swapcounter = 0;
  568. for (swapcounter = 0; swapcounter < sz; ++swapcounter)
  569. (((uint32_t*)out)[swapcounter]) = swab32(((uint32_t*)in)[swapcounter]);
  570. }
  571. #define LOCAL_swap32(type, var, sz) \
  572. type __swapped_ ## var[sz * 4 / sizeof(type)]; \
  573. swap32yes(__swapped_ ## var, var, sz); \
  574. var = __swapped_ ## var; \
  575. // end
  576. #ifdef WORDS_BIGENDIAN
  577. # define swap32tobe(out, in, sz) ((out == in) ? (void)0 : memmove(out, in, sz))
  578. # define LOCAL_swap32be(type, var, sz) ;
  579. # define swap32tole(out, in, sz) swap32yes(out, in, sz)
  580. # define LOCAL_swap32le(type, var, sz) LOCAL_swap32(type, var, sz)
  581. #else
  582. # define swap32tobe(out, in, sz) swap32yes(out, in, sz)
  583. # define LOCAL_swap32be(type, var, sz) LOCAL_swap32(type, var, sz)
  584. # define swap32tole(out, in, sz) ((out == in) ? (void)0 : memmove(out, in, sz))
  585. # define LOCAL_swap32le(type, var, sz) ;
  586. #endif
  587. static inline void swab256(void *dest_p, const void *src_p)
  588. {
  589. uint32_t *dest = dest_p;
  590. const uint32_t *src = src_p;
  591. dest[0] = swab32(src[7]);
  592. dest[1] = swab32(src[6]);
  593. dest[2] = swab32(src[5]);
  594. dest[3] = swab32(src[4]);
  595. dest[4] = swab32(src[3]);
  596. dest[5] = swab32(src[2]);
  597. dest[6] = swab32(src[1]);
  598. dest[7] = swab32(src[0]);
  599. }
  600. #define flip32(dest_p, src_p) swap32yes(dest_p, src_p, 32 / 4)
  601. extern void quit(int status, const char *format, ...) NORETURN FORMAT_SYNTAX_CHECK(printf, 2, 3);
  602. static inline void mutex_lock(pthread_mutex_t *lock)
  603. {
  604. if (unlikely(pthread_mutex_lock(lock)))
  605. quit(1, "WTF MUTEX ERROR ON LOCK!");
  606. }
  607. static inline void mutex_unlock(pthread_mutex_t *lock)
  608. {
  609. if (unlikely(pthread_mutex_unlock(lock)))
  610. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  611. }
  612. static inline int mutex_trylock(pthread_mutex_t *lock)
  613. {
  614. return pthread_mutex_trylock(lock);
  615. }
  616. static inline void wr_lock(pthread_rwlock_t *lock)
  617. {
  618. if (unlikely(pthread_rwlock_wrlock(lock)))
  619. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  620. }
  621. static inline void rd_lock(pthread_rwlock_t *lock)
  622. {
  623. if (unlikely(pthread_rwlock_rdlock(lock)))
  624. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  625. }
  626. static inline void rw_unlock(pthread_rwlock_t *lock)
  627. {
  628. if (unlikely(pthread_rwlock_unlock(lock)))
  629. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  630. }
  631. static inline void rd_unlock(pthread_rwlock_t *lock)
  632. {
  633. rw_unlock(lock);
  634. }
  635. static inline void wr_unlock(pthread_rwlock_t *lock)
  636. {
  637. rw_unlock(lock);
  638. }
  639. static inline void mutex_init(pthread_mutex_t *lock)
  640. {
  641. if (unlikely(pthread_mutex_init(lock, NULL)))
  642. quit(1, "Failed to pthread_mutex_init");
  643. }
  644. static inline void rwlock_init(pthread_rwlock_t *lock)
  645. {
  646. if (unlikely(pthread_rwlock_init(lock, NULL)))
  647. quit(1, "Failed to pthread_rwlock_init");
  648. }
  649. struct pool;
  650. extern bool opt_protocol;
  651. extern char *opt_coinbase_sig;
  652. extern bool have_longpoll;
  653. extern int opt_skip_checks;
  654. extern char *opt_kernel_path;
  655. extern char *opt_socks_proxy;
  656. extern char *cgminer_path;
  657. extern bool opt_fail_only;
  658. extern bool opt_autofan;
  659. extern bool opt_autoengine;
  660. extern bool use_curses;
  661. extern char *opt_api_allow;
  662. extern char *opt_api_groups;
  663. extern char *opt_api_description;
  664. extern int opt_api_port;
  665. extern bool opt_api_listen;
  666. extern bool opt_api_network;
  667. extern bool opt_delaynet;
  668. extern bool opt_restart;
  669. extern char *opt_icarus_options;
  670. extern char *opt_icarus_timing;
  671. extern bool opt_worktime;
  672. extern char *opt_avalon_options;
  673. #ifdef USE_BITFORCE
  674. extern bool opt_bfl_noncerange;
  675. #endif
  676. extern int swork_id;
  677. extern pthread_rwlock_t netacc_lock;
  678. extern const uint32_t sha256_init_state[];
  679. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  680. const char *rpc_req, bool, bool, int *,
  681. struct pool *pool, bool);
  682. extern bool our_curl_supports_proxy_uris();
  683. extern char *bin2hex(const unsigned char *p, size_t len);
  684. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  685. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  686. unsigned char *pdata,
  687. unsigned char *phash1, unsigned char *phash,
  688. const unsigned char *ptarget,
  689. uint32_t max_nonce,
  690. uint32_t *last_nonce,
  691. uint32_t nonce);
  692. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  693. extern int opt_queue;
  694. extern int opt_scantime;
  695. extern int opt_expiry;
  696. extern pthread_mutex_t hash_lock;
  697. extern pthread_mutex_t console_lock;
  698. extern pthread_mutex_t ch_lock;
  699. extern void thread_reportin(struct thr_info *thr);
  700. extern void thread_reportout(struct thr_info *);
  701. extern void clear_stratum_shares(struct pool *pool);
  702. extern void hashmeter2(struct thr_info *);
  703. extern bool stale_work(struct work *, bool share);
  704. extern bool stale_work_future(struct work *, bool share, unsigned long ustime);
  705. extern void kill_work(void);
  706. extern void app_restart(void);
  707. extern void mt_enable(struct thr_info *thr);
  708. extern void proc_enable(struct cgpu_info *);
  709. extern void reinit_device(struct cgpu_info *cgpu);
  710. #ifdef HAVE_ADL
  711. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  712. extern int set_fanspeed(int gpu, int iFanSpeed);
  713. extern int set_vddc(int gpu, float fVddc);
  714. extern int set_engineclock(int gpu, int iEngineClock);
  715. extern int set_memoryclock(int gpu, int iMemoryClock);
  716. #endif
  717. extern void api(int thr_id);
  718. extern struct pool *current_pool(void);
  719. extern int enabled_pools;
  720. extern bool detect_stratum(struct pool *pool, char *url);
  721. extern void print_summary(void);
  722. extern struct pool *add_pool(void);
  723. extern void add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  724. #define MAX_GPUDEVICES 16
  725. #define MIN_INTENSITY -10
  726. #define _MIN_INTENSITY_STR "-10"
  727. #ifdef USE_SCRYPT
  728. #define MAX_INTENSITY 20
  729. #define _MAX_INTENSITY_STR "20"
  730. #else
  731. #define MAX_INTENSITY 14
  732. #define _MAX_INTENSITY_STR "14"
  733. #endif
  734. extern struct list_head scan_devices;
  735. extern bool opt_force_dev_init;
  736. extern int nDevs;
  737. extern int opt_n_threads;
  738. extern int num_processors;
  739. extern int hw_errors;
  740. extern bool use_syslog;
  741. extern bool opt_quiet;
  742. extern struct thr_info *thr_info;
  743. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  744. extern int gpu_threads;
  745. #ifdef USE_SCRYPT
  746. extern bool opt_scrypt;
  747. #else
  748. #define opt_scrypt (0)
  749. #endif
  750. extern double total_secs;
  751. extern int mining_threads;
  752. extern struct cgpu_info *cpus;
  753. extern int total_devices;
  754. extern struct cgpu_info **devices;
  755. extern int total_pools;
  756. extern struct pool **pools;
  757. extern const char *algo_names[];
  758. extern enum sha256_algos opt_algo;
  759. extern struct strategies strategies[];
  760. extern enum pool_strategy pool_strategy;
  761. extern int opt_rotate_period;
  762. extern double total_mhashes_done;
  763. extern unsigned int new_blocks;
  764. extern unsigned int found_blocks;
  765. extern int total_accepted, total_rejected, total_diff1;;
  766. extern int total_getworks, total_stale, total_discarded;
  767. extern uint64_t total_bytes_xfer;
  768. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  769. extern unsigned int local_work;
  770. extern unsigned int total_go, total_ro;
  771. extern const int opt_cutofftemp;
  772. extern int opt_hysteresis;
  773. extern int opt_fail_pause;
  774. extern int opt_log_interval;
  775. extern unsigned long long global_hashrate;
  776. extern char *current_fullhash;
  777. extern double current_diff;
  778. extern uint64_t best_diff;
  779. extern struct timeval block_timeval;
  780. #ifdef HAVE_OPENCL
  781. typedef struct {
  782. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  783. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  784. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  785. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  786. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  787. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  788. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  789. cl_uint W16; cl_uint W17; cl_uint W2;
  790. cl_uint PreVal4; cl_uint T1;
  791. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  792. cl_uint PreVal4addT1; cl_uint T1substate0;
  793. cl_uint PreVal4_2;
  794. cl_uint PreVal0;
  795. cl_uint PreW18;
  796. cl_uint PreW19;
  797. cl_uint PreW31;
  798. cl_uint PreW32;
  799. /* For diakgcn */
  800. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  801. cl_uint zeroA, zeroB;
  802. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  803. #ifdef USE_SCRYPT
  804. struct work *work;
  805. #endif
  806. } dev_blk_ctx;
  807. #else
  808. typedef struct {
  809. uint32_t nonce;
  810. } dev_blk_ctx;
  811. #endif
  812. struct curl_ent {
  813. CURL *curl;
  814. struct list_head node;
  815. struct timeval tv;
  816. };
  817. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  818. * equal disabled */
  819. enum pool_enable {
  820. POOL_DISABLED,
  821. POOL_ENABLED,
  822. POOL_REJECTING,
  823. };
  824. enum pool_protocol {
  825. PLP_NONE,
  826. PLP_GETWORK,
  827. PLP_GETBLOCKTEMPLATE,
  828. };
  829. struct stratum_work {
  830. char *job_id;
  831. char *prev_hash;
  832. char *coinbase1;
  833. char *coinbase2;
  834. char **merkle;
  835. char *bbversion;
  836. char *nbit;
  837. char *ntime;
  838. bool clean;
  839. size_t cb1_len;
  840. size_t cb2_len;
  841. size_t cb_len;
  842. size_t header_len;
  843. int merkles;
  844. double diff;
  845. bool transparency_probed;
  846. time_t transparency_time;
  847. bool opaque;
  848. };
  849. #define RBUFSIZE 8192
  850. #define RECVSIZE (RBUFSIZE - 4)
  851. struct pool {
  852. int pool_no;
  853. int prio;
  854. int accepted, rejected;
  855. int seq_rejects;
  856. int seq_getfails;
  857. int solved;
  858. int diff1;
  859. char diff[8];
  860. double diff_accepted;
  861. double diff_rejected;
  862. double diff_stale;
  863. bool submit_fail;
  864. bool idle;
  865. bool lagging;
  866. bool probed;
  867. int force_rollntime;
  868. enum pool_enable enabled;
  869. bool submit_old;
  870. bool removed;
  871. bool lp_started;
  872. unsigned char work_restart_id;
  873. uint32_t block_id;
  874. enum pool_protocol proto;
  875. char *hdr_path;
  876. char *lp_url;
  877. char *lp_id;
  878. enum pool_protocol lp_proto;
  879. curl_socket_t lp_socket;
  880. unsigned int getwork_requested;
  881. unsigned int stale_shares;
  882. unsigned int discarded_work;
  883. unsigned int getfail_occasions;
  884. unsigned int remotefail_occasions;
  885. struct timeval tv_idle;
  886. double utility;
  887. int last_shares, shares;
  888. char *rpc_url;
  889. char *rpc_userpass;
  890. char *rpc_user, *rpc_pass;
  891. char *rpc_proxy;
  892. pthread_mutex_t pool_lock;
  893. struct thread_q *submit_q;
  894. struct thread_q *getwork_q;
  895. pthread_t longpoll_thread;
  896. pthread_t submit_thread;
  897. pthread_t getwork_thread;
  898. int curls;
  899. pthread_cond_t cr_cond;
  900. struct list_head curlring;
  901. struct submit_work_state *sws_waiting_on_curl;
  902. time_t last_work_time;
  903. time_t last_share_time;
  904. double last_share_diff;
  905. uint64_t best_diff;
  906. struct cgminer_stats cgminer_stats;
  907. struct cgminer_pool_stats cgminer_pool_stats;
  908. /* Stratum variables */
  909. char *stratum_url;
  910. char *stratum_port;
  911. CURL *stratum_curl;
  912. SOCKETTYPE sock;
  913. char *sockbuf;
  914. size_t sockbuf_size;
  915. char *sockaddr_url; /* stripped url used for sockaddr */
  916. char *nonce1;
  917. size_t n1_len;
  918. uint32_t nonce2;
  919. int n2size;
  920. char *sessionid;
  921. bool has_stratum;
  922. bool stratum_active;
  923. bool stratum_auth;
  924. bool stratum_notify;
  925. struct stratum_work swork;
  926. pthread_t stratum_thread;
  927. pthread_mutex_t stratum_lock;
  928. char *admin_msg;
  929. pthread_mutex_t last_work_lock;
  930. struct work *last_work_copy;
  931. };
  932. #define GETWORK_MODE_TESTPOOL 'T'
  933. #define GETWORK_MODE_POOL 'P'
  934. #define GETWORK_MODE_LP 'L'
  935. #define GETWORK_MODE_BENCHMARK 'B'
  936. #define GETWORK_MODE_STRATUM 'S'
  937. #define GETWORK_MODE_GBT 'G'
  938. struct work {
  939. unsigned char data[128];
  940. unsigned char midstate[32];
  941. unsigned char target[32];
  942. unsigned char hash[32];
  943. int rolls;
  944. dev_blk_ctx blk;
  945. struct thr_info *thr;
  946. int thr_id;
  947. struct pool *pool;
  948. struct timeval tv_staged;
  949. bool mined;
  950. bool clone;
  951. bool cloned;
  952. int rolltime;
  953. bool longpoll;
  954. bool stale;
  955. bool mandatory;
  956. bool block;
  957. bool queued;
  958. bool stratum;
  959. char *job_id;
  960. char *nonce2;
  961. char *ntime;
  962. double sdiff;
  963. char *nonce1;
  964. unsigned char work_restart_id;
  965. int id;
  966. UT_hash_handle hh;
  967. double work_difficulty;
  968. blktemplate_t *tmpl;
  969. int *tmpl_refcount;
  970. unsigned int dataid;
  971. bool do_foreign_submit;
  972. struct timeval tv_getwork;
  973. struct timeval tv_getwork_reply;
  974. struct timeval tv_cloned;
  975. struct timeval tv_work_start;
  976. struct timeval tv_work_found;
  977. char getwork_mode;
  978. /* Used to queue shares in submit_waiting */
  979. struct list_head list;
  980. };
  981. extern void get_datestamp(char *, struct timeval *);
  982. enum test_nonce2_result {
  983. TNR_GOOD,
  984. TNR_HIGH,
  985. TNR_BAD,
  986. };
  987. extern enum test_nonce2_result _test_nonce2(struct work *, uint32_t nonce, bool checktarget);
  988. #define test_nonce(work, nonce, checktarget) (_test_nonce2(work, nonce, checktarget) == TNR_GOOD)
  989. #define test_nonce2(work, nonce) (_test_nonce2(work, nonce, true))
  990. extern void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  991. extern bool abandon_work(struct work *, struct timeval *work_runtime, uint64_t hashes);
  992. extern void tailsprintf(char *f, const char *fmt, ...) FORMAT_SYNTAX_CHECK(printf, 2, 3);
  993. extern void wlog(const char *f, ...) FORMAT_SYNTAX_CHECK(printf, 1, 2);
  994. extern void wlogprint(const char *f, ...) FORMAT_SYNTAX_CHECK(printf, 1, 2);
  995. extern int curses_int(const char *query);
  996. extern char *curses_input(const char *query);
  997. extern double stats_elapsed(struct cgminer_stats *);
  998. extern void kill_work(void);
  999. extern int prioritize_pools(char *param, int *pid);
  1000. extern void validate_pool_priorities(void);
  1001. extern void switch_pools(struct pool *selected);
  1002. extern void remove_pool(struct pool *pool);
  1003. extern void write_config(FILE *fcfg);
  1004. extern void zero_bestshare(void);
  1005. extern void zero_stats(void);
  1006. extern void default_save_file(char *filename);
  1007. extern bool log_curses_only(int prio, const char *f, va_list ap) FORMAT_SYNTAX_CHECK(printf, 2, 0);
  1008. extern void clear_logwin(void);
  1009. extern bool pool_tclear(struct pool *pool, bool *var);
  1010. extern struct thread_q *tq_new(void);
  1011. extern void tq_free(struct thread_q *tq);
  1012. extern bool tq_push(struct thread_q *tq, void *data);
  1013. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  1014. extern void tq_freeze(struct thread_q *tq);
  1015. extern void tq_thaw(struct thread_q *tq);
  1016. extern bool successful_connect;
  1017. extern void adl(void);
  1018. extern void clean_work(struct work *work);
  1019. extern void free_work(struct work *work);
  1020. extern void __copy_work(struct work *work, const struct work *base_work);
  1021. extern struct work *copy_work(const struct work *base_work);
  1022. enum api_data_type {
  1023. API_ESCAPE,
  1024. API_STRING,
  1025. API_CONST,
  1026. API_INT,
  1027. API_UINT,
  1028. API_UINT32,
  1029. API_UINT64,
  1030. API_DOUBLE,
  1031. API_ELAPSED,
  1032. API_BOOL,
  1033. API_TIMEVAL,
  1034. API_TIME,
  1035. API_MHS,
  1036. API_MHTOTAL,
  1037. API_TEMP,
  1038. API_UTILITY,
  1039. API_FREQ,
  1040. API_VOLTS,
  1041. API_HS,
  1042. API_DIFF,
  1043. API_JSON,
  1044. };
  1045. struct api_data {
  1046. enum api_data_type type;
  1047. char *name;
  1048. void *data;
  1049. bool data_was_malloc;
  1050. struct api_data *prev;
  1051. struct api_data *next;
  1052. };
  1053. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  1054. extern struct api_data *api_add_string(struct api_data *root, char *name, const char *data, bool copy_data);
  1055. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  1056. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  1057. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  1058. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  1059. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  1060. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  1061. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  1062. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  1063. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  1064. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  1065. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  1066. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  1067. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  1068. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  1069. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  1070. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  1071. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  1072. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  1073. extern struct api_data *api_add_json(struct api_data *root, char *name, json_t *data, bool copy_data);
  1074. #endif /* __MINER_H__ */