miner.h 44 KB

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