main.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425
  1. /*
  2. * Copyright 2011 Con Kolivas
  3. * Copyright 2010 Jeff Garzik
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 2 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <curses.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include <unistd.h>
  18. #include <sys/time.h>
  19. #include <time.h>
  20. #include <math.h>
  21. #include <stdarg.h>
  22. #include <assert.h>
  23. #include <signal.h>
  24. #ifndef WIN32
  25. #include <sys/resource.h>
  26. #endif
  27. #include <ccan/opt/opt.h>
  28. #include <jansson.h>
  29. #include <curl/curl.h>
  30. #include "compat.h"
  31. #include "miner.h"
  32. #include "findnonce.h"
  33. #include "ocl.h"
  34. #define PROGRAM_NAME "cgminer"
  35. #define DEF_RPC_URL "http://127.0.0.1:8332/"
  36. #define DEF_RPC_USERNAME "rpcuser"
  37. #define DEF_RPC_PASSWORD "rpcpass"
  38. #define DEF_RPC_USERPASS DEF_RPC_USERNAME ":" DEF_RPC_PASSWORD
  39. #ifdef __linux /* Linux specific policy and affinity management */
  40. #include <sched.h>
  41. static inline void drop_policy(void)
  42. {
  43. struct sched_param param;
  44. #ifdef SCHED_BATCH
  45. #ifdef SCHED_IDLE
  46. if (unlikely(sched_setscheduler(0, SCHED_IDLE, &param) == -1))
  47. #endif
  48. sched_setscheduler(0, SCHED_BATCH, &param);
  49. #endif
  50. }
  51. static inline void affine_to_cpu(int id, int cpu)
  52. {
  53. cpu_set_t set;
  54. CPU_ZERO(&set);
  55. CPU_SET(cpu, &set);
  56. sched_setaffinity(0, sizeof(&set), &set);
  57. applog(LOG_INFO, "Binding cpu mining thread %d to cpu %d", id, cpu);
  58. }
  59. #else
  60. static inline void drop_policy(void)
  61. {
  62. }
  63. static inline void affine_to_cpu(int id, int cpu)
  64. {
  65. }
  66. #endif
  67. enum workio_commands {
  68. WC_GET_WORK,
  69. WC_SUBMIT_WORK,
  70. WC_DIE,
  71. };
  72. struct workio_cmd {
  73. enum workio_commands cmd;
  74. struct thr_info *thr;
  75. union {
  76. struct work *work;
  77. } u;
  78. };
  79. enum sha256_algos {
  80. ALGO_C, /* plain C */
  81. ALGO_4WAY, /* parallel SSE2 */
  82. ALGO_VIA, /* VIA padlock */
  83. ALGO_CRYPTOPP, /* Crypto++ (C) */
  84. ALGO_CRYPTOPP_ASM32, /* Crypto++ 32-bit assembly */
  85. ALGO_SSE2_64, /* SSE2 for x86_64 */
  86. };
  87. static const char *algo_names[] = {
  88. [ALGO_C] = "c",
  89. #ifdef WANT_SSE2_4WAY
  90. [ALGO_4WAY] = "4way",
  91. #endif
  92. #ifdef WANT_VIA_PADLOCK
  93. [ALGO_VIA] = "via",
  94. #endif
  95. [ALGO_CRYPTOPP] = "cryptopp",
  96. #ifdef WANT_CRYPTOPP_ASM32
  97. [ALGO_CRYPTOPP_ASM32] = "cryptopp_asm32",
  98. #endif
  99. #ifdef WANT_X8664_SSE2
  100. [ALGO_SSE2_64] = "sse2_64",
  101. #endif
  102. };
  103. bool opt_debug = false;
  104. bool opt_protocol = false;
  105. bool want_longpoll = true;
  106. bool have_longpoll = false;
  107. bool use_syslog = false;
  108. static bool opt_quiet = false;
  109. static int opt_retries = -1;
  110. static int opt_fail_pause = 5;
  111. static int opt_log_interval = 5;
  112. bool opt_log_output = false;
  113. static bool opt_dynamic = true;
  114. static int opt_queue;
  115. int opt_vectors;
  116. int opt_worksize;
  117. int opt_scantime = 60;
  118. static const bool opt_time = true;
  119. #ifdef WANT_X8664_SSE2
  120. static enum sha256_algos opt_algo = ALGO_SSE2_64;
  121. #else
  122. static enum sha256_algos opt_algo = ALGO_C;
  123. #endif
  124. static int nDevs;
  125. static int opt_g_threads = 2;
  126. static int opt_device;
  127. static int total_devices;
  128. static bool gpu_devices[16];
  129. static int gpu_threads;
  130. static bool forced_n_threads;
  131. static int opt_n_threads;
  132. static int mining_threads;
  133. static int num_processors;
  134. static int scan_intensity;
  135. static bool use_curses = true;
  136. static char *rpc_url;
  137. static char *rpc_userpass;
  138. static char *rpc_user, *rpc_pass;
  139. struct thr_info *thr_info;
  140. static int work_thr_id;
  141. int longpoll_thr_id;
  142. static int stage_thr_id;
  143. static int watchdog_thr_id;
  144. struct work_restart *work_restart = NULL;
  145. static pthread_mutex_t hash_lock;
  146. static pthread_mutex_t qd_lock;
  147. static pthread_mutex_t stgd_lock;
  148. static pthread_mutex_t curses_lock;
  149. static double total_mhashes_done;
  150. static struct timeval total_tv_start, total_tv_end;
  151. static int accepted, rejected;
  152. int hw_errors;
  153. static int total_queued, total_staged, lp_staged;
  154. static bool localgen = false;
  155. static bool idlenet = false;
  156. static unsigned int getwork_requested;
  157. static unsigned int stale_shares;
  158. static unsigned int discarded_work;
  159. static unsigned int new_blocks;
  160. static unsigned int local_work;
  161. static unsigned int localgen_occasions;
  162. static unsigned int remotefail_occasions;
  163. static char current_block[37];
  164. static char longpoll_block[37];
  165. static char blank[37];
  166. static char datestamp[40];
  167. static char blockdate[40];
  168. struct sigaction termhandler, inthandler;
  169. struct thread_q *getq;
  170. static void applog_and_exit(const char *fmt, ...)
  171. {
  172. va_list ap;
  173. va_start(ap, fmt);
  174. vapplog(LOG_ERR, fmt, ap);
  175. va_end(ap);
  176. exit(1);
  177. }
  178. /* FIXME: Use asprintf for better errors. */
  179. static char *set_algo(const char *arg, enum sha256_algos *algo)
  180. {
  181. enum sha256_algos i;
  182. for (i = 0; i < ARRAY_SIZE(algo_names); i++) {
  183. if (algo_names[i] && !strcmp(arg, algo_names[i])) {
  184. *algo = i;
  185. return NULL;
  186. }
  187. }
  188. return "Unknown algorithm";
  189. }
  190. static void show_algo(char buf[OPT_SHOW_LEN], const enum sha256_algos *algo)
  191. {
  192. strncpy(buf, algo_names[*algo], OPT_SHOW_LEN);
  193. }
  194. static char *set_int_range(const char *arg, int *i, int min, int max)
  195. {
  196. char *err = opt_set_intval(arg, i);
  197. if (err)
  198. return err;
  199. if (*i < min || *i > max)
  200. return "Value out of range";
  201. return NULL;
  202. }
  203. static char *set_int_0_to_9999(const char *arg, int *i)
  204. {
  205. return set_int_range(arg, i, 0, 9999);
  206. }
  207. static char *forced_int_0_to_14(const char *arg, int *i)
  208. {
  209. opt_dynamic = false;
  210. return set_int_range(arg, i, 0, 14);
  211. }
  212. static char *force_nthreads_int(const char *arg, int *i)
  213. {
  214. forced_n_threads = true;
  215. return set_int_range(arg, i, 0, 9999);
  216. }
  217. static char *set_int_0_to_10(const char *arg, int *i)
  218. {
  219. return set_int_range(arg, i, 0, 10);
  220. }
  221. static char *set_devices(const char *arg, int *i)
  222. {
  223. char *err = opt_set_intval(arg, i);
  224. if (err)
  225. return err;
  226. if (*i < 0 || *i > 15)
  227. return "Invalid GPU device number";
  228. total_devices++;
  229. gpu_devices[*i] = true;
  230. return NULL;
  231. }
  232. static char *set_url(const char *arg, char **p)
  233. {
  234. opt_set_charp(arg, p);
  235. if (strncmp(arg, "http://", 7) &&
  236. strncmp(arg, "https://", 8))
  237. return "URL must start with http:// or https://";
  238. return NULL;
  239. }
  240. static char *set_vector(const char *arg, int *i)
  241. {
  242. char *err = opt_set_intval(arg, i);
  243. if (err)
  244. return err;
  245. if (*i != 1 && *i != 2 && *i != 4)
  246. return "Valid vectors are 1, 2 or 4";
  247. return NULL;
  248. }
  249. static char *enable_debug(bool *flag)
  250. {
  251. *flag = true;
  252. /* Turn out verbose output, too. */
  253. opt_log_output = true;
  254. return NULL;
  255. }
  256. /* These options are available from config file or commandline */
  257. static struct opt_table opt_config_table[] = {
  258. OPT_WITH_ARG("--algo|-a",
  259. set_algo, show_algo, &opt_algo,
  260. "Specify sha256 implementation:\n"
  261. "\tc\t\tLinux kernel sha256, implemented in C"
  262. #ifdef WANT_SSE2_4WAY
  263. "\n\t4way\t\ttcatm's 4-way SSE2 implementation"
  264. #endif
  265. #ifdef WANT_VIA_PADLOCK
  266. "\n\tvia\t\tVIA padlock implementation"
  267. #endif
  268. "\n\tcryptopp\tCrypto++ C/C++ implementation"
  269. #ifdef WANT_CRYPTOPP_ASM32
  270. "\n\tcryptopp_asm32\tCrypto++ 32-bit assembler implementation"
  271. #endif
  272. #ifdef WANT_X8664_SSE2
  273. "\n\tsse2_64\t\tSSE2 implementation for x86_64 machines"
  274. #endif
  275. ),
  276. OPT_WITH_ARG("--cpu-threads|-t",
  277. force_nthreads_int, opt_show_intval, &opt_n_threads,
  278. "Number of miner CPU threads"),
  279. OPT_WITHOUT_ARG("--debug|-D",
  280. enable_debug, &opt_debug,
  281. "Enable debug output"),
  282. #ifdef HAVE_OPENCL
  283. OPT_WITH_ARG("--device|-d",
  284. set_devices, NULL, &opt_device,
  285. "Select device to use, (Use repeat -d for multiple devices, default: all)"),
  286. OPT_WITH_ARG("--gpu-threads|-g",
  287. set_int_0_to_10, opt_show_intval, &opt_g_threads,
  288. "Number of threads per GPU (0 - 10)"),
  289. OPT_WITH_ARG("--intensity|-I",
  290. forced_int_0_to_14, opt_show_intval, &scan_intensity,
  291. "Intensity of GPU scanning (0 - 14, default: dynamic to maintain desktop interactivity)"),
  292. #endif
  293. OPT_WITH_ARG("--log|-l",
  294. set_int_0_to_9999, opt_show_intval, &opt_log_interval,
  295. "Interval in seconds between log output"),
  296. OPT_WITHOUT_ARG("--no-longpoll",
  297. opt_set_invbool, &want_longpoll,
  298. "Disable X-Long-Polling support"),
  299. OPT_WITH_ARG("--pass|-p",
  300. opt_set_charp, NULL, &rpc_pass,
  301. "Password for bitcoin JSON-RPC server"),
  302. OPT_WITHOUT_ARG("--protocol-dump|-P",
  303. opt_set_bool, &opt_protocol,
  304. "Verbose dump of protocol-level activities"),
  305. OPT_WITH_ARG("--queue|-Q",
  306. set_int_0_to_10, opt_show_intval, &opt_queue,
  307. "Number of extra work items to queue (0 - 10)"),
  308. OPT_WITHOUT_ARG("--quiet|-q",
  309. opt_set_bool, &opt_quiet,
  310. "Disable per-thread hashmeter output"),
  311. OPT_WITH_ARG("--retries|-r",
  312. opt_set_intval, opt_show_intval, &opt_retries,
  313. "Number of times to retry before giving up, if JSON-RPC call fails (-1 means never)"),
  314. OPT_WITH_ARG("--retry-pause|-R",
  315. set_int_0_to_9999, opt_show_intval, &opt_fail_pause,
  316. "Number of seconds to pause, between retries"),
  317. OPT_WITH_ARG("--scan-time|-s",
  318. set_int_0_to_9999, opt_show_intval, &opt_scantime,
  319. "Upper bound on time spent scanning current work, in seconds"),
  320. #ifdef HAVE_SYSLOG_H
  321. OPT_WITHOUT_ARG("--syslog",
  322. opt_set_bool, &use_syslog,
  323. "Use system log for output messages (default: standard error)"),
  324. #endif
  325. OPT_WITHOUT_ARG("--text-only|-T",
  326. opt_set_invbool, &use_curses,
  327. "Disable ncurses formatted screen output"),
  328. OPT_WITH_ARG("--url|-o",
  329. set_url, opt_show_charp, &rpc_url,
  330. "URL for bitcoin JSON-RPC server"),
  331. OPT_WITH_ARG("--user|-u",
  332. opt_set_charp, NULL, &rpc_user,
  333. "Username for bitcoin JSON-RPC server"),
  334. #ifdef HAVE_OPENCL
  335. OPT_WITH_ARG("--vectors|-v",
  336. set_vector, NULL, &opt_vectors,
  337. "Override detected optimal vector width (1, 2 or 4)"),
  338. #endif
  339. OPT_WITHOUT_ARG("--verbose",
  340. opt_set_bool, &opt_log_output,
  341. "Log verbose output to stderr as well as status output"),
  342. #ifdef HAVE_OPENCL
  343. OPT_WITH_ARG("--worksize|-w",
  344. set_int_0_to_9999, opt_show_intval, &opt_worksize,
  345. "Override detected optimal worksize"),
  346. #endif
  347. OPT_WITH_ARG("--userpass|-O",
  348. opt_set_charp, NULL, &rpc_userpass,
  349. "Username:Password pair for bitcoin JSON-RPC server"),
  350. OPT_ENDTABLE
  351. };
  352. static char *parse_config(json_t *config)
  353. {
  354. static char err_buf[200];
  355. json_t *val;
  356. struct opt_table *opt;
  357. for (opt = opt_config_table; opt->type != OPT_END; opt++) {
  358. char *p, *name;
  359. /* We don't handle subtables. */
  360. assert(!(opt->type & OPT_SUBTABLE));
  361. /* Pull apart the option name(s). */
  362. name = strdup(opt->names);
  363. for (p = strtok(name, "|"); p; p = strtok(NULL, "|")) {
  364. char *err;
  365. /* Ignore short options. */
  366. if (p[1] != '-')
  367. continue;
  368. val = json_object_get(config, p+2);
  369. if (!val)
  370. continue;
  371. if ((opt->type & OPT_HASARG) && json_is_string(val)) {
  372. err = opt->cb_arg(json_string_value(val),
  373. opt->u.arg);
  374. } else if ((opt->type&OPT_NOARG) && json_is_true(val)) {
  375. err = opt->cb(opt->u.arg);
  376. } else {
  377. err = "Invalid value";
  378. }
  379. if (err) {
  380. sprintf(err_buf, "Parsing JSON option %s: %s",
  381. p, err);
  382. return err_buf;
  383. }
  384. }
  385. free(name);
  386. }
  387. return NULL;
  388. }
  389. static char *load_config(const char *arg, void *unused)
  390. {
  391. json_error_t err;
  392. json_t *config;
  393. config = json_load_file(arg, 0, &err);
  394. if (!json_is_object(config))
  395. return "JSON decode of file failed";
  396. /* Parse the config now, so we can override it. That can keep pointers
  397. * so don't free config object. */
  398. return parse_config(config);
  399. }
  400. static char *print_ndevs_and_exit(int *ndevs)
  401. {
  402. printf("%i GPU devices detected\n", *ndevs);
  403. exit(*ndevs);
  404. }
  405. /* These options are available from commandline only */
  406. static struct opt_table opt_cmdline_table[] = {
  407. OPT_WITH_ARG("--config|-c",
  408. load_config, NULL, NULL,
  409. "Load a JSON-format configuration file\n"
  410. "See example-cfg.json for an example configuration."),
  411. OPT_WITHOUT_ARG("--help|-h",
  412. opt_usage_and_exit,
  413. #ifdef HAVE_OPENCL
  414. "\nBuilt with CPU and GPU mining support.\n\n",
  415. #else
  416. "\nBuilt with CPU mining support only.\n\n",
  417. #endif
  418. "Print this message"),
  419. OPT_WITHOUT_ARG("--ndevs|-n",
  420. print_ndevs_and_exit, &nDevs,
  421. "Enumerate number of detected GPUs and exit"),
  422. OPT_ENDTABLE
  423. };
  424. static bool jobj_binary(const json_t *obj, const char *key,
  425. void *buf, size_t buflen)
  426. {
  427. const char *hexstr;
  428. json_t *tmp;
  429. tmp = json_object_get(obj, key);
  430. if (unlikely(!tmp)) {
  431. applog(LOG_ERR, "JSON key '%s' not found", key);
  432. return false;
  433. }
  434. hexstr = json_string_value(tmp);
  435. if (unlikely(!hexstr)) {
  436. applog(LOG_ERR, "JSON key '%s' is not a string", key);
  437. return false;
  438. }
  439. if (!hex2bin(buf, hexstr, buflen))
  440. return false;
  441. return true;
  442. }
  443. static bool work_decode(const json_t *val, struct work *work)
  444. {
  445. if (unlikely(!jobj_binary(val, "midstate",
  446. work->midstate, sizeof(work->midstate)))) {
  447. applog(LOG_ERR, "JSON inval midstate");
  448. goto err_out;
  449. }
  450. if (unlikely(!jobj_binary(val, "data", work->data, sizeof(work->data)))) {
  451. applog(LOG_ERR, "JSON inval data");
  452. goto err_out;
  453. }
  454. if (unlikely(!jobj_binary(val, "hash1", work->hash1, sizeof(work->hash1)))) {
  455. applog(LOG_ERR, "JSON inval hash1");
  456. goto err_out;
  457. }
  458. if (unlikely(!jobj_binary(val, "target", work->target, sizeof(work->target)))) {
  459. applog(LOG_ERR, "JSON inval target");
  460. goto err_out;
  461. }
  462. memset(work->hash, 0, sizeof(work->hash));
  463. return true;
  464. err_out:
  465. return false;
  466. }
  467. static inline int dev_from_id(int thr_id)
  468. {
  469. return thr_info[thr_id].cgpu->cpu_gpu;
  470. }
  471. static WINDOW *mainwin, *statuswin, *logwin;
  472. static double total_secs = 0.1;
  473. static char statusline[256];
  474. static int cpucursor, gpucursor, logstart, logcursor;
  475. static bool curses_active = false;
  476. static struct cgpu_info *gpus, *cpus;
  477. static void text_print_status(int thr_id)
  478. {
  479. struct cgpu_info *cgpu = thr_info[thr_id].cgpu;
  480. printf(" %sPU %d: [%.1f Mh/s] [Q:%d A:%d R:%d HW:%d E:%.0f%% U:%.2f/m]\n",
  481. cgpu->is_gpu ? "G" : "C", cgpu->cpu_gpu, cgpu->total_mhashes / total_secs,
  482. cgpu->getworks, cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  483. cgpu->efficiency, cgpu->utility);
  484. }
  485. /* Must be called with curses mutex lock held and curses_active */
  486. static void curses_print_status(int thr_id)
  487. {
  488. wmove(statuswin, 0, 0);
  489. wattron(statuswin, A_BOLD);
  490. wprintw(statuswin, " " PROGRAM_NAME " version " VERSION " - Started: %s", datestamp);
  491. wattroff(statuswin, A_BOLD);
  492. wmove(statuswin, 1, 0);
  493. whline(statuswin, '-', 80);
  494. wmove(statuswin, 2,0);
  495. wprintw(statuswin, " %s", statusline);
  496. wclrtoeol(statuswin);
  497. wmove(statuswin, 3,0);
  498. wprintw(statuswin, " TQ: %d ST: %d LS: %d SS: %d DW: %d NB: %d LW: %d LO: %d RF: %d I: %d",
  499. total_queued, total_staged, lp_staged, stale_shares, discarded_work, new_blocks,
  500. local_work, localgen_occasions, remotefail_occasions, scan_intensity);
  501. wclrtoeol(statuswin);
  502. wmove(statuswin, 4, 0);
  503. wprintw(statuswin, " Block %s started: %s", current_block + 4, blockdate);
  504. wmove(statuswin, 5, 0);
  505. whline(statuswin, '-', 80);
  506. wmove(statuswin, logstart - 1, 0);
  507. whline(statuswin, '-', 80);
  508. if (thr_id >= 0 && thr_id < gpu_threads) {
  509. int gpu = dev_from_id(thr_id);
  510. struct cgpu_info *cgpu = &gpus[gpu];
  511. wmove(statuswin, gpucursor + gpu, 0);
  512. wprintw(statuswin, " GPU %d: [%.1f Mh/s] [Q:%d A:%d R:%d HW:%d E:%.0f%% U:%.2f/m]",
  513. gpu, cgpu->total_mhashes / total_secs,
  514. cgpu->getworks, cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  515. cgpu->efficiency, cgpu->utility);
  516. wclrtoeol(statuswin);
  517. } else if (thr_id >= gpu_threads) {
  518. int cpu = dev_from_id(thr_id);
  519. struct cgpu_info *cgpu = &cpus[cpu];
  520. wmove(statuswin, cpucursor + cpu, 0);
  521. wprintw(statuswin, " CPU %d: [%.1f Mh/s] [Q:%d A:%d R:%d HW:%d E:%.0f%% U:%.2f/m]",
  522. cpu, cgpu->total_mhashes / total_secs,
  523. cgpu->getworks, cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  524. cgpu->efficiency, cgpu->utility);
  525. wclrtoeol(statuswin);
  526. }
  527. wrefresh(statuswin);
  528. }
  529. static void print_status(int thr_id)
  530. {
  531. if (!curses_active)
  532. text_print_status(thr_id);
  533. else {
  534. pthread_mutex_lock(&curses_lock);
  535. curses_print_status(thr_id);
  536. wrefresh(statuswin);
  537. pthread_mutex_unlock(&curses_lock);
  538. }
  539. }
  540. void log_curses(const char *f, va_list ap)
  541. {
  542. if (curses_active) {
  543. pthread_mutex_lock(&curses_lock);
  544. vw_printw(logwin, f, ap);
  545. wrefresh(logwin);
  546. pthread_mutex_unlock(&curses_lock);
  547. } else
  548. vprintf(f, ap);
  549. }
  550. static bool submit_fail = false;
  551. static bool submit_upstream_work(const struct work *work)
  552. {
  553. char *hexstr = NULL;
  554. json_t *val, *res;
  555. char s[345];
  556. bool rc = false;
  557. int thr_id = work->thr_id;
  558. struct cgpu_info *cgpu = thr_info[thr_id].cgpu;
  559. CURL *curl = curl_easy_init();
  560. if (unlikely(!curl)) {
  561. applog(LOG_ERR, "CURL initialisation failed");
  562. return rc;
  563. }
  564. /* build hex string */
  565. hexstr = bin2hex(work->data, sizeof(work->data));
  566. if (unlikely(!hexstr)) {
  567. applog(LOG_ERR, "submit_upstream_work OOM");
  568. goto out_nofree;
  569. }
  570. /* build JSON-RPC request */
  571. sprintf(s,
  572. "{\"method\": \"getwork\", \"params\": [ \"%s\" ], \"id\":1}\r\n",
  573. hexstr);
  574. if (opt_debug)
  575. applog(LOG_DEBUG, "DBG: sending RPC call: %s", s);
  576. /* issue JSON-RPC request */
  577. val = json_rpc_call(curl, rpc_url, rpc_userpass, s, false, false);
  578. if (unlikely(!val)) {
  579. applog(LOG_INFO, "submit_upstream_work json_rpc_call failed");
  580. if (!submit_fail) {
  581. submit_fail = true;
  582. remotefail_occasions++;
  583. applog(LOG_WARNING, "Upstream communication failure, caching submissions");
  584. }
  585. goto out;
  586. } else if (submit_fail) {
  587. submit_fail = false;
  588. applog(LOG_WARNING, "Upstream communication resumed, submitting work");
  589. }
  590. res = json_object_get(val, "result");
  591. /* Theoretically threads could race when modifying accepted and
  592. * rejected values but the chance of two submits completing at the
  593. * same time is zero so there is no point adding extra locking */
  594. if (json_is_true(res)) {
  595. cgpu->accepted++;
  596. accepted++;
  597. if (opt_debug)
  598. applog(LOG_DEBUG, "PROOF OF WORK RESULT: true (yay!!!)");
  599. if (!opt_quiet)
  600. applog(LOG_WARNING, "Share accepted from %sPU %d thread %d",
  601. cgpu->is_gpu? "G" : "C", cgpu->cpu_gpu, thr_id);
  602. } else {
  603. cgpu->rejected++;
  604. rejected++;
  605. if (opt_debug)
  606. applog(LOG_DEBUG, "PROOF OF WORK RESULT: false (booooo)");
  607. if (!opt_quiet)
  608. applog(LOG_WARNING, "Share rejected from %sPU %d thread %d",
  609. cgpu->is_gpu? "G" : "C", cgpu->cpu_gpu, thr_id);
  610. }
  611. cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
  612. cgpu->efficiency = cgpu->getworks ? cgpu->accepted * 100.0 / cgpu->getworks : 0.0;
  613. if (!opt_quiet)
  614. print_status(thr_id);
  615. applog(LOG_INFO, "%sPU %d Q:%d A:%d R:%d HW:%d E:%.0f%% U:%.2f/m",
  616. cgpu->is_gpu? "G" : "C", cgpu->cpu_gpu, cgpu->getworks, cgpu->accepted,
  617. cgpu->rejected, cgpu->hw_errors, cgpu->efficiency, cgpu->utility);
  618. json_decref(val);
  619. rc = true;
  620. out:
  621. free(hexstr);
  622. out_nofree:
  623. curl_easy_cleanup(curl);
  624. return rc;
  625. }
  626. static const char *rpc_req =
  627. "{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
  628. static bool get_upstream_work(struct work *work)
  629. {
  630. json_t *val;
  631. bool rc = false;
  632. CURL *curl = curl_easy_init();
  633. if (unlikely(!curl)) {
  634. applog(LOG_ERR, "CURL initialisation failed");
  635. return rc;
  636. }
  637. val = json_rpc_call(curl, rpc_url, rpc_userpass, rpc_req,
  638. want_longpoll, false);
  639. if (unlikely(!val)) {
  640. applog(LOG_DEBUG, "Failed json_rpc_call in get_upstream_work");
  641. goto out;
  642. }
  643. rc = work_decode(json_object_get(val, "result"), work);
  644. json_decref(val);
  645. out:
  646. curl_easy_cleanup(curl);
  647. return rc;
  648. }
  649. static void workio_cmd_free(struct workio_cmd *wc)
  650. {
  651. if (!wc)
  652. return;
  653. switch (wc->cmd) {
  654. case WC_SUBMIT_WORK:
  655. free(wc->u.work);
  656. break;
  657. default: /* do nothing */
  658. break;
  659. }
  660. memset(wc, 0, sizeof(*wc)); /* poison */
  661. free(wc);
  662. }
  663. static void disable_curses(void)
  664. {
  665. if (curses_active) {
  666. curses_active = false;
  667. delwin(logwin);
  668. delwin(statuswin);
  669. delwin(mainwin);
  670. endwin();
  671. refresh();
  672. }
  673. }
  674. void kill_work(void)
  675. {
  676. struct workio_cmd *wc;
  677. struct thr_info *thr;
  678. unsigned int i;
  679. disable_curses();
  680. applog(LOG_INFO, "Received kill message");
  681. /* Kill the watchdog thread */
  682. thr = &thr_info[watchdog_thr_id];
  683. pthread_cancel(thr->pth);
  684. /* Stop the mining threads*/
  685. for (i = 0; i < mining_threads; i++) {
  686. thr = &thr_info[i];
  687. tq_freeze(thr->q);
  688. /* No need to check if this succeeds or not */
  689. pthread_cancel(thr->pth);
  690. }
  691. /* Stop the others */
  692. thr = &thr_info[stage_thr_id];
  693. pthread_cancel(thr->pth);
  694. thr = &thr_info[longpoll_thr_id];
  695. pthread_cancel(thr->pth);
  696. wc = calloc(1, sizeof(*wc));
  697. if (unlikely(!wc)) {
  698. applog(LOG_ERR, "Failed to calloc wc in kill_work");
  699. /* We're just trying to die anyway, so forget graceful */
  700. exit (1);
  701. }
  702. wc->cmd = WC_DIE;
  703. wc->thr = 0;
  704. if (unlikely(!tq_push(thr_info[work_thr_id].q, wc))) {
  705. applog(LOG_ERR, "Failed to tq_push work in kill_work");
  706. exit (1);
  707. }
  708. }
  709. static void sighandler(int sig)
  710. {
  711. /* Restore signal handlers so we can still quit if kill_work fails */
  712. sigaction(SIGTERM, &termhandler, NULL);
  713. sigaction(SIGINT, &inthandler, NULL);
  714. kill_work();
  715. }
  716. static void *get_work_thread(void *userdata)
  717. {
  718. struct workio_cmd *wc = (struct workio_cmd *)userdata;
  719. struct work *ret_work;
  720. int failures = 0;
  721. pthread_detach(pthread_self());
  722. ret_work = calloc(1, sizeof(*ret_work));
  723. if (unlikely(!ret_work)) {
  724. applog(LOG_ERR, "Failed to calloc ret_work in workio_get_work");
  725. kill_work();
  726. goto out;
  727. }
  728. /* obtain new work from bitcoin via JSON-RPC */
  729. while (!get_upstream_work(ret_work)) {
  730. if (unlikely((opt_retries >= 0) && (++failures > opt_retries))) {
  731. applog(LOG_ERR, "json_rpc_call failed, terminating workio thread");
  732. free(ret_work);
  733. kill_work();
  734. goto out;
  735. }
  736. /* pause, then restart work-request loop */
  737. applog(LOG_DEBUG, "json_rpc_call failed on get work, retry after %d seconds",
  738. opt_fail_pause);
  739. sleep(opt_fail_pause);
  740. }
  741. /* send work to requesting thread */
  742. if (unlikely(!tq_push(thr_info[stage_thr_id].q, ret_work))) {
  743. applog(LOG_ERR, "Failed to tq_push work in workio_get_work");
  744. kill_work();
  745. free(ret_work);
  746. }
  747. out:
  748. workio_cmd_free(wc);
  749. return NULL;
  750. }
  751. static bool workio_get_work(struct workio_cmd *wc)
  752. {
  753. pthread_t get_thread;
  754. if (unlikely(pthread_create(&get_thread, NULL, get_work_thread, (void *)wc))) {
  755. applog(LOG_ERR, "Failed to create get_work_thread");
  756. return false;
  757. }
  758. return true;
  759. }
  760. static bool stale_work(struct work *work)
  761. {
  762. bool ret = false;
  763. char *hexstr;
  764. if (!strncmp(blank, current_block, 36))
  765. return ret;
  766. hexstr = bin2hex(work->data, 36);
  767. if (unlikely(!hexstr)) {
  768. applog(LOG_ERR, "submit_work_thread OOM");
  769. return ret;
  770. }
  771. if (strncmp(hexstr, current_block, 36))
  772. ret = true;
  773. free(hexstr);
  774. return ret;
  775. }
  776. static void *submit_work_thread(void *userdata)
  777. {
  778. struct workio_cmd *wc = (struct workio_cmd *)userdata;
  779. int failures = 0;
  780. pthread_detach(pthread_self());
  781. if (stale_work(wc->u.work)) {
  782. applog(LOG_WARNING, "Stale share detected, discarding");
  783. stale_shares++;
  784. goto out;
  785. }
  786. /* submit solution to bitcoin via JSON-RPC */
  787. while (!submit_upstream_work(wc->u.work)) {
  788. if (stale_work(wc->u.work)) {
  789. applog(LOG_WARNING, "Stale share detected, discarding");
  790. stale_shares++;
  791. break;
  792. }
  793. if (unlikely((opt_retries >= 0) && (++failures > opt_retries))) {
  794. applog(LOG_ERR, "Failed %d retries ...terminating workio thread", opt_retries);
  795. kill_work();
  796. break;
  797. }
  798. /* pause, then restart work-request loop */
  799. applog(LOG_INFO, "json_rpc_call failed on submit_work, retry after %d seconds",
  800. opt_fail_pause);
  801. sleep(opt_fail_pause);
  802. }
  803. out:
  804. workio_cmd_free(wc);
  805. return NULL;
  806. }
  807. static bool workio_submit_work(struct workio_cmd *wc)
  808. {
  809. pthread_t submit_thread;
  810. if (unlikely(pthread_create(&submit_thread, NULL, submit_work_thread, (void *)wc))) {
  811. applog(LOG_ERR, "Failed to create submit_work_thread");
  812. return false;
  813. }
  814. return true;
  815. }
  816. static void inc_staged(int inc, bool lp)
  817. {
  818. pthread_mutex_lock(&stgd_lock);
  819. if (lp) {
  820. lp_staged += inc;
  821. total_staged += inc;
  822. idlenet = true;
  823. } else if (lp_staged) {
  824. if (!--lp_staged) {
  825. unsigned int i;
  826. /* Make sure the watchdog thread doesn't kill the mining
  827. * threads once we unset the idlenet flag */
  828. for (i = 0; i < mining_threads; i++)
  829. gettimeofday(&thr_info[i].last, NULL);
  830. idlenet = false;
  831. }
  832. } else
  833. total_staged += inc;
  834. pthread_mutex_unlock(&stgd_lock);
  835. }
  836. static void dec_staged(int inc)
  837. {
  838. pthread_mutex_lock(&stgd_lock);
  839. total_staged -= inc;
  840. pthread_mutex_unlock(&stgd_lock);
  841. }
  842. static int requests_staged(void)
  843. {
  844. int ret;
  845. pthread_mutex_lock(&stgd_lock);
  846. ret = total_staged;
  847. pthread_mutex_unlock(&stgd_lock);
  848. return ret;
  849. }
  850. static void set_curblock(char *hexstr)
  851. {
  852. struct timeval tv_now;
  853. struct tm tm;
  854. memcpy(current_block, hexstr, 36);
  855. gettimeofday(&tv_now, NULL);
  856. localtime_r(&tv_now.tv_sec, &tm);
  857. sprintf(blockdate, "[%d-%02d-%02d %02d:%02d:%02d]",
  858. tm.tm_year + 1900,
  859. tm.tm_mon + 1,
  860. tm.tm_mday,
  861. tm.tm_hour,
  862. tm.tm_min,
  863. tm.tm_sec);
  864. }
  865. static void *stage_thread(void *userdata)
  866. {
  867. struct thr_info *mythr = userdata;
  868. bool ok = true;
  869. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  870. while (ok) {
  871. struct work *work = NULL;
  872. char *hexstr;
  873. work = tq_pop(mythr->q, NULL);
  874. if (unlikely(!work)) {
  875. applog(LOG_ERR, "Failed to tq_pop in stage_thread");
  876. ok = false;
  877. break;
  878. }
  879. hexstr = bin2hex(work->data, 36);
  880. if (unlikely(!hexstr)) {
  881. applog(LOG_ERR, "stage_thread OOM");
  882. break;
  883. }
  884. /* current_block is blanked out on successful longpoll */
  885. if (likely(strncmp(current_block, blank, 36))) {
  886. if (unlikely(strncmp(hexstr, current_block, 36))) {
  887. new_blocks++;
  888. if (have_longpoll)
  889. applog(LOG_WARNING, "New block detected on network before longpoll, waiting on fresh work");
  890. else
  891. applog(LOG_WARNING, "New block detected on network, waiting on fresh work");
  892. /* As we can't flush the work from here, signal
  893. * the wakeup thread to restart all the
  894. * threads */
  895. work_restart[watchdog_thr_id].restart = 1;
  896. set_curblock(hexstr);
  897. }
  898. } else {
  899. set_curblock(hexstr);
  900. memcpy(longpoll_block, hexstr, 36);
  901. }
  902. free(hexstr);
  903. if (unlikely(!tq_push(getq, work))) {
  904. applog(LOG_ERR, "Failed to tq_push work in stage_thread");
  905. ok = false;
  906. break;
  907. }
  908. inc_staged(1, false);
  909. }
  910. tq_freeze(mythr->q);
  911. return NULL;
  912. }
  913. static void *workio_thread(void *userdata)
  914. {
  915. struct thr_info *mythr = userdata;
  916. bool ok = true;
  917. while (ok) {
  918. struct workio_cmd *wc;
  919. /* wait for workio_cmd sent to us, on our queue */
  920. wc = tq_pop(mythr->q, NULL);
  921. if (unlikely(!wc)) {
  922. applog(LOG_ERR, "Failed to tq_pop in workio_thread");
  923. ok = false;
  924. break;
  925. }
  926. /* process workio_cmd */
  927. switch (wc->cmd) {
  928. case WC_GET_WORK:
  929. ok = workio_get_work(wc);
  930. break;
  931. case WC_SUBMIT_WORK:
  932. ok = workio_submit_work(wc);
  933. break;
  934. case WC_DIE:
  935. default:
  936. ok = false;
  937. break;
  938. }
  939. }
  940. tq_freeze(mythr->q);
  941. return NULL;
  942. }
  943. static void hashmeter(int thr_id, struct timeval *diff,
  944. unsigned long hashes_done)
  945. {
  946. struct timeval temp_tv_end, total_diff;
  947. double khashes, secs;
  948. double local_secs;
  949. double utility, efficiency = 0.0;
  950. static double local_mhashes_done = 0;
  951. static double rolling_local = 0;
  952. double local_mhashes = (double)hashes_done / 1000000.0;
  953. struct cgpu_info *cgpu = thr_info[thr_id].cgpu;
  954. /* Update the last time this thread reported in */
  955. if (thr_id >= 0)
  956. gettimeofday(&thr_info[thr_id].last, NULL);
  957. /* Don't bother calculating anything if we're not displaying it */
  958. if (opt_quiet || !opt_log_interval)
  959. return;
  960. khashes = hashes_done / 1000.0;
  961. secs = (double)diff->tv_sec + ((double)diff->tv_usec / 1000000.0);
  962. if (thr_id >= 0 && secs) {
  963. /* So we can call hashmeter from a non worker thread */
  964. if (opt_debug)
  965. applog(LOG_DEBUG, "[thread %d: %lu hashes, %.0f khash/sec]",
  966. thr_id, hashes_done, hashes_done / secs);
  967. cgpu->local_mhashes += local_mhashes;
  968. cgpu->total_mhashes += local_mhashes;
  969. }
  970. /* Totals are updated by all threads so can race without locking */
  971. pthread_mutex_lock(&hash_lock);
  972. gettimeofday(&temp_tv_end, NULL);
  973. timeval_subtract(&total_diff, &temp_tv_end, &total_tv_end);
  974. local_secs = (double)total_diff.tv_sec + ((double)total_diff.tv_usec / 1000000.0);
  975. total_mhashes_done += local_mhashes;
  976. local_mhashes_done += local_mhashes;
  977. if (total_diff.tv_sec < opt_log_interval)
  978. /* Only update the total every opt_log_interval seconds */
  979. goto out_unlock;
  980. gettimeofday(&total_tv_end, NULL);
  981. /* Use a rolling average by faking an exponential decay over 5 * log */
  982. rolling_local = ((rolling_local * 0.9) + local_mhashes_done) / 1.9;
  983. timeval_subtract(&total_diff, &total_tv_end, &total_tv_start);
  984. total_secs = (double)total_diff.tv_sec +
  985. ((double)total_diff.tv_usec / 1000000.0);
  986. utility = accepted / ( total_secs ? total_secs : 1 ) * 60;
  987. efficiency = getwork_requested ? accepted * 100.0 / getwork_requested : 0.0;
  988. sprintf(statusline, "[(%ds):%.1f (avg):%.1f Mh/s] [Q:%d A:%d R:%d HW:%d E:%.0f%% U:%.2f/m]",
  989. opt_log_interval, rolling_local / local_secs, total_mhashes_done / total_secs,
  990. getwork_requested, accepted, rejected, hw_errors, efficiency, utility);
  991. if (!curses_active) {
  992. printf("%s \r", statusline);
  993. fflush(stdout);
  994. } else
  995. applog(LOG_INFO, "%s", statusline);
  996. local_mhashes_done = 0;
  997. out_unlock:
  998. pthread_mutex_unlock(&hash_lock);
  999. }
  1000. /* This is overkill, but at least we'll know accurately how much work is
  1001. * queued to prevent ever being left without work */
  1002. static void inc_queued(void)
  1003. {
  1004. pthread_mutex_lock(&qd_lock);
  1005. total_queued++;
  1006. pthread_mutex_unlock(&qd_lock);
  1007. }
  1008. static void dec_queued(void)
  1009. {
  1010. pthread_mutex_lock(&qd_lock);
  1011. total_queued--;
  1012. pthread_mutex_unlock(&qd_lock);
  1013. dec_staged(1);
  1014. }
  1015. static int requests_queued(void)
  1016. {
  1017. int ret;
  1018. pthread_mutex_lock(&qd_lock);
  1019. ret = total_queued;
  1020. pthread_mutex_unlock(&qd_lock);
  1021. return ret;
  1022. }
  1023. static bool queue_request(void)
  1024. {
  1025. struct workio_cmd *wc;
  1026. /* If we've been generating lots of local work we may already have
  1027. * enough in the queue */
  1028. if (requests_queued() >= opt_queue + mining_threads)
  1029. return true;
  1030. /* fill out work request message */
  1031. wc = calloc(1, sizeof(*wc));
  1032. if (unlikely(!wc)) {
  1033. applog(LOG_ERR, "Failed to tq_pop in queue_request");
  1034. return false;
  1035. }
  1036. wc->cmd = WC_GET_WORK;
  1037. /* The get work does not belong to any thread */
  1038. wc->thr = NULL;
  1039. /* send work request to workio thread */
  1040. if (unlikely(!tq_push(thr_info[work_thr_id].q, wc))) {
  1041. applog(LOG_ERR, "Failed to tq_push in queue_request");
  1042. workio_cmd_free(wc);
  1043. return false;
  1044. }
  1045. getwork_requested++;
  1046. inc_queued();
  1047. return true;
  1048. }
  1049. static bool discard_request(void)
  1050. {
  1051. struct work *work_heap;
  1052. /* Just in case we fell in a hole and missed a queue filling */
  1053. if (unlikely(!requests_queued())) {
  1054. applog(LOG_WARNING, "Tried to discard_request with nil queued");
  1055. return true;
  1056. }
  1057. work_heap = tq_pop(getq, NULL);
  1058. if (unlikely(!work_heap)) {
  1059. applog(LOG_ERR, "Failed to tq_pop in discard_request");
  1060. return false;
  1061. }
  1062. free(work_heap);
  1063. dec_queued();
  1064. discarded_work++;
  1065. return true;
  1066. }
  1067. static void flush_requests(bool longpoll)
  1068. {
  1069. int i, extra;
  1070. extra = requests_queued();
  1071. /* When flushing from longpoll, we don't know the new work yet. When
  1072. * not flushing from longpoll, the first work item is valid so do not
  1073. * discard it */
  1074. if (longpoll)
  1075. memcpy(current_block, blank, 36);
  1076. else
  1077. extra--;
  1078. /* Temporarily increase the staged count so that get_work thinks there
  1079. * is work available instead of making threads reuse existing work */
  1080. inc_staged(mining_threads, true);
  1081. for (i = 0; i < extra; i++) {
  1082. /* Queue a whole batch of new requests */
  1083. if (unlikely(!queue_request())) {
  1084. applog(LOG_ERR, "Failed to queue requests in flush_requests");
  1085. kill_work();
  1086. break;
  1087. }
  1088. /* Pop off the old requests. Cancelling the requests would be better
  1089. * but is tricky */
  1090. if (unlikely(!discard_request())) {
  1091. applog(LOG_ERR, "Failed to discard requests in flush_requests");
  1092. kill_work();
  1093. break;
  1094. }
  1095. }
  1096. }
  1097. static bool get_work(struct work *work, bool queued)
  1098. {
  1099. static struct timeval tv_localgen = {};
  1100. struct work *work_heap;
  1101. bool ret = false;
  1102. int failures = 0;
  1103. retry:
  1104. if (unlikely(!queued && !queue_request())) {
  1105. applog(LOG_WARNING, "Failed to queue_request in get_work");
  1106. goto out;
  1107. }
  1108. if (!requests_staged() && !stale_work(work)) {
  1109. uint32_t *work_ntime;
  1110. uint32_t ntime;
  1111. /* Only print this message once each time we shift to localgen */
  1112. if (!localgen) {
  1113. localgen = true;
  1114. applog(LOG_WARNING, "Server not providing work fast enough, generating work locally");
  1115. localgen_occasions++;
  1116. gettimeofday(&tv_localgen, NULL);
  1117. } else {
  1118. struct timeval tv_now, diff;
  1119. gettimeofday(&tv_now, NULL);
  1120. timeval_subtract(&diff, &tv_now, &tv_localgen);
  1121. if (diff.tv_sec > 600) {
  1122. /* A new block appears on average every 10 mins */
  1123. applog(LOG_WARNING, "Prolonged outage. Going idle till network recovers.");
  1124. /* Force every thread to wait for new work */
  1125. inc_staged(mining_threads, true);
  1126. goto retry;
  1127. }
  1128. }
  1129. work_ntime = (uint32_t *)(work->data + 68);
  1130. ntime = be32toh(*work_ntime);
  1131. ntime++;
  1132. *work_ntime = htobe32(ntime);
  1133. ret = true;
  1134. local_work++;
  1135. goto out;
  1136. }
  1137. /* wait for 1st response, or get cached response */
  1138. work_heap = tq_pop(getq, NULL);
  1139. if (unlikely(!work_heap)) {
  1140. applog(LOG_WARNING, "Failed to tq_pop in get_work");
  1141. goto out;
  1142. }
  1143. /* If we make it here we have succeeded in getting fresh work */
  1144. if (localgen) {
  1145. localgen = false;
  1146. applog(LOG_WARNING, "Resuming with work from server");
  1147. }
  1148. dec_queued();
  1149. memcpy(work, work_heap, sizeof(*work));
  1150. ret = true;
  1151. free(work_heap);
  1152. out:
  1153. if (unlikely(ret == false)) {
  1154. if ((opt_retries >= 0) && (++failures > opt_retries)) {
  1155. applog(LOG_ERR, "Failed %d times to get_work");
  1156. return ret;
  1157. }
  1158. applog(LOG_DEBUG, "Retrying after %d seconds", opt_fail_pause);
  1159. sleep(opt_fail_pause);
  1160. goto retry;
  1161. }
  1162. return ret;
  1163. }
  1164. static bool submit_work_sync(struct thr_info *thr, const struct work *work_in)
  1165. {
  1166. struct workio_cmd *wc;
  1167. /* fill out work request message */
  1168. wc = calloc(1, sizeof(*wc));
  1169. if (unlikely(!wc)) {
  1170. applog(LOG_ERR, "Failed to calloc wc in submit_work_sync");
  1171. return false;
  1172. }
  1173. wc->u.work = malloc(sizeof(*work_in));
  1174. if (unlikely(!wc->u.work)) {
  1175. applog(LOG_ERR, "Failed to calloc work in submit_work_sync");
  1176. goto err_out;
  1177. }
  1178. wc->cmd = WC_SUBMIT_WORK;
  1179. wc->thr = thr;
  1180. memcpy(wc->u.work, work_in, sizeof(*work_in));
  1181. /* send solution to workio thread */
  1182. if (unlikely(!tq_push(thr_info[work_thr_id].q, wc))) {
  1183. applog(LOG_ERR, "Failed to tq_push work in submit_work_sync");
  1184. goto err_out;
  1185. }
  1186. return true;
  1187. err_out:
  1188. workio_cmd_free(wc);
  1189. return false;
  1190. }
  1191. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
  1192. {
  1193. work->data[64+12+0] = (nonce>>0) & 0xff;
  1194. work->data[64+12+1] = (nonce>>8) & 0xff;
  1195. work->data[64+12+2] = (nonce>>16) & 0xff;
  1196. work->data[64+12+3] = (nonce>>24) & 0xff;
  1197. return submit_work_sync(thr, work);
  1198. }
  1199. static void *miner_thread(void *userdata)
  1200. {
  1201. struct thr_info *mythr = userdata;
  1202. const int thr_id = mythr->id;
  1203. uint32_t max_nonce = 0xffffff;
  1204. unsigned long hashes_done = max_nonce;
  1205. bool needs_work = true;
  1206. /* Try to cycle approximately 5 times before each log update */
  1207. const unsigned long cycle = opt_log_interval / 5 ? : 1;
  1208. /* Request the next work item at 2/3 of the scantime */
  1209. unsigned const int request_interval = opt_scantime * 2 / 3 ? : 1;
  1210. unsigned const long request_nonce = MAXTHREADS / 3 * 2;
  1211. bool requested = true;
  1212. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  1213. /* Set worker threads to nice 19 and then preferentially to SCHED_IDLE
  1214. * and if that fails, then SCHED_BATCH. No need for this to be an
  1215. * error if it fails */
  1216. setpriority(PRIO_PROCESS, 0, 19);
  1217. drop_policy();
  1218. /* Cpu affinity only makes sense if the number of threads is a multiple
  1219. * of the number of CPUs */
  1220. if (!(opt_n_threads % num_processors))
  1221. affine_to_cpu(thr_id - gpu_threads, dev_from_id(thr_id));
  1222. while (1) {
  1223. struct work work __attribute__((aligned(128)));
  1224. struct timeval tv_workstart, tv_start, tv_end, diff;
  1225. uint64_t max64;
  1226. bool rc;
  1227. if (needs_work) {
  1228. gettimeofday(&tv_workstart, NULL);
  1229. /* obtain new work from internal workio thread */
  1230. if (unlikely(!get_work(&work, requested))) {
  1231. applog(LOG_ERR, "work retrieval failed, exiting "
  1232. "mining thread %d", thr_id);
  1233. goto out;
  1234. }
  1235. mythr->cgpu->getworks++;
  1236. work.thr_id = thr_id;
  1237. needs_work = requested = false;
  1238. work.blk.nonce = 0;
  1239. max_nonce = hashes_done;
  1240. }
  1241. hashes_done = 0;
  1242. gettimeofday(&tv_start, NULL);
  1243. /* scan nonces for a proof-of-work hash */
  1244. switch (opt_algo) {
  1245. case ALGO_C:
  1246. rc = scanhash_c(thr_id, work.midstate, work.data + 64,
  1247. work.hash1, work.hash, work.target,
  1248. max_nonce, &hashes_done,
  1249. work.blk.nonce);
  1250. break;
  1251. #ifdef WANT_X8664_SSE2
  1252. case ALGO_SSE2_64: {
  1253. unsigned int rc5 =
  1254. scanhash_sse2_64(thr_id, work.midstate, work.data + 64,
  1255. work.hash1, work.hash,
  1256. work.target,
  1257. max_nonce, &hashes_done,
  1258. work.blk.nonce);
  1259. rc = (rc5 == -1) ? false : true;
  1260. }
  1261. break;
  1262. #endif
  1263. #ifdef WANT_SSE2_4WAY
  1264. case ALGO_4WAY: {
  1265. unsigned int rc4 =
  1266. ScanHash_4WaySSE2(thr_id, work.midstate, work.data + 64,
  1267. work.hash1, work.hash,
  1268. work.target,
  1269. max_nonce, &hashes_done,
  1270. work.blk.nonce);
  1271. rc = (rc4 == -1) ? false : true;
  1272. }
  1273. break;
  1274. #endif
  1275. #ifdef WANT_VIA_PADLOCK
  1276. case ALGO_VIA:
  1277. rc = scanhash_via(thr_id, work.data, work.target,
  1278. max_nonce, &hashes_done,
  1279. work.blk.nonce);
  1280. break;
  1281. #endif
  1282. case ALGO_CRYPTOPP:
  1283. rc = scanhash_cryptopp(thr_id, work.midstate, work.data + 64,
  1284. work.hash1, work.hash, work.target,
  1285. max_nonce, &hashes_done,
  1286. work.blk.nonce);
  1287. break;
  1288. #ifdef WANT_CRYPTOPP_ASM32
  1289. case ALGO_CRYPTOPP_ASM32:
  1290. rc = scanhash_asm32(thr_id, work.midstate, work.data + 64,
  1291. work.hash1, work.hash, work.target,
  1292. max_nonce, &hashes_done,
  1293. work.blk.nonce);
  1294. break;
  1295. #endif
  1296. default:
  1297. /* should never happen */
  1298. goto out;
  1299. }
  1300. /* record scanhash elapsed time */
  1301. gettimeofday(&tv_end, NULL);
  1302. timeval_subtract(&diff, &tv_end, &tv_start);
  1303. hashes_done -= work.blk.nonce;
  1304. hashmeter(thr_id, &diff, hashes_done);
  1305. work.blk.nonce += hashes_done;
  1306. /* adjust max_nonce to meet target cycle time */
  1307. if (diff.tv_usec > 500000)
  1308. diff.tv_sec++;
  1309. if (diff.tv_sec && diff.tv_sec != cycle) {
  1310. max64 = work.blk.nonce +
  1311. ((uint64_t)hashes_done * cycle) / diff.tv_sec;
  1312. } else
  1313. max64 = work.blk.nonce + hashes_done;
  1314. if (max64 > 0xfffffffaULL)
  1315. max64 = 0xfffffffaULL;
  1316. max_nonce = max64;
  1317. /* if nonce found, submit work */
  1318. if (unlikely(rc)) {
  1319. if (opt_debug)
  1320. applog(LOG_DEBUG, "CPU %d found something?", dev_from_id(thr_id));
  1321. if (unlikely(!submit_work_sync(mythr, &work))) {
  1322. applog(LOG_ERR, "Failed to submit_work_sync in miner_thread %d", thr_id);
  1323. break;
  1324. }
  1325. work.blk.nonce += 4;
  1326. }
  1327. timeval_subtract(&diff, &tv_end, &tv_workstart);
  1328. if (!requested && (diff.tv_sec > request_interval || work.blk.nonce > request_nonce)) {
  1329. if (unlikely(!queue_request())) {
  1330. applog(LOG_ERR, "Failed to queue_request in miner_thread %d", thr_id);
  1331. goto out;
  1332. }
  1333. requested = true;
  1334. }
  1335. if (diff.tv_sec > opt_scantime || work_restart[thr_id].restart ||
  1336. work.blk.nonce >= MAXTHREADS - hashes_done ||
  1337. stale_work(&work))
  1338. needs_work = true;
  1339. }
  1340. out:
  1341. tq_freeze(mythr->q);
  1342. return NULL;
  1343. }
  1344. enum {
  1345. STAT_SLEEP_INTERVAL = 1,
  1346. STAT_CTR_INTERVAL = 10000000,
  1347. FAILURE_INTERVAL = 30,
  1348. };
  1349. #ifdef HAVE_OPENCL
  1350. static _clState *clStates[16];
  1351. static inline cl_int queue_kernel_parameters(_clState *clState, dev_blk_ctx *blk)
  1352. {
  1353. cl_kernel *kernel = &clState->kernel;
  1354. cl_int status = 0;
  1355. int num = 0;
  1356. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_a);
  1357. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_b);
  1358. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_c);
  1359. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_d);
  1360. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_e);
  1361. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_f);
  1362. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_g);
  1363. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_h);
  1364. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_b);
  1365. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_c);
  1366. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_d);
  1367. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_f);
  1368. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_g);
  1369. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_h);
  1370. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->nonce);
  1371. if (clState->hasBitAlign == true) {
  1372. /* Parameters for phatk kernel */
  1373. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->W2);
  1374. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->W16);
  1375. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->W17);
  1376. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->PreVal4);
  1377. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->T1);
  1378. } else {
  1379. /* Parameters for poclbm kernel */
  1380. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW0);
  1381. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW1);
  1382. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW2);
  1383. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW3);
  1384. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW15);
  1385. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW01r);
  1386. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e);
  1387. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e2);
  1388. }
  1389. status |= clSetKernelArg(*kernel, num++, sizeof(clState->outputBuffer),
  1390. (void *)&clState->outputBuffer);
  1391. return status;
  1392. }
  1393. static void set_threads_hashes(unsigned int vectors, unsigned int *threads,
  1394. unsigned int *hashes, size_t *globalThreads)
  1395. {
  1396. *globalThreads = *threads = 1 << (15 + scan_intensity);
  1397. *hashes = *threads * vectors;
  1398. }
  1399. static void *gpuminer_thread(void *userdata)
  1400. {
  1401. const unsigned long cycle = opt_log_interval / 5 ? : 1;
  1402. struct timeval tv_start, tv_end, diff, tv_workstart;
  1403. struct thr_info *mythr = userdata;
  1404. const int thr_id = mythr->id;
  1405. uint32_t *res, *blank_res;
  1406. double gpu_ms_average = 7;
  1407. size_t globalThreads[1];
  1408. size_t localThreads[1];
  1409. cl_int status;
  1410. _clState *clState = clStates[thr_id];
  1411. const cl_kernel *kernel = &clState->kernel;
  1412. struct work *work = malloc(sizeof(struct work));
  1413. unsigned int threads = 1 << (15 + scan_intensity);
  1414. unsigned const int vectors = clState->preferred_vwidth;
  1415. unsigned int hashes = threads * vectors;
  1416. unsigned int hashes_done = 0;
  1417. /* Request the next work item at 2/3 of the scantime */
  1418. unsigned const int request_interval = opt_scantime * 2 / 3 ? : 1;
  1419. unsigned const long request_nonce = MAXTHREADS / 3 * 2;
  1420. bool requested = true;
  1421. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  1422. res = calloc(BUFFERSIZE, 1);
  1423. blank_res = calloc(BUFFERSIZE, 1);
  1424. if (!res || !blank_res) {
  1425. applog(LOG_ERR, "Failed to calloc in gpuminer_thread");
  1426. goto out;
  1427. }
  1428. gettimeofday(&tv_start, NULL);
  1429. globalThreads[0] = threads;
  1430. localThreads[0] = clState->work_size;
  1431. diff.tv_sec = 0;
  1432. gettimeofday(&tv_end, NULL);
  1433. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  1434. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1435. if (unlikely(status != CL_SUCCESS))
  1436. { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
  1437. gettimeofday(&tv_workstart, NULL);
  1438. /* obtain new work from internal workio thread */
  1439. if (unlikely(!get_work(work, requested))) {
  1440. applog(LOG_ERR, "work retrieval failed, exiting "
  1441. "gpu mining thread %d", mythr->id);
  1442. goto out;
  1443. }
  1444. mythr->cgpu->getworks++;
  1445. work->thr_id = thr_id;
  1446. requested = false;
  1447. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1448. work->blk.nonce = 0;
  1449. while (1) {
  1450. struct timeval tv_gpustart, tv_gpuend;
  1451. suseconds_t gpu_us;
  1452. gettimeofday(&tv_gpustart, NULL);
  1453. timeval_subtract(&diff, &tv_gpustart, &tv_gpuend);
  1454. /* This finish flushes the readbuffer set with CL_FALSE later */
  1455. clFinish(clState->commandQueue);
  1456. gettimeofday(&tv_gpuend, NULL);
  1457. timeval_subtract(&diff, &tv_gpuend, &tv_gpustart);
  1458. gpu_us = diff.tv_sec * 1000000 + diff.tv_usec;
  1459. gpu_ms_average = ((gpu_us / 1000) + gpu_ms_average * 0.9) / 1.9;
  1460. if (opt_dynamic) {
  1461. /* Try to not let the GPU be out for longer than 6ms, but
  1462. * increase intensity when the system is idle, unless
  1463. * dynamic is disabled. */
  1464. if (gpu_ms_average > 7) {
  1465. if (scan_intensity > 0)
  1466. scan_intensity--;
  1467. set_threads_hashes(vectors, &threads, &hashes, globalThreads);
  1468. } else if (gpu_ms_average < 3) {
  1469. if (scan_intensity < 14)
  1470. scan_intensity++;
  1471. set_threads_hashes(vectors, &threads, &hashes, globalThreads);
  1472. }
  1473. }
  1474. if (diff.tv_sec > opt_scantime ||
  1475. work->blk.nonce >= MAXTHREADS - hashes ||
  1476. work_restart[thr_id].restart ||
  1477. stale_work(work)) {
  1478. /* Ignore any reads since we're getting new work and queue a clean buffer */
  1479. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1480. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1481. if (unlikely(status != CL_SUCCESS))
  1482. { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
  1483. memset(res, 0, BUFFERSIZE);
  1484. gettimeofday(&tv_workstart, NULL);
  1485. /* obtain new work from internal workio thread */
  1486. if (unlikely(!get_work(work, requested))) {
  1487. applog(LOG_ERR, "work retrieval failed, exiting "
  1488. "gpu mining thread %d", mythr->id);
  1489. goto out;
  1490. }
  1491. mythr->cgpu->getworks++;
  1492. work->thr_id = thr_id;
  1493. requested = false;
  1494. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1495. work->blk.nonce = 0;
  1496. work_restart[thr_id].restart = 0;
  1497. if (opt_debug)
  1498. applog(LOG_DEBUG, "getwork thread %d", thr_id);
  1499. /* Flushes the writebuffer set with CL_FALSE above */
  1500. clFinish(clState->commandQueue);
  1501. }
  1502. status = queue_kernel_parameters(clState, &work->blk);
  1503. if (unlikely(status != CL_SUCCESS))
  1504. { applog(LOG_ERR, "Error: clSetKernelArg of all params failed."); goto out; }
  1505. /* MAXBUFFERS entry is used as a flag to say nonces exist */
  1506. if (res[MAXBUFFERS]) {
  1507. /* Clear the buffer again */
  1508. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1509. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1510. if (unlikely(status != CL_SUCCESS))
  1511. { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
  1512. if (opt_debug)
  1513. applog(LOG_DEBUG, "GPU %d found something?", dev_from_id(thr_id));
  1514. postcalc_hash_async(mythr, work, res);
  1515. memset(res, 0, BUFFERSIZE);
  1516. clFinish(clState->commandQueue);
  1517. }
  1518. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1519. globalThreads, localThreads, 0, NULL, NULL);
  1520. if (unlikely(status != CL_SUCCESS))
  1521. { applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)"); goto out; }
  1522. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1523. BUFFERSIZE, res, 0, NULL, NULL);
  1524. if (unlikely(status != CL_SUCCESS))
  1525. { applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)"); goto out;}
  1526. gettimeofday(&tv_end, NULL);
  1527. timeval_subtract(&diff, &tv_end, &tv_start);
  1528. hashes_done += hashes;
  1529. work->blk.nonce += hashes;
  1530. if (diff.tv_usec > 500000)
  1531. diff.tv_sec++;
  1532. if (diff.tv_sec >= cycle) {
  1533. hashmeter(thr_id, &diff, hashes_done);
  1534. gettimeofday(&tv_start, NULL);
  1535. hashes_done = 0;
  1536. }
  1537. timeval_subtract(&diff, &tv_end, &tv_workstart);
  1538. if (!requested && (diff.tv_sec > request_interval || work->blk.nonce > request_nonce)) {
  1539. if (unlikely(!queue_request())) {
  1540. applog(LOG_ERR, "Failed to queue_request in gpuminer_thread %d", thr_id);
  1541. goto out;
  1542. }
  1543. requested = true;
  1544. }
  1545. }
  1546. out:
  1547. tq_freeze(mythr->q);
  1548. return NULL;
  1549. }
  1550. #endif /* HAVE_OPENCL */
  1551. static void restart_threads(bool longpoll)
  1552. {
  1553. int i;
  1554. /* Discard old queued requests and get new ones */
  1555. flush_requests(longpoll);
  1556. for (i = 0; i < mining_threads; i++)
  1557. work_restart[i].restart = 1;
  1558. }
  1559. static void *longpoll_thread(void *userdata)
  1560. {
  1561. struct thr_info *mythr = userdata;
  1562. CURL *curl = NULL;
  1563. char *copy_start, *hdr_path, *lp_url = NULL;
  1564. bool need_slash = false;
  1565. int failures = 0;
  1566. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  1567. hdr_path = tq_pop(mythr->q, NULL);
  1568. if (!hdr_path)
  1569. goto out;
  1570. /* full URL */
  1571. if (strstr(hdr_path, "://")) {
  1572. lp_url = hdr_path;
  1573. hdr_path = NULL;
  1574. }
  1575. /* absolute path, on current server */
  1576. else {
  1577. copy_start = (*hdr_path == '/') ? (hdr_path + 1) : hdr_path;
  1578. if (rpc_url[strlen(rpc_url) - 1] != '/')
  1579. need_slash = true;
  1580. lp_url = malloc(strlen(rpc_url) + strlen(copy_start) + 2);
  1581. if (!lp_url)
  1582. goto out;
  1583. sprintf(lp_url, "%s%s%s", rpc_url, need_slash ? "/" : "", copy_start);
  1584. }
  1585. applog(LOG_INFO, "Long-polling activated for %s", lp_url);
  1586. curl = curl_easy_init();
  1587. if (unlikely(!curl)) {
  1588. applog(LOG_ERR, "CURL initialisation failed");
  1589. goto out;
  1590. }
  1591. while (1) {
  1592. struct timeval start, end;
  1593. json_t *val;
  1594. gettimeofday(&start, NULL);
  1595. val = json_rpc_call(curl, lp_url, rpc_userpass, rpc_req,
  1596. false, true);
  1597. if (likely(val)) {
  1598. failures = 0;
  1599. json_decref(val);
  1600. /* Keep track of who ordered a restart_threads to make
  1601. * sure it's only done once per new block */
  1602. if (likely(!strncmp(longpoll_block, blank, 36) ||
  1603. !strncmp(longpoll_block, current_block, 36))) {
  1604. new_blocks++;
  1605. applog(LOG_WARNING, "LONGPOLL detected new block on network, waiting on fresh work");
  1606. restart_threads(true);
  1607. } else
  1608. applog(LOG_WARNING, "LONGPOLL received after new block already detected");
  1609. } else {
  1610. /* Some pools regularly drop the longpoll request so
  1611. * only see this as longpoll failure if it happens
  1612. * immediately and just restart it the rest of the
  1613. * time. */
  1614. gettimeofday(&end, NULL);
  1615. if (end.tv_sec - start.tv_sec > 30)
  1616. continue;
  1617. if (failures++ < 10) {
  1618. sleep(30);
  1619. applog(LOG_WARNING,
  1620. "longpoll failed, sleeping for 30s");
  1621. } else {
  1622. applog(LOG_ERR,
  1623. "longpoll failed, ending thread");
  1624. goto out;
  1625. }
  1626. }
  1627. memcpy(longpoll_block, current_block, 36);
  1628. }
  1629. out:
  1630. free(hdr_path);
  1631. free(lp_url);
  1632. tq_freeze(mythr->q);
  1633. if (curl)
  1634. curl_easy_cleanup(curl);
  1635. return NULL;
  1636. }
  1637. static void reinit_cputhread(int thr_id)
  1638. {
  1639. struct thr_info *thr = &thr_info[thr_id];
  1640. tq_freeze(thr->q);
  1641. if (!(pthread_cancel(thr->pth)) && pthread_join(thr->pth, NULL)) {
  1642. applog(LOG_ERR, "Failed to pthread_join in reinit_cputhread");
  1643. goto failed_out;
  1644. }
  1645. applog(LOG_INFO, "Reinit CPU thread %d", thr_id);
  1646. tq_thaw(thr->q);
  1647. gettimeofday(&thr->last, NULL);
  1648. if (unlikely(pthread_create(&thr->pth, NULL, miner_thread, thr))) {
  1649. applog(LOG_ERR, "thread %d create failed", thr_id);
  1650. goto failed_out;
  1651. }
  1652. return;
  1653. failed_out:
  1654. kill_work();
  1655. }
  1656. #ifdef HAVE_OPENCL
  1657. static void reinit_gputhread(int thr_id)
  1658. {
  1659. int gpu = dev_from_id(thr_id);
  1660. struct thr_info *thr = &thr_info[thr_id];
  1661. char name[256];
  1662. tq_freeze(thr->q);
  1663. if (!(pthread_cancel(thr->pth)) && pthread_join(thr->pth, NULL)) {
  1664. applog(LOG_ERR, "Failed to pthread_join in reinit_gputhread");
  1665. goto failed_out;
  1666. }
  1667. free(clStates[thr_id]);
  1668. applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
  1669. tq_thaw(thr->q);
  1670. clStates[thr_id] = initCl(gpu, name, sizeof(name));
  1671. if (!clStates[thr_id]) {
  1672. applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
  1673. goto failed_out;
  1674. }
  1675. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1676. gettimeofday(&thr->last, NULL);
  1677. if (unlikely(pthread_create(&thr->pth, NULL, gpuminer_thread, thr))) {
  1678. applog(LOG_ERR, "thread %d create failed", thr_id);
  1679. goto failed_out;
  1680. }
  1681. return;
  1682. failed_out:
  1683. kill_work();
  1684. }
  1685. static void reinit_thread(int thr_id)
  1686. {
  1687. if (thr_id < gpu_threads)
  1688. reinit_gputhread(thr_id);
  1689. else
  1690. reinit_cputhread(thr_id);
  1691. }
  1692. #else
  1693. static void reinit_thread(int thr_id)
  1694. {
  1695. reinit_cputhread(thr_id);
  1696. }
  1697. #endif
  1698. /* Determine which are the first threads belonging to a device and if they're
  1699. * active */
  1700. static bool active_device(int thr_id)
  1701. {
  1702. if (thr_id < gpu_threads) {
  1703. if (thr_id >= total_devices)
  1704. return false;
  1705. if (!gpu_devices[dev_from_id(thr_id)])
  1706. return false;
  1707. } else if (thr_id > gpu_threads + num_processors)
  1708. return false;
  1709. return true;
  1710. }
  1711. /* Makes sure the hashmeter keeps going even if mining threads stall, updates
  1712. * the screen at regular intervals, and restarts threads if they appear to have
  1713. * died. */
  1714. static void *watchdog_thread(void *userdata)
  1715. {
  1716. const unsigned int interval = opt_log_interval / 2 ? : 1;
  1717. struct timeval zero_tv;
  1718. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  1719. memset(&zero_tv, 0, sizeof(struct timeval));
  1720. while (1) {
  1721. int x, y, logx, logy, i;
  1722. struct timeval now;
  1723. sleep(interval);
  1724. if (requests_queued() < opt_queue)
  1725. queue_request();
  1726. hashmeter(-1, &zero_tv, 0);
  1727. if (curses_active) {
  1728. pthread_mutex_lock(&curses_lock);
  1729. getmaxyx(mainwin, y, x);
  1730. getmaxyx(logwin, logy, logx);
  1731. y -= logcursor;
  1732. /* Detect screen size change */
  1733. if (x != logx || y != logy)
  1734. wresize(logwin, y, x);
  1735. for (i = 0; i < mining_threads; i++) {
  1736. if (active_device(i))
  1737. curses_print_status(i);
  1738. }
  1739. redrawwin(logwin);
  1740. redrawwin(statuswin);
  1741. pthread_mutex_unlock(&curses_lock);
  1742. }
  1743. if (unlikely(work_restart[watchdog_thr_id].restart)) {
  1744. restart_threads(false);
  1745. work_restart[watchdog_thr_id].restart = 0;
  1746. }
  1747. gettimeofday(&now, NULL);
  1748. #if 0
  1749. for (i = 0; i < mining_threads; i++) {
  1750. #else
  1751. for (i = 0; i < gpu_threads; i++) {
  1752. #endif
  1753. struct thr_info *thr = &thr_info[i];
  1754. /* Do not kill threads waiting on longpoll staged work
  1755. * or idle network */
  1756. if (now.tv_sec - thr->last.tv_sec > 60 && !idlenet) {
  1757. applog(LOG_ERR, "Attempting to restart thread %d, idle for more than 60 seconds", i);
  1758. /* Create one mandatory work item */
  1759. inc_staged(1, true);
  1760. if (unlikely(!queue_request())) {
  1761. applog(LOG_ERR, "Failed to queue_request in watchdog_thread");
  1762. kill_work();
  1763. break;
  1764. }
  1765. reinit_thread(i);
  1766. applog(LOG_WARNING, "Thread %d restarted", i);
  1767. }
  1768. }
  1769. }
  1770. return NULL;
  1771. }
  1772. static void print_summary(void)
  1773. {
  1774. struct timeval diff;
  1775. int hours, mins, secs, i;
  1776. double utility, efficiency = 0.0;
  1777. timeval_subtract(&diff, &total_tv_end, &total_tv_start);
  1778. hours = diff.tv_sec / 3600;
  1779. mins = (diff.tv_sec % 3600) / 60;
  1780. secs = diff.tv_sec % 60;
  1781. utility = accepted / ( total_secs ? total_secs : 1 ) * 60;
  1782. efficiency = getwork_requested ? accepted * 100.0 / getwork_requested : 0.0;
  1783. printf("\nSummary of runtime statistics:\n\n");
  1784. printf("Started at %s\n", datestamp);
  1785. printf("Runtime: %d hrs : %d mins : %d secs\n", hours, mins, secs);
  1786. if (total_secs)
  1787. printf("Average hashrate: %.1f Megahash/s\n", total_mhashes_done / total_secs);
  1788. printf("Queued work requests: %d\n", getwork_requested);
  1789. printf("Share submissions: %d\n", accepted + rejected);
  1790. printf("Accepted shares: %d\n", accepted);
  1791. printf("Rejected shares: %d\n", rejected);
  1792. if (accepted || rejected)
  1793. printf("Reject ratio: %.1f\n", (double)(rejected * 100) / (double)(accepted + rejected));
  1794. printf("Hardware errors: %d\n", hw_errors);
  1795. printf("Efficiency (accepted / queued): %.0f%%\n", efficiency);
  1796. printf("Utility (accepted shares / min): %.2f/min\n\n", utility);
  1797. printf("Discarded work due to new blocks: %d\n", discarded_work);
  1798. printf("Stale submissions discarded due to new blocks: %d\n", stale_shares);
  1799. printf("Unable to get work from server occasions: %d\n", localgen_occasions);
  1800. printf("Work items generated locally: %d\n", local_work);
  1801. printf("Submitting work remotely delay occasions: %d\n", remotefail_occasions);
  1802. printf("New blocks detected on network: %d\n\n", new_blocks);
  1803. printf("Summary of per device statistics:\n\n");
  1804. for (i = 0; i < mining_threads; i++) {
  1805. if (active_device(i))
  1806. print_status(i);
  1807. }
  1808. printf("\n");
  1809. }
  1810. int main (int argc, char *argv[])
  1811. {
  1812. unsigned int i, j = 0, x, y;
  1813. struct sigaction handler;
  1814. struct thr_info *thr;
  1815. char name[256];
  1816. struct tm tm;
  1817. if (unlikely(pthread_mutex_init(&hash_lock, NULL)))
  1818. return 1;
  1819. if (unlikely(pthread_mutex_init(&qd_lock, NULL)))
  1820. return 1;
  1821. if (unlikely(pthread_mutex_init(&stgd_lock, NULL)))
  1822. return 1;
  1823. if (unlikely(pthread_mutex_init(&curses_lock, NULL)))
  1824. return 1;
  1825. handler.sa_handler = &sighandler;
  1826. sigaction(SIGTERM, &handler, &termhandler);
  1827. sigaction(SIGINT, &handler, &inthandler);
  1828. gettimeofday(&total_tv_start, NULL);
  1829. gettimeofday(&total_tv_end, NULL);
  1830. localtime_r(&total_tv_start.tv_sec, &tm);
  1831. sprintf(datestamp, "[%d-%02d-%02d %02d:%02d:%02d]",
  1832. tm.tm_year + 1900,
  1833. tm.tm_mon + 1,
  1834. tm.tm_mday,
  1835. tm.tm_hour,
  1836. tm.tm_min,
  1837. tm.tm_sec);
  1838. for (i = 0; i < 36; i++) {
  1839. strcat(blank, "0");
  1840. strcat(current_block, "0");
  1841. strcat(longpoll_block, "0");
  1842. }
  1843. #ifdef WIN32
  1844. opt_n_threads = num_processors = 1;
  1845. #else
  1846. num_processors = sysconf(_SC_NPROCESSORS_ONLN);
  1847. opt_n_threads = num_processors;
  1848. #endif /* !WIN32 */
  1849. #ifdef HAVE_OPENCL
  1850. for (i = 0; i < 16; i++)
  1851. gpu_devices[i] = false;
  1852. nDevs = clDevicesNum();
  1853. if (nDevs < 0)
  1854. return 1;
  1855. #endif
  1856. if (nDevs)
  1857. opt_n_threads = 0;
  1858. rpc_url = strdup(DEF_RPC_URL);
  1859. /* parse command line */
  1860. opt_register_table(opt_config_table,
  1861. "Options for both config file and command line");
  1862. opt_register_table(opt_cmdline_table,
  1863. "Options for command line only");
  1864. opt_parse(&argc, argv, applog_and_exit);
  1865. if (argc != 1) {
  1866. applog(LOG_ERR, "Unexpected extra commandline arguments");
  1867. return 1;
  1868. }
  1869. if (total_devices) {
  1870. if (total_devices > nDevs) {
  1871. applog(LOG_ERR, "More devices specified than exist");
  1872. return 1;
  1873. }
  1874. for (i = 0; i < 16; i++)
  1875. if (gpu_devices[i] && i + 1 > nDevs) {
  1876. applog(LOG_ERR, "Command line options set a device that doesn't exist");
  1877. return 1;
  1878. }
  1879. gpu_threads = total_devices * opt_g_threads;
  1880. } else {
  1881. gpu_threads = nDevs * opt_g_threads;
  1882. for (i = 0; i < nDevs; i++)
  1883. gpu_devices[i] = true;
  1884. total_devices = nDevs;
  1885. }
  1886. if (!gpu_threads && !forced_n_threads) {
  1887. /* Maybe they turned GPU off; restore default CPU threads. */
  1888. opt_n_threads = num_processors;
  1889. }
  1890. logcursor = 6;
  1891. mining_threads = opt_n_threads + gpu_threads;
  1892. gpucursor = logcursor;
  1893. cpucursor = gpucursor + nDevs;
  1894. logstart = cpucursor + (opt_n_threads ? num_processors : 0) + 1;
  1895. logcursor = logstart + 1;
  1896. if (!rpc_userpass) {
  1897. if (!rpc_user || !rpc_pass) {
  1898. applog(LOG_ERR, "No login credentials supplied");
  1899. return 1;
  1900. }
  1901. rpc_userpass = malloc(strlen(rpc_user) + strlen(rpc_pass) + 2);
  1902. if (!rpc_userpass)
  1903. return 1;
  1904. sprintf(rpc_userpass, "%s:%s", rpc_user, rpc_pass);
  1905. }
  1906. if (unlikely(curl_global_init(CURL_GLOBAL_ALL)))
  1907. return 1;
  1908. #ifdef HAVE_SYSLOG_H
  1909. if (use_syslog)
  1910. openlog("cpuminer", LOG_PID, LOG_USER);
  1911. #endif
  1912. work_restart = calloc(mining_threads + 4, sizeof(*work_restart));
  1913. if (!work_restart)
  1914. return 1;
  1915. thr_info = calloc(mining_threads + 4, sizeof(*thr));
  1916. if (!thr_info)
  1917. return 1;
  1918. /* init workio thread info */
  1919. work_thr_id = mining_threads;
  1920. thr = &thr_info[work_thr_id];
  1921. thr->id = work_thr_id;
  1922. thr->q = tq_new();
  1923. if (!thr->q)
  1924. return 1;
  1925. /* start work I/O thread */
  1926. if (pthread_create(&thr->pth, NULL, workio_thread, thr)) {
  1927. applog(LOG_ERR, "workio thread create failed");
  1928. return 1;
  1929. }
  1930. /* init longpoll thread info */
  1931. if (want_longpoll) {
  1932. longpoll_thr_id = mining_threads + 1;
  1933. thr = &thr_info[longpoll_thr_id];
  1934. thr->id = longpoll_thr_id;
  1935. thr->q = tq_new();
  1936. if (!thr->q)
  1937. return 1;
  1938. /* start longpoll thread */
  1939. if (unlikely(pthread_create(&thr->pth, NULL, longpoll_thread, thr))) {
  1940. applog(LOG_ERR, "longpoll thread create failed");
  1941. return 1;
  1942. }
  1943. pthread_detach(thr->pth);
  1944. } else
  1945. longpoll_thr_id = -1;
  1946. if (opt_n_threads ) {
  1947. cpus = calloc(num_processors, sizeof(struct cgpu_info));
  1948. if (unlikely(!cpus)) {
  1949. applog(LOG_ERR, "Failed to calloc cpus");
  1950. return 1;
  1951. }
  1952. }
  1953. if (gpu_threads) {
  1954. gpus = calloc(nDevs, sizeof(struct cgpu_info));
  1955. if (unlikely(!gpus)) {
  1956. applog(LOG_ERR, "Failed to calloc gpus");
  1957. return 1;
  1958. }
  1959. }
  1960. stage_thr_id = mining_threads + 3;
  1961. thr = &thr_info[stage_thr_id];
  1962. thr->q = tq_new();
  1963. if (!thr->q)
  1964. return 1;
  1965. /* start stage thread */
  1966. if (pthread_create(&thr->pth, NULL, stage_thread, thr)) {
  1967. applog(LOG_ERR, "stage thread create failed");
  1968. return 1;
  1969. }
  1970. /* Flag the work as ready forcing the mining threads to wait till we
  1971. * actually put something into the queue */
  1972. inc_staged(mining_threads, true);
  1973. /* Create a unique get work queue */
  1974. getq = tq_new();
  1975. if (!getq) {
  1976. applog(LOG_ERR, "Failed to create getq");
  1977. return 1;
  1978. }
  1979. #ifdef HAVE_OPENCL
  1980. i = 0;
  1981. /* start GPU mining threads */
  1982. for (j = 0; j < nDevs * opt_g_threads; j++) {
  1983. int gpu = j % nDevs;
  1984. gpus[gpu].is_gpu = 1;
  1985. gpus[gpu].cpu_gpu = gpu;
  1986. /* Skip devices not set to mine */
  1987. if (!gpu_devices[gpu])
  1988. continue;
  1989. thr = &thr_info[i];
  1990. thr->id = i;
  1991. thr->cgpu = &gpus[gpu];
  1992. thr->q = tq_new();
  1993. if (!thr->q) {
  1994. applog(LOG_ERR, "tq_new failed in starting gpu mining threads");
  1995. return 1;
  1996. }
  1997. applog(LOG_INFO, "Init GPU thread %i", i);
  1998. clStates[i] = initCl(gpu, name, sizeof(name));
  1999. if (!clStates[i]) {
  2000. applog(LOG_ERR, "Failed to init GPU thread %d", i);
  2001. continue;
  2002. }
  2003. applog(LOG_INFO, "initCl() finished. Found %s", name);
  2004. gettimeofday(&thr->last, NULL);
  2005. if (unlikely(pthread_create(&thr->pth, NULL, gpuminer_thread, thr))) {
  2006. applog(LOG_ERR, "thread %d create failed", i);
  2007. return 1;
  2008. }
  2009. i++;
  2010. }
  2011. applog(LOG_INFO, "%d gpu miner threads started", gpu_threads);
  2012. #endif
  2013. /* start CPU mining threads */
  2014. for (i = gpu_threads; i < mining_threads; i++) {
  2015. int cpu = (i - gpu_threads) % num_processors;
  2016. thr = &thr_info[i];
  2017. thr->id = i;
  2018. cpus[cpu].cpu_gpu = cpu;
  2019. thr->cgpu = &cpus[cpu];
  2020. thr->q = tq_new();
  2021. if (!thr->q) {
  2022. applog(LOG_ERR, "tq_new failed in starting cpu mining threads");
  2023. return 1;
  2024. }
  2025. gettimeofday(&thr->last, NULL);
  2026. if (unlikely(pthread_create(&thr->pth, NULL, miner_thread, thr))) {
  2027. applog(LOG_ERR, "thread %d create failed", i);
  2028. return 1;
  2029. }
  2030. }
  2031. applog(LOG_INFO, "%d cpu miner threads started, "
  2032. "using SHA256 '%s' algorithm.",
  2033. opt_n_threads,
  2034. algo_names[opt_algo]);
  2035. watchdog_thr_id = mining_threads + 2;
  2036. thr = &thr_info[watchdog_thr_id];
  2037. /* start wakeup thread */
  2038. if (pthread_create(&thr->pth, NULL, watchdog_thread, NULL)) {
  2039. applog(LOG_ERR, "wakeup thread create failed");
  2040. return 1;
  2041. }
  2042. /* Restart count as it will be wrong till all threads are started */
  2043. pthread_mutex_lock(&hash_lock);
  2044. gettimeofday(&total_tv_start, NULL);
  2045. gettimeofday(&total_tv_end, NULL);
  2046. total_mhashes_done = 0;
  2047. pthread_mutex_unlock(&hash_lock);
  2048. /* Set up the ncurses interface */
  2049. if (!opt_quiet && use_curses) {
  2050. mainwin = initscr();
  2051. getmaxyx(mainwin, y, x);
  2052. statuswin = newwin(logstart, x, 0, 0);
  2053. logwin = newwin(y - logcursor, 0, logcursor, 0);
  2054. idlok(logwin, true);
  2055. scrollok(logwin, true);
  2056. leaveok(logwin, true);
  2057. leaveok(statuswin, true);
  2058. curses_active = true;
  2059. for (i = 0; i < mining_threads; i++)
  2060. print_status(i);
  2061. }
  2062. /* Now that everything's ready put enough work in the queue */
  2063. for (i = 0; i < opt_queue + mining_threads; i++) {
  2064. if (unlikely(!queue_request())) {
  2065. applog(LOG_ERR, "Failed to queue_request in main");
  2066. return 1;
  2067. }
  2068. }
  2069. /* main loop - simply wait for workio thread to exit */
  2070. pthread_join(thr_info[work_thr_id].pth, NULL);
  2071. applog(LOG_INFO, "workio thread dead, exiting.");
  2072. gettimeofday(&total_tv_end, NULL);
  2073. curl_global_cleanup();
  2074. disable_curses();
  2075. if (!opt_quiet && successful_connect)
  2076. print_summary();
  2077. if (gpu_threads)
  2078. free(gpus);
  2079. if (opt_n_threads)
  2080. free(cpus);
  2081. return 0;
  2082. }