miner.h 43 KB

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