miner.h 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  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. #ifdef HAVE_LIBCURL
  10. #include <curl/curl.h>
  11. #else
  12. typedef char CURL;
  13. extern char *curly;
  14. #define curl_easy_init(curl) (curly)
  15. #define curl_easy_cleanup(curl) {}
  16. #define curl_global_cleanup() {}
  17. #define CURL_GLOBAL_ALL 0
  18. #define curl_global_init(X) (0)
  19. #endif
  20. #include <sched.h>
  21. #include "elist.h"
  22. #include "uthash.h"
  23. #include "logging.h"
  24. #include "util.h"
  25. #include <sys/types.h>
  26. #ifndef WIN32
  27. # include <sys/socket.h>
  28. # include <netdb.h>
  29. #endif
  30. #ifdef USE_USBUTILS
  31. #include <semaphore.h>
  32. #endif
  33. #ifdef HAVE_OPENCL
  34. #ifdef __APPLE_CC__
  35. #include <OpenCL/opencl.h>
  36. #else
  37. #include <CL/cl.h>
  38. #endif
  39. #endif /* HAVE_OPENCL */
  40. #ifdef STDC_HEADERS
  41. # include <stdlib.h>
  42. # include <stddef.h>
  43. #else
  44. # ifdef HAVE_STDLIB_H
  45. # include <stdlib.h>
  46. # endif
  47. #endif
  48. #ifdef HAVE_ALLOCA_H
  49. # include <alloca.h>
  50. #elif defined __GNUC__
  51. # ifndef WIN32
  52. # define alloca __builtin_alloca
  53. # else
  54. # include <malloc.h>
  55. # endif
  56. #elif defined _AIX
  57. # define alloca __alloca
  58. #elif defined _MSC_VER
  59. # include <malloc.h>
  60. # define alloca _alloca
  61. #else
  62. # ifndef HAVE_ALLOCA
  63. # ifdef __cplusplus
  64. extern "C"
  65. # endif
  66. void *alloca (size_t);
  67. # endif
  68. #endif
  69. #ifdef __MINGW32__
  70. #include <windows.h>
  71. #include <io.h>
  72. static inline int fsync (int fd)
  73. {
  74. return (FlushFileBuffers ((HANDLE) _get_osfhandle (fd))) ? 0 : -1;
  75. }
  76. #ifndef EWOULDBLOCK
  77. # define EWOULDBLOCK EAGAIN
  78. #endif
  79. #ifndef MSG_DONTWAIT
  80. # define MSG_DONTWAIT 0x1000000
  81. #endif
  82. #endif /* __MINGW32__ */
  83. #if defined (__linux)
  84. #ifndef LINUX
  85. #define LINUX
  86. #endif
  87. #endif
  88. #ifdef WIN32
  89. #ifndef timersub
  90. #define timersub(a, b, result) \
  91. do { \
  92. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  93. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  94. if ((result)->tv_usec < 0) { \
  95. --(result)->tv_sec; \
  96. (result)->tv_usec += 1000000; \
  97. } \
  98. } while (0)
  99. #endif
  100. #ifndef timeradd
  101. # define timeradd(a, b, result) \
  102. do { \
  103. (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
  104. (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
  105. if ((result)->tv_usec >= 1000000) \
  106. { \
  107. ++(result)->tv_sec; \
  108. (result)->tv_usec -= 1000000; \
  109. } \
  110. } while (0)
  111. #endif
  112. #endif
  113. #ifdef HAVE_ADL
  114. #include "ADL_SDK/adl_sdk.h"
  115. #endif
  116. #ifdef USE_USBUTILS
  117. #include <libusb.h>
  118. #endif
  119. #ifdef USE_USBUTILS
  120. #include "usbutils.h"
  121. #endif
  122. #if (!defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))) \
  123. || (defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)))
  124. #ifndef bswap_16
  125. #define bswap_16 __builtin_bswap16
  126. #define bswap_32 __builtin_bswap32
  127. #define bswap_64 __builtin_bswap64
  128. #endif
  129. #else
  130. #if HAVE_BYTESWAP_H
  131. #include <byteswap.h>
  132. #elif defined(USE_SYS_ENDIAN_H)
  133. #include <sys/endian.h>
  134. #elif defined(__APPLE__)
  135. #include <libkern/OSByteOrder.h>
  136. #define bswap_16 OSSwapInt16
  137. #define bswap_32 OSSwapInt32
  138. #define bswap_64 OSSwapInt64
  139. #else
  140. #define bswap_16(value) \
  141. ((((value) & 0xff) << 8) | ((value) >> 8))
  142. #define bswap_32(value) \
  143. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  144. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  145. #define bswap_64(value) \
  146. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  147. << 32) | \
  148. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  149. #endif
  150. #endif /* !defined(__GLXBYTEORDER_H__) */
  151. /* This assumes htobe32 is a macro in endian.h, and if it doesn't exist, then
  152. * htobe64 also won't exist */
  153. #ifndef htobe32
  154. # if __BYTE_ORDER == __LITTLE_ENDIAN
  155. # define htole16(x) (x)
  156. # define htole32(x) (x)
  157. # define le32toh(x) (x)
  158. # define be32toh(x) bswap_32(x)
  159. # define be64toh(x) bswap_64(x)
  160. # define htobe32(x) bswap_32(x)
  161. # define htobe64(x) bswap_64(x)
  162. # elif __BYTE_ORDER == __BIG_ENDIAN
  163. # define htole16(x) bswap_16(x)
  164. # define htole32(x) bswap_32(x)
  165. # define le32toh(x) bswap_32(x)
  166. # define be32toh(x) (x)
  167. # define be64toh(x) (x)
  168. # define htobe32(x) (x)
  169. # define htobe64(x) (x)
  170. #else
  171. #error UNKNOWN BYTE ORDER
  172. #endif
  173. #endif
  174. #undef unlikely
  175. #undef likely
  176. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  177. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  178. #define likely(expr) (__builtin_expect(!!(expr), 1))
  179. #else
  180. #define unlikely(expr) (expr)
  181. #define likely(expr) (expr)
  182. #endif
  183. #define __maybe_unused __attribute__((unused))
  184. #define uninitialised_var(x) x = x
  185. #if defined(__i386__)
  186. #define WANT_CRYPTOPP_ASM32
  187. #endif
  188. #ifndef ARRAY_SIZE
  189. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  190. #endif
  191. #ifdef MIPSEB
  192. #ifndef roundl
  193. #define roundl(x) (long double)((long long)((x==0)?0.0:((x)+((x)>0)?0.5:-0.5)))
  194. #endif
  195. #endif
  196. /* No semtimedop on apple so ignore timeout till we implement one */
  197. #ifdef __APPLE__
  198. #define semtimedop(SEM, SOPS, VAL, TIMEOUT) semop(SEM, SOPS, VAL)
  199. #endif
  200. #define MIN(x, y) ((x) > (y) ? (y) : (x))
  201. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  202. /* Put avalon last to make it the last device it tries to detect to prevent it
  203. * trying to claim same chip but different devices. Adding a device here will
  204. * update all macros in the code that use the *_PARSE_COMMANDS macros for each
  205. * listed driver. */
  206. #define FPGA_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
  207. DRIVER_ADD_COMMAND(bitforce) \
  208. DRIVER_ADD_COMMAND(icarus) \
  209. DRIVER_ADD_COMMAND(modminer)
  210. #define ASIC_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
  211. DRIVER_ADD_COMMAND(bflsc) \
  212. DRIVER_ADD_COMMAND(bitfury) \
  213. DRIVER_ADD_COMMAND(avalon) \
  214. DRIVER_ADD_COMMAND(klondike)
  215. #define DRIVER_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
  216. DRIVER_ADD_COMMAND(opencl) \
  217. FPGA_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
  218. ASIC_PARSE_COMMANDS(DRIVER_ADD_COMMAND)
  219. #define DRIVER_ENUM(X) DRIVER_##X,
  220. #define DRIVER_PROTOTYPE(X) struct device_drv X##_drv;
  221. /* Create drv_driver enum from DRIVER_PARSE_COMMANDS macro */
  222. enum drv_driver {
  223. DRIVER_PARSE_COMMANDS(DRIVER_ENUM)
  224. DRIVER_MAX
  225. };
  226. /* Use DRIVER_PARSE_COMMANDS to generate extern device_drv prototypes */
  227. DRIVER_PARSE_COMMANDS(DRIVER_PROTOTYPE)
  228. enum alive {
  229. LIFE_WELL,
  230. LIFE_SICK,
  231. LIFE_DEAD,
  232. LIFE_NOSTART,
  233. LIFE_INIT,
  234. };
  235. enum pool_strategy {
  236. POOL_FAILOVER,
  237. POOL_ROUNDROBIN,
  238. POOL_ROTATE,
  239. POOL_LOADBALANCE,
  240. POOL_BALANCE,
  241. };
  242. #define TOP_STRATEGY (POOL_BALANCE)
  243. struct strategies {
  244. const char *s;
  245. };
  246. struct cgpu_info;
  247. #ifdef HAVE_ADL
  248. struct gpu_adl {
  249. ADLTemperature lpTemperature;
  250. int iAdapterIndex;
  251. int lpAdapterID;
  252. int iBusNumber;
  253. char strAdapterName[256];
  254. ADLPMActivity lpActivity;
  255. ADLODParameters lpOdParameters;
  256. ADLODPerformanceLevels *DefPerfLev;
  257. ADLFanSpeedInfo lpFanSpeedInfo;
  258. ADLFanSpeedValue lpFanSpeedValue;
  259. ADLFanSpeedValue DefFanSpeedValue;
  260. int iEngineClock;
  261. int iMemoryClock;
  262. int iVddc;
  263. int iPercentage;
  264. bool autofan;
  265. bool autoengine;
  266. bool managed; /* Were the values ever changed on this card */
  267. int lastengine;
  268. int lasttemp;
  269. int targetfan;
  270. int targettemp;
  271. int overtemp;
  272. int minspeed;
  273. int maxspeed;
  274. int gpu;
  275. bool has_fanspeed;
  276. struct gpu_adl *twin;
  277. };
  278. #endif
  279. extern void blank_get_statline_before(char *buf, size_t bufsiz, struct cgpu_info __maybe_unused *cgpu);
  280. struct api_data;
  281. struct thr_info;
  282. struct work;
  283. struct device_drv {
  284. enum drv_driver drv_id;
  285. char *dname;
  286. char *name;
  287. // DRV-global functions
  288. void (*drv_detect)(bool);
  289. // Device-specific functions
  290. void (*reinit_device)(struct cgpu_info *);
  291. void (*get_statline_before)(char *, size_t, struct cgpu_info *);
  292. void (*get_statline)(char *, size_t, struct cgpu_info *);
  293. struct api_data *(*get_api_stats)(struct cgpu_info *);
  294. bool (*get_stats)(struct cgpu_info *);
  295. void (*identify_device)(struct cgpu_info *); // e.g. to flash a led
  296. char *(*set_device)(struct cgpu_info *, char *option, char *setting, char *replybuf);
  297. // Thread-specific functions
  298. bool (*thread_prepare)(struct thr_info *);
  299. uint64_t (*can_limit_work)(struct thr_info *);
  300. bool (*thread_init)(struct thr_info *);
  301. bool (*prepare_work)(struct thr_info *, struct work *);
  302. /* Which hash work loop this driver uses. */
  303. void (*hash_work)(struct thr_info *);
  304. /* Two variants depending on whether the device divides work up into
  305. * small pieces or works with whole work items and may or may not have
  306. * a queue of its own. */
  307. int64_t (*scanhash)(struct thr_info *, struct work *, int64_t);
  308. int64_t (*scanwork)(struct thr_info *);
  309. /* Used to extract work from the hash table of queued work and tell
  310. * the main loop that it should not add any further work to the table.
  311. */
  312. bool (*queue_full)(struct cgpu_info *);
  313. void (*flush_work)(struct cgpu_info *);
  314. void (*hw_error)(struct thr_info *);
  315. void (*thread_shutdown)(struct thr_info *);
  316. void (*thread_enable)(struct thr_info *);
  317. // Does it need to be free()d?
  318. bool copy;
  319. /* Highest target diff the device supports */
  320. double max_diff;
  321. double working_diff;
  322. };
  323. extern struct device_drv *copy_drv(struct device_drv*);
  324. enum dev_enable {
  325. DEV_ENABLED,
  326. DEV_DISABLED,
  327. DEV_RECOVER,
  328. };
  329. enum cl_kernels {
  330. KL_NONE,
  331. KL_POCLBM,
  332. KL_PHATK,
  333. KL_DIAKGCN,
  334. KL_DIABLO,
  335. KL_SCRYPT,
  336. };
  337. enum dev_reason {
  338. REASON_THREAD_FAIL_INIT,
  339. REASON_THREAD_ZERO_HASH,
  340. REASON_THREAD_FAIL_QUEUE,
  341. REASON_DEV_SICK_IDLE_60,
  342. REASON_DEV_DEAD_IDLE_600,
  343. REASON_DEV_NOSTART,
  344. REASON_DEV_OVER_HEAT,
  345. REASON_DEV_THERMAL_CUTOFF,
  346. REASON_DEV_COMMS_ERROR,
  347. REASON_DEV_THROTTLE,
  348. };
  349. #define REASON_NONE "None"
  350. #define REASON_THREAD_FAIL_INIT_STR "Thread failed to init"
  351. #define REASON_THREAD_ZERO_HASH_STR "Thread got zero hashes"
  352. #define REASON_THREAD_FAIL_QUEUE_STR "Thread failed to queue work"
  353. #define REASON_DEV_SICK_IDLE_60_STR "Device idle for 60s"
  354. #define REASON_DEV_DEAD_IDLE_600_STR "Device dead - idle for 600s"
  355. #define REASON_DEV_NOSTART_STR "Device failed to start"
  356. #define REASON_DEV_OVER_HEAT_STR "Device over heated"
  357. #define REASON_DEV_THERMAL_CUTOFF_STR "Device reached thermal cutoff"
  358. #define REASON_DEV_COMMS_ERROR_STR "Device comms error"
  359. #define REASON_DEV_THROTTLE_STR "Device throttle"
  360. #define REASON_UNKNOWN_STR "Unknown reason - code bug"
  361. #define MIN_SEC_UNSET 99999999
  362. struct cgminer_stats {
  363. uint32_t getwork_calls;
  364. struct timeval getwork_wait;
  365. struct timeval getwork_wait_max;
  366. struct timeval getwork_wait_min;
  367. };
  368. // Just the actual network getworks to the pool
  369. struct cgminer_pool_stats {
  370. uint32_t getwork_calls;
  371. uint32_t getwork_attempts;
  372. struct timeval getwork_wait;
  373. struct timeval getwork_wait_max;
  374. struct timeval getwork_wait_min;
  375. double getwork_wait_rolling;
  376. bool hadrolltime;
  377. bool canroll;
  378. bool hadexpire;
  379. uint32_t rolltime;
  380. double min_diff;
  381. double max_diff;
  382. double last_diff;
  383. uint32_t min_diff_count;
  384. uint32_t max_diff_count;
  385. uint64_t times_sent;
  386. uint64_t bytes_sent;
  387. uint64_t net_bytes_sent;
  388. uint64_t times_received;
  389. uint64_t bytes_received;
  390. uint64_t net_bytes_received;
  391. };
  392. struct cgpu_info {
  393. int cgminer_id;
  394. struct device_drv *drv;
  395. int device_id;
  396. char *name;
  397. char *device_path;
  398. void *device_data;
  399. #ifdef USE_USBUTILS
  400. struct cg_usb_device *usbdev;
  401. #endif
  402. #ifdef USE_AVALON
  403. struct work **works;
  404. int work_array;
  405. int queued;
  406. int results;
  407. #endif
  408. #ifdef USE_USBUTILS
  409. struct cg_usb_info usbinfo;
  410. #endif
  411. #ifdef USE_MODMINER
  412. char fpgaid;
  413. unsigned char clock;
  414. pthread_mutex_t *modminer_mutex;
  415. #endif
  416. #ifdef USE_BITFORCE
  417. struct timeval work_start_tv;
  418. unsigned int wait_ms;
  419. unsigned int sleep_ms;
  420. double avg_wait_f;
  421. unsigned int avg_wait_d;
  422. uint32_t nonces;
  423. bool nonce_range;
  424. bool polling;
  425. bool flash_led;
  426. #endif /* USE_BITFORCE */
  427. #if defined(USE_BITFORCE) || defined(USE_BFLSC)
  428. pthread_mutex_t device_mutex;
  429. #endif /* USE_BITFORCE || USE_BFLSC */
  430. enum dev_enable deven;
  431. int accepted;
  432. int rejected;
  433. int hw_errors;
  434. double rolling;
  435. double total_mhashes;
  436. double utility;
  437. enum alive status;
  438. char init[40];
  439. struct timeval last_message_tv;
  440. int threads;
  441. struct thr_info **thr;
  442. int64_t max_hashes;
  443. const char *kname;
  444. #ifdef HAVE_OPENCL
  445. bool mapped;
  446. int virtual_gpu;
  447. int virtual_adl;
  448. int intensity;
  449. bool dynamic;
  450. cl_uint vwidth;
  451. size_t work_size;
  452. enum cl_kernels kernel;
  453. cl_ulong max_alloc;
  454. #ifdef USE_SCRYPT
  455. int opt_lg, lookup_gap;
  456. size_t opt_tc, thread_concurrency;
  457. size_t shaders;
  458. #endif
  459. struct timeval tv_gpustart;
  460. int intervals;
  461. #endif
  462. bool new_work;
  463. float temp;
  464. int cutofftemp;
  465. #ifdef HAVE_ADL
  466. bool has_adl;
  467. struct gpu_adl adl;
  468. int gpu_engine;
  469. int min_engine;
  470. int gpu_fan;
  471. int min_fan;
  472. int gpu_memclock;
  473. int gpu_memdiff;
  474. int gpu_powertune;
  475. float gpu_vddc;
  476. #endif
  477. int diff1;
  478. double diff_accepted;
  479. double diff_rejected;
  480. int last_share_pool;
  481. time_t last_share_pool_time;
  482. double last_share_diff;
  483. time_t last_device_valid_work;
  484. time_t device_last_well;
  485. time_t device_last_not_well;
  486. enum dev_reason device_not_well_reason;
  487. int thread_fail_init_count;
  488. int thread_zero_hash_count;
  489. int thread_fail_queue_count;
  490. int dev_sick_idle_60_count;
  491. int dev_dead_idle_600_count;
  492. int dev_nostart_count;
  493. int dev_over_heat_count; // It's a warning but worth knowing
  494. int dev_thermal_cutoff_count;
  495. int dev_comms_error_count;
  496. int dev_throttle_count;
  497. struct cgminer_stats cgminer_stats;
  498. pthread_rwlock_t qlock;
  499. struct work *queued_work;
  500. unsigned int queued_count;
  501. bool shutdown;
  502. struct timeval dev_start_tv;
  503. };
  504. extern bool add_cgpu(struct cgpu_info*);
  505. struct thread_q {
  506. struct list_head q;
  507. bool frozen;
  508. pthread_mutex_t mutex;
  509. pthread_cond_t cond;
  510. };
  511. struct thr_info {
  512. int id;
  513. int device_thread;
  514. bool primary_thread;
  515. pthread_t pth;
  516. cgsem_t sem;
  517. struct thread_q *q;
  518. struct cgpu_info *cgpu;
  519. void *cgpu_data;
  520. struct timeval last;
  521. struct timeval sick;
  522. bool pause;
  523. bool getwork;
  524. double rolling;
  525. bool work_restart;
  526. };
  527. struct string_elist {
  528. char *string;
  529. bool free_me;
  530. struct list_head list;
  531. };
  532. static inline void string_elist_add(const char *s, struct list_head *head)
  533. {
  534. struct string_elist *n;
  535. n = calloc(1, sizeof(*n));
  536. n->string = strdup(s);
  537. n->free_me = true;
  538. list_add_tail(&n->list, head);
  539. }
  540. static inline void string_elist_del(struct string_elist *item)
  541. {
  542. if (item->free_me)
  543. free(item->string);
  544. list_del(&item->list);
  545. }
  546. static inline uint32_t swab32(uint32_t v)
  547. {
  548. return bswap_32(v);
  549. }
  550. static inline void swap256(void *dest_p, const void *src_p)
  551. {
  552. uint32_t *dest = dest_p;
  553. const uint32_t *src = src_p;
  554. dest[0] = src[7];
  555. dest[1] = src[6];
  556. dest[2] = src[5];
  557. dest[3] = src[4];
  558. dest[4] = src[3];
  559. dest[5] = src[2];
  560. dest[6] = src[1];
  561. dest[7] = src[0];
  562. }
  563. static inline void swab256(void *dest_p, const void *src_p)
  564. {
  565. uint32_t *dest = dest_p;
  566. const uint32_t *src = src_p;
  567. dest[0] = swab32(src[7]);
  568. dest[1] = swab32(src[6]);
  569. dest[2] = swab32(src[5]);
  570. dest[3] = swab32(src[4]);
  571. dest[4] = swab32(src[3]);
  572. dest[5] = swab32(src[2]);
  573. dest[6] = swab32(src[1]);
  574. dest[7] = swab32(src[0]);
  575. }
  576. static inline void flip32(void *dest_p, const void *src_p)
  577. {
  578. uint32_t *dest = dest_p;
  579. const uint32_t *src = src_p;
  580. int i;
  581. for (i = 0; i < 8; i++)
  582. dest[i] = swab32(src[i]);
  583. }
  584. static inline void flip64(void *dest_p, const void *src_p)
  585. {
  586. uint32_t *dest = dest_p;
  587. const uint32_t *src = src_p;
  588. int i;
  589. for (i = 0; i < 16; i++)
  590. dest[i] = swab32(src[i]);
  591. }
  592. static inline void flip80(void *dest_p, const void *src_p)
  593. {
  594. uint32_t *dest = dest_p;
  595. const uint32_t *src = src_p;
  596. int i;
  597. for (i = 0; i < 20; i++)
  598. dest[i] = swab32(src[i]);
  599. }
  600. static inline void flip128(void *dest_p, const void *src_p)
  601. {
  602. uint32_t *dest = dest_p;
  603. const uint32_t *src = src_p;
  604. int i;
  605. for (i = 0; i < 32; i++)
  606. dest[i] = swab32(src[i]);
  607. }
  608. /* For flipping to the correct endianness if necessary */
  609. #if defined(__BIG_ENDIAN__) || defined(MIPSEB)
  610. static inline void endian_flip32(void *dest_p, const void *src_p)
  611. {
  612. flip32(dest_p, src_p);
  613. }
  614. static inline void endian_flip128(void *dest_p, const void *src_p)
  615. {
  616. flip128(dest_p, src_p);
  617. }
  618. #else
  619. static inline void
  620. endian_flip32(void __maybe_unused *dest_p, const void __maybe_unused *src_p)
  621. {
  622. }
  623. static inline void
  624. endian_flip128(void __maybe_unused *dest_p, const void __maybe_unused *src_p)
  625. {
  626. }
  627. #endif
  628. extern void _quit(int status);
  629. /*
  630. * Set this to non-zero to enable lock tracking
  631. * Use the API lockstats command to see the locking status on stderr
  632. * i.e. in your log file if you 2> log.log - but not on the screen
  633. * API lockstats is privilidged but will always exist and will return
  634. * success if LOCK_TRACKING is enabled and warning if disabled
  635. * In production code, this should never be enabled since it will slow down all locking
  636. * So, e.g. use it to track down a deadlock - after a reproducable deadlock occurs
  637. * ... Of course if the API code itself deadlocks, it wont help :)
  638. */
  639. #define LOCK_TRACKING 0
  640. #if LOCK_TRACKING
  641. enum cglock_typ {
  642. CGLOCK_MUTEX,
  643. CGLOCK_RW,
  644. CGLOCK_UNKNOWN
  645. };
  646. extern uint64_t api_getlock(void *lock, const char *file, const char *func, const int line);
  647. extern void api_gotlock(uint64_t id, void *lock, const char *file, const char *func, const int line);
  648. extern uint64_t api_trylock(void *lock, const char *file, const char *func, const int line);
  649. extern void api_didlock(uint64_t id, int ret, void *lock, const char *file, const char *func, const int line);
  650. extern void api_gunlock(void *lock, const char *file, const char *func, const int line);
  651. extern void api_initlock(void *lock, enum cglock_typ typ, const char *file, const char *func, const int line);
  652. #define GETLOCK(_lock, _file, _func, _line) uint64_t _id1 = api_getlock((void *)(_lock), _file, _func, _line)
  653. #define GOTLOCK(_lock, _file, _func, _line) api_gotlock(_id1, (void *)(_lock), _file, _func, _line)
  654. #define TRYLOCK(_lock, _file, _func, _line) uint64_t _id2 = api_trylock((void *)(_lock), _file, _func, _line)
  655. #define DIDLOCK(_ret, _lock, _file, _func, _line) api_didlock(_id2, _ret, (void *)(_lock), _file, _func, _line)
  656. #define GUNLOCK(_lock, _file, _func, _line) api_gunlock((void *)(_lock), _file, _func, _line)
  657. #define INITLOCK(_lock, _typ, _file, _func, _line) api_initlock((void *)(_lock), _typ, _file, _func, _line)
  658. #else
  659. #define GETLOCK(_lock, _file, _func, _line)
  660. #define GOTLOCK(_lock, _file, _func, _line)
  661. #define TRYLOCK(_lock, _file, _func, _line)
  662. #define DIDLOCK(_ret, _lock, _file, _func, _line)
  663. #define GUNLOCK(_lock, _file, _func, _line)
  664. #define INITLOCK(_typ, _lock, _file, _func, _line)
  665. #endif
  666. #define mutex_lock(_lock) _mutex_lock(_lock, __FILE__, __func__, __LINE__)
  667. #define mutex_unlock_noyield(_lock) _mutex_unlock_noyield(_lock, __FILE__, __func__, __LINE__)
  668. #define mutex_unlock(_lock) _mutex_unlock(_lock, __FILE__, __func__, __LINE__)
  669. #define mutex_trylock(_lock) _mutex_trylock(_lock, __FILE__, __func__, __LINE__)
  670. #define wr_lock(_lock) _wr_lock(_lock, __FILE__, __func__, __LINE__)
  671. #define rd_lock(_lock) _rd_lock(_lock, __FILE__, __func__, __LINE__)
  672. #define rw_unlock(_lock) _rw_unlock(_lock, __FILE__, __func__, __LINE__)
  673. #define rd_unlock_noyield(_lock) _rd_unlock_noyield(_lock, __FILE__, __func__, __LINE__)
  674. #define wr_unlock_noyield(_lock) _wr_unlock_noyield(_lock, __FILE__, __func__, __LINE__)
  675. #define rd_unlock(_lock) _rd_unlock(_lock, __FILE__, __func__, __LINE__)
  676. #define wr_unlock(_lock) _wr_unlock(_lock, __FILE__, __func__, __LINE__)
  677. #define mutex_init(_lock) _mutex_init(_lock, __FILE__, __func__, __LINE__)
  678. #define rwlock_init(_lock) _rwlock_init(_lock, __FILE__, __func__, __LINE__)
  679. #define cglock_init(_lock) _cglock_init(_lock, __FILE__, __func__, __LINE__)
  680. #define cg_rlock(_lock) _cg_rlock(_lock, __FILE__, __func__, __LINE__)
  681. #define cg_ilock(_lock) _cg_ilock(_lock, __FILE__, __func__, __LINE__)
  682. #define cg_ulock(_lock) _cg_ulock(_lock, __FILE__, __func__, __LINE__)
  683. #define cg_wlock(_lock) _cg_wlock(_lock, __FILE__, __func__, __LINE__)
  684. #define cg_dwlock(_lock) _cg_dwlock(_lock, __FILE__, __func__, __LINE__)
  685. #define cg_dwilock(_lock) _cg_dwilock(_lock, __FILE__, __func__, __LINE__)
  686. #define cg_dlock(_lock) _cg_dlock(_lock, __FILE__, __func__, __LINE__)
  687. #define cg_runlock(_lock) _cg_runlock(_lock, __FILE__, __func__, __LINE__)
  688. #define cg_ruwlock(_lock) _cg_ruwlock(_lock, __FILE__, __func__, __LINE__)
  689. #define cg_wunlock(_lock) _cg_wunlock(_lock, __FILE__, __func__, __LINE__)
  690. static inline void _mutex_lock(pthread_mutex_t *lock, const char *file, const char *func, const int line)
  691. {
  692. GETLOCK(lock, file, func, line);
  693. if (unlikely(pthread_mutex_lock(lock)))
  694. quitfrom(1, file, func, line, "WTF MUTEX ERROR ON LOCK! errno=%d", errno);
  695. GOTLOCK(lock, file, func, line);
  696. }
  697. static inline void _mutex_unlock_noyield(pthread_mutex_t *lock, const char *file, const char *func, const int line)
  698. {
  699. if (unlikely(pthread_mutex_unlock(lock)))
  700. quitfrom(1, file, func, line, "WTF MUTEX ERROR ON UNLOCK! errno=%d", errno);
  701. GUNLOCK(lock, file, func, line);
  702. }
  703. static inline void _mutex_unlock(pthread_mutex_t *lock, const char *file, const char *func, const int line)
  704. {
  705. _mutex_unlock_noyield(lock, file, func, line);
  706. sched_yield();
  707. }
  708. static inline int _mutex_trylock(pthread_mutex_t *lock, __maybe_unused const char *file, __maybe_unused const char *func, __maybe_unused const int line)
  709. {
  710. TRYLOCK(lock, file, func, line);
  711. int ret = pthread_mutex_trylock(lock);
  712. DIDLOCK(ret, lock, file, func, line);
  713. return ret;
  714. }
  715. static inline void _wr_lock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  716. {
  717. GETLOCK(lock, file, func, line);
  718. if (unlikely(pthread_rwlock_wrlock(lock)))
  719. quitfrom(1, file, func, line, "WTF WRLOCK ERROR ON LOCK! errno=%d", errno);
  720. GOTLOCK(lock, file, func, line);
  721. }
  722. static inline void _rd_lock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  723. {
  724. GETLOCK(lock, file, func, line);
  725. if (unlikely(pthread_rwlock_rdlock(lock)))
  726. quitfrom(1, file, func, line, "WTF RDLOCK ERROR ON LOCK! errno=%d", errno);
  727. GOTLOCK(lock, file, func, line);
  728. }
  729. static inline void _rw_unlock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  730. {
  731. if (unlikely(pthread_rwlock_unlock(lock)))
  732. quitfrom(1, file, func, line, "WTF RWLOCK ERROR ON UNLOCK! errno=%d", errno);
  733. GUNLOCK(lock, file, func, line);
  734. }
  735. static inline void _rd_unlock_noyield(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  736. {
  737. _rw_unlock(lock, file, func, line);
  738. }
  739. static inline void _wr_unlock_noyield(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  740. {
  741. _rw_unlock(lock, file, func, line);
  742. }
  743. static inline void _rd_unlock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  744. {
  745. _rw_unlock(lock, file, func, line);
  746. sched_yield();
  747. }
  748. static inline void _wr_unlock(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  749. {
  750. _rw_unlock(lock, file, func, line);
  751. sched_yield();
  752. }
  753. static inline void _mutex_init(pthread_mutex_t *lock, const char *file, const char *func, const int line)
  754. {
  755. if (unlikely(pthread_mutex_init(lock, NULL)))
  756. quitfrom(1, file, func, line, "Failed to pthread_mutex_init errno=%d", errno);
  757. INITLOCK(lock, CGLOCK_MUTEX, file, func, line);
  758. }
  759. static inline void _rwlock_init(pthread_rwlock_t *lock, const char *file, const char *func, const int line)
  760. {
  761. if (unlikely(pthread_rwlock_init(lock, NULL)))
  762. quitfrom(1, file, func, line, "Failed to pthread_rwlock_init errno=%d", errno);
  763. INITLOCK(lock, CGLOCK_RW, file, func, line);
  764. }
  765. static inline void _cglock_init(cglock_t *lock, const char *file, const char *func, const int line)
  766. {
  767. _mutex_init(&lock->mutex, file, func, line);
  768. _rwlock_init(&lock->rwlock, file, func, line);
  769. }
  770. /* Read lock variant of cglock. Cannot be promoted. */
  771. static inline void _cg_rlock(cglock_t *lock, const char *file, const char *func, const int line)
  772. {
  773. _mutex_lock(&lock->mutex, file, func, line);
  774. _rd_lock(&lock->rwlock, file, func, line);
  775. _mutex_unlock_noyield(&lock->mutex, file, func, line);
  776. }
  777. /* Intermediate variant of cglock - behaves as a read lock but can be promoted
  778. * to a write lock or demoted to read lock. */
  779. static inline void _cg_ilock(cglock_t *lock, const char *file, const char *func, const int line)
  780. {
  781. _mutex_lock(&lock->mutex, file, func, line);
  782. }
  783. /* Upgrade intermediate variant to a write lock */
  784. static inline void _cg_ulock(cglock_t *lock, const char *file, const char *func, const int line)
  785. {
  786. _wr_lock(&lock->rwlock, file, func, line);
  787. }
  788. /* Write lock variant of cglock */
  789. static inline void _cg_wlock(cglock_t *lock, const char *file, const char *func, const int line)
  790. {
  791. _mutex_lock(&lock->mutex, file, func, line);
  792. _wr_lock(&lock->rwlock, file, func, line);
  793. }
  794. /* Downgrade write variant to a read lock */
  795. static inline void _cg_dwlock(cglock_t *lock, const char *file, const char *func, const int line)
  796. {
  797. _wr_unlock_noyield(&lock->rwlock, file, func, line);
  798. _rd_lock(&lock->rwlock, file, func, line);
  799. _mutex_unlock_noyield(&lock->mutex, file, func, line);
  800. }
  801. /* Demote a write variant to an intermediate variant */
  802. static inline void _cg_dwilock(cglock_t *lock, const char *file, const char *func, const int line)
  803. {
  804. _wr_unlock(&lock->rwlock, file, func, line);
  805. }
  806. /* Downgrade intermediate variant to a read lock */
  807. static inline void _cg_dlock(cglock_t *lock, const char *file, const char *func, const int line)
  808. {
  809. _rd_lock(&lock->rwlock, file, func, line);
  810. _mutex_unlock_noyield(&lock->mutex, file, func, line);
  811. }
  812. static inline void _cg_runlock(cglock_t *lock, const char *file, const char *func, const int line)
  813. {
  814. _rd_unlock(&lock->rwlock, file, func, line);
  815. }
  816. /* This drops the read lock and grabs a write lock. It does NOT protect data
  817. * between the two locks! */
  818. static inline void _cg_ruwlock(cglock_t *lock, const char *file, const char *func, const int line)
  819. {
  820. _rd_unlock_noyield(&lock->rwlock, file, func, line);
  821. _cg_wlock(lock, file, func, line);
  822. }
  823. static inline void _cg_wunlock(cglock_t *lock, const char *file, const char *func, const int line)
  824. {
  825. _wr_unlock_noyield(&lock->rwlock, file, func, line);
  826. _mutex_unlock(&lock->mutex, file, func, line);
  827. }
  828. struct pool;
  829. #define API_MCAST_CODE "FTW"
  830. #define API_MCAST_ADDR "224.0.0.75"
  831. extern bool opt_protocol;
  832. extern bool have_longpoll;
  833. extern char *opt_kernel_path;
  834. extern char *opt_socks_proxy;
  835. extern char *cgminer_path;
  836. extern bool opt_fail_only;
  837. extern bool opt_autofan;
  838. extern bool opt_autoengine;
  839. extern bool use_curses;
  840. extern char *opt_api_allow;
  841. extern bool opt_api_mcast;
  842. extern char *opt_api_mcast_addr;
  843. extern char *opt_api_mcast_code;
  844. extern char *opt_api_mcast_des;
  845. extern int opt_api_mcast_port;
  846. extern char *opt_api_groups;
  847. extern char *opt_api_description;
  848. extern int opt_api_port;
  849. extern bool opt_api_listen;
  850. extern bool opt_api_network;
  851. extern bool opt_delaynet;
  852. extern bool opt_restart;
  853. extern bool opt_nogpu;
  854. extern char *opt_icarus_options;
  855. extern char *opt_icarus_timing;
  856. extern bool opt_worktime;
  857. #ifdef USE_AVALON
  858. extern char *opt_avalon_options;
  859. #endif
  860. #ifdef USE_KLONDIKE
  861. extern char *opt_klondike_options;
  862. #endif
  863. #ifdef USE_USBUTILS
  864. extern char *opt_usb_select;
  865. extern int opt_usbdump;
  866. extern bool opt_usb_list_all;
  867. extern cgsem_t usb_resource_sem;
  868. #endif
  869. #ifdef USE_BITFORCE
  870. extern bool opt_bfl_noncerange;
  871. #endif
  872. extern int swork_id;
  873. #if LOCK_TRACKING
  874. extern pthread_mutex_t lockstat_lock;
  875. #endif
  876. extern pthread_rwlock_t netacc_lock;
  877. extern const uint32_t sha256_init_state[];
  878. #ifdef HAVE_LIBCURL
  879. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  880. const char *rpc_req, bool, bool, int *,
  881. struct pool *pool, bool);
  882. #endif
  883. extern const char *proxytype(proxytypes_t proxytype);
  884. extern char *get_proxy(char *url, struct pool *pool);
  885. extern void __bin2hex(char *s, const unsigned char *p, size_t len);
  886. extern char *bin2hex(const unsigned char *p, size_t len);
  887. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  888. typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
  889. unsigned char *pdata,
  890. unsigned char *phash1, unsigned char *phash,
  891. const unsigned char *ptarget,
  892. uint32_t max_nonce,
  893. uint32_t *last_nonce,
  894. uint32_t nonce);
  895. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  896. extern int opt_queue;
  897. extern int opt_scantime;
  898. extern int opt_expiry;
  899. #ifdef USE_USBUTILS
  900. extern pthread_mutex_t cgusb_lock;
  901. extern pthread_mutex_t cgusbres_lock;
  902. extern cglock_t cgusb_fd_lock;
  903. #endif
  904. extern cglock_t control_lock;
  905. extern pthread_mutex_t hash_lock;
  906. extern pthread_mutex_t console_lock;
  907. extern cglock_t ch_lock;
  908. extern pthread_rwlock_t mining_thr_lock;
  909. extern pthread_rwlock_t devices_lock;
  910. extern pthread_mutex_t restart_lock;
  911. extern pthread_cond_t restart_cond;
  912. extern void clear_stratum_shares(struct pool *pool);
  913. extern void set_target(unsigned char *dest_target, double diff);
  914. extern int restart_wait(struct thr_info *thr, unsigned int mstime);
  915. extern void kill_work(void);
  916. extern void reinit_device(struct cgpu_info *cgpu);
  917. #ifdef HAVE_ADL
  918. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  919. extern int set_fanspeed(int gpu, int iFanSpeed);
  920. extern int set_vddc(int gpu, float fVddc);
  921. extern int set_engineclock(int gpu, int iEngineClock);
  922. extern int set_memoryclock(int gpu, int iMemoryClock);
  923. #endif
  924. extern void api(int thr_id);
  925. extern struct pool *current_pool(void);
  926. extern int enabled_pools;
  927. extern void get_intrange(char *arg, int *val1, int *val2);
  928. extern bool detect_stratum(struct pool *pool, char *url);
  929. extern void print_summary(void);
  930. extern void adjust_quota_gcd(void);
  931. extern struct pool *add_pool(void);
  932. extern bool add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
  933. #define MAX_GPUDEVICES 16
  934. #define MAX_DEVICES 4096
  935. #define MIN_SHA_INTENSITY -10
  936. #define MIN_SHA_INTENSITY_STR "-10"
  937. #define MAX_SHA_INTENSITY 14
  938. #define MAX_SHA_INTENSITY_STR "14"
  939. #define MIN_SCRYPT_INTENSITY 8
  940. #define MIN_SCRYPT_INTENSITY_STR "8"
  941. #define MAX_SCRYPT_INTENSITY 20
  942. #define MAX_SCRYPT_INTENSITY_STR "20"
  943. #ifdef USE_SCRYPT
  944. #define MIN_INTENSITY (opt_scrypt ? MIN_SCRYPT_INTENSITY : MIN_SHA_INTENSITY)
  945. #define MIN_INTENSITY_STR (opt_scrypt ? MIN_SCRYPT_INTENSITY_STR : MIN_SHA_INTENSITY_STR)
  946. #define MAX_INTENSITY (opt_scrypt ? MAX_SCRYPT_INTENSITY : MAX_SHA_INTENSITY)
  947. #define MAX_INTENSITY_STR (opt_scrypt ? MAX_SCRYPT_INTENSITY_STR : MAX_SHA_INTENSITY_STR)
  948. #define MAX_GPU_INTENSITY MAX_SCRYPT_INTENSITY
  949. #else
  950. #define MIN_INTENSITY MIN_SHA_INTENSITY
  951. #define MIN_INTENSITY_STR MIN_SHA_INTENSITY_STR
  952. #define MAX_INTENSITY MAX_SHA_INTENSITY
  953. #define MAX_INTENSITY_STR MAX_SHA_INTENSITY_STR
  954. #define MAX_GPU_INTENSITY MAX_SHA_INTENSITY
  955. #endif
  956. extern bool hotplug_mode;
  957. extern int hotplug_time;
  958. extern struct list_head scan_devices;
  959. extern int nDevs;
  960. extern int num_processors;
  961. extern int hw_errors;
  962. extern bool use_syslog;
  963. extern bool opt_quiet;
  964. extern struct thr_info *control_thr;
  965. extern struct thr_info **mining_thr;
  966. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  967. extern int gpu_threads;
  968. #ifdef USE_SCRYPT
  969. extern bool opt_scrypt;
  970. #else
  971. #define opt_scrypt (0)
  972. #endif
  973. extern double total_secs;
  974. extern int mining_threads;
  975. extern int total_devices;
  976. extern int zombie_devs;
  977. extern struct cgpu_info **devices;
  978. extern int total_pools;
  979. extern struct pool **pools;
  980. extern struct strategies strategies[];
  981. extern enum pool_strategy pool_strategy;
  982. extern int opt_rotate_period;
  983. extern double total_rolling;
  984. extern double total_mhashes_done;
  985. extern unsigned int new_blocks;
  986. extern unsigned int found_blocks;
  987. extern int total_accepted, total_rejected, total_diff1;;
  988. extern int total_getworks, total_stale, total_discarded;
  989. extern double total_diff_accepted, total_diff_rejected, total_diff_stale;
  990. extern unsigned int local_work;
  991. extern unsigned int total_go, total_ro;
  992. extern const int opt_cutofftemp;
  993. extern int opt_log_interval;
  994. extern unsigned long long global_hashrate;
  995. extern char *current_fullhash;
  996. extern double current_diff;
  997. extern uint64_t best_diff;
  998. extern struct timeval block_timeval;
  999. extern char *workpadding;
  1000. #ifdef HAVE_OPENCL
  1001. typedef struct {
  1002. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  1003. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  1004. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  1005. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  1006. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  1007. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  1008. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  1009. cl_uint W16; cl_uint W17; cl_uint W2;
  1010. cl_uint PreVal4; cl_uint T1;
  1011. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  1012. cl_uint PreVal4addT1; cl_uint T1substate0;
  1013. cl_uint PreVal4_2;
  1014. cl_uint PreVal0;
  1015. cl_uint PreW18;
  1016. cl_uint PreW19;
  1017. cl_uint PreW31;
  1018. cl_uint PreW32;
  1019. /* For diakgcn */
  1020. cl_uint B1addK6, PreVal0addK7, W16addK16, W17addK17;
  1021. cl_uint zeroA, zeroB;
  1022. cl_uint oneA, twoA, threeA, fourA, fiveA, sixA, sevenA;
  1023. #ifdef USE_SCRYPT
  1024. struct work *work;
  1025. #endif
  1026. } dev_blk_ctx;
  1027. #else
  1028. typedef struct {
  1029. uint32_t nonce;
  1030. } dev_blk_ctx;
  1031. #endif
  1032. struct curl_ent {
  1033. CURL *curl;
  1034. struct list_head node;
  1035. struct timeval tv;
  1036. };
  1037. /* Disabled needs to be the lowest enum as a freshly calloced value will then
  1038. * equal disabled */
  1039. enum pool_enable {
  1040. POOL_DISABLED,
  1041. POOL_ENABLED,
  1042. POOL_REJECTING,
  1043. };
  1044. struct stratum_work {
  1045. char *job_id;
  1046. char *prev_hash;
  1047. unsigned char **merkle_bin;
  1048. char *bbversion;
  1049. char *nbit;
  1050. char *ntime;
  1051. bool clean;
  1052. size_t cb_len;
  1053. size_t header_len;
  1054. int merkles;
  1055. double diff;
  1056. };
  1057. #define RBUFSIZE 8192
  1058. #define RECVSIZE (RBUFSIZE - 4)
  1059. struct pool {
  1060. int pool_no;
  1061. int prio;
  1062. int accepted, rejected;
  1063. int seq_rejects;
  1064. int seq_getfails;
  1065. int solved;
  1066. int diff1;
  1067. char diff[8];
  1068. int quota;
  1069. int quota_gcd;
  1070. int quota_used;
  1071. double diff_accepted;
  1072. double diff_rejected;
  1073. double diff_stale;
  1074. bool submit_fail;
  1075. bool idle;
  1076. bool lagging;
  1077. bool probed;
  1078. enum pool_enable enabled;
  1079. bool submit_old;
  1080. bool removed;
  1081. bool lp_started;
  1082. char *hdr_path;
  1083. char *lp_url;
  1084. unsigned int getwork_requested;
  1085. unsigned int stale_shares;
  1086. unsigned int discarded_work;
  1087. unsigned int getfail_occasions;
  1088. unsigned int remotefail_occasions;
  1089. struct timeval tv_idle;
  1090. double utility;
  1091. int last_shares, shares;
  1092. char *rpc_req;
  1093. char *rpc_url;
  1094. char *rpc_userpass;
  1095. char *rpc_user, *rpc_pass;
  1096. proxytypes_t rpc_proxytype;
  1097. char *rpc_proxy;
  1098. pthread_mutex_t pool_lock;
  1099. cglock_t data_lock;
  1100. struct thread_q *submit_q;
  1101. struct thread_q *getwork_q;
  1102. pthread_t longpoll_thread;
  1103. pthread_t test_thread;
  1104. bool testing;
  1105. int curls;
  1106. pthread_cond_t cr_cond;
  1107. struct list_head curlring;
  1108. time_t last_share_time;
  1109. double last_share_diff;
  1110. uint64_t best_diff;
  1111. struct cgminer_stats cgminer_stats;
  1112. struct cgminer_pool_stats cgminer_pool_stats;
  1113. /* Stratum variables */
  1114. char *stratum_url;
  1115. char *stratum_port;
  1116. struct addrinfo stratum_hints;
  1117. SOCKETTYPE sock;
  1118. char *sockbuf;
  1119. size_t sockbuf_size;
  1120. char *sockaddr_url; /* stripped url used for sockaddr */
  1121. char *sockaddr_proxy_url;
  1122. char *sockaddr_proxy_port;
  1123. char *nonce1;
  1124. unsigned char *nonce1bin;
  1125. size_t n1_len;
  1126. uint32_t nonce2;
  1127. int n2size;
  1128. char *sessionid;
  1129. bool has_stratum;
  1130. bool stratum_active;
  1131. bool stratum_init;
  1132. bool stratum_notify;
  1133. struct stratum_work swork;
  1134. pthread_t stratum_sthread;
  1135. pthread_t stratum_rthread;
  1136. pthread_mutex_t stratum_lock;
  1137. struct thread_q *stratum_q;
  1138. int sshares; /* stratum shares submitted waiting on response */
  1139. /* GBT variables */
  1140. bool has_gbt;
  1141. cglock_t gbt_lock;
  1142. unsigned char previousblockhash[32];
  1143. unsigned char gbt_target[32];
  1144. char *coinbasetxn;
  1145. char *longpollid;
  1146. char *gbt_workid;
  1147. int gbt_expires;
  1148. uint32_t gbt_version;
  1149. uint32_t curtime;
  1150. uint32_t gbt_bits;
  1151. unsigned char *txn_hashes;
  1152. int gbt_txns;
  1153. int coinbase_len;
  1154. /* Shared by both stratum & GBT */
  1155. unsigned char *coinbase;
  1156. int nonce2_offset;
  1157. unsigned char header_bin[128];
  1158. int merkle_offset;
  1159. struct timeval tv_lastwork;
  1160. };
  1161. #define GETWORK_MODE_TESTPOOL 'T'
  1162. #define GETWORK_MODE_POOL 'P'
  1163. #define GETWORK_MODE_LP 'L'
  1164. #define GETWORK_MODE_BENCHMARK 'B'
  1165. #define GETWORK_MODE_STRATUM 'S'
  1166. #define GETWORK_MODE_GBT 'G'
  1167. struct work {
  1168. unsigned char data[128];
  1169. unsigned char midstate[32];
  1170. unsigned char target[32];
  1171. unsigned char hash[32];
  1172. #ifdef USE_SCRYPT
  1173. unsigned char device_target[32];
  1174. #endif
  1175. double device_diff;
  1176. uint64_t share_diff;
  1177. unsigned char hash2[32];
  1178. int rolls;
  1179. dev_blk_ctx blk;
  1180. struct thr_info *thr;
  1181. int thr_id;
  1182. struct pool *pool;
  1183. struct timeval tv_staged;
  1184. bool mined;
  1185. bool clone;
  1186. bool cloned;
  1187. int rolltime;
  1188. bool longpoll;
  1189. bool stale;
  1190. bool mandatory;
  1191. bool block;
  1192. bool queued;
  1193. bool stratum;
  1194. char *job_id;
  1195. uint32_t nonce2;
  1196. size_t nonce2_len;
  1197. char *ntime;
  1198. double sdiff;
  1199. char *nonce1;
  1200. bool gbt;
  1201. char *coinbase;
  1202. int gbt_txns;
  1203. unsigned int work_block;
  1204. int id;
  1205. UT_hash_handle hh;
  1206. double work_difficulty;
  1207. // Allow devices to identify work if multiple sub-devices
  1208. int subid;
  1209. // Allow devices to flag work for their own purposes
  1210. bool devflag;
  1211. struct timeval tv_getwork;
  1212. struct timeval tv_getwork_reply;
  1213. struct timeval tv_cloned;
  1214. struct timeval tv_work_start;
  1215. struct timeval tv_work_found;
  1216. char getwork_mode;
  1217. };
  1218. #ifdef USE_MODMINER
  1219. struct modminer_fpga_state {
  1220. bool work_running;
  1221. struct work running_work;
  1222. struct timeval tv_workstart;
  1223. uint32_t hashes;
  1224. char next_work_cmd[46];
  1225. char fpgaid;
  1226. bool overheated;
  1227. bool new_work;
  1228. uint32_t shares;
  1229. uint32_t shares_last_hw;
  1230. uint32_t hw_errors;
  1231. uint32_t shares_to_good;
  1232. uint32_t timeout_fail;
  1233. uint32_t success_more;
  1234. struct timeval last_changed;
  1235. struct timeval last_nonce;
  1236. struct timeval first_work;
  1237. bool death_stage_one;
  1238. bool tried_two_byte_temp;
  1239. bool one_byte_temp;
  1240. };
  1241. #endif
  1242. #define TAILBUFSIZ 64
  1243. #define tailsprintf(buf, bufsiz, fmt, ...) do { \
  1244. char tmp13[TAILBUFSIZ]; \
  1245. size_t len13, buflen = strlen(buf); \
  1246. snprintf(tmp13, sizeof(tmp13), fmt, ##__VA_ARGS__); \
  1247. len13 = strlen(tmp13); \
  1248. if ((buflen + len13) >= bufsiz) \
  1249. quit(1, "tailsprintf buffer overflow in %s %s line %d", __FILE__, __func__, __LINE__); \
  1250. strcat(buf, tmp13); \
  1251. } while (0)
  1252. extern void get_datestamp(char *, size_t, struct timeval *);
  1253. extern void inc_hw_errors(struct thr_info *thr);
  1254. extern bool test_nonce(struct work *work, uint32_t nonce);
  1255. extern void submit_tested_work(struct thr_info *thr, struct work *work);
  1256. extern bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  1257. extern bool submit_noffset_nonce(struct thr_info *thr, struct work *work, uint32_t nonce,
  1258. int noffset);
  1259. extern struct work *get_work(struct thr_info *thr, const int thr_id);
  1260. extern struct work *get_queued(struct cgpu_info *cgpu);
  1261. extern struct work *__find_work_bymidstate(struct work *que, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1262. extern struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1263. extern struct work *clone_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1264. extern void work_completed(struct cgpu_info *cgpu, struct work *work);
  1265. extern struct work *take_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
  1266. extern void hash_driver_work(struct thr_info *mythr);
  1267. extern void hash_queued_work(struct thr_info *mythr);
  1268. extern void _wlog(const char *str);
  1269. extern void _wlogprint(const char *str);
  1270. extern int curses_int(const char *query);
  1271. extern char *curses_input(const char *query);
  1272. extern void kill_work(void);
  1273. extern void switch_pools(struct pool *selected);
  1274. extern void discard_work(struct work *work);
  1275. extern void remove_pool(struct pool *pool);
  1276. extern void write_config(FILE *fcfg);
  1277. extern void zero_bestshare(void);
  1278. extern void zero_stats(void);
  1279. extern void default_save_file(char *filename);
  1280. extern bool log_curses_only(int prio, const char *datetime, const char *str);
  1281. extern void clear_logwin(void);
  1282. extern void logwin_update(void);
  1283. extern bool pool_tclear(struct pool *pool, bool *var);
  1284. extern struct thread_q *tq_new(void);
  1285. extern void tq_free(struct thread_q *tq);
  1286. extern bool tq_push(struct thread_q *tq, void *data);
  1287. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  1288. extern void tq_freeze(struct thread_q *tq);
  1289. extern void tq_thaw(struct thread_q *tq);
  1290. extern bool successful_connect;
  1291. extern void adl(void);
  1292. extern void app_restart(void);
  1293. extern void clean_work(struct work *work);
  1294. extern void free_work(struct work *work);
  1295. extern struct work *copy_work(struct work *base_work);
  1296. extern struct thr_info *get_thread(int thr_id);
  1297. extern struct cgpu_info *get_devices(int id);
  1298. enum api_data_type {
  1299. API_ESCAPE,
  1300. API_STRING,
  1301. API_CONST,
  1302. API_INT,
  1303. API_UINT,
  1304. API_UINT32,
  1305. API_UINT64,
  1306. API_DOUBLE,
  1307. API_ELAPSED,
  1308. API_BOOL,
  1309. API_TIMEVAL,
  1310. API_TIME,
  1311. API_MHS,
  1312. API_MHTOTAL,
  1313. API_TEMP,
  1314. API_UTILITY,
  1315. API_FREQ,
  1316. API_VOLTS,
  1317. API_HS,
  1318. API_DIFF,
  1319. API_PERCENT
  1320. };
  1321. struct api_data {
  1322. enum api_data_type type;
  1323. char *name;
  1324. void *data;
  1325. bool data_was_malloc;
  1326. struct api_data *prev;
  1327. struct api_data *next;
  1328. };
  1329. extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
  1330. extern struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data);
  1331. extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
  1332. extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
  1333. extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
  1334. extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
  1335. extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
  1336. extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
  1337. extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
  1338. extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
  1339. extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
  1340. extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
  1341. extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
  1342. extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
  1343. extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
  1344. extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
  1345. extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
  1346. extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
  1347. extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
  1348. extern struct api_data *api_add_diff(struct api_data *root, char *name, double *data, bool copy_data);
  1349. extern struct api_data *api_add_percent(struct api_data *root, char *name, double *data, bool copy_data);
  1350. #endif /* __MINER_H__ */