main.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  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 <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include <unistd.h>
  17. #include <sys/time.h>
  18. #include <time.h>
  19. #include <math.h>
  20. #include <stdarg.h>
  21. #include <assert.h>
  22. #ifndef WIN32
  23. #include <sys/resource.h>
  24. #endif
  25. #include <ccan/opt/opt.h>
  26. #include <jansson.h>
  27. #include <curl/curl.h>
  28. #include "compat.h"
  29. #include "miner.h"
  30. #include "findnonce.h"
  31. #include "ocl.h"
  32. #define PROGRAM_NAME "cgminer"
  33. #define DEF_RPC_URL "http://127.0.0.1:8332/"
  34. #define DEF_RPC_USERNAME "rpcuser"
  35. #define DEF_RPC_PASSWORD "rpcpass"
  36. #define DEF_RPC_USERPASS DEF_RPC_USERNAME ":" DEF_RPC_PASSWORD
  37. #ifdef __linux /* Linux specific policy and affinity management */
  38. #include <sched.h>
  39. static inline void drop_policy(void)
  40. {
  41. struct sched_param param;
  42. #ifdef SCHED_BATCH
  43. #ifdef SCHED_IDLE
  44. if (unlikely(sched_setscheduler(0, SCHED_IDLE, &param) == -1))
  45. #endif
  46. sched_setscheduler(0, SCHED_BATCH, &param);
  47. #endif
  48. }
  49. static inline void affine_to_cpu(int id, int cpu)
  50. {
  51. cpu_set_t set;
  52. CPU_ZERO(&set);
  53. CPU_SET(cpu, &set);
  54. sched_setaffinity(0, sizeof(&set), &set);
  55. applog(LOG_INFO, "Binding cpu mining thread %d to cpu %d", id, cpu);
  56. }
  57. #else
  58. static inline void drop_policy(void)
  59. {
  60. }
  61. static inline void affine_to_cpu(int id, int cpu)
  62. {
  63. }
  64. #endif
  65. enum workio_commands {
  66. WC_GET_WORK,
  67. WC_SUBMIT_WORK,
  68. WC_DIE,
  69. };
  70. struct workio_cmd {
  71. enum workio_commands cmd;
  72. struct thr_info *thr;
  73. union {
  74. struct work *work;
  75. } u;
  76. };
  77. enum sha256_algos {
  78. ALGO_C, /* plain C */
  79. ALGO_4WAY, /* parallel SSE2 */
  80. ALGO_VIA, /* VIA padlock */
  81. ALGO_CRYPTOPP, /* Crypto++ (C) */
  82. ALGO_CRYPTOPP_ASM32, /* Crypto++ 32-bit assembly */
  83. ALGO_SSE2_64, /* SSE2 for x86_64 */
  84. };
  85. static const char *algo_names[] = {
  86. [ALGO_C] = "c",
  87. #ifdef WANT_SSE2_4WAY
  88. [ALGO_4WAY] = "4way",
  89. #endif
  90. #ifdef WANT_VIA_PADLOCK
  91. [ALGO_VIA] = "via",
  92. #endif
  93. [ALGO_CRYPTOPP] = "cryptopp",
  94. #ifdef WANT_CRYPTOPP_ASM32
  95. [ALGO_CRYPTOPP_ASM32] = "cryptopp_asm32",
  96. #endif
  97. #ifdef WANT_X8664_SSE2
  98. [ALGO_SSE2_64] = "sse2_64",
  99. #endif
  100. };
  101. bool opt_debug = false;
  102. bool opt_protocol = false;
  103. bool want_longpoll = true;
  104. bool have_longpoll = false;
  105. bool use_syslog = false;
  106. static bool opt_quiet = false;
  107. static int opt_retries = -1;
  108. static int opt_fail_pause = 5;
  109. static int opt_log_interval = 5;
  110. bool opt_log_output = false;
  111. static int opt_queue = 1;
  112. int opt_vectors;
  113. int opt_worksize;
  114. int opt_scantime = 60;
  115. static const bool opt_time = true;
  116. #ifdef WANT_X8664_SSE2
  117. static enum sha256_algos opt_algo = ALGO_SSE2_64;
  118. #else
  119. static enum sha256_algos opt_algo = ALGO_C;
  120. #endif
  121. static int nDevs;
  122. static int opt_g_threads = 2;
  123. static int gpu_threads;
  124. static bool forced_n_threads;
  125. static int opt_n_threads;
  126. static int num_processors;
  127. static int scan_intensity = 4;
  128. static char *rpc_url;
  129. static char *rpc_userpass;
  130. static char *rpc_user, *rpc_pass;
  131. struct thr_info *thr_info;
  132. static int work_thr_id;
  133. int longpoll_thr_id;
  134. static int stage_thr_id;
  135. struct work_restart *work_restart = NULL;
  136. pthread_mutex_t time_lock;
  137. static pthread_mutex_t hash_lock;
  138. static pthread_mutex_t qd_lock;
  139. static pthread_mutex_t stgd_lock;
  140. static double total_mhashes_done;
  141. static struct timeval total_tv_start, total_tv_end;
  142. static int accepted, rejected;
  143. int hw_errors;
  144. static int total_queued, total_staged, lp_staged;
  145. static bool localgen = false;
  146. static unsigned int getwork_requested = 0;
  147. static char current_block[37];
  148. static char longpoll_block[37];
  149. static char blank[37];
  150. static void applog_and_exit(const char *fmt, ...)
  151. {
  152. va_list ap;
  153. va_start(ap, fmt);
  154. vapplog(LOG_ERR, fmt, ap);
  155. va_end(ap);
  156. exit(1);
  157. }
  158. /* FIXME: Use asprintf for better errors. */
  159. static char *set_algo(const char *arg, enum sha256_algos *algo)
  160. {
  161. enum sha256_algos i;
  162. for (i = 0; i < ARRAY_SIZE(algo_names); i++) {
  163. if (algo_names[i] && !strcmp(arg, algo_names[i])) {
  164. *algo = i;
  165. return NULL;
  166. }
  167. }
  168. return "Unknown algorithm";
  169. }
  170. static void show_algo(char buf[OPT_SHOW_LEN], const enum sha256_algos *algo)
  171. {
  172. strncpy(buf, algo_names[*algo], OPT_SHOW_LEN);
  173. }
  174. static char *set_int_range(const char *arg, int *i, int min, int max)
  175. {
  176. char *err = opt_set_intval(arg, i);
  177. if (err)
  178. return err;
  179. if (*i < min || *i > max)
  180. return "Value out of range";
  181. return NULL;
  182. }
  183. static char *set_int_0_to_9999(const char *arg, int *i)
  184. {
  185. return set_int_range(arg, i, 0, 9999);
  186. }
  187. static char *set_int_0_to_14(const char *arg, int *i)
  188. {
  189. return set_int_range(arg, i, 0, 14);
  190. }
  191. static char *force_nthreads_int(const char *arg, int *i)
  192. {
  193. forced_n_threads = true;
  194. return set_int_range(arg, i, 0, 9999);
  195. }
  196. static char *set_int_0_to_10(const char *arg, int *i)
  197. {
  198. return set_int_range(arg, i, 0, 10);
  199. }
  200. static char *set_int_1_to_10(const char *arg, int *i)
  201. {
  202. return set_int_range(arg, i, 1, 10);
  203. }
  204. static char *set_url(const char *arg, char **p)
  205. {
  206. opt_set_charp(arg, p);
  207. if (strncmp(arg, "http://", 7) &&
  208. strncmp(arg, "https://", 8))
  209. return "URL must start with http:// or https://";
  210. return NULL;
  211. }
  212. static char *set_vector(const char *arg, int *i)
  213. {
  214. char *err = opt_set_intval(arg, i);
  215. if (err)
  216. return err;
  217. if (*i != 1 && *i != 2 && *i != 4)
  218. return "Valid vectors are 1, 2 or 4";
  219. return NULL;
  220. }
  221. static char *enable_debug(bool *flag)
  222. {
  223. *flag = true;
  224. /* Turn out verbose output, too. */
  225. opt_log_output = true;
  226. return NULL;
  227. }
  228. /* These options are available from config file or commandline */
  229. static struct opt_table opt_config_table[] = {
  230. OPT_WITH_ARG("--algo|-a",
  231. set_algo, show_algo, &opt_algo,
  232. "Specify sha256 implementation:\n"
  233. "\tc\t\tLinux kernel sha256, implemented in C"
  234. #ifdef WANT_SSE2_4WAY
  235. "\n\t4way\t\ttcatm's 4-way SSE2 implementation"
  236. #endif
  237. #ifdef WANT_VIA_PADLOCK
  238. "\n\tvia\t\tVIA padlock implementation"
  239. #endif
  240. "\n\tcryptopp\tCrypto++ C/C++ implementation"
  241. #ifdef WANT_CRYPTOPP_ASM32
  242. "\n\tcryptopp_asm32\tCrypto++ 32-bit assembler implementation"
  243. #endif
  244. #ifdef WANT_X8664_SSE2
  245. "\n\tsse2_64\t\tSSE2 implementation for x86_64 machines"
  246. #endif
  247. ),
  248. OPT_WITH_ARG("--cpu-threads|-t",
  249. force_nthreads_int, opt_show_intval, &opt_n_threads,
  250. "Number of miner CPU threads"),
  251. OPT_WITHOUT_ARG("--debug|-D",
  252. enable_debug, &opt_debug,
  253. "Enable debug output"),
  254. #ifdef HAVE_OPENCL
  255. OPT_WITH_ARG("--gpu-threads|-g",
  256. set_int_0_to_10, opt_show_intval, &opt_g_threads,
  257. "Number of threads per GPU (0 - 10)"),
  258. OPT_WITH_ARG("--intensity|-I",
  259. set_int_0_to_14, opt_show_intval, &scan_intensity,
  260. "Intensity of GPU scanning (0 - 14)"),
  261. #endif
  262. OPT_WITH_ARG("--log|-l",
  263. set_int_0_to_9999, opt_show_intval, &opt_log_interval,
  264. "Interval in seconds between log output"),
  265. OPT_WITHOUT_ARG("--no-longpoll",
  266. opt_set_invbool, &want_longpoll,
  267. "Disable X-Long-Polling support"),
  268. OPT_WITH_ARG("--pass|-p",
  269. opt_set_charp, NULL, &rpc_pass,
  270. "Password for bitcoin JSON-RPC server"),
  271. OPT_WITHOUT_ARG("--protocol-dump|-P",
  272. opt_set_bool, &opt_protocol,
  273. "Verbose dump of protocol-level activities"),
  274. OPT_WITH_ARG("--queue|-Q",
  275. set_int_1_to_10, opt_show_intval, &opt_queue,
  276. "Number of extra work items to queue (1 - 10)"),
  277. OPT_WITHOUT_ARG("--quiet|-q",
  278. opt_set_bool, &opt_quiet,
  279. "Disable per-thread hashmeter output"),
  280. OPT_WITH_ARG("--retries|-r",
  281. opt_set_intval, opt_show_intval, &opt_retries,
  282. "Number of times to retry before giving up, if JSON-RPC call fails (-1 means never)"),
  283. OPT_WITH_ARG("--retry-pause|-R",
  284. set_int_0_to_9999, opt_show_intval, &opt_fail_pause,
  285. "Number of seconds to pause, between retries"),
  286. OPT_WITH_ARG("--scan-time|-s",
  287. set_int_0_to_9999, opt_show_intval, &opt_scantime,
  288. "Upper bound on time spent scanning current work, in seconds"),
  289. #ifdef HAVE_SYSLOG_H
  290. OPT_WITHOUT_ARG("--syslog",
  291. opt_set_bool, &use_syslog,
  292. "Use system log for output messages (default: standard error)"),
  293. #endif
  294. OPT_WITH_ARG("--url|-o",
  295. set_url, opt_show_charp, &rpc_url,
  296. "URL for bitcoin JSON-RPC server"),
  297. OPT_WITH_ARG("--user|-u",
  298. opt_set_charp, NULL, &rpc_user,
  299. "Username for bitcoin JSON-RPC server"),
  300. #ifdef HAVE_OPENCL
  301. OPT_WITH_ARG("--vectors|-v",
  302. set_vector, NULL, &opt_vectors,
  303. "Override detected optimal vector width (1, 2 or 4)"),
  304. #endif
  305. OPT_WITHOUT_ARG("--verbose",
  306. opt_set_bool, &opt_log_output,
  307. "Log verbose output to stderr as well as status output"),
  308. #ifdef HAVE_OPENCL
  309. OPT_WITH_ARG("--worksize|-w",
  310. set_int_0_to_9999, opt_show_intval, &opt_worksize,
  311. "Override detected optimal worksize"),
  312. #endif
  313. OPT_WITH_ARG("--userpass|-O",
  314. opt_set_charp, NULL, &rpc_userpass,
  315. "Username:Password pair for bitcoin JSON-RPC server"),
  316. OPT_ENDTABLE
  317. };
  318. static char *parse_config(json_t *config)
  319. {
  320. static char err_buf[200];
  321. json_t *val;
  322. struct opt_table *opt;
  323. for (opt = opt_config_table; opt->type != OPT_END; opt++) {
  324. char *p, *name;
  325. /* We don't handle subtables. */
  326. assert(!(opt->type & OPT_SUBTABLE));
  327. /* Pull apart the option name(s). */
  328. name = strdup(opt->names);
  329. for (p = strtok(name, "|"); p; p = strtok(NULL, "|")) {
  330. char *err;
  331. /* Ignore short options. */
  332. if (p[1] != '-')
  333. continue;
  334. val = json_object_get(config, p+2);
  335. if (!val)
  336. continue;
  337. if ((opt->type & OPT_HASARG) && json_is_string(val)) {
  338. err = opt->cb_arg(json_string_value(val),
  339. opt->u.arg);
  340. } else if ((opt->type&OPT_NOARG) && json_is_true(val)) {
  341. err = opt->cb(opt->u.arg);
  342. } else {
  343. err = "Invalid value";
  344. }
  345. if (err) {
  346. sprintf(err_buf, "Parsing JSON option %s: %s",
  347. p, err);
  348. return err_buf;
  349. }
  350. }
  351. free(name);
  352. }
  353. return NULL;
  354. }
  355. static char *load_config(const char *arg, void *unused)
  356. {
  357. json_error_t err;
  358. json_t *config;
  359. config = json_load_file(arg, &err);
  360. if (!json_is_object(config))
  361. return "JSON decode of file failed";
  362. /* Parse the config now, so we can override it. That can keep pointers
  363. * so don't free config object. */
  364. return parse_config(config);
  365. }
  366. static char *print_ndevs_and_exit(int *ndevs)
  367. {
  368. printf("%i", *ndevs);
  369. exit(*ndevs);
  370. }
  371. /* These options are available from commandline only */
  372. static struct opt_table opt_cmdline_table[] = {
  373. OPT_WITH_ARG("--config|-c",
  374. load_config, NULL, NULL,
  375. "Load a JSON-format configuration file\n"
  376. "See example-cfg.json for an example configuration."),
  377. OPT_WITHOUT_ARG("--help|-h",
  378. opt_usage_and_exit,
  379. #ifdef HAVE_OPENCL
  380. "\nBuilt with CPU and GPU mining support.\n\n",
  381. #else
  382. "\nBuilt with CPU mining support only.\n\n",
  383. #endif
  384. "Print this message"),
  385. OPT_WITHOUT_ARG("--ndevs|-n",
  386. print_ndevs_and_exit, &nDevs,
  387. "Display number of detected GPUs and exit"),
  388. OPT_ENDTABLE
  389. };
  390. static bool jobj_binary(const json_t *obj, const char *key,
  391. void *buf, size_t buflen)
  392. {
  393. const char *hexstr;
  394. json_t *tmp;
  395. tmp = json_object_get(obj, key);
  396. if (unlikely(!tmp)) {
  397. applog(LOG_ERR, "JSON key '%s' not found", key);
  398. return false;
  399. }
  400. hexstr = json_string_value(tmp);
  401. if (unlikely(!hexstr)) {
  402. applog(LOG_ERR, "JSON key '%s' is not a string", key);
  403. return false;
  404. }
  405. if (!hex2bin(buf, hexstr, buflen))
  406. return false;
  407. return true;
  408. }
  409. static bool work_decode(const json_t *val, struct work *work)
  410. {
  411. if (unlikely(!jobj_binary(val, "midstate",
  412. work->midstate, sizeof(work->midstate)))) {
  413. applog(LOG_ERR, "JSON inval midstate");
  414. goto err_out;
  415. }
  416. if (unlikely(!jobj_binary(val, "data", work->data, sizeof(work->data)))) {
  417. applog(LOG_ERR, "JSON inval data");
  418. goto err_out;
  419. }
  420. if (unlikely(!jobj_binary(val, "hash1", work->hash1, sizeof(work->hash1)))) {
  421. applog(LOG_ERR, "JSON inval hash1");
  422. goto err_out;
  423. }
  424. if (unlikely(!jobj_binary(val, "target", work->target, sizeof(work->target)))) {
  425. applog(LOG_ERR, "JSON inval target");
  426. goto err_out;
  427. }
  428. memset(work->hash, 0, sizeof(work->hash));
  429. return true;
  430. err_out:
  431. return false;
  432. }
  433. static double total_secs;
  434. static char statusline[256];
  435. static inline void print_status(void)
  436. {
  437. printf("%s\r", statusline);
  438. fflush(stdout);
  439. }
  440. static bool submit_upstream_work(const struct work *work)
  441. {
  442. char *hexstr = NULL;
  443. json_t *val, *res;
  444. char s[345];
  445. bool rc = false;
  446. struct cgpu_info *cgpu = thr_info[work->thr_id].cgpu;
  447. CURL *curl = curl_easy_init();
  448. double utility, efficiency;
  449. if (unlikely(!curl)) {
  450. applog(LOG_ERR, "CURL initialisation failed");
  451. return rc;
  452. }
  453. /* build hex string */
  454. hexstr = bin2hex(work->data, sizeof(work->data));
  455. if (unlikely(!hexstr)) {
  456. applog(LOG_ERR, "submit_upstream_work OOM");
  457. goto out_nofree;
  458. }
  459. /* build JSON-RPC request */
  460. sprintf(s,
  461. "{\"method\": \"getwork\", \"params\": [ \"%s\" ], \"id\":1}\r\n",
  462. hexstr);
  463. if (opt_debug)
  464. applog(LOG_DEBUG, "DBG: sending RPC call: %s", s);
  465. /* issue JSON-RPC request */
  466. val = json_rpc_call(curl, rpc_url, rpc_userpass, s, false, false);
  467. if (unlikely(!val)) {
  468. applog(LOG_ERR, "submit_upstream_work json_rpc_call failed");
  469. goto out;
  470. }
  471. res = json_object_get(val, "result");
  472. /* Theoretically threads could race when modifying accepted and
  473. * rejected values but the chance of two submits completing at the
  474. * same time is zero so there is no point adding extra locking */
  475. if (json_is_true(res)) {
  476. cgpu->accepted++;
  477. accepted++;
  478. if (opt_debug)
  479. applog(LOG_DEBUG, "PROOF OF WORK RESULT: true (yay!!!)");
  480. if (!opt_quiet)
  481. printf("[Accepted] ");
  482. } else {
  483. cgpu->rejected++;
  484. rejected++;
  485. if (opt_debug)
  486. applog(LOG_DEBUG, "PROOF OF WORK RESULT: false (booooo)");
  487. if (!opt_quiet)
  488. printf("[Rejected] ");
  489. }
  490. utility = accepted / ( total_secs ? total_secs : 1 ) * 60;
  491. efficiency = getwork_requested ? cgpu->accepted * 100.0 / getwork_requested : 0.0;
  492. if (!opt_quiet) {
  493. printf("[%sPU %d] [%.1f Mh/s] [Q:%d A:%d R:%d HW:%d E:%.0f%% U:%.2f/m] \n",
  494. cgpu->is_gpu? "G" : "C", cgpu->cpu_gpu, cgpu->total_mhashes / total_secs,
  495. getwork_requested, cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  496. efficiency, utility);
  497. print_status();
  498. }
  499. applog(LOG_INFO, "%sPU %d Requested:%d Accepted:%d Rejected:%d HW errors:%d Efficiency:%.0f%% Utility:%.2f/m",
  500. cgpu->is_gpu? "G" : "C", cgpu->cpu_gpu, getwork_requested, cgpu->accepted, cgpu->rejected, cgpu->hw_errors, efficiency, utility
  501. );
  502. json_decref(val);
  503. rc = true;
  504. out:
  505. free(hexstr);
  506. out_nofree:
  507. curl_easy_cleanup(curl);
  508. return rc;
  509. }
  510. static const char *rpc_req =
  511. "{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
  512. static bool get_upstream_work(struct work *work)
  513. {
  514. json_t *val;
  515. bool rc = false;
  516. CURL *curl = curl_easy_init();
  517. if (unlikely(!curl)) {
  518. applog(LOG_ERR, "CURL initialisation failed");
  519. return rc;
  520. }
  521. val = json_rpc_call(curl, rpc_url, rpc_userpass, rpc_req,
  522. want_longpoll, false);
  523. if (unlikely(!val)) {
  524. applog(LOG_DEBUG, "Failed json_rpc_call in get_upstream_work");
  525. goto out;
  526. }
  527. rc = work_decode(json_object_get(val, "result"), work);
  528. json_decref(val);
  529. out:
  530. curl_easy_cleanup(curl);
  531. return rc;
  532. }
  533. static void workio_cmd_free(struct workio_cmd *wc)
  534. {
  535. if (!wc)
  536. return;
  537. switch (wc->cmd) {
  538. case WC_SUBMIT_WORK:
  539. free(wc->u.work);
  540. break;
  541. default: /* do nothing */
  542. break;
  543. }
  544. memset(wc, 0, sizeof(*wc)); /* poison */
  545. free(wc);
  546. }
  547. static void kill_work(void)
  548. {
  549. struct workio_cmd *wc;
  550. applog(LOG_INFO, "Received kill message");
  551. wc = calloc(1, sizeof(*wc));
  552. if (unlikely(!wc)) {
  553. applog(LOG_ERR, "Failed to calloc wc in kill_work");
  554. /* We're just trying to die anyway, so forget graceful */
  555. exit (1);
  556. }
  557. wc->cmd = WC_DIE;
  558. wc->thr = 0;
  559. if (unlikely(!tq_push(thr_info[work_thr_id].q, wc))) {
  560. applog(LOG_ERR, "Failed to tq_push work in kill_work");
  561. exit (1);
  562. }
  563. }
  564. static void *get_work_thread(void *userdata)
  565. {
  566. struct workio_cmd *wc = (struct workio_cmd *)userdata;
  567. struct work *ret_work;
  568. int failures = 0;
  569. pthread_detach(pthread_self());
  570. ret_work = calloc(1, sizeof(*ret_work));
  571. if (unlikely(!ret_work)) {
  572. applog(LOG_ERR, "Failed to calloc ret_work in workio_get_work");
  573. kill_work();
  574. goto out;
  575. }
  576. /* obtain new work from bitcoin via JSON-RPC */
  577. while (!get_upstream_work(ret_work)) {
  578. if (unlikely((opt_retries >= 0) && (++failures > opt_retries))) {
  579. applog(LOG_ERR, "json_rpc_call failed, terminating workio thread");
  580. free(ret_work);
  581. kill_work();
  582. goto out;
  583. }
  584. /* pause, then restart work-request loop */
  585. applog(LOG_DEBUG, "json_rpc_call failed on get work, retry after %d seconds",
  586. opt_fail_pause);
  587. sleep(opt_fail_pause);
  588. }
  589. /* send work to requesting thread */
  590. if (unlikely(!tq_push(thr_info[stage_thr_id].q, ret_work))) {
  591. applog(LOG_ERR, "Failed to tq_push work in workio_get_work");
  592. kill_work();
  593. free(ret_work);
  594. }
  595. out:
  596. workio_cmd_free(wc);
  597. return NULL;
  598. }
  599. static bool workio_get_work(struct workio_cmd *wc)
  600. {
  601. pthread_t get_thread;
  602. if (unlikely(pthread_create(&get_thread, NULL, get_work_thread, (void *)wc))) {
  603. applog(LOG_ERR, "Failed to create get_work_thread");
  604. return false;
  605. }
  606. return true;
  607. }
  608. static void *submit_work_thread(void *userdata)
  609. {
  610. struct workio_cmd *wc = (struct workio_cmd *)userdata;
  611. int failures = 0;
  612. char *hexstr;
  613. pthread_detach(pthread_self());
  614. hexstr = bin2hex(wc->u.work->data, 36);
  615. if (unlikely(!hexstr)) {
  616. applog(LOG_ERR, "submit_work_thread OOM");
  617. goto out;
  618. }
  619. if (unlikely(strncmp(hexstr, current_block, 36))) {
  620. applog(LOG_INFO, "Stale work detected, discarding");
  621. goto out_free;
  622. }
  623. /* submit solution to bitcoin via JSON-RPC */
  624. while (!submit_upstream_work(wc->u.work)) {
  625. if (unlikely(strncmp(hexstr, current_block, 36))) {
  626. applog(LOG_INFO, "Stale work detected, discarding");
  627. goto out_free;
  628. }
  629. if (unlikely((opt_retries >= 0) && (++failures > opt_retries))) {
  630. applog(LOG_ERR, "Failed %d retries ...terminating workio thread", opt_retries);
  631. kill_work();
  632. goto out_free;
  633. }
  634. /* pause, then restart work-request loop */
  635. applog(LOG_ERR, "json_rpc_call failed on submit_work, retry after %d seconds",
  636. opt_fail_pause);
  637. sleep(opt_fail_pause);
  638. }
  639. out_free:
  640. free(hexstr);
  641. out:
  642. workio_cmd_free(wc);
  643. return NULL;
  644. }
  645. static bool workio_submit_work(struct workio_cmd *wc)
  646. {
  647. pthread_t submit_thread;
  648. if (unlikely(pthread_create(&submit_thread, NULL, submit_work_thread, (void *)wc))) {
  649. applog(LOG_ERR, "Failed to create submit_work_thread");
  650. return false;
  651. }
  652. return true;
  653. }
  654. static void inc_staged(int inc, bool lp)
  655. {
  656. pthread_mutex_lock(&stgd_lock);
  657. total_staged += inc;
  658. if (lp)
  659. lp_staged += inc;
  660. pthread_mutex_unlock(&stgd_lock);
  661. }
  662. static void dec_staged(int inc)
  663. {
  664. pthread_mutex_lock(&stgd_lock);
  665. if (lp_staged)
  666. lp_staged -= inc;
  667. total_staged -= inc;
  668. pthread_mutex_unlock(&stgd_lock);
  669. }
  670. static int requests_staged(void)
  671. {
  672. int ret;
  673. pthread_mutex_lock(&stgd_lock);
  674. ret = total_staged;
  675. pthread_mutex_unlock(&stgd_lock);
  676. return ret;
  677. }
  678. static void *stage_thread(void *userdata)
  679. {
  680. struct thr_info *mythr = userdata;
  681. bool ok = true;
  682. unsigned int i;
  683. for (i = 0; i < 36; i++) {
  684. strcat(current_block, "0");
  685. strcat(blank, "0");
  686. }
  687. while (ok) {
  688. struct work *work = NULL;
  689. char *hexstr;
  690. work = tq_pop(mythr->q, NULL);
  691. if (unlikely(!work)) {
  692. applog(LOG_ERR, "Failed to tq_pop in stage_thread");
  693. ok = false;
  694. break;
  695. }
  696. hexstr = bin2hex(work->data, 36);
  697. if (unlikely(!hexstr)) {
  698. applog(LOG_ERR, "stage_thread OOM");
  699. break;
  700. }
  701. /* current_block is blanked out on successful longpoll */
  702. if (likely(strncmp(current_block, blank, 36))) {
  703. if (unlikely(strncmp(hexstr, current_block, 36))) {
  704. if (want_longpoll)
  705. applog(LOG_WARNING, "New block detected, possible missed longpoll, flushing work queue");
  706. else
  707. applog(LOG_WARNING, "New block detected, flushing work queue ");
  708. /* As we can't flush the work from here, signal
  709. * the wakeup thread to restart all the
  710. * threads */
  711. work_restart[stage_thr_id].restart = 1;
  712. }
  713. } else
  714. memcpy(longpoll_block, hexstr, 36);
  715. memcpy(current_block, hexstr, 36);
  716. free(hexstr);
  717. if (unlikely(!tq_push(thr_info[0].q, work))) {
  718. applog(LOG_ERR, "Failed to tq_push work in stage_thread");
  719. ok = false;
  720. break;
  721. }
  722. inc_staged(1, false);
  723. }
  724. tq_freeze(mythr->q);
  725. return NULL;
  726. }
  727. static void *workio_thread(void *userdata)
  728. {
  729. struct thr_info *mythr = userdata;
  730. bool ok = true;
  731. while (ok) {
  732. struct workio_cmd *wc;
  733. /* wait for workio_cmd sent to us, on our queue */
  734. wc = tq_pop(mythr->q, NULL);
  735. if (unlikely(!wc)) {
  736. applog(LOG_ERR, "Failed to tq_pop in workio_thread");
  737. ok = false;
  738. break;
  739. }
  740. /* process workio_cmd */
  741. switch (wc->cmd) {
  742. case WC_GET_WORK:
  743. ok = workio_get_work(wc);
  744. break;
  745. case WC_SUBMIT_WORK:
  746. ok = workio_submit_work(wc);
  747. break;
  748. case WC_DIE:
  749. default:
  750. ok = false;
  751. break;
  752. }
  753. }
  754. tq_freeze(mythr->q);
  755. return NULL;
  756. }
  757. static void hashmeter(int thr_id, struct timeval *diff,
  758. unsigned long hashes_done)
  759. {
  760. struct timeval temp_tv_end, total_diff;
  761. double khashes, secs;
  762. double local_secs;
  763. double utility, efficiency = 0.0;
  764. static double local_mhashes_done = 0;
  765. static double rolling_local = 0;
  766. double local_mhashes = (double)hashes_done / 1000000.0;
  767. /* Don't bother calculating anything if we're not displaying it */
  768. if (opt_quiet || !opt_log_interval)
  769. return;
  770. khashes = hashes_done / 1000.0;
  771. secs = (double)diff->tv_sec + ((double)diff->tv_usec / 1000000.0);
  772. if (thr_id >= 0) {
  773. /* So we can call hashmeter from a non worker thread */
  774. struct cgpu_info *cgpu = thr_info[thr_id].cgpu;
  775. if (opt_debug)
  776. applog(LOG_DEBUG, "[thread %d: %lu hashes, %.0f khash/sec]",
  777. thr_id, hashes_done, hashes_done / secs);
  778. cgpu->local_mhashes += local_mhashes;
  779. cgpu->total_mhashes += local_mhashes;
  780. }
  781. /* Totals are updated by all threads so can race without locking */
  782. pthread_mutex_lock(&hash_lock);
  783. gettimeofday(&temp_tv_end, NULL);
  784. timeval_subtract(&total_diff, &temp_tv_end, &total_tv_end);
  785. local_secs = (double)total_diff.tv_sec + ((double)total_diff.tv_usec / 1000000.0);
  786. total_mhashes_done += local_mhashes;
  787. local_mhashes_done += local_mhashes;
  788. if (total_diff.tv_sec < opt_log_interval)
  789. /* Only update the total every opt_log_interval seconds */
  790. goto out_unlock;
  791. gettimeofday(&total_tv_end, NULL);
  792. /* Use a rolling average by faking an exponential decay over 5 * log */
  793. rolling_local = ((rolling_local * 0.9) + local_mhashes_done) / 1.9;
  794. timeval_subtract(&total_diff, &total_tv_end, &total_tv_start);
  795. total_secs = (double)total_diff.tv_sec +
  796. ((double)total_diff.tv_usec / 1000000.0);
  797. utility = accepted / ( total_secs ? total_secs : 1 ) * 60;
  798. efficiency = getwork_requested ? accepted * 100.0 / getwork_requested : 0.0;
  799. sprintf(statusline, "[(%ds):%.1f (avg):%.1f Mh/s] [Q:%d A:%d R:%d HW:%d E:%.0f%% U:%.2f/m] ",
  800. opt_log_interval, rolling_local / local_secs, total_mhashes_done / total_secs,
  801. getwork_requested, accepted, rejected, hw_errors, efficiency, utility);
  802. print_status();
  803. applog(LOG_INFO, "[Rate (%ds):%.1f (avg):%.2f Mhash/s] [Requested:%d Accepted:%d Rejected:%d HW errors:%d Efficiency:%.0f%% Utility:%.2f/m]",
  804. opt_log_interval, rolling_local / local_secs, total_mhashes_done / total_secs,
  805. getwork_requested, accepted, rejected, hw_errors, efficiency, utility);
  806. local_mhashes_done = 0;
  807. out_unlock:
  808. pthread_mutex_unlock(&hash_lock);
  809. }
  810. /* This is overkill, but at least we'll know accurately how much work is
  811. * queued to prevent ever being left without work */
  812. static void inc_queued(void)
  813. {
  814. pthread_mutex_lock(&qd_lock);
  815. total_queued++;
  816. pthread_mutex_unlock(&qd_lock);
  817. }
  818. static void dec_queued(void)
  819. {
  820. pthread_mutex_lock(&qd_lock);
  821. total_queued--;
  822. pthread_mutex_unlock(&qd_lock);
  823. dec_staged(1);
  824. }
  825. static int requests_queued(void)
  826. {
  827. int ret;
  828. pthread_mutex_lock(&qd_lock);
  829. ret = total_queued;
  830. pthread_mutex_unlock(&qd_lock);
  831. return ret;
  832. }
  833. /* All work is queued flagged as being for thread 0 and then the mining thread
  834. * flags it as its own */
  835. static bool queue_request(void)
  836. {
  837. struct thr_info *thr = &thr_info[0];
  838. struct workio_cmd *wc;
  839. /* fill out work request message */
  840. wc = calloc(1, sizeof(*wc));
  841. if (unlikely(!wc)) {
  842. applog(LOG_ERR, "Failed to tq_pop in queue_request");
  843. return false;
  844. }
  845. wc->cmd = WC_GET_WORK;
  846. wc->thr = thr;
  847. /* send work request to workio thread */
  848. if (unlikely(!tq_push(thr_info[work_thr_id].q, wc))) {
  849. applog(LOG_ERR, "Failed to tq_push in queue_request");
  850. workio_cmd_free(wc);
  851. return false;
  852. }
  853. inc_queued();
  854. return true;
  855. }
  856. static bool discard_request(void)
  857. {
  858. struct thr_info *thr = &thr_info[0];
  859. struct work *work_heap;
  860. /* Just in case we fell in a hole and missed a queue filling */
  861. if (unlikely(!requests_queued())) {
  862. applog(LOG_WARNING, "Tried to discard_request with nil queued");
  863. return true;
  864. }
  865. work_heap = tq_pop(thr->q, NULL);
  866. if (unlikely(!work_heap)) {
  867. applog(LOG_ERR, "Failed to tq_pop in discard_request");
  868. return false;
  869. }
  870. free(work_heap);
  871. dec_queued();
  872. return true;
  873. }
  874. static void flush_requests(bool longpoll)
  875. {
  876. int i, extra;
  877. extra = requests_queued();
  878. /* When flushing from longpoll, we don't know the new work yet. When
  879. * not flushing from longpoll, the first work item is valid so do not
  880. * discard it */
  881. if (longpoll)
  882. memcpy(current_block, blank, 36);
  883. else
  884. extra--;
  885. /* Temporarily increase the staged count so that get_work thinks there
  886. * is work available instead of making threads reuse existing work */
  887. inc_staged(extra, true);
  888. for (i = 0; i < extra; i++) {
  889. /* Queue a whole batch of new requests */
  890. if (unlikely(!queue_request())) {
  891. applog(LOG_ERR, "Failed to queue requests in flush_requests");
  892. kill_work();
  893. break;
  894. }
  895. /* Pop off the old requests. Cancelling the requests would be better
  896. * but is tricky */
  897. if (unlikely(!discard_request())) {
  898. applog(LOG_ERR, "Failed to discard requests in flush_requests");
  899. kill_work();
  900. break;
  901. }
  902. }
  903. }
  904. static bool get_work(struct work *work, bool queued)
  905. {
  906. struct thr_info *thr = &thr_info[0];
  907. struct work *work_heap;
  908. bool ret = false;
  909. int failures = 0;
  910. retry:
  911. if (unlikely(!queued && !queue_request())) {
  912. applog(LOG_WARNING, "Failed to queue_request in get_work");
  913. goto out;
  914. }
  915. if (!requests_staged()) {
  916. uint32_t *work_ntime;
  917. uint32_t ntime;
  918. /* Only print this message once each time we shift to localgen */
  919. if (!localgen)
  920. applog(LOG_WARNING, "Server not providing work fast enough, generating work locally");
  921. localgen = true;
  922. work_ntime = (uint32_t *)(work->data + 68);
  923. ntime = be32toh(*work_ntime);
  924. ntime++;
  925. *work_ntime = htobe32(ntime);
  926. ret = true;
  927. goto out;
  928. } else if (localgen) {
  929. localgen = false;
  930. applog(LOG_WARNING, "Resumed retrieving work from server");
  931. }
  932. /* wait for 1st response, or get cached response */
  933. work_heap = tq_pop(thr->q, NULL);
  934. if (unlikely(!work_heap)) {
  935. applog(LOG_WARNING, "Failed to tq_pop in get_work");
  936. goto out;
  937. }
  938. dec_queued();
  939. memcpy(work, work_heap, sizeof(*work));
  940. ret = true;
  941. free(work_heap);
  942. out:
  943. if (unlikely(ret == false)) {
  944. if ((opt_retries >= 0) && (++failures > opt_retries)) {
  945. applog(LOG_ERR, "Failed %d times to get_work");
  946. return ret;
  947. }
  948. applog(LOG_DEBUG, "Retrying after %d seconds", opt_fail_pause);
  949. sleep(opt_fail_pause);
  950. goto retry;
  951. }
  952. return ret;
  953. }
  954. static bool submit_work_sync(struct thr_info *thr, const struct work *work_in)
  955. {
  956. struct workio_cmd *wc;
  957. /* fill out work request message */
  958. wc = calloc(1, sizeof(*wc));
  959. if (unlikely(!wc)) {
  960. applog(LOG_ERR, "Failed to calloc wc in submit_work_sync");
  961. return false;
  962. }
  963. wc->u.work = malloc(sizeof(*work_in));
  964. if (unlikely(!wc->u.work)) {
  965. applog(LOG_ERR, "Failed to calloc work in submit_work_sync");
  966. goto err_out;
  967. }
  968. wc->cmd = WC_SUBMIT_WORK;
  969. wc->thr = thr;
  970. memcpy(wc->u.work, work_in, sizeof(*work_in));
  971. /* send solution to workio thread */
  972. if (unlikely(!tq_push(thr_info[work_thr_id].q, wc))) {
  973. applog(LOG_ERR, "Failed to tq_push work in submit_work_sync");
  974. goto err_out;
  975. }
  976. return true;
  977. err_out:
  978. workio_cmd_free(wc);
  979. return false;
  980. }
  981. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
  982. {
  983. work->data[64+12+0] = (nonce>>0) & 0xff;
  984. work->data[64+12+1] = (nonce>>8) & 0xff;
  985. work->data[64+12+2] = (nonce>>16) & 0xff;
  986. work->data[64+12+3] = (nonce>>24) & 0xff;
  987. return submit_work_sync(thr, work);
  988. }
  989. static inline int cpu_from_thr_id(int thr_id)
  990. {
  991. return (thr_id - gpu_threads) % num_processors;
  992. }
  993. static void *miner_thread(void *userdata)
  994. {
  995. struct thr_info *mythr = userdata;
  996. const int thr_id = mythr->id;
  997. uint32_t max_nonce = 0xffffff;
  998. bool needs_work = true;
  999. /* Try to cycle approximately 5 times before each log update */
  1000. const unsigned long cycle = opt_log_interval / 5 ? : 1;
  1001. /* Request the next work item at 2/3 of the scantime */
  1002. unsigned const int request_interval = opt_scantime * 2 / 3 ? : 1;
  1003. unsigned const long request_nonce = MAXTHREADS / 3 * 2;
  1004. bool requested = true;
  1005. /* Set worker threads to nice 19 and then preferentially to SCHED_IDLE
  1006. * and if that fails, then SCHED_BATCH. No need for this to be an
  1007. * error if it fails */
  1008. setpriority(PRIO_PROCESS, 0, 19);
  1009. drop_policy();
  1010. /* Cpu affinity only makes sense if the number of threads is a multiple
  1011. * of the number of CPUs */
  1012. if (!(opt_n_threads % num_processors))
  1013. affine_to_cpu(thr_id - gpu_threads, cpu_from_thr_id(thr_id));
  1014. while (1) {
  1015. struct work work __attribute__((aligned(128)));
  1016. unsigned long hashes_done;
  1017. struct timeval tv_workstart, tv_start, tv_end, diff;
  1018. uint64_t max64;
  1019. bool rc;
  1020. if (needs_work) {
  1021. gettimeofday(&tv_workstart, NULL);
  1022. /* obtain new work from internal workio thread */
  1023. if (unlikely(!get_work(&work, requested))) {
  1024. applog(LOG_ERR, "work retrieval failed, exiting "
  1025. "mining thread %d", mythr->id);
  1026. goto out;
  1027. }
  1028. work.thr_id = thr_id;
  1029. needs_work = requested = false;
  1030. work.blk.nonce = 0;
  1031. }
  1032. hashes_done = 0;
  1033. gettimeofday(&tv_start, NULL);
  1034. /* scan nonces for a proof-of-work hash */
  1035. switch (opt_algo) {
  1036. case ALGO_C:
  1037. rc = scanhash_c(thr_id, work.midstate, work.data + 64,
  1038. work.hash1, work.hash, work.target,
  1039. max_nonce, &hashes_done,
  1040. work.blk.nonce);
  1041. break;
  1042. #ifdef WANT_X8664_SSE2
  1043. case ALGO_SSE2_64: {
  1044. unsigned int rc5 =
  1045. scanhash_sse2_64(thr_id, work.midstate, work.data + 64,
  1046. work.hash1, work.hash,
  1047. work.target,
  1048. max_nonce, &hashes_done,
  1049. work.blk.nonce);
  1050. rc = (rc5 == -1) ? false : true;
  1051. }
  1052. break;
  1053. #endif
  1054. #ifdef WANT_SSE2_4WAY
  1055. case ALGO_4WAY: {
  1056. unsigned int rc4 =
  1057. ScanHash_4WaySSE2(thr_id, work.midstate, work.data + 64,
  1058. work.hash1, work.hash,
  1059. work.target,
  1060. max_nonce, &hashes_done,
  1061. work.blk.nonce);
  1062. rc = (rc4 == -1) ? false : true;
  1063. }
  1064. break;
  1065. #endif
  1066. #ifdef WANT_VIA_PADLOCK
  1067. case ALGO_VIA:
  1068. rc = scanhash_via(thr_id, work.data, work.target,
  1069. max_nonce, &hashes_done,
  1070. work.blk.nonce);
  1071. break;
  1072. #endif
  1073. case ALGO_CRYPTOPP:
  1074. rc = scanhash_cryptopp(thr_id, work.midstate, work.data + 64,
  1075. work.hash1, work.hash, work.target,
  1076. max_nonce, &hashes_done,
  1077. work.blk.nonce);
  1078. break;
  1079. #ifdef WANT_CRYPTOPP_ASM32
  1080. case ALGO_CRYPTOPP_ASM32:
  1081. rc = scanhash_asm32(thr_id, work.midstate, work.data + 64,
  1082. work.hash1, work.hash, work.target,
  1083. max_nonce, &hashes_done,
  1084. work.blk.nonce);
  1085. break;
  1086. #endif
  1087. default:
  1088. /* should never happen */
  1089. goto out;
  1090. }
  1091. /* record scanhash elapsed time */
  1092. gettimeofday(&tv_end, NULL);
  1093. timeval_subtract(&diff, &tv_end, &tv_start);
  1094. hashes_done -= work.blk.nonce;
  1095. hashmeter(thr_id, &diff, hashes_done);
  1096. work.blk.nonce += hashes_done;
  1097. /* adjust max_nonce to meet target cycle time */
  1098. if (diff.tv_usec > 500000)
  1099. diff.tv_sec++;
  1100. if (diff.tv_sec && diff.tv_sec != cycle) {
  1101. max64 = work.blk.nonce +
  1102. ((uint64_t)hashes_done * cycle) / diff.tv_sec;
  1103. } else
  1104. max64 = work.blk.nonce + hashes_done;
  1105. if (max64 > 0xfffffffaULL)
  1106. max64 = 0xfffffffaULL;
  1107. max_nonce = max64;
  1108. /* if nonce found, submit work */
  1109. if (unlikely(rc)) {
  1110. if (opt_debug)
  1111. applog(LOG_DEBUG, "CPU %d found something?", cpu_from_thr_id(thr_id));
  1112. if (unlikely(!submit_work_sync(mythr, &work))) {
  1113. applog(LOG_ERR, "Failed to submit_work_sync in miner_thread %d", thr_id);
  1114. break;
  1115. }
  1116. work.blk.nonce += 4;
  1117. }
  1118. timeval_subtract(&diff, &tv_end, &tv_workstart);
  1119. if (!requested && (diff.tv_sec > request_interval || work.blk.nonce > request_nonce)) {
  1120. if (unlikely(!queue_request())) {
  1121. applog(LOG_ERR, "Failed to queue_request in miner_thread %d", thr_id);
  1122. goto out;
  1123. }
  1124. requested = true;
  1125. }
  1126. if (diff.tv_sec > opt_scantime || work_restart[thr_id].restart ||
  1127. work.blk.nonce >= MAXTHREADS - hashes_done)
  1128. needs_work = true;
  1129. }
  1130. out:
  1131. tq_freeze(mythr->q);
  1132. return NULL;
  1133. }
  1134. enum {
  1135. STAT_SLEEP_INTERVAL = 1,
  1136. STAT_CTR_INTERVAL = 10000000,
  1137. FAILURE_INTERVAL = 30,
  1138. };
  1139. #ifdef HAVE_OPENCL
  1140. static _clState *clStates[16];
  1141. static inline cl_int queue_kernel_parameters(_clState *clState, dev_blk_ctx *blk)
  1142. {
  1143. cl_kernel *kernel = &clState->kernel;
  1144. cl_int status = 0;
  1145. int num = 0;
  1146. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_a);
  1147. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_b);
  1148. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_c);
  1149. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_d);
  1150. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_e);
  1151. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_f);
  1152. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_g);
  1153. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_h);
  1154. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_b);
  1155. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_c);
  1156. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_d);
  1157. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_f);
  1158. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_g);
  1159. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_h);
  1160. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->nonce);
  1161. if (clState->hasBitAlign == true) {
  1162. /* Parameters for phatk kernel */
  1163. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->W2);
  1164. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->W16);
  1165. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->W17);
  1166. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->PreVal4);
  1167. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->T1);
  1168. } else {
  1169. /* Parameters for poclbm kernel */
  1170. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW0);
  1171. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW1);
  1172. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW2);
  1173. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW3);
  1174. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW15);
  1175. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW01r);
  1176. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e);
  1177. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e2);
  1178. }
  1179. status |= clSetKernelArg(*kernel, num++, sizeof(clState->outputBuffer),
  1180. (void *)&clState->outputBuffer);
  1181. return status;
  1182. }
  1183. static inline int gpu_from_thr_id(int thr_id)
  1184. {
  1185. return thr_id % nDevs;
  1186. }
  1187. static void *gpuminer_thread(void *userdata)
  1188. {
  1189. const unsigned long cycle = opt_log_interval / 5 ? : 1;
  1190. struct timeval tv_start, tv_end, diff;
  1191. struct thr_info *mythr = userdata;
  1192. const int thr_id = mythr->id;
  1193. uint32_t *res, *blank_res;
  1194. size_t globalThreads[1];
  1195. size_t localThreads[1];
  1196. cl_int status;
  1197. _clState *clState = clStates[thr_id];
  1198. const cl_kernel *kernel = &clState->kernel;
  1199. struct work *work = malloc(sizeof(struct work));
  1200. unsigned const int threads = 1 << (15 + scan_intensity);
  1201. unsigned const int vectors = clState->preferred_vwidth;
  1202. unsigned const int hashes = threads * vectors;
  1203. unsigned int hashes_done = 0;
  1204. /* Request the next work item at 2/3 of the scantime */
  1205. unsigned const int request_interval = opt_scantime * 2 / 3 ? : 1;
  1206. unsigned const long request_nonce = MAXTHREADS / 3 * 2;
  1207. bool requested = true;
  1208. res = calloc(BUFFERSIZE, 1);
  1209. blank_res = calloc(BUFFERSIZE, 1);
  1210. if (!res || !blank_res) {
  1211. applog(LOG_ERR, "Failed to calloc in gpuminer_thread");
  1212. goto out;
  1213. }
  1214. gettimeofday(&tv_start, NULL);
  1215. globalThreads[0] = threads;
  1216. localThreads[0] = clState->work_size;
  1217. diff.tv_sec = ~0UL;
  1218. gettimeofday(&tv_end, NULL);
  1219. while (1) {
  1220. struct timeval tv_workstart;
  1221. /* This finish flushes the readbuffer set with CL_FALSE later */
  1222. clFinish(clState->commandQueue);
  1223. if (diff.tv_sec > opt_scantime || work->blk.nonce >= MAXTHREADS - hashes || work_restart[thr_id].restart) {
  1224. /* Ignore any reads since we're getting new work and queue a clean buffer */
  1225. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1226. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1227. if (unlikely(status != CL_SUCCESS))
  1228. { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
  1229. memset(res, 0, BUFFERSIZE);
  1230. gettimeofday(&tv_workstart, NULL);
  1231. /* obtain new work from internal workio thread */
  1232. if (unlikely(!get_work(work, requested))) {
  1233. applog(LOG_ERR, "work retrieval failed, exiting "
  1234. "gpu mining thread %d", mythr->id);
  1235. goto out;
  1236. }
  1237. work->thr_id = thr_id;
  1238. requested = false;
  1239. getwork_requested++;
  1240. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1241. work->blk.nonce = 0;
  1242. work_restart[thr_id].restart = 0;
  1243. if (opt_debug)
  1244. applog(LOG_DEBUG, "getwork thread %d", thr_id);
  1245. /* Flushes the writebuffer set with CL_FALSE above */
  1246. clFinish(clState->commandQueue);
  1247. }
  1248. status = queue_kernel_parameters(clState, &work->blk);
  1249. if (unlikely(status != CL_SUCCESS))
  1250. { applog(LOG_ERR, "Error: clSetKernelArg of all params failed."); goto out; }
  1251. /* MAXBUFFERS entry is used as a flag to say nonces exist */
  1252. if (res[MAXBUFFERS]) {
  1253. /* Clear the buffer again */
  1254. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1255. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1256. if (unlikely(status != CL_SUCCESS))
  1257. { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
  1258. if (opt_debug)
  1259. applog(LOG_DEBUG, "GPU %d found something?", gpu_from_thr_id(thr_id));
  1260. postcalc_hash_async(mythr, work, res);
  1261. memset(res, 0, BUFFERSIZE);
  1262. clFinish(clState->commandQueue);
  1263. }
  1264. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1265. globalThreads, localThreads, 0, NULL, NULL);
  1266. if (unlikely(status != CL_SUCCESS))
  1267. { applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)"); goto out; }
  1268. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1269. BUFFERSIZE, res, 0, NULL, NULL);
  1270. if (unlikely(status != CL_SUCCESS))
  1271. { applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)"); goto out;}
  1272. gettimeofday(&tv_end, NULL);
  1273. timeval_subtract(&diff, &tv_end, &tv_start);
  1274. hashes_done += hashes;
  1275. work->blk.nonce += hashes;
  1276. if (diff.tv_usec > 500000)
  1277. diff.tv_sec++;
  1278. if (diff.tv_sec >= cycle) {
  1279. hashmeter(thr_id, &diff, hashes_done);
  1280. gettimeofday(&tv_start, NULL);
  1281. hashes_done = 0;
  1282. }
  1283. timeval_subtract(&diff, &tv_end, &tv_workstart);
  1284. if (!requested && (diff.tv_sec > request_interval || work->blk.nonce > request_nonce)) {
  1285. if (unlikely(!queue_request())) {
  1286. applog(LOG_ERR, "Failed to queue_request in gpuminer_thread %d", thr_id);
  1287. goto out;
  1288. }
  1289. requested = true;
  1290. }
  1291. }
  1292. out:
  1293. tq_freeze(mythr->q);
  1294. return NULL;
  1295. }
  1296. #endif /* HAVE_OPENCL */
  1297. static void restart_threads(bool longpoll)
  1298. {
  1299. int i;
  1300. /* Discard old queued requests and get new ones */
  1301. flush_requests(longpoll);
  1302. for (i = 0; i < opt_n_threads + gpu_threads; i++)
  1303. work_restart[i].restart = 1;
  1304. }
  1305. static void *longpoll_thread(void *userdata)
  1306. {
  1307. struct thr_info *mythr = userdata;
  1308. CURL *curl = NULL;
  1309. char *copy_start, *hdr_path, *lp_url = NULL;
  1310. bool need_slash = false;
  1311. int failures = 0;
  1312. unsigned int i;
  1313. hdr_path = tq_pop(mythr->q, NULL);
  1314. if (!hdr_path)
  1315. goto out;
  1316. /* full URL */
  1317. if (strstr(hdr_path, "://")) {
  1318. lp_url = hdr_path;
  1319. hdr_path = NULL;
  1320. }
  1321. /* absolute path, on current server */
  1322. else {
  1323. copy_start = (*hdr_path == '/') ? (hdr_path + 1) : hdr_path;
  1324. if (rpc_url[strlen(rpc_url) - 1] != '/')
  1325. need_slash = true;
  1326. lp_url = malloc(strlen(rpc_url) + strlen(copy_start) + 2);
  1327. if (!lp_url)
  1328. goto out;
  1329. sprintf(lp_url, "%s%s%s", rpc_url, need_slash ? "/" : "", copy_start);
  1330. }
  1331. applog(LOG_INFO, "Long-polling activated for %s", lp_url);
  1332. curl = curl_easy_init();
  1333. if (unlikely(!curl)) {
  1334. applog(LOG_ERR, "CURL initialisation failed");
  1335. goto out;
  1336. }
  1337. for (i = 0; i < 36; i++)
  1338. strcat(longpoll_block, "0");
  1339. while (1) {
  1340. json_t *val;
  1341. val = json_rpc_call(curl, lp_url, rpc_userpass, rpc_req,
  1342. false, true);
  1343. if (likely(val)) {
  1344. failures = 0;
  1345. json_decref(val);
  1346. /* Keep track of who ordered a restart_threads to make
  1347. * sure it's only done once per new block */
  1348. if (likely(!strncmp(longpoll_block, blank, 36) ||
  1349. !strncmp(longpoll_block, current_block, 36))) {
  1350. applog(LOG_WARNING, "LONGPOLL detected new block, flushing work queue ");
  1351. restart_threads(true);
  1352. } else
  1353. applog(LOG_WARNING, "LONGPOLL received - new block detected and work flushed already ");
  1354. } else {
  1355. if (failures++ < 10) {
  1356. sleep(30);
  1357. applog(LOG_WARNING,
  1358. "longpoll failed, sleeping for 30s");
  1359. } else {
  1360. applog(LOG_ERR,
  1361. "longpoll failed, ending thread");
  1362. goto out;
  1363. }
  1364. }
  1365. memcpy(longpoll_block, current_block, 36);
  1366. }
  1367. out:
  1368. free(hdr_path);
  1369. free(lp_url);
  1370. tq_freeze(mythr->q);
  1371. if (curl)
  1372. curl_easy_cleanup(curl);
  1373. return NULL;
  1374. }
  1375. /* Makes sure the hashmeter keeps going even if mining threads stall */
  1376. static void *wakeup_thread(void *userdata)
  1377. {
  1378. const unsigned int interval = opt_log_interval / 2 ? : 1;
  1379. struct timeval zero_tv;
  1380. memset(&zero_tv, 0, sizeof(struct timeval));
  1381. while (1) {
  1382. sleep(interval);
  1383. if (requests_queued() < opt_queue)
  1384. queue_request();
  1385. hashmeter(-1, &zero_tv, 0);
  1386. if (unlikely(work_restart[stage_thr_id].restart)) {
  1387. restart_threads(false);
  1388. work_restart[stage_thr_id].restart = 0;
  1389. }
  1390. }
  1391. return NULL;
  1392. }
  1393. int main (int argc, char *argv[])
  1394. {
  1395. struct thr_info *thr;
  1396. unsigned int i;
  1397. char name[32];
  1398. struct cgpu_info *gpus = NULL, *cpus = NULL;
  1399. if (unlikely(pthread_mutex_init(&time_lock, NULL)))
  1400. return 1;
  1401. if (unlikely(pthread_mutex_init(&hash_lock, NULL)))
  1402. return 1;
  1403. if (unlikely(pthread_mutex_init(&qd_lock, NULL)))
  1404. return 1;
  1405. if (unlikely(pthread_mutex_init(&stgd_lock, NULL)))
  1406. return 1;
  1407. #ifdef WIN32
  1408. opt_n_threads = num_processors = 1;
  1409. #else
  1410. num_processors = sysconf(_SC_NPROCESSORS_ONLN);
  1411. opt_n_threads = num_processors;
  1412. #endif /* !WIN32 */
  1413. #ifdef HAVE_OPENCL
  1414. nDevs = clDevicesNum();
  1415. if (nDevs < 0)
  1416. return 1;
  1417. #endif
  1418. if (nDevs)
  1419. opt_n_threads = 0;
  1420. rpc_url = strdup(DEF_RPC_URL);
  1421. /* parse command line */
  1422. opt_register_table(opt_config_table,
  1423. "Options for both config file and command line");
  1424. opt_register_table(opt_cmdline_table,
  1425. "Options for command line only");
  1426. opt_parse(&argc, argv, applog_and_exit);
  1427. if (argc != 1) {
  1428. applog(LOG_ERR, "Unexpected extra commandline arguments");
  1429. return 1;
  1430. }
  1431. gpu_threads = nDevs * opt_g_threads;
  1432. if (!gpu_threads && !forced_n_threads) {
  1433. /* Maybe they turned GPU off; restore default CPU threads. */
  1434. opt_n_threads = num_processors;
  1435. }
  1436. if (!rpc_userpass) {
  1437. if (!rpc_user || !rpc_pass) {
  1438. applog(LOG_ERR, "No login credentials supplied");
  1439. return 1;
  1440. }
  1441. rpc_userpass = malloc(strlen(rpc_user) + strlen(rpc_pass) + 2);
  1442. if (!rpc_userpass)
  1443. return 1;
  1444. sprintf(rpc_userpass, "%s:%s", rpc_user, rpc_pass);
  1445. }
  1446. if (unlikely(curl_global_init(CURL_GLOBAL_ALL)))
  1447. return 1;
  1448. #ifdef HAVE_SYSLOG_H
  1449. if (use_syslog)
  1450. openlog("cpuminer", LOG_PID, LOG_USER);
  1451. #endif
  1452. work_restart = calloc(opt_n_threads + 4 + gpu_threads, sizeof(*work_restart));
  1453. if (!work_restart)
  1454. return 1;
  1455. thr_info = calloc(opt_n_threads + 4 + gpu_threads, sizeof(*thr));
  1456. if (!thr_info)
  1457. return 1;
  1458. /* init workio thread info */
  1459. work_thr_id = opt_n_threads + gpu_threads;
  1460. thr = &thr_info[work_thr_id];
  1461. thr->id = work_thr_id;
  1462. thr->q = tq_new();
  1463. if (!thr->q)
  1464. return 1;
  1465. /* start work I/O thread */
  1466. if (pthread_create(&thr->pth, NULL, workio_thread, thr)) {
  1467. applog(LOG_ERR, "workio thread create failed");
  1468. return 1;
  1469. }
  1470. /* init longpoll thread info */
  1471. if (want_longpoll) {
  1472. longpoll_thr_id = opt_n_threads + gpu_threads + 1;
  1473. thr = &thr_info[longpoll_thr_id];
  1474. thr->id = longpoll_thr_id;
  1475. thr->q = tq_new();
  1476. if (!thr->q)
  1477. return 1;
  1478. /* start longpoll thread */
  1479. if (unlikely(pthread_create(&thr->pth, NULL, longpoll_thread, thr))) {
  1480. applog(LOG_ERR, "longpoll thread create failed");
  1481. return 1;
  1482. }
  1483. pthread_detach(thr->pth);
  1484. } else
  1485. longpoll_thr_id = -1;
  1486. gettimeofday(&total_tv_start, NULL);
  1487. gettimeofday(&total_tv_end, NULL);
  1488. if (opt_n_threads ) {
  1489. cpus = calloc(num_processors, sizeof(struct cgpu_info));
  1490. if (unlikely(!cpus)) {
  1491. applog(LOG_ERR, "Failed to calloc cpus");
  1492. return 1;
  1493. }
  1494. }
  1495. if (gpu_threads) {
  1496. gpus = calloc(nDevs, sizeof(struct cgpu_info));
  1497. if (unlikely(!gpus)) {
  1498. applog(LOG_ERR, "Failed to calloc gpus");
  1499. return 1;
  1500. }
  1501. }
  1502. stage_thr_id = opt_n_threads + gpu_threads + 3;
  1503. thr = &thr_info[stage_thr_id];
  1504. thr->q = tq_new();
  1505. if (!thr->q)
  1506. return 1;
  1507. /* start stage thread */
  1508. if (pthread_create(&thr->pth, NULL, stage_thread, thr)) {
  1509. applog(LOG_ERR, "stage thread create failed");
  1510. return 1;
  1511. }
  1512. /* Put enough work in the queue */
  1513. for (i = 0; i < opt_queue + opt_n_threads + gpu_threads; i++) {
  1514. if (unlikely(!queue_request())) {
  1515. applog(LOG_ERR, "Failed to queue_request in main");
  1516. return 1;
  1517. }
  1518. }
  1519. #ifdef HAVE_OPENCL
  1520. /* start GPU mining threads */
  1521. for (i = 0; i < gpu_threads; i++) {
  1522. int gpu = gpu_from_thr_id(i);
  1523. thr = &thr_info[i];
  1524. thr->id = i;
  1525. gpus[gpu].is_gpu = 1;
  1526. gpus[gpu].cpu_gpu = gpu;
  1527. thr->cgpu = &gpus[gpu];
  1528. thr->q = tq_new();
  1529. if (!thr->q) {
  1530. applog(LOG_ERR, "tq_new failed in starting gpu mining threads");
  1531. return 1;
  1532. }
  1533. applog(LOG_INFO, "Init GPU thread %i", i);
  1534. clStates[i] = initCl(gpu, name, sizeof(name));
  1535. if (!clStates[i]) {
  1536. applog(LOG_ERR, "Failed to init GPU thread %d", i);
  1537. continue;
  1538. }
  1539. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1540. if (unlikely(pthread_create(&thr->pth, NULL, gpuminer_thread, thr))) {
  1541. applog(LOG_ERR, "thread %d create failed", i);
  1542. return 1;
  1543. }
  1544. pthread_detach(thr->pth);
  1545. }
  1546. applog(LOG_INFO, "%d gpu miner threads started", i);
  1547. #endif
  1548. /* start CPU mining threads */
  1549. for (i = gpu_threads; i < gpu_threads + opt_n_threads; i++) {
  1550. int cpu = cpu_from_thr_id(i);
  1551. thr = &thr_info[i];
  1552. thr->id = i;
  1553. cpus[cpu].cpu_gpu = cpu;
  1554. thr->cgpu = &cpus[cpu];
  1555. thr->q = tq_new();
  1556. if (!thr->q) {
  1557. applog(LOG_ERR, "tq_new failed in starting cpu mining threads");
  1558. return 1;
  1559. }
  1560. if (unlikely(pthread_create(&thr->pth, NULL, miner_thread, thr))) {
  1561. applog(LOG_ERR, "thread %d create failed", i);
  1562. return 1;
  1563. }
  1564. pthread_detach(thr->pth);
  1565. }
  1566. applog(LOG_INFO, "%d cpu miner threads started, "
  1567. "using SHA256 '%s' algorithm.",
  1568. opt_n_threads,
  1569. algo_names[opt_algo]);
  1570. thr = &thr_info[opt_n_threads + gpu_threads + 2];
  1571. /* start wakeup thread */
  1572. if (pthread_create(&thr->pth, NULL, wakeup_thread, NULL)) {
  1573. applog(LOG_ERR, "wakeup thread create failed");
  1574. return 1;
  1575. }
  1576. /* Restart count as it will be wrong till all threads are started */
  1577. pthread_mutex_lock(&hash_lock);
  1578. gettimeofday(&total_tv_start, NULL);
  1579. gettimeofday(&total_tv_end, NULL);
  1580. total_mhashes_done = 0;
  1581. pthread_mutex_unlock(&hash_lock);
  1582. /* main loop - simply wait for workio thread to exit */
  1583. pthread_join(thr_info[work_thr_id].pth, NULL);
  1584. curl_global_cleanup();
  1585. if (gpu_threads)
  1586. free(gpus);
  1587. if (opt_n_threads)
  1588. free(cpus);
  1589. applog(LOG_INFO, "workio thread dead, exiting.");
  1590. return 0;
  1591. }