main.c 46 KB

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