main.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  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_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 = getwork_requested ? cgpu->accepted * 100.0 / getwork_requested : 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. getwork_requested, 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, getwork_requested, 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. retry:
  919. if (unlikely(!queued && !queue_request())) {
  920. applog(LOG_WARNING, "Failed to queue_request in get_work");
  921. goto out;
  922. }
  923. if (!requests_staged()) {
  924. uint32_t *work_ntime;
  925. uint32_t ntime;
  926. /* Only print this message once each time we shift to localgen */
  927. if (!localgen)
  928. applog(LOG_WARNING, "Server not providing work fast enough, generating work locally");
  929. localgen = true;
  930. work_ntime = (uint32_t *)(work->data + 68);
  931. ntime = be32toh(*work_ntime);
  932. ntime++;
  933. *work_ntime = htobe32(ntime);
  934. ret = true;
  935. goto out;
  936. } else if (localgen) {
  937. localgen = false;
  938. applog(LOG_WARNING, "Resumed retrieving work from server");
  939. }
  940. /* wait for 1st response, or get cached response */
  941. work_heap = tq_pop(thr->q, NULL);
  942. if (unlikely(!work_heap)) {
  943. applog(LOG_WARNING, "Failed to tq_pop in get_work");
  944. goto out;
  945. }
  946. dec_queued();
  947. memcpy(work, work_heap, sizeof(*work));
  948. ret = true;
  949. free(work_heap);
  950. out:
  951. if (unlikely(ret == false)) {
  952. if ((opt_retries >= 0) && (++failures > opt_retries)) {
  953. applog(LOG_ERR, "Failed %d times to get_work");
  954. return ret;
  955. }
  956. applog(LOG_DEBUG, "Retrying after %d seconds", opt_fail_pause);
  957. sleep(opt_fail_pause);
  958. goto retry;
  959. }
  960. return ret;
  961. }
  962. static bool submit_work_sync(struct thr_info *thr, const struct work *work_in)
  963. {
  964. struct workio_cmd *wc;
  965. /* fill out work request message */
  966. wc = calloc(1, sizeof(*wc));
  967. if (unlikely(!wc)) {
  968. applog(LOG_ERR, "Failed to calloc wc in submit_work_sync");
  969. return false;
  970. }
  971. wc->u.work = malloc(sizeof(*work_in));
  972. if (unlikely(!wc->u.work)) {
  973. applog(LOG_ERR, "Failed to calloc work in submit_work_sync");
  974. goto err_out;
  975. }
  976. wc->cmd = WC_SUBMIT_WORK;
  977. wc->thr = thr;
  978. memcpy(wc->u.work, work_in, sizeof(*work_in));
  979. /* send solution to workio thread */
  980. if (unlikely(!tq_push(thr_info[work_thr_id].q, wc))) {
  981. applog(LOG_ERR, "Failed to tq_push work in submit_work_sync");
  982. goto err_out;
  983. }
  984. return true;
  985. err_out:
  986. workio_cmd_free(wc);
  987. return false;
  988. }
  989. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
  990. {
  991. work->data[64+12+0] = (nonce>>0) & 0xff;
  992. work->data[64+12+1] = (nonce>>8) & 0xff;
  993. work->data[64+12+2] = (nonce>>16) & 0xff;
  994. work->data[64+12+3] = (nonce>>24) & 0xff;
  995. return submit_work_sync(thr, work);
  996. }
  997. static inline int cpu_from_thr_id(int thr_id)
  998. {
  999. return (thr_id - gpu_threads) % num_processors;
  1000. }
  1001. static void *miner_thread(void *userdata)
  1002. {
  1003. struct thr_info *mythr = userdata;
  1004. const int thr_id = mythr->id;
  1005. uint32_t max_nonce = 0xffffff;
  1006. bool needs_work = true;
  1007. /* Try to cycle approximately 5 times before each log update */
  1008. const unsigned long cycle = opt_log_interval / 5 ? : 1;
  1009. /* Request the next work item at 2/3 of the scantime */
  1010. unsigned const int request_interval = opt_scantime * 2 / 3 ? : 1;
  1011. unsigned const long request_nonce = MAXTHREADS / 3 * 2;
  1012. bool requested = true;
  1013. /* Set worker threads to nice 19 and then preferentially to SCHED_IDLE
  1014. * and if that fails, then SCHED_BATCH. No need for this to be an
  1015. * error if it fails */
  1016. setpriority(PRIO_PROCESS, 0, 19);
  1017. drop_policy();
  1018. /* Cpu affinity only makes sense if the number of threads is a multiple
  1019. * of the number of CPUs */
  1020. if (!(opt_n_threads % num_processors))
  1021. affine_to_cpu(thr_id - gpu_threads, cpu_from_thr_id(thr_id));
  1022. while (1) {
  1023. struct work work __attribute__((aligned(128)));
  1024. unsigned long hashes_done;
  1025. struct timeval tv_workstart, tv_start, tv_end, diff;
  1026. uint64_t max64;
  1027. bool rc;
  1028. if (needs_work) {
  1029. gettimeofday(&tv_workstart, NULL);
  1030. /* obtain new work from internal workio thread */
  1031. if (unlikely(!get_work(&work, requested))) {
  1032. applog(LOG_ERR, "work retrieval failed, exiting "
  1033. "mining thread %d", mythr->id);
  1034. goto out;
  1035. }
  1036. work.thr_id = thr_id;
  1037. needs_work = requested = false;
  1038. work.blk.nonce = 0;
  1039. }
  1040. hashes_done = 0;
  1041. gettimeofday(&tv_start, NULL);
  1042. /* scan nonces for a proof-of-work hash */
  1043. switch (opt_algo) {
  1044. case ALGO_C:
  1045. rc = scanhash_c(thr_id, work.midstate, work.data + 64,
  1046. work.hash1, work.hash, work.target,
  1047. max_nonce, &hashes_done,
  1048. work.blk.nonce);
  1049. break;
  1050. #ifdef WANT_X8664_SSE2
  1051. case ALGO_SSE2_64: {
  1052. unsigned int rc5 =
  1053. scanhash_sse2_64(thr_id, work.midstate, work.data + 64,
  1054. work.hash1, work.hash,
  1055. work.target,
  1056. max_nonce, &hashes_done,
  1057. work.blk.nonce);
  1058. rc = (rc5 == -1) ? false : true;
  1059. }
  1060. break;
  1061. #endif
  1062. #ifdef WANT_SSE2_4WAY
  1063. case ALGO_4WAY: {
  1064. unsigned int rc4 =
  1065. ScanHash_4WaySSE2(thr_id, work.midstate, work.data + 64,
  1066. work.hash1, work.hash,
  1067. work.target,
  1068. max_nonce, &hashes_done,
  1069. work.blk.nonce);
  1070. rc = (rc4 == -1) ? false : true;
  1071. }
  1072. break;
  1073. #endif
  1074. #ifdef WANT_VIA_PADLOCK
  1075. case ALGO_VIA:
  1076. rc = scanhash_via(thr_id, work.data, work.target,
  1077. max_nonce, &hashes_done,
  1078. work.blk.nonce);
  1079. break;
  1080. #endif
  1081. case ALGO_CRYPTOPP:
  1082. rc = scanhash_cryptopp(thr_id, work.midstate, work.data + 64,
  1083. work.hash1, work.hash, work.target,
  1084. max_nonce, &hashes_done,
  1085. work.blk.nonce);
  1086. break;
  1087. #ifdef WANT_CRYPTOPP_ASM32
  1088. case ALGO_CRYPTOPP_ASM32:
  1089. rc = scanhash_asm32(thr_id, work.midstate, work.data + 64,
  1090. work.hash1, work.hash, work.target,
  1091. max_nonce, &hashes_done,
  1092. work.blk.nonce);
  1093. break;
  1094. #endif
  1095. default:
  1096. /* should never happen */
  1097. goto out;
  1098. }
  1099. /* record scanhash elapsed time */
  1100. gettimeofday(&tv_end, NULL);
  1101. timeval_subtract(&diff, &tv_end, &tv_start);
  1102. hashes_done -= work.blk.nonce;
  1103. hashmeter(thr_id, &diff, hashes_done);
  1104. work.blk.nonce += hashes_done;
  1105. /* adjust max_nonce to meet target cycle time */
  1106. if (diff.tv_usec > 500000)
  1107. diff.tv_sec++;
  1108. if (diff.tv_sec && diff.tv_sec != cycle) {
  1109. max64 = work.blk.nonce +
  1110. ((uint64_t)hashes_done * cycle) / diff.tv_sec;
  1111. } else
  1112. max64 = work.blk.nonce + hashes_done;
  1113. if (max64 > 0xfffffffaULL)
  1114. max64 = 0xfffffffaULL;
  1115. max_nonce = max64;
  1116. /* if nonce found, submit work */
  1117. if (unlikely(rc)) {
  1118. if (opt_debug)
  1119. applog(LOG_DEBUG, "CPU %d found something?", cpu_from_thr_id(thr_id));
  1120. if (unlikely(!submit_work_sync(mythr, &work))) {
  1121. applog(LOG_ERR, "Failed to submit_work_sync in miner_thread %d", thr_id);
  1122. break;
  1123. }
  1124. work.blk.nonce += 4;
  1125. }
  1126. timeval_subtract(&diff, &tv_end, &tv_workstart);
  1127. if (!requested && (diff.tv_sec > request_interval || work.blk.nonce > request_nonce)) {
  1128. if (unlikely(!queue_request())) {
  1129. applog(LOG_ERR, "Failed to queue_request in miner_thread %d", thr_id);
  1130. goto out;
  1131. }
  1132. requested = true;
  1133. }
  1134. if (diff.tv_sec > opt_scantime || work_restart[thr_id].restart ||
  1135. work.blk.nonce >= MAXTHREADS - hashes_done)
  1136. needs_work = true;
  1137. }
  1138. out:
  1139. tq_freeze(mythr->q);
  1140. return NULL;
  1141. }
  1142. enum {
  1143. STAT_SLEEP_INTERVAL = 1,
  1144. STAT_CTR_INTERVAL = 10000000,
  1145. FAILURE_INTERVAL = 30,
  1146. };
  1147. #ifdef HAVE_OPENCL
  1148. static _clState *clStates[16];
  1149. static inline cl_int queue_kernel_parameters(_clState *clState, dev_blk_ctx *blk)
  1150. {
  1151. cl_kernel *kernel = &clState->kernel;
  1152. cl_int status = 0;
  1153. int num = 0;
  1154. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_a);
  1155. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_b);
  1156. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_c);
  1157. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_d);
  1158. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_e);
  1159. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_f);
  1160. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_g);
  1161. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_h);
  1162. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_b);
  1163. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_c);
  1164. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_d);
  1165. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_f);
  1166. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_g);
  1167. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_h);
  1168. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->nonce);
  1169. if (clState->hasBitAlign == true) {
  1170. /* Parameters for phatk kernel */
  1171. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->W2);
  1172. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->W16);
  1173. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->W17);
  1174. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->PreVal4);
  1175. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->T1);
  1176. } else {
  1177. /* Parameters for poclbm kernel */
  1178. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW0);
  1179. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW1);
  1180. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW2);
  1181. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW3);
  1182. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW15);
  1183. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW01r);
  1184. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e);
  1185. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e2);
  1186. }
  1187. status |= clSetKernelArg(*kernel, num++, sizeof(clState->outputBuffer),
  1188. (void *)&clState->outputBuffer);
  1189. return status;
  1190. }
  1191. static inline int gpu_from_thr_id(int thr_id)
  1192. {
  1193. return thr_id % nDevs;
  1194. }
  1195. static void *gpuminer_thread(void *userdata)
  1196. {
  1197. const unsigned long cycle = opt_log_interval / 5 ? : 1;
  1198. struct timeval tv_start, tv_end, diff;
  1199. struct thr_info *mythr = userdata;
  1200. const int thr_id = mythr->id;
  1201. uint32_t *res, *blank_res;
  1202. size_t globalThreads[1];
  1203. size_t localThreads[1];
  1204. cl_int status;
  1205. _clState *clState = clStates[thr_id];
  1206. const cl_kernel *kernel = &clState->kernel;
  1207. struct work *work = malloc(sizeof(struct work));
  1208. unsigned const int threads = 1 << (15 + scan_intensity);
  1209. unsigned const int vectors = clState->preferred_vwidth;
  1210. unsigned const int hashes = threads * vectors;
  1211. unsigned int hashes_done = 0;
  1212. /* Request the next work item at 2/3 of the scantime */
  1213. unsigned const int request_interval = opt_scantime * 2 / 3 ? : 1;
  1214. unsigned const long request_nonce = MAXTHREADS / 3 * 2;
  1215. bool requested = true;
  1216. res = calloc(BUFFERSIZE, 1);
  1217. blank_res = calloc(BUFFERSIZE, 1);
  1218. if (!res || !blank_res) {
  1219. applog(LOG_ERR, "Failed to calloc in gpuminer_thread");
  1220. goto out;
  1221. }
  1222. gettimeofday(&tv_start, NULL);
  1223. globalThreads[0] = threads;
  1224. localThreads[0] = clState->work_size;
  1225. diff.tv_sec = ~0UL;
  1226. gettimeofday(&tv_end, NULL);
  1227. while (1) {
  1228. struct timeval tv_workstart;
  1229. /* This finish flushes the readbuffer set with CL_FALSE later */
  1230. clFinish(clState->commandQueue);
  1231. if (diff.tv_sec > opt_scantime || work->blk.nonce >= MAXTHREADS - hashes || work_restart[thr_id].restart) {
  1232. /* Ignore any reads since we're getting new work and queue a clean buffer */
  1233. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1234. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1235. if (unlikely(status != CL_SUCCESS))
  1236. { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
  1237. memset(res, 0, BUFFERSIZE);
  1238. gettimeofday(&tv_workstart, NULL);
  1239. /* obtain new work from internal workio thread */
  1240. if (unlikely(!get_work(work, requested))) {
  1241. applog(LOG_ERR, "work retrieval failed, exiting "
  1242. "gpu mining thread %d", mythr->id);
  1243. goto out;
  1244. }
  1245. work->thr_id = thr_id;
  1246. requested = false;
  1247. getwork_requested++;
  1248. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1249. work->blk.nonce = 0;
  1250. work_restart[thr_id].restart = 0;
  1251. if (opt_debug)
  1252. applog(LOG_DEBUG, "getwork thread %d", thr_id);
  1253. /* Flushes the writebuffer set with CL_FALSE above */
  1254. clFinish(clState->commandQueue);
  1255. }
  1256. status = queue_kernel_parameters(clState, &work->blk);
  1257. if (unlikely(status != CL_SUCCESS))
  1258. { applog(LOG_ERR, "Error: clSetKernelArg of all params failed."); goto out; }
  1259. /* MAXBUFFERS entry is used as a flag to say nonces exist */
  1260. if (res[MAXBUFFERS]) {
  1261. /* Clear the buffer again */
  1262. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1263. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1264. if (unlikely(status != CL_SUCCESS))
  1265. { applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed."); goto out; }
  1266. if (opt_debug)
  1267. applog(LOG_DEBUG, "GPU %d found something?", gpu_from_thr_id(thr_id));
  1268. postcalc_hash_async(mythr, work, res);
  1269. memset(res, 0, BUFFERSIZE);
  1270. clFinish(clState->commandQueue);
  1271. }
  1272. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1273. globalThreads, localThreads, 0, NULL, NULL);
  1274. if (unlikely(status != CL_SUCCESS))
  1275. { applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)"); goto out; }
  1276. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1277. BUFFERSIZE, res, 0, NULL, NULL);
  1278. if (unlikely(status != CL_SUCCESS))
  1279. { applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)"); goto out;}
  1280. gettimeofday(&tv_end, NULL);
  1281. timeval_subtract(&diff, &tv_end, &tv_start);
  1282. hashes_done += hashes;
  1283. work->blk.nonce += hashes;
  1284. if (diff.tv_usec > 500000)
  1285. diff.tv_sec++;
  1286. if (diff.tv_sec >= cycle) {
  1287. hashmeter(thr_id, &diff, hashes_done);
  1288. gettimeofday(&tv_start, NULL);
  1289. hashes_done = 0;
  1290. }
  1291. timeval_subtract(&diff, &tv_end, &tv_workstart);
  1292. if (!requested && (diff.tv_sec > request_interval || work->blk.nonce > request_nonce)) {
  1293. if (unlikely(!queue_request())) {
  1294. applog(LOG_ERR, "Failed to queue_request in gpuminer_thread %d", thr_id);
  1295. goto out;
  1296. }
  1297. requested = true;
  1298. }
  1299. }
  1300. out:
  1301. tq_freeze(mythr->q);
  1302. return NULL;
  1303. }
  1304. #endif /* HAVE_OPENCL */
  1305. static void restart_threads(bool longpoll)
  1306. {
  1307. int i;
  1308. /* Discard old queued requests and get new ones */
  1309. flush_requests(longpoll);
  1310. for (i = 0; i < opt_n_threads + gpu_threads; i++)
  1311. work_restart[i].restart = 1;
  1312. }
  1313. static void *longpoll_thread(void *userdata)
  1314. {
  1315. struct thr_info *mythr = userdata;
  1316. CURL *curl = NULL;
  1317. char *copy_start, *hdr_path, *lp_url = NULL;
  1318. bool need_slash = false;
  1319. int failures = 0;
  1320. unsigned int i;
  1321. hdr_path = tq_pop(mythr->q, NULL);
  1322. if (!hdr_path)
  1323. goto out;
  1324. /* full URL */
  1325. if (strstr(hdr_path, "://")) {
  1326. lp_url = hdr_path;
  1327. hdr_path = NULL;
  1328. }
  1329. /* absolute path, on current server */
  1330. else {
  1331. copy_start = (*hdr_path == '/') ? (hdr_path + 1) : hdr_path;
  1332. if (rpc_url[strlen(rpc_url) - 1] != '/')
  1333. need_slash = true;
  1334. lp_url = malloc(strlen(rpc_url) + strlen(copy_start) + 2);
  1335. if (!lp_url)
  1336. goto out;
  1337. sprintf(lp_url, "%s%s%s", rpc_url, need_slash ? "/" : "", copy_start);
  1338. }
  1339. applog(LOG_INFO, "Long-polling activated for %s", lp_url);
  1340. curl = curl_easy_init();
  1341. if (unlikely(!curl)) {
  1342. applog(LOG_ERR, "CURL initialisation failed");
  1343. goto out;
  1344. }
  1345. for (i = 0; i < 36; i++)
  1346. strcat(longpoll_block, "0");
  1347. while (1) {
  1348. json_t *val;
  1349. val = json_rpc_call(curl, lp_url, rpc_userpass, rpc_req,
  1350. false, true);
  1351. if (likely(val)) {
  1352. failures = 0;
  1353. json_decref(val);
  1354. /* Keep track of who ordered a restart_threads to make
  1355. * sure it's only done once per new block */
  1356. if (likely(!strncmp(longpoll_block, blank, 36) ||
  1357. !strncmp(longpoll_block, current_block, 36))) {
  1358. applog(LOG_WARNING, "LONGPOLL detected new block, flushing work queue");
  1359. restart_threads(true);
  1360. } else
  1361. applog(LOG_WARNING, "LONGPOLL received - new block detected and work flushed already");
  1362. } else {
  1363. if (failures++ < 10) {
  1364. sleep(30);
  1365. applog(LOG_WARNING,
  1366. "longpoll failed, sleeping for 30s");
  1367. } else {
  1368. applog(LOG_ERR,
  1369. "longpoll failed, ending thread");
  1370. goto out;
  1371. }
  1372. }
  1373. memcpy(longpoll_block, current_block, 36);
  1374. }
  1375. out:
  1376. free(hdr_path);
  1377. free(lp_url);
  1378. tq_freeze(mythr->q);
  1379. if (curl)
  1380. curl_easy_cleanup(curl);
  1381. return NULL;
  1382. }
  1383. /* Makes sure the hashmeter keeps going even if mining threads stall */
  1384. static void *wakeup_thread(void *userdata)
  1385. {
  1386. const unsigned int interval = opt_log_interval / 2 ? : 1;
  1387. struct timeval zero_tv;
  1388. memset(&zero_tv, 0, sizeof(struct timeval));
  1389. while (1) {
  1390. sleep(interval);
  1391. if (requests_queued() < opt_queue)
  1392. queue_request();
  1393. hashmeter(-1, &zero_tv, 0);
  1394. if (unlikely(work_restart[stage_thr_id].restart)) {
  1395. restart_threads(false);
  1396. work_restart[stage_thr_id].restart = 0;
  1397. }
  1398. }
  1399. return NULL;
  1400. }
  1401. int main (int argc, char *argv[])
  1402. {
  1403. struct thr_info *thr;
  1404. unsigned int i;
  1405. char name[32];
  1406. struct cgpu_info *gpus = NULL, *cpus = NULL;
  1407. if (unlikely(pthread_mutex_init(&time_lock, NULL)))
  1408. return 1;
  1409. if (unlikely(pthread_mutex_init(&hash_lock, NULL)))
  1410. return 1;
  1411. if (unlikely(pthread_mutex_init(&qd_lock, NULL)))
  1412. return 1;
  1413. if (unlikely(pthread_mutex_init(&stgd_lock, NULL)))
  1414. return 1;
  1415. #ifdef WIN32
  1416. opt_n_threads = num_processors = 1;
  1417. #else
  1418. num_processors = sysconf(_SC_NPROCESSORS_ONLN);
  1419. opt_n_threads = num_processors;
  1420. #endif /* !WIN32 */
  1421. #ifdef HAVE_OPENCL
  1422. nDevs = clDevicesNum();
  1423. if (nDevs < 0)
  1424. return 1;
  1425. #endif
  1426. if (nDevs)
  1427. opt_n_threads = 0;
  1428. rpc_url = strdup(DEF_RPC_URL);
  1429. /* parse command line */
  1430. opt_register_table(opt_config_table,
  1431. "Options for both config file and command line");
  1432. opt_register_table(opt_cmdline_table,
  1433. "Options for command line only");
  1434. opt_parse(&argc, argv, applog_and_exit);
  1435. if (argc != 1) {
  1436. applog(LOG_ERR, "Unexpected extra commandline arguments");
  1437. return 1;
  1438. }
  1439. gpu_threads = nDevs * opt_g_threads;
  1440. if (!gpu_threads && !forced_n_threads) {
  1441. /* Maybe they turned GPU off; restore default CPU threads. */
  1442. opt_n_threads = num_processors;
  1443. }
  1444. if (!rpc_userpass) {
  1445. if (!rpc_user || !rpc_pass) {
  1446. applog(LOG_ERR, "No login credentials supplied");
  1447. return 1;
  1448. }
  1449. rpc_userpass = malloc(strlen(rpc_user) + strlen(rpc_pass) + 2);
  1450. if (!rpc_userpass)
  1451. return 1;
  1452. sprintf(rpc_userpass, "%s:%s", rpc_user, rpc_pass);
  1453. }
  1454. if (unlikely(curl_global_init(CURL_GLOBAL_ALL)))
  1455. return 1;
  1456. #ifdef HAVE_SYSLOG_H
  1457. if (use_syslog)
  1458. openlog("cpuminer", LOG_PID, LOG_USER);
  1459. #endif
  1460. work_restart = calloc(opt_n_threads + 4 + gpu_threads, sizeof(*work_restart));
  1461. if (!work_restart)
  1462. return 1;
  1463. thr_info = calloc(opt_n_threads + 4 + gpu_threads, sizeof(*thr));
  1464. if (!thr_info)
  1465. return 1;
  1466. /* init workio thread info */
  1467. work_thr_id = opt_n_threads + gpu_threads;
  1468. thr = &thr_info[work_thr_id];
  1469. thr->id = work_thr_id;
  1470. thr->q = tq_new();
  1471. if (!thr->q)
  1472. return 1;
  1473. /* start work I/O thread */
  1474. if (pthread_create(&thr->pth, NULL, workio_thread, thr)) {
  1475. applog(LOG_ERR, "workio thread create failed");
  1476. return 1;
  1477. }
  1478. /* init longpoll thread info */
  1479. if (want_longpoll) {
  1480. longpoll_thr_id = opt_n_threads + gpu_threads + 1;
  1481. thr = &thr_info[longpoll_thr_id];
  1482. thr->id = longpoll_thr_id;
  1483. thr->q = tq_new();
  1484. if (!thr->q)
  1485. return 1;
  1486. /* start longpoll thread */
  1487. if (unlikely(pthread_create(&thr->pth, NULL, longpoll_thread, thr))) {
  1488. applog(LOG_ERR, "longpoll thread create failed");
  1489. return 1;
  1490. }
  1491. pthread_detach(thr->pth);
  1492. } else
  1493. longpoll_thr_id = -1;
  1494. gettimeofday(&total_tv_start, NULL);
  1495. gettimeofday(&total_tv_end, NULL);
  1496. if (opt_n_threads ) {
  1497. cpus = calloc(num_processors, sizeof(struct cgpu_info));
  1498. if (unlikely(!cpus)) {
  1499. applog(LOG_ERR, "Failed to calloc cpus");
  1500. return 1;
  1501. }
  1502. }
  1503. if (gpu_threads) {
  1504. gpus = calloc(nDevs, sizeof(struct cgpu_info));
  1505. if (unlikely(!gpus)) {
  1506. applog(LOG_ERR, "Failed to calloc gpus");
  1507. return 1;
  1508. }
  1509. }
  1510. stage_thr_id = opt_n_threads + gpu_threads + 3;
  1511. thr = &thr_info[stage_thr_id];
  1512. thr->q = tq_new();
  1513. if (!thr->q)
  1514. return 1;
  1515. /* start stage thread */
  1516. if (pthread_create(&thr->pth, NULL, stage_thread, thr)) {
  1517. applog(LOG_ERR, "stage thread create failed");
  1518. return 1;
  1519. }
  1520. /* Put enough work in the queue */
  1521. for (i = 0; i < opt_queue + opt_n_threads + gpu_threads; i++) {
  1522. if (unlikely(!queue_request())) {
  1523. applog(LOG_ERR, "Failed to queue_request in main");
  1524. return 1;
  1525. }
  1526. inc_staged(1, true);
  1527. }
  1528. #ifdef HAVE_OPENCL
  1529. /* start GPU mining threads */
  1530. for (i = 0; i < gpu_threads; i++) {
  1531. int gpu = gpu_from_thr_id(i);
  1532. thr = &thr_info[i];
  1533. thr->id = i;
  1534. gpus[gpu].is_gpu = 1;
  1535. gpus[gpu].cpu_gpu = gpu;
  1536. thr->cgpu = &gpus[gpu];
  1537. thr->q = tq_new();
  1538. if (!thr->q) {
  1539. applog(LOG_ERR, "tq_new failed in starting gpu mining threads");
  1540. return 1;
  1541. }
  1542. applog(LOG_INFO, "Init GPU thread %i", i);
  1543. clStates[i] = initCl(gpu, name, sizeof(name));
  1544. if (!clStates[i]) {
  1545. applog(LOG_ERR, "Failed to init GPU thread %d", i);
  1546. continue;
  1547. }
  1548. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1549. if (unlikely(pthread_create(&thr->pth, NULL, gpuminer_thread, thr))) {
  1550. applog(LOG_ERR, "thread %d create failed", i);
  1551. return 1;
  1552. }
  1553. pthread_detach(thr->pth);
  1554. }
  1555. applog(LOG_INFO, "%d gpu miner threads started", i);
  1556. #endif
  1557. /* start CPU mining threads */
  1558. for (i = gpu_threads; i < gpu_threads + opt_n_threads; i++) {
  1559. int cpu = cpu_from_thr_id(i);
  1560. thr = &thr_info[i];
  1561. thr->id = i;
  1562. cpus[cpu].cpu_gpu = cpu;
  1563. thr->cgpu = &cpus[cpu];
  1564. thr->q = tq_new();
  1565. if (!thr->q) {
  1566. applog(LOG_ERR, "tq_new failed in starting cpu mining threads");
  1567. return 1;
  1568. }
  1569. if (unlikely(pthread_create(&thr->pth, NULL, miner_thread, thr))) {
  1570. applog(LOG_ERR, "thread %d create failed", i);
  1571. return 1;
  1572. }
  1573. pthread_detach(thr->pth);
  1574. }
  1575. applog(LOG_INFO, "%d cpu miner threads started, "
  1576. "using SHA256 '%s' algorithm.",
  1577. opt_n_threads,
  1578. algo_names[opt_algo]);
  1579. thr = &thr_info[opt_n_threads + gpu_threads + 2];
  1580. /* start wakeup thread */
  1581. if (pthread_create(&thr->pth, NULL, wakeup_thread, NULL)) {
  1582. applog(LOG_ERR, "wakeup thread create failed");
  1583. return 1;
  1584. }
  1585. /* Restart count as it will be wrong till all threads are started */
  1586. pthread_mutex_lock(&hash_lock);
  1587. gettimeofday(&total_tv_start, NULL);
  1588. gettimeofday(&total_tv_end, NULL);
  1589. total_mhashes_done = 0;
  1590. pthread_mutex_unlock(&hash_lock);
  1591. /* main loop - simply wait for workio thread to exit */
  1592. pthread_join(thr_info[work_thr_id].pth, NULL);
  1593. curl_global_cleanup();
  1594. if (gpu_threads)
  1595. free(gpus);
  1596. if (opt_n_threads)
  1597. free(cpus);
  1598. applog(LOG_INFO, "workio thread dead, exiting.");
  1599. return 0;
  1600. }