main.c 46 KB

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