main.c 46 KB

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