main.c 46 KB

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