main.c 45 KB

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