main.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  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;
  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_fail = false;
  441. static bool submit_upstream_work(const struct work *work)
  442. {
  443. char *hexstr = NULL;
  444. json_t *val, *res;
  445. char s[345];
  446. bool rc = false;
  447. struct cgpu_info *cgpu = thr_info[work->thr_id].cgpu;
  448. CURL *curl = curl_easy_init();
  449. double utility, efficiency;
  450. if (unlikely(!curl)) {
  451. applog(LOG_ERR, "CURL initialisation failed");
  452. return rc;
  453. }
  454. /* build hex string */
  455. hexstr = bin2hex(work->data, sizeof(work->data));
  456. if (unlikely(!hexstr)) {
  457. applog(LOG_ERR, "submit_upstream_work OOM");
  458. goto out_nofree;
  459. }
  460. /* build JSON-RPC request */
  461. sprintf(s,
  462. "{\"method\": \"getwork\", \"params\": [ \"%s\" ], \"id\":1}\r\n",
  463. hexstr);
  464. if (opt_debug)
  465. applog(LOG_DEBUG, "DBG: sending RPC call: %s", s);
  466. /* issue JSON-RPC request */
  467. val = json_rpc_call(curl, rpc_url, rpc_userpass, s, false, false);
  468. if (unlikely(!val)) {
  469. applog(LOG_INFO, "submit_upstream_work json_rpc_call failed");
  470. if (!submit_fail) {
  471. submit_fail = true;
  472. applog(LOG_WARNING, "Upstream communication failure, caching submissions");
  473. }
  474. goto out;
  475. } else if (submit_fail) {
  476. submit_fail = false;
  477. applog(LOG_WARNING, "Upstream communication resumed, submitting work");
  478. }
  479. res = json_object_get(val, "result");
  480. /* Theoretically threads could race when modifying accepted and
  481. * rejected values but the chance of two submits completing at the
  482. * same time is zero so there is no point adding extra locking */
  483. if (json_is_true(res)) {
  484. cgpu->accepted++;
  485. accepted++;
  486. if (opt_debug)
  487. applog(LOG_DEBUG, "PROOF OF WORK RESULT: true (yay!!!)");
  488. if (!opt_quiet)
  489. printf("[Accepted] ");
  490. } else {
  491. cgpu->rejected++;
  492. rejected++;
  493. if (opt_debug)
  494. applog(LOG_DEBUG, "PROOF OF WORK RESULT: false (booooo)");
  495. if (!opt_quiet)
  496. printf("[Rejected] ");
  497. }
  498. utility = accepted / ( total_secs ? total_secs : 1 ) * 60;
  499. efficiency = cgpu->getworks ? cgpu->accepted * 100.0 / cgpu->getworks : 0.0;
  500. if (!opt_quiet) {
  501. printf("[%sPU %d] [%.1f Mh/s] [Q:%d A:%d R:%d HW:%d E:%.0f%% U:%.2f/m] \n",
  502. cgpu->is_gpu? "G" : "C", cgpu->cpu_gpu, cgpu->total_mhashes / total_secs,
  503. cgpu->getworks, cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  504. efficiency, utility);
  505. print_status();
  506. }
  507. applog(LOG_INFO, "%sPU %d Requested:%d Accepted:%d Rejected:%d HW errors:%d Efficiency:%.0f%% Utility:%.2f/m",
  508. cgpu->is_gpu? "G" : "C", cgpu->cpu_gpu, cgpu->getworks, cgpu->accepted, cgpu->rejected, cgpu->hw_errors, efficiency, utility
  509. );
  510. json_decref(val);
  511. rc = true;
  512. out:
  513. free(hexstr);
  514. out_nofree:
  515. curl_easy_cleanup(curl);
  516. return rc;
  517. }
  518. static const char *rpc_req =
  519. "{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
  520. static bool get_upstream_work(struct work *work)
  521. {
  522. json_t *val;
  523. bool rc = false;
  524. CURL *curl = curl_easy_init();
  525. if (unlikely(!curl)) {
  526. applog(LOG_ERR, "CURL initialisation failed");
  527. return rc;
  528. }
  529. val = json_rpc_call(curl, rpc_url, rpc_userpass, rpc_req,
  530. want_longpoll, false);
  531. if (unlikely(!val)) {
  532. applog(LOG_DEBUG, "Failed json_rpc_call in get_upstream_work");
  533. goto out;
  534. }
  535. rc = work_decode(json_object_get(val, "result"), work);
  536. json_decref(val);
  537. out:
  538. curl_easy_cleanup(curl);
  539. return rc;
  540. }
  541. static void workio_cmd_free(struct workio_cmd *wc)
  542. {
  543. if (!wc)
  544. return;
  545. switch (wc->cmd) {
  546. case WC_SUBMIT_WORK:
  547. free(wc->u.work);
  548. break;
  549. default: /* do nothing */
  550. break;
  551. }
  552. memset(wc, 0, sizeof(*wc)); /* poison */
  553. free(wc);
  554. }
  555. static void kill_work(void)
  556. {
  557. struct workio_cmd *wc;
  558. applog(LOG_INFO, "Received kill message");
  559. wc = calloc(1, sizeof(*wc));
  560. if (unlikely(!wc)) {
  561. applog(LOG_ERR, "Failed to calloc wc in kill_work");
  562. /* We're just trying to die anyway, so forget graceful */
  563. exit (1);
  564. }
  565. wc->cmd = WC_DIE;
  566. wc->thr = 0;
  567. if (unlikely(!tq_push(thr_info[work_thr_id].q, wc))) {
  568. applog(LOG_ERR, "Failed to tq_push work in kill_work");
  569. exit (1);
  570. }
  571. }
  572. static void *get_work_thread(void *userdata)
  573. {
  574. struct workio_cmd *wc = (struct workio_cmd *)userdata;
  575. struct work *ret_work;
  576. int failures = 0;
  577. pthread_detach(pthread_self());
  578. ret_work = calloc(1, sizeof(*ret_work));
  579. if (unlikely(!ret_work)) {
  580. applog(LOG_ERR, "Failed to calloc ret_work in workio_get_work");
  581. kill_work();
  582. goto out;
  583. }
  584. /* obtain new work from bitcoin via JSON-RPC */
  585. while (!get_upstream_work(ret_work)) {
  586. if (unlikely((opt_retries >= 0) && (++failures > opt_retries))) {
  587. applog(LOG_ERR, "json_rpc_call failed, terminating workio thread");
  588. free(ret_work);
  589. kill_work();
  590. goto out;
  591. }
  592. /* pause, then restart work-request loop */
  593. applog(LOG_DEBUG, "json_rpc_call failed on get work, retry after %d seconds",
  594. opt_fail_pause);
  595. sleep(opt_fail_pause);
  596. }
  597. /* send work to requesting thread */
  598. if (unlikely(!tq_push(thr_info[stage_thr_id].q, ret_work))) {
  599. applog(LOG_ERR, "Failed to tq_push work in workio_get_work");
  600. kill_work();
  601. free(ret_work);
  602. }
  603. out:
  604. workio_cmd_free(wc);
  605. return NULL;
  606. }
  607. static bool workio_get_work(struct workio_cmd *wc)
  608. {
  609. pthread_t get_thread;
  610. if (unlikely(pthread_create(&get_thread, NULL, get_work_thread, (void *)wc))) {
  611. applog(LOG_ERR, "Failed to create get_work_thread");
  612. return false;
  613. }
  614. return true;
  615. }
  616. static void *submit_work_thread(void *userdata)
  617. {
  618. struct workio_cmd *wc = (struct workio_cmd *)userdata;
  619. int failures = 0;
  620. char *hexstr;
  621. pthread_detach(pthread_self());
  622. hexstr = bin2hex(wc->u.work->data, 36);
  623. if (unlikely(!hexstr)) {
  624. applog(LOG_ERR, "submit_work_thread OOM");
  625. goto out;
  626. }
  627. if (unlikely(strncmp(hexstr, current_block, 36))) {
  628. applog(LOG_WARNING, "Stale work detected, discarding");
  629. goto out_free;
  630. }
  631. /* submit solution to bitcoin via JSON-RPC */
  632. while (!submit_upstream_work(wc->u.work)) {
  633. if (unlikely(strncmp(hexstr, current_block, 36))) {
  634. applog(LOG_WARNING, "Stale work detected, discarding");
  635. goto out_free;
  636. }
  637. if (unlikely((opt_retries >= 0) && (++failures > opt_retries))) {
  638. applog(LOG_ERR, "Failed %d retries ...terminating workio thread", opt_retries);
  639. kill_work();
  640. goto out_free;
  641. }
  642. /* pause, then restart work-request loop */
  643. applog(LOG_INFO, "json_rpc_call failed on submit_work, retry after %d seconds",
  644. opt_fail_pause);
  645. sleep(opt_fail_pause);
  646. }
  647. out_free:
  648. free(hexstr);
  649. out:
  650. workio_cmd_free(wc);
  651. return NULL;
  652. }
  653. static bool workio_submit_work(struct workio_cmd *wc)
  654. {
  655. pthread_t submit_thread;
  656. if (unlikely(pthread_create(&submit_thread, NULL, submit_work_thread, (void *)wc))) {
  657. applog(LOG_ERR, "Failed to create submit_work_thread");
  658. return false;
  659. }
  660. return true;
  661. }
  662. static void inc_staged(int inc, bool lp)
  663. {
  664. pthread_mutex_lock(&stgd_lock);
  665. total_staged += inc;
  666. if (lp)
  667. lp_staged += inc;
  668. pthread_mutex_unlock(&stgd_lock);
  669. }
  670. static void dec_staged(int inc)
  671. {
  672. pthread_mutex_lock(&stgd_lock);
  673. if (lp_staged)
  674. lp_staged -= inc;
  675. total_staged -= inc;
  676. pthread_mutex_unlock(&stgd_lock);
  677. }
  678. static int requests_staged(void)
  679. {
  680. int ret;
  681. pthread_mutex_lock(&stgd_lock);
  682. ret = total_staged;
  683. pthread_mutex_unlock(&stgd_lock);
  684. return ret;
  685. }
  686. static void *stage_thread(void *userdata)
  687. {
  688. struct thr_info *mythr = userdata;
  689. bool ok = true;
  690. unsigned int i;
  691. for (i = 0; i < 36; i++) {
  692. strcat(current_block, "0");
  693. strcat(blank, "0");
  694. }
  695. while (ok) {
  696. struct work *work = NULL;
  697. char *hexstr;
  698. work = tq_pop(mythr->q, NULL);
  699. if (unlikely(!work)) {
  700. applog(LOG_ERR, "Failed to tq_pop in stage_thread");
  701. ok = false;
  702. break;
  703. }
  704. hexstr = bin2hex(work->data, 36);
  705. if (unlikely(!hexstr)) {
  706. applog(LOG_ERR, "stage_thread OOM");
  707. break;
  708. }
  709. /* current_block is blanked out on successful longpoll */
  710. if (likely(strncmp(current_block, blank, 36))) {
  711. if (unlikely(strncmp(hexstr, current_block, 36))) {
  712. if (want_longpoll)
  713. applog(LOG_WARNING, "New block detected, possible missed longpoll, flushing work queue");
  714. else
  715. applog(LOG_WARNING, "New block detected, flushing work queue");
  716. /* As we can't flush the work from here, signal
  717. * the wakeup thread to restart all the
  718. * threads */
  719. work_restart[stage_thr_id].restart = 1;
  720. }
  721. } else
  722. memcpy(longpoll_block, hexstr, 36);
  723. memcpy(current_block, hexstr, 36);
  724. free(hexstr);
  725. if (unlikely(!tq_push(thr_info[0].q, work))) {
  726. applog(LOG_ERR, "Failed to tq_push work in stage_thread");
  727. ok = false;
  728. break;
  729. }
  730. inc_staged(1, false);
  731. }
  732. tq_freeze(mythr->q);
  733. return NULL;
  734. }
  735. static void *workio_thread(void *userdata)
  736. {
  737. struct thr_info *mythr = userdata;
  738. bool ok = true;
  739. while (ok) {
  740. struct workio_cmd *wc;
  741. /* wait for workio_cmd sent to us, on our queue */
  742. wc = tq_pop(mythr->q, NULL);
  743. if (unlikely(!wc)) {
  744. applog(LOG_ERR, "Failed to tq_pop in workio_thread");
  745. ok = false;
  746. break;
  747. }
  748. /* process workio_cmd */
  749. switch (wc->cmd) {
  750. case WC_GET_WORK:
  751. ok = workio_get_work(wc);
  752. break;
  753. case WC_SUBMIT_WORK:
  754. ok = workio_submit_work(wc);
  755. break;
  756. case WC_DIE:
  757. default:
  758. ok = false;
  759. break;
  760. }
  761. }
  762. tq_freeze(mythr->q);
  763. return NULL;
  764. }
  765. static void hashmeter(int thr_id, struct timeval *diff,
  766. unsigned long hashes_done)
  767. {
  768. struct timeval temp_tv_end, total_diff;
  769. double khashes, secs;
  770. double local_secs;
  771. double utility, efficiency = 0.0;
  772. static double local_mhashes_done = 0;
  773. static double rolling_local = 0;
  774. double local_mhashes = (double)hashes_done / 1000000.0;
  775. /* Don't bother calculating anything if we're not displaying it */
  776. if (opt_quiet || !opt_log_interval)
  777. return;
  778. khashes = hashes_done / 1000.0;
  779. secs = (double)diff->tv_sec + ((double)diff->tv_usec / 1000000.0);
  780. if (thr_id >= 0) {
  781. /* So we can call hashmeter from a non worker thread */
  782. struct cgpu_info *cgpu = thr_info[thr_id].cgpu;
  783. if (opt_debug)
  784. applog(LOG_DEBUG, "[thread %d: %lu hashes, %.0f khash/sec]",
  785. thr_id, hashes_done, hashes_done / secs);
  786. cgpu->local_mhashes += local_mhashes;
  787. cgpu->total_mhashes += local_mhashes;
  788. }
  789. /* Totals are updated by all threads so can race without locking */
  790. pthread_mutex_lock(&hash_lock);
  791. gettimeofday(&temp_tv_end, NULL);
  792. timeval_subtract(&total_diff, &temp_tv_end, &total_tv_end);
  793. local_secs = (double)total_diff.tv_sec + ((double)total_diff.tv_usec / 1000000.0);
  794. total_mhashes_done += local_mhashes;
  795. local_mhashes_done += local_mhashes;
  796. if (total_diff.tv_sec < opt_log_interval)
  797. /* Only update the total every opt_log_interval seconds */
  798. goto out_unlock;
  799. gettimeofday(&total_tv_end, NULL);
  800. /* Use a rolling average by faking an exponential decay over 5 * log */
  801. rolling_local = ((rolling_local * 0.9) + local_mhashes_done) / 1.9;
  802. timeval_subtract(&total_diff, &total_tv_end, &total_tv_start);
  803. total_secs = (double)total_diff.tv_sec +
  804. ((double)total_diff.tv_usec / 1000000.0);
  805. utility = accepted / ( total_secs ? total_secs : 1 ) * 60;
  806. efficiency = getwork_requested ? accepted * 100.0 / getwork_requested : 0.0;
  807. sprintf(statusline, "[(%ds):%.1f (avg):%.1f Mh/s] [Q:%d A:%d R:%d HW:%d E:%.0f%% U:%.2f/m] ",
  808. opt_log_interval, rolling_local / local_secs, total_mhashes_done / total_secs,
  809. getwork_requested, accepted, rejected, hw_errors, efficiency, utility);
  810. print_status();
  811. applog(LOG_INFO, "[Rate (%ds):%.1f (avg):%.2f Mhash/s] [Requested:%d Accepted:%d Rejected:%d HW errors:%d Efficiency:%.0f%% Utility:%.2f/m]",
  812. opt_log_interval, rolling_local / local_secs, total_mhashes_done / total_secs,
  813. getwork_requested, accepted, rejected, hw_errors, efficiency, utility);
  814. local_mhashes_done = 0;
  815. out_unlock:
  816. pthread_mutex_unlock(&hash_lock);
  817. }
  818. /* This is overkill, but at least we'll know accurately how much work is
  819. * queued to prevent ever being left without work */
  820. static void inc_queued(void)
  821. {
  822. pthread_mutex_lock(&qd_lock);
  823. total_queued++;
  824. pthread_mutex_unlock(&qd_lock);
  825. }
  826. static void dec_queued(void)
  827. {
  828. pthread_mutex_lock(&qd_lock);
  829. total_queued--;
  830. pthread_mutex_unlock(&qd_lock);
  831. dec_staged(1);
  832. }
  833. static int requests_queued(void)
  834. {
  835. int ret;
  836. pthread_mutex_lock(&qd_lock);
  837. ret = total_queued;
  838. pthread_mutex_unlock(&qd_lock);
  839. return ret;
  840. }
  841. /* All work is queued flagged as being for thread 0 and then the mining thread
  842. * flags it as its own */
  843. static bool queue_request(void)
  844. {
  845. struct thr_info *thr = &thr_info[0];
  846. struct workio_cmd *wc;
  847. /* fill out work request message */
  848. wc = calloc(1, sizeof(*wc));
  849. if (unlikely(!wc)) {
  850. applog(LOG_ERR, "Failed to tq_pop in queue_request");
  851. return false;
  852. }
  853. wc->cmd = WC_GET_WORK;
  854. wc->thr = thr;
  855. /* send work request to workio thread */
  856. if (unlikely(!tq_push(thr_info[work_thr_id].q, wc))) {
  857. applog(LOG_ERR, "Failed to tq_push in queue_request");
  858. workio_cmd_free(wc);
  859. return false;
  860. }
  861. inc_queued();
  862. return true;
  863. }
  864. static bool discard_request(void)
  865. {
  866. struct thr_info *thr = &thr_info[0];
  867. struct work *work_heap;
  868. /* Just in case we fell in a hole and missed a queue filling */
  869. if (unlikely(!requests_queued())) {
  870. applog(LOG_WARNING, "Tried to discard_request with nil queued");
  871. return true;
  872. }
  873. work_heap = tq_pop(thr->q, NULL);
  874. if (unlikely(!work_heap)) {
  875. applog(LOG_ERR, "Failed to tq_pop in discard_request");
  876. return false;
  877. }
  878. free(work_heap);
  879. dec_queued();
  880. return true;
  881. }
  882. static void flush_requests(bool longpoll)
  883. {
  884. int i, extra;
  885. extra = requests_queued();
  886. /* When flushing from longpoll, we don't know the new work yet. When
  887. * not flushing from longpoll, the first work item is valid so do not
  888. * discard it */
  889. if (longpoll)
  890. memcpy(current_block, blank, 36);
  891. else
  892. extra--;
  893. /* Temporarily increase the staged count so that get_work thinks there
  894. * is work available instead of making threads reuse existing work */
  895. inc_staged(extra, true);
  896. for (i = 0; i < extra; i++) {
  897. /* Queue a whole batch of new requests */
  898. if (unlikely(!queue_request())) {
  899. applog(LOG_ERR, "Failed to queue requests in flush_requests");
  900. kill_work();
  901. break;
  902. }
  903. /* Pop off the old requests. Cancelling the requests would be better
  904. * but is tricky */
  905. if (unlikely(!discard_request())) {
  906. applog(LOG_ERR, "Failed to discard requests in flush_requests");
  907. kill_work();
  908. break;
  909. }
  910. }
  911. }
  912. static bool get_work(struct work *work, bool queued)
  913. {
  914. struct thr_info *thr = &thr_info[0];
  915. struct work *work_heap;
  916. bool ret = false;
  917. int failures = 0;
  918. getwork_requested++;
  919. retry:
  920. if (unlikely(!queued && !queue_request())) {
  921. applog(LOG_WARNING, "Failed to queue_request in get_work");
  922. goto out;
  923. }
  924. if (!requests_staged()) {
  925. uint32_t *work_ntime;
  926. uint32_t ntime;
  927. /* Only print this message once each time we shift to localgen */
  928. if (!localgen)
  929. applog(LOG_WARNING, "Server not providing work fast enough, generating work locally");
  930. localgen = true;
  931. work_ntime = (uint32_t *)(work->data + 68);
  932. ntime = be32toh(*work_ntime);
  933. ntime++;
  934. *work_ntime = htobe32(ntime);
  935. ret = true;
  936. goto out;
  937. } else if (localgen) {
  938. localgen = false;
  939. applog(LOG_WARNING, "Resumed retrieving work from server");
  940. }
  941. /* wait for 1st response, or get cached response */
  942. work_heap = tq_pop(thr->q, NULL);
  943. if (unlikely(!work_heap)) {
  944. applog(LOG_WARNING, "Failed to tq_pop in get_work");
  945. goto out;
  946. }
  947. dec_queued();
  948. memcpy(work, work_heap, sizeof(*work));
  949. ret = true;
  950. free(work_heap);
  951. out:
  952. if (unlikely(ret == false)) {
  953. if ((opt_retries >= 0) && (++failures > opt_retries)) {
  954. applog(LOG_ERR, "Failed %d times to get_work");
  955. return ret;
  956. }
  957. applog(LOG_DEBUG, "Retrying after %d seconds", opt_fail_pause);
  958. sleep(opt_fail_pause);
  959. goto retry;
  960. }
  961. return ret;
  962. }
  963. static bool submit_work_sync(struct thr_info *thr, const struct work *work_in)
  964. {
  965. struct workio_cmd *wc;
  966. /* fill out work request message */
  967. wc = calloc(1, sizeof(*wc));
  968. if (unlikely(!wc)) {
  969. applog(LOG_ERR, "Failed to calloc wc in submit_work_sync");
  970. return false;
  971. }
  972. wc->u.work = malloc(sizeof(*work_in));
  973. if (unlikely(!wc->u.work)) {
  974. applog(LOG_ERR, "Failed to calloc work in submit_work_sync");
  975. goto err_out;
  976. }
  977. wc->cmd = WC_SUBMIT_WORK;
  978. wc->thr = thr;
  979. memcpy(wc->u.work, work_in, sizeof(*work_in));
  980. /* send solution to workio thread */
  981. if (unlikely(!tq_push(thr_info[work_thr_id].q, wc))) {
  982. applog(LOG_ERR, "Failed to tq_push work in submit_work_sync");
  983. goto err_out;
  984. }
  985. return true;
  986. err_out:
  987. workio_cmd_free(wc);
  988. return false;
  989. }
  990. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
  991. {
  992. work->data[64+12+0] = (nonce>>0) & 0xff;
  993. work->data[64+12+1] = (nonce>>8) & 0xff;
  994. work->data[64+12+2] = (nonce>>16) & 0xff;
  995. work->data[64+12+3] = (nonce>>24) & 0xff;
  996. return submit_work_sync(thr, work);
  997. }
  998. static inline int cpu_from_thr_id(int thr_id)
  999. {
  1000. return (thr_id - gpu_threads) % num_processors;
  1001. }
  1002. static void *miner_thread(void *userdata)
  1003. {
  1004. struct thr_info *mythr = userdata;
  1005. const int thr_id = mythr->id;
  1006. uint32_t max_nonce = 0xffffff;
  1007. bool needs_work = true;
  1008. /* Try to cycle approximately 5 times before each log update */
  1009. const unsigned long cycle = opt_log_interval / 5 ? : 1;
  1010. /* Request the next work item at 2/3 of the scantime */
  1011. unsigned const int request_interval = opt_scantime * 2 / 3 ? : 1;
  1012. unsigned const long request_nonce = MAXTHREADS / 3 * 2;
  1013. bool requested = true;
  1014. /* Set worker threads to nice 19 and then preferentially to SCHED_IDLE
  1015. * and if that fails, then SCHED_BATCH. No need for this to be an
  1016. * error if it fails */
  1017. setpriority(PRIO_PROCESS, 0, 19);
  1018. drop_policy();
  1019. /* Cpu affinity only makes sense if the number of threads is a multiple
  1020. * of the number of CPUs */
  1021. if (!(opt_n_threads % num_processors))
  1022. affine_to_cpu(thr_id - gpu_threads, cpu_from_thr_id(thr_id));
  1023. while (1) {
  1024. struct work work __attribute__((aligned(128)));
  1025. unsigned long hashes_done;
  1026. struct timeval tv_workstart, tv_start, tv_end, diff;
  1027. uint64_t max64;
  1028. bool rc;
  1029. if (needs_work) {
  1030. gettimeofday(&tv_workstart, NULL);
  1031. /* obtain new work from internal workio thread */
  1032. if (unlikely(!get_work(&work, requested))) {
  1033. applog(LOG_ERR, "work retrieval failed, exiting "
  1034. "mining thread %d", thr_id);
  1035. goto out;
  1036. }
  1037. mythr->cgpu->getworks++;
  1038. work.thr_id = thr_id;
  1039. needs_work = requested = false;
  1040. work.blk.nonce = 0;
  1041. }
  1042. hashes_done = 0;
  1043. gettimeofday(&tv_start, NULL);
  1044. /* scan nonces for a proof-of-work hash */
  1045. switch (opt_algo) {
  1046. case ALGO_C:
  1047. rc = scanhash_c(thr_id, work.midstate, work.data + 64,
  1048. work.hash1, work.hash, work.target,
  1049. max_nonce, &hashes_done,
  1050. work.blk.nonce);
  1051. break;
  1052. #ifdef WANT_X8664_SSE2
  1053. case ALGO_SSE2_64: {
  1054. unsigned int rc5 =
  1055. scanhash_sse2_64(thr_id, work.midstate, work.data + 64,
  1056. work.hash1, work.hash,
  1057. work.target,
  1058. max_nonce, &hashes_done,
  1059. work.blk.nonce);
  1060. rc = (rc5 == -1) ? false : true;
  1061. }
  1062. break;
  1063. #endif
  1064. #ifdef WANT_SSE2_4WAY
  1065. case ALGO_4WAY: {
  1066. unsigned int rc4 =
  1067. ScanHash_4WaySSE2(thr_id, work.midstate, work.data + 64,
  1068. work.hash1, work.hash,
  1069. work.target,
  1070. max_nonce, &hashes_done,
  1071. work.blk.nonce);
  1072. rc = (rc4 == -1) ? false : true;
  1073. }
  1074. break;
  1075. #endif
  1076. #ifdef WANT_VIA_PADLOCK
  1077. case ALGO_VIA:
  1078. rc = scanhash_via(thr_id, work.data, work.target,
  1079. max_nonce, &hashes_done,
  1080. work.blk.nonce);
  1081. break;
  1082. #endif
  1083. case ALGO_CRYPTOPP:
  1084. rc = scanhash_cryptopp(thr_id, work.midstate, work.data + 64,
  1085. work.hash1, work.hash, work.target,
  1086. max_nonce, &hashes_done,
  1087. work.blk.nonce);
  1088. break;
  1089. #ifdef WANT_CRYPTOPP_ASM32
  1090. case ALGO_CRYPTOPP_ASM32:
  1091. rc = scanhash_asm32(thr_id, work.midstate, work.data + 64,
  1092. work.hash1, work.hash, work.target,
  1093. max_nonce, &hashes_done,
  1094. work.blk.nonce);
  1095. break;
  1096. #endif
  1097. default:
  1098. /* should never happen */
  1099. goto out;
  1100. }
  1101. /* record scanhash elapsed time */
  1102. gettimeofday(&tv_end, NULL);
  1103. timeval_subtract(&diff, &tv_end, &tv_start);
  1104. hashes_done -= work.blk.nonce;
  1105. hashmeter(thr_id, &diff, hashes_done);
  1106. work.blk.nonce += hashes_done;
  1107. /* adjust max_nonce to meet target cycle time */
  1108. if (diff.tv_usec > 500000)
  1109. diff.tv_sec++;
  1110. if (diff.tv_sec && diff.tv_sec != cycle) {
  1111. max64 = work.blk.nonce +
  1112. ((uint64_t)hashes_done * cycle) / diff.tv_sec;
  1113. } else
  1114. max64 = work.blk.nonce + hashes_done;
  1115. if (max64 > 0xfffffffaULL)
  1116. max64 = 0xfffffffaULL;
  1117. max_nonce = max64;
  1118. /* if nonce found, submit work */
  1119. if (unlikely(rc)) {
  1120. if (opt_debug)
  1121. applog(LOG_DEBUG, "CPU %d found something?", cpu_from_thr_id(thr_id));
  1122. if (unlikely(!submit_work_sync(mythr, &work))) {
  1123. applog(LOG_ERR, "Failed to submit_work_sync in miner_thread %d", thr_id);
  1124. break;
  1125. }
  1126. work.blk.nonce += 4;
  1127. }
  1128. timeval_subtract(&diff, &tv_end, &tv_workstart);
  1129. if (!requested && (diff.tv_sec > request_interval || work.blk.nonce > request_nonce)) {
  1130. if (unlikely(!queue_request())) {
  1131. applog(LOG_ERR, "Failed to queue_request in miner_thread %d", thr_id);
  1132. goto out;
  1133. }
  1134. requested = true;
  1135. }
  1136. if (diff.tv_sec > opt_scantime || work_restart[thr_id].restart ||
  1137. work.blk.nonce >= MAXTHREADS - hashes_done)
  1138. needs_work = true;
  1139. }
  1140. out:
  1141. tq_freeze(mythr->q);
  1142. return NULL;
  1143. }
  1144. enum {
  1145. STAT_SLEEP_INTERVAL = 1,
  1146. STAT_CTR_INTERVAL = 10000000,
  1147. FAILURE_INTERVAL = 30,
  1148. };
  1149. #ifdef HAVE_OPENCL
  1150. static _clState *clStates[16];
  1151. static inline cl_int queue_kernel_parameters(_clState *clState, dev_blk_ctx *blk)
  1152. {
  1153. cl_kernel *kernel = &clState->kernel;
  1154. cl_int status = 0;
  1155. int num = 0;
  1156. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_a);
  1157. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_b);
  1158. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_c);
  1159. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_d);
  1160. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_e);
  1161. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_f);
  1162. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_g);
  1163. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_h);
  1164. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_b);
  1165. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_c);
  1166. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_d);
  1167. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_f);
  1168. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_g);
  1169. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_h);
  1170. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->nonce);
  1171. if (clState->hasBitAlign == true) {
  1172. /* Parameters for phatk kernel */
  1173. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->W2);
  1174. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->W16);
  1175. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->W17);
  1176. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->PreVal4);
  1177. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->T1);
  1178. } else {
  1179. /* Parameters for poclbm kernel */
  1180. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW0);
  1181. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW1);
  1182. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW2);
  1183. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW3);
  1184. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW15);
  1185. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW01r);
  1186. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e);
  1187. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e2);
  1188. }
  1189. status |= clSetKernelArg(*kernel, num++, sizeof(clState->outputBuffer),
  1190. (void *)&clState->outputBuffer);
  1191. return status;
  1192. }
  1193. static inline int gpu_from_thr_id(int thr_id)
  1194. {
  1195. return thr_id % nDevs;
  1196. }
  1197. static void *gpuminer_thread(void *userdata)
  1198. {
  1199. const unsigned long cycle = opt_log_interval / 5 ? : 1;
  1200. struct timeval tv_start, tv_end, diff;
  1201. struct thr_info *mythr = userdata;
  1202. const int thr_id = mythr->id;
  1203. uint32_t *res, *blank_res;
  1204. size_t globalThreads[1];
  1205. size_t localThreads[1];
  1206. cl_int status;
  1207. _clState *clState = clStates[thr_id];
  1208. const cl_kernel *kernel = &clState->kernel;
  1209. struct work *work = malloc(sizeof(struct work));
  1210. unsigned const int threads = 1 << (15 + scan_intensity);
  1211. unsigned const int vectors = clState->preferred_vwidth;
  1212. unsigned const int hashes = threads * vectors;
  1213. unsigned int hashes_done = 0;
  1214. /* Request the next work item at 2/3 of the scantime */
  1215. unsigned const int request_interval = opt_scantime * 2 / 3 ? : 1;
  1216. unsigned const long request_nonce = MAXTHREADS / 3 * 2;
  1217. bool requested = true;
  1218. res = calloc(BUFFERSIZE, 1);
  1219. blank_res = calloc(BUFFERSIZE, 1);
  1220. if (!res || !blank_res) {
  1221. applog(LOG_ERR, "Failed to calloc in gpuminer_thread");
  1222. goto out;
  1223. }
  1224. gettimeofday(&tv_start, NULL);
  1225. globalThreads[0] = threads;
  1226. localThreads[0] = clState->work_size;
  1227. diff.tv_sec = ~0UL;
  1228. gettimeofday(&tv_end, NULL);
  1229. while (1) {
  1230. struct timeval tv_workstart;
  1231. /* This finish flushes the readbuffer set with CL_FALSE later */
  1232. clFinish(clState->commandQueue);
  1233. if (diff.tv_sec > opt_scantime || work->blk.nonce >= MAXTHREADS - hashes || work_restart[thr_id].restart) {
  1234. /* Ignore any reads since we're getting new work and queue a clean buffer */
  1235. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1236. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1237. if (unlikely(status != CL_SUCCESS))
  1238. { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
  1239. memset(res, 0, BUFFERSIZE);
  1240. gettimeofday(&tv_workstart, NULL);
  1241. /* obtain new work from internal workio thread */
  1242. if (unlikely(!get_work(work, requested))) {
  1243. applog(LOG_ERR, "work retrieval failed, exiting "
  1244. "gpu mining thread %d", mythr->id);
  1245. goto out;
  1246. }
  1247. mythr->cgpu->getworks++;
  1248. work->thr_id = thr_id;
  1249. requested = false;
  1250. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1251. work->blk.nonce = 0;
  1252. work_restart[thr_id].restart = 0;
  1253. if (opt_debug)
  1254. applog(LOG_DEBUG, "getwork thread %d", thr_id);
  1255. /* Flushes the writebuffer set with CL_FALSE above */
  1256. clFinish(clState->commandQueue);
  1257. }
  1258. status = queue_kernel_parameters(clState, &work->blk);
  1259. if (unlikely(status != CL_SUCCESS))
  1260. { applog(LOG_ERR, "Error: clSetKernelArg of all params failed."); goto out; }
  1261. /* MAXBUFFERS entry is used as a flag to say nonces exist */
  1262. if (res[MAXBUFFERS]) {
  1263. /* Clear the buffer again */
  1264. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1265. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1266. if (unlikely(status != CL_SUCCESS))
  1267. { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
  1268. if (opt_debug)
  1269. applog(LOG_DEBUG, "GPU %d found something?", gpu_from_thr_id(thr_id));
  1270. postcalc_hash_async(mythr, work, res);
  1271. memset(res, 0, BUFFERSIZE);
  1272. clFinish(clState->commandQueue);
  1273. }
  1274. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1275. globalThreads, localThreads, 0, NULL, NULL);
  1276. if (unlikely(status != CL_SUCCESS))
  1277. { applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)"); goto out; }
  1278. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1279. BUFFERSIZE, res, 0, NULL, NULL);
  1280. if (unlikely(status != CL_SUCCESS))
  1281. { applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)"); goto out;}
  1282. gettimeofday(&tv_end, NULL);
  1283. timeval_subtract(&diff, &tv_end, &tv_start);
  1284. hashes_done += hashes;
  1285. work->blk.nonce += hashes;
  1286. if (diff.tv_usec > 500000)
  1287. diff.tv_sec++;
  1288. if (diff.tv_sec >= cycle) {
  1289. hashmeter(thr_id, &diff, hashes_done);
  1290. gettimeofday(&tv_start, NULL);
  1291. hashes_done = 0;
  1292. }
  1293. timeval_subtract(&diff, &tv_end, &tv_workstart);
  1294. if (!requested && (diff.tv_sec > request_interval || work->blk.nonce > request_nonce)) {
  1295. if (unlikely(!queue_request())) {
  1296. applog(LOG_ERR, "Failed to queue_request in gpuminer_thread %d", thr_id);
  1297. goto out;
  1298. }
  1299. requested = true;
  1300. }
  1301. }
  1302. out:
  1303. tq_freeze(mythr->q);
  1304. return NULL;
  1305. }
  1306. #endif /* HAVE_OPENCL */
  1307. static void restart_threads(bool longpoll)
  1308. {
  1309. int i;
  1310. /* Discard old queued requests and get new ones */
  1311. flush_requests(longpoll);
  1312. for (i = 0; i < opt_n_threads + gpu_threads; i++)
  1313. work_restart[i].restart = 1;
  1314. }
  1315. static void *longpoll_thread(void *userdata)
  1316. {
  1317. struct thr_info *mythr = userdata;
  1318. CURL *curl = NULL;
  1319. char *copy_start, *hdr_path, *lp_url = NULL;
  1320. bool need_slash = false;
  1321. int failures = 0;
  1322. unsigned int i;
  1323. hdr_path = tq_pop(mythr->q, NULL);
  1324. if (!hdr_path)
  1325. goto out;
  1326. /* full URL */
  1327. if (strstr(hdr_path, "://")) {
  1328. lp_url = hdr_path;
  1329. hdr_path = NULL;
  1330. }
  1331. /* absolute path, on current server */
  1332. else {
  1333. copy_start = (*hdr_path == '/') ? (hdr_path + 1) : hdr_path;
  1334. if (rpc_url[strlen(rpc_url) - 1] != '/')
  1335. need_slash = true;
  1336. lp_url = malloc(strlen(rpc_url) + strlen(copy_start) + 2);
  1337. if (!lp_url)
  1338. goto out;
  1339. sprintf(lp_url, "%s%s%s", rpc_url, need_slash ? "/" : "", copy_start);
  1340. }
  1341. applog(LOG_INFO, "Long-polling activated for %s", lp_url);
  1342. curl = curl_easy_init();
  1343. if (unlikely(!curl)) {
  1344. applog(LOG_ERR, "CURL initialisation failed");
  1345. goto out;
  1346. }
  1347. for (i = 0; i < 36; i++)
  1348. strcat(longpoll_block, "0");
  1349. while (1) {
  1350. json_t *val;
  1351. val = json_rpc_call(curl, lp_url, rpc_userpass, rpc_req,
  1352. false, true);
  1353. if (likely(val)) {
  1354. failures = 0;
  1355. json_decref(val);
  1356. /* Keep track of who ordered a restart_threads to make
  1357. * sure it's only done once per new block */
  1358. if (likely(!strncmp(longpoll_block, blank, 36) ||
  1359. !strncmp(longpoll_block, current_block, 36))) {
  1360. applog(LOG_WARNING, "LONGPOLL detected new block, flushing work queue");
  1361. restart_threads(true);
  1362. } else
  1363. applog(LOG_WARNING, "LONGPOLL received - new block detected and work flushed already");
  1364. } else {
  1365. if (failures++ < 10) {
  1366. sleep(30);
  1367. applog(LOG_WARNING,
  1368. "longpoll failed, sleeping for 30s");
  1369. } else {
  1370. applog(LOG_ERR,
  1371. "longpoll failed, ending thread");
  1372. goto out;
  1373. }
  1374. }
  1375. memcpy(longpoll_block, current_block, 36);
  1376. }
  1377. out:
  1378. free(hdr_path);
  1379. free(lp_url);
  1380. tq_freeze(mythr->q);
  1381. if (curl)
  1382. curl_easy_cleanup(curl);
  1383. return NULL;
  1384. }
  1385. /* Makes sure the hashmeter keeps going even if mining threads stall */
  1386. static void *wakeup_thread(void *userdata)
  1387. {
  1388. const unsigned int interval = opt_log_interval / 2 ? : 1;
  1389. struct timeval zero_tv;
  1390. memset(&zero_tv, 0, sizeof(struct timeval));
  1391. while (1) {
  1392. sleep(interval);
  1393. if (requests_queued() < opt_queue)
  1394. queue_request();
  1395. hashmeter(-1, &zero_tv, 0);
  1396. if (unlikely(work_restart[stage_thr_id].restart)) {
  1397. restart_threads(false);
  1398. work_restart[stage_thr_id].restart = 0;
  1399. }
  1400. }
  1401. return NULL;
  1402. }
  1403. int main (int argc, char *argv[])
  1404. {
  1405. struct thr_info *thr;
  1406. unsigned int i;
  1407. char name[32];
  1408. struct cgpu_info *gpus = NULL, *cpus = NULL;
  1409. if (unlikely(pthread_mutex_init(&time_lock, NULL)))
  1410. return 1;
  1411. if (unlikely(pthread_mutex_init(&hash_lock, NULL)))
  1412. return 1;
  1413. if (unlikely(pthread_mutex_init(&qd_lock, NULL)))
  1414. return 1;
  1415. if (unlikely(pthread_mutex_init(&stgd_lock, NULL)))
  1416. return 1;
  1417. #ifdef WIN32
  1418. opt_n_threads = num_processors = 1;
  1419. #else
  1420. num_processors = sysconf(_SC_NPROCESSORS_ONLN);
  1421. opt_n_threads = num_processors;
  1422. #endif /* !WIN32 */
  1423. #ifdef HAVE_OPENCL
  1424. nDevs = clDevicesNum();
  1425. if (nDevs < 0)
  1426. return 1;
  1427. #endif
  1428. if (nDevs)
  1429. opt_n_threads = 0;
  1430. rpc_url = strdup(DEF_RPC_URL);
  1431. /* parse command line */
  1432. opt_register_table(opt_config_table,
  1433. "Options for both config file and command line");
  1434. opt_register_table(opt_cmdline_table,
  1435. "Options for command line only");
  1436. opt_parse(&argc, argv, applog_and_exit);
  1437. if (argc != 1) {
  1438. applog(LOG_ERR, "Unexpected extra commandline arguments");
  1439. return 1;
  1440. }
  1441. gpu_threads = nDevs * opt_g_threads;
  1442. if (!gpu_threads && !forced_n_threads) {
  1443. /* Maybe they turned GPU off; restore default CPU threads. */
  1444. opt_n_threads = num_processors;
  1445. }
  1446. if (!rpc_userpass) {
  1447. if (!rpc_user || !rpc_pass) {
  1448. applog(LOG_ERR, "No login credentials supplied");
  1449. return 1;
  1450. }
  1451. rpc_userpass = malloc(strlen(rpc_user) + strlen(rpc_pass) + 2);
  1452. if (!rpc_userpass)
  1453. return 1;
  1454. sprintf(rpc_userpass, "%s:%s", rpc_user, rpc_pass);
  1455. }
  1456. if (unlikely(curl_global_init(CURL_GLOBAL_ALL)))
  1457. return 1;
  1458. #ifdef HAVE_SYSLOG_H
  1459. if (use_syslog)
  1460. openlog("cpuminer", LOG_PID, LOG_USER);
  1461. #endif
  1462. work_restart = calloc(opt_n_threads + 4 + gpu_threads, sizeof(*work_restart));
  1463. if (!work_restart)
  1464. return 1;
  1465. thr_info = calloc(opt_n_threads + 4 + gpu_threads, sizeof(*thr));
  1466. if (!thr_info)
  1467. return 1;
  1468. /* init workio thread info */
  1469. work_thr_id = opt_n_threads + gpu_threads;
  1470. thr = &thr_info[work_thr_id];
  1471. thr->id = work_thr_id;
  1472. thr->q = tq_new();
  1473. if (!thr->q)
  1474. return 1;
  1475. /* start work I/O thread */
  1476. if (pthread_create(&thr->pth, NULL, workio_thread, thr)) {
  1477. applog(LOG_ERR, "workio thread create failed");
  1478. return 1;
  1479. }
  1480. /* init longpoll thread info */
  1481. if (want_longpoll) {
  1482. longpoll_thr_id = opt_n_threads + gpu_threads + 1;
  1483. thr = &thr_info[longpoll_thr_id];
  1484. thr->id = longpoll_thr_id;
  1485. thr->q = tq_new();
  1486. if (!thr->q)
  1487. return 1;
  1488. /* start longpoll thread */
  1489. if (unlikely(pthread_create(&thr->pth, NULL, longpoll_thread, thr))) {
  1490. applog(LOG_ERR, "longpoll thread create failed");
  1491. return 1;
  1492. }
  1493. pthread_detach(thr->pth);
  1494. } else
  1495. longpoll_thr_id = -1;
  1496. gettimeofday(&total_tv_start, NULL);
  1497. gettimeofday(&total_tv_end, NULL);
  1498. if (opt_n_threads ) {
  1499. cpus = calloc(num_processors, sizeof(struct cgpu_info));
  1500. if (unlikely(!cpus)) {
  1501. applog(LOG_ERR, "Failed to calloc cpus");
  1502. return 1;
  1503. }
  1504. }
  1505. if (gpu_threads) {
  1506. gpus = calloc(nDevs, sizeof(struct cgpu_info));
  1507. if (unlikely(!gpus)) {
  1508. applog(LOG_ERR, "Failed to calloc gpus");
  1509. return 1;
  1510. }
  1511. }
  1512. stage_thr_id = opt_n_threads + gpu_threads + 3;
  1513. thr = &thr_info[stage_thr_id];
  1514. thr->q = tq_new();
  1515. if (!thr->q)
  1516. return 1;
  1517. /* start stage thread */
  1518. if (pthread_create(&thr->pth, NULL, stage_thread, thr)) {
  1519. applog(LOG_ERR, "stage thread create failed");
  1520. return 1;
  1521. }
  1522. /* Put enough work in the queue */
  1523. for (i = 0; i < opt_queue + opt_n_threads + gpu_threads; i++) {
  1524. if (unlikely(!queue_request())) {
  1525. applog(LOG_ERR, "Failed to queue_request in main");
  1526. return 1;
  1527. }
  1528. inc_staged(1, true);
  1529. }
  1530. #ifdef HAVE_OPENCL
  1531. /* start GPU mining threads */
  1532. for (i = 0; i < gpu_threads; i++) {
  1533. int gpu = gpu_from_thr_id(i);
  1534. thr = &thr_info[i];
  1535. thr->id = i;
  1536. gpus[gpu].is_gpu = 1;
  1537. gpus[gpu].cpu_gpu = gpu;
  1538. thr->cgpu = &gpus[gpu];
  1539. thr->q = tq_new();
  1540. if (!thr->q) {
  1541. applog(LOG_ERR, "tq_new failed in starting gpu mining threads");
  1542. return 1;
  1543. }
  1544. applog(LOG_INFO, "Init GPU thread %i", i);
  1545. clStates[i] = initCl(gpu, name, sizeof(name));
  1546. if (!clStates[i]) {
  1547. applog(LOG_ERR, "Failed to init GPU thread %d", i);
  1548. continue;
  1549. }
  1550. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1551. if (unlikely(pthread_create(&thr->pth, NULL, gpuminer_thread, thr))) {
  1552. applog(LOG_ERR, "thread %d create failed", i);
  1553. return 1;
  1554. }
  1555. pthread_detach(thr->pth);
  1556. }
  1557. applog(LOG_INFO, "%d gpu miner threads started", i);
  1558. #endif
  1559. /* start CPU mining threads */
  1560. for (i = gpu_threads; i < gpu_threads + opt_n_threads; i++) {
  1561. int cpu = cpu_from_thr_id(i);
  1562. thr = &thr_info[i];
  1563. thr->id = i;
  1564. cpus[cpu].cpu_gpu = cpu;
  1565. thr->cgpu = &cpus[cpu];
  1566. thr->q = tq_new();
  1567. if (!thr->q) {
  1568. applog(LOG_ERR, "tq_new failed in starting cpu mining threads");
  1569. return 1;
  1570. }
  1571. if (unlikely(pthread_create(&thr->pth, NULL, miner_thread, thr))) {
  1572. applog(LOG_ERR, "thread %d create failed", i);
  1573. return 1;
  1574. }
  1575. pthread_detach(thr->pth);
  1576. }
  1577. applog(LOG_INFO, "%d cpu miner threads started, "
  1578. "using SHA256 '%s' algorithm.",
  1579. opt_n_threads,
  1580. algo_names[opt_algo]);
  1581. thr = &thr_info[opt_n_threads + gpu_threads + 2];
  1582. /* start wakeup thread */
  1583. if (pthread_create(&thr->pth, NULL, wakeup_thread, NULL)) {
  1584. applog(LOG_ERR, "wakeup thread create failed");
  1585. return 1;
  1586. }
  1587. /* Restart count as it will be wrong till all threads are started */
  1588. pthread_mutex_lock(&hash_lock);
  1589. gettimeofday(&total_tv_start, NULL);
  1590. gettimeofday(&total_tv_end, NULL);
  1591. total_mhashes_done = 0;
  1592. pthread_mutex_unlock(&hash_lock);
  1593. /* main loop - simply wait for workio thread to exit */
  1594. pthread_join(thr_info[work_thr_id].pth, NULL);
  1595. curl_global_cleanup();
  1596. if (gpu_threads)
  1597. free(gpus);
  1598. if (opt_n_threads)
  1599. free(cpus);
  1600. applog(LOG_INFO, "workio thread dead, exiting.");
  1601. return 0;
  1602. }