miner.h 43 KB

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