miner.h 44 KB

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