main.c 43 KB

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