main.c 45 KB

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