main.c 48 KB

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