driver-icarus.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. * Copyright 2012 Xiangfu <xiangfu@openmobilefree.com>
  4. * Copyright 2012 Andrew Smith
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. /*
  12. * Those code should be works fine with V2 and V3 bitstream of Icarus.
  13. * Operation:
  14. * No detection implement.
  15. * Input: 64B = 32B midstate + 20B fill bytes + last 12 bytes of block head.
  16. * Return: send back 32bits immediately when Icarus found a valid nonce.
  17. * no query protocol implemented here, if no data send back in ~11.3
  18. * seconds (full cover time on 32bit nonce range by 380MH/s speed)
  19. * just send another work.
  20. * Notice:
  21. * 1. Icarus will start calculate when you push a work to them, even they
  22. * are busy.
  23. * 2. The 2 FPGAs on Icarus will distribute the job, one will calculate the
  24. * 0 ~ 7FFFFFFF, another one will cover the 80000000 ~ FFFFFFFF.
  25. * 3. It's possible for 2 FPGAs both find valid nonce in the meantime, the 2
  26. * valid nonce will all be send back.
  27. * 4. Icarus will stop work when: a valid nonce has been found or 32 bits
  28. * nonce range is completely calculated.
  29. */
  30. #include "config.h"
  31. #include <limits.h>
  32. #include <pthread.h>
  33. #include <stdio.h>
  34. #include <sys/time.h>
  35. #include <sys/types.h>
  36. #include <dirent.h>
  37. #include <unistd.h>
  38. #ifndef WIN32
  39. #include <termios.h>
  40. #include <sys/stat.h>
  41. #include <fcntl.h>
  42. #ifndef O_CLOEXEC
  43. #define O_CLOEXEC 0
  44. #endif
  45. #else
  46. #include <windows.h>
  47. #include <io.h>
  48. #endif
  49. #ifdef HAVE_SYS_EPOLL_H
  50. #include <sys/epoll.h>
  51. #define HAVE_EPOLL
  52. #endif
  53. #include "elist.h"
  54. #include "fpgautils.h"
  55. #include "miner.h"
  56. // The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h
  57. #define ICARUS_IO_SPEED 115200
  58. // The size of a successful nonce read
  59. #define ICARUS_READ_SIZE 4
  60. // A stupid constant that must be 10. Don't change it.
  61. #define TIME_FACTOR 10
  62. // Ensure the sizes are correct for the Serial read
  63. #if (ICARUS_READ_SIZE != 4)
  64. #error ICARUS_READ_SIZE must be 4
  65. #endif
  66. #if (TIME_FACTOR != 10)
  67. #error TIME_FACTOR must be 10
  68. #endif
  69. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  70. ASSERT1(sizeof(uint32_t) == 4);
  71. #define ICARUS_READ_TIME ((double)ICARUS_READ_SIZE * (double)8.0 / (double)ICARUS_IO_SPEED)
  72. // Minimum precision of longpolls, in deciseconds
  73. #define ICARUS_READ_FAULT_DECISECONDS (1)
  74. // In timing mode: Default starting value until an estimate can be obtained
  75. // 5 seconds allows for up to a ~840MH/s device
  76. #define ICARUS_READ_FAULT_COUNT_DEFAULT (50)
  77. // For a standard Icarus REV3
  78. #define ICARUS_REV3_HASH_TIME 0.00000000264083
  79. #define NANOSEC 1000000000.0
  80. // Icarus Rev3 doesn't send a completion message when it finishes
  81. // the full nonce range, so to avoid being idle we must abort the
  82. // work (by starting a new work) shortly before it finishes
  83. //
  84. // Thus we need to estimate 2 things:
  85. // 1) How many hashes were done if the work was aborted
  86. // 2) How high can the timeout be before the Icarus is idle,
  87. // to minimise the number of work started
  88. // We set 2) to 'the calculated estimate' - 1
  89. // to ensure the estimate ends before idle
  90. //
  91. // The simple calculation used is:
  92. // Tn = Total time in seconds to calculate n hashes
  93. // Hs = seconds per hash
  94. // Xn = number of hashes
  95. // W = code overhead per work
  96. //
  97. // Rough but reasonable estimate:
  98. // Tn = Hs * Xn + W (of the form y = mx + b)
  99. //
  100. // Thus:
  101. // Line of best fit (using least squares)
  102. //
  103. // Hs = (n*Sum(XiTi)-Sum(Xi)*Sum(Ti))/(n*Sum(Xi^2)-Sum(Xi)^2)
  104. // W = Sum(Ti)/n - (Hs*Sum(Xi))/n
  105. //
  106. // N.B. W is less when aborting work since we aren't waiting for the reply
  107. // to be transferred back (ICARUS_READ_TIME)
  108. // Calculating the hashes aborted at n seconds is thus just n/Hs
  109. // (though this is still a slight overestimate due to code delays)
  110. //
  111. // Both below must be exceeded to complete a set of data
  112. // Minimum how long after the first, the last data point must be
  113. #define HISTORY_SEC 60
  114. // Minimum how many points a single ICARUS_HISTORY should have
  115. #define MIN_DATA_COUNT 5
  116. // The value above used is doubled each history until it exceeds:
  117. #define MAX_MIN_DATA_COUNT 100
  118. static struct timeval history_sec = { HISTORY_SEC, 0 };
  119. // Store the last INFO_HISTORY data sets
  120. // [0] = current data, not yet ready to be included as an estimate
  121. // Each new data set throws the last old set off the end thus
  122. // keeping a ongoing average of recent data
  123. #define INFO_HISTORY 10
  124. struct ICARUS_HISTORY {
  125. struct timeval finish;
  126. double sumXiTi;
  127. double sumXi;
  128. double sumTi;
  129. double sumXi2;
  130. uint32_t values;
  131. uint32_t hash_count_min;
  132. uint32_t hash_count_max;
  133. };
  134. enum timing_mode { MODE_DEFAULT, MODE_SHORT, MODE_LONG, MODE_VALUE };
  135. static const char *MODE_DEFAULT_STR = "default";
  136. static const char *MODE_SHORT_STR = "short";
  137. static const char *MODE_LONG_STR = "long";
  138. static const char *MODE_VALUE_STR = "value";
  139. static const char *MODE_UNKNOWN_STR = "unknown";
  140. struct ICARUS_INFO {
  141. struct ICARUS_HISTORY history[INFO_HISTORY+1];
  142. uint32_t min_data_count;
  143. // seconds per Hash
  144. double Hs;
  145. int read_count;
  146. enum timing_mode timing_mode;
  147. bool do_icarus_timing;
  148. double fullnonce;
  149. int count;
  150. double W;
  151. uint32_t values;
  152. uint64_t hash_count_range;
  153. // Determine the cost of history processing
  154. // (which will only affect W)
  155. uint64_t history_count;
  156. struct timeval history_time;
  157. };
  158. // One for each possible device
  159. static struct ICARUS_INFO *icarus_info[MAX_DEVICES];
  160. struct device_api icarus_api;
  161. static void rev(unsigned char *s, size_t l)
  162. {
  163. size_t i, j;
  164. unsigned char t;
  165. for (i = 0, j = l - 1; i < j; i++, j--) {
  166. t = s[i];
  167. s[i] = s[j];
  168. s[j] = t;
  169. }
  170. }
  171. #define icarus_open2(devpath, purge) serial_open(devpath, 115200, ICARUS_READ_FAULT_DECISECONDS, purge)
  172. #define icarus_open(devpath) icarus_open2(devpath, false)
  173. static int icarus_gets(unsigned char *buf, int fd, struct timeval *tv_finish, struct thr_info*thr, int read_count)
  174. {
  175. ssize_t ret = 0;
  176. int rc = 0;
  177. int epollfd = -1;
  178. int read_amount = ICARUS_READ_SIZE;
  179. bool first = true;
  180. #ifdef HAVE_EPOLL
  181. struct epoll_event ev;
  182. struct epoll_event evr[2];
  183. int epoll_timeout = ICARUS_READ_FAULT_DECISECONDS * 100;
  184. epollfd = epoll_create(2);
  185. if (epollfd != -1) {
  186. ev.events = EPOLLIN;
  187. ev.data.fd = fd;
  188. if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) {
  189. close(epollfd);
  190. epollfd = -1;
  191. }
  192. if (thr->work_restart_fd != -1)
  193. {
  194. ev.data.fd = thr->work_restart_fd;
  195. if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, thr->work_restart_fd, &ev))
  196. applog(LOG_ERR, "Icarus: Error adding work restart fd to epoll");
  197. else
  198. {
  199. epoll_timeout *= read_count;
  200. read_count = 1;
  201. }
  202. }
  203. }
  204. else
  205. applog(LOG_ERR, "Icarus: Error creating epoll");
  206. #endif
  207. // Read reply 1 byte at a time to get earliest tv_finish
  208. while (true) {
  209. #ifdef HAVE_EPOLL
  210. if (epollfd != -1 && (ret = epoll_wait(epollfd, evr, 2, epoll_timeout)) != -1)
  211. {
  212. if (ret == 1 && evr[0].data.fd == fd)
  213. ret = read(fd, buf, 1);
  214. else
  215. {
  216. if (ret)
  217. // work restart trigger
  218. (void)read(thr->work_restart_fd, buf, read_amount);
  219. ret = 0;
  220. }
  221. }
  222. else
  223. #endif
  224. ret = read(fd, buf, 1);
  225. if (first)
  226. gettimeofday(tv_finish, NULL);
  227. if (ret >= read_amount)
  228. {
  229. if (epollfd != -1)
  230. close(epollfd);
  231. return 0;
  232. }
  233. if (ret > 0) {
  234. buf += ret;
  235. read_amount -= ret;
  236. first = false;
  237. continue;
  238. }
  239. rc++;
  240. if (rc >= read_count || thr->work_restart) {
  241. if (epollfd != -1)
  242. close(epollfd);
  243. if (opt_debug) {
  244. rc *= ICARUS_READ_FAULT_DECISECONDS;
  245. applog(LOG_DEBUG,
  246. "Icarus Read: %s %d.%d seconds",
  247. thr->work_restart ? "Work restart at" : "No data in",
  248. rc / 10, rc % 10);
  249. }
  250. return 1;
  251. }
  252. }
  253. }
  254. static int icarus_write(int fd, const void *buf, size_t bufLen)
  255. {
  256. size_t ret;
  257. ret = write(fd, buf, bufLen);
  258. if (unlikely(ret != bufLen))
  259. return 1;
  260. return 0;
  261. }
  262. #define icarus_close(fd) close(fd)
  263. static const char *timing_mode_str(enum timing_mode timing_mode)
  264. {
  265. switch(timing_mode) {
  266. case MODE_DEFAULT:
  267. return MODE_DEFAULT_STR;
  268. case MODE_SHORT:
  269. return MODE_SHORT_STR;
  270. case MODE_LONG:
  271. return MODE_LONG_STR;
  272. case MODE_VALUE:
  273. return MODE_VALUE_STR;
  274. default:
  275. return MODE_UNKNOWN_STR;
  276. }
  277. }
  278. static void set_timing_mode(struct cgpu_info *icarus)
  279. {
  280. struct ICARUS_INFO *info = icarus_info[icarus->device_id];
  281. double Hs;
  282. char buf[BUFSIZ+1];
  283. char *ptr, *comma, *eq;
  284. size_t max;
  285. int i;
  286. if (opt_icarus_timing == NULL)
  287. buf[0] = '\0';
  288. else {
  289. ptr = opt_icarus_timing;
  290. for (i = 0; i < icarus->device_id; i++) {
  291. comma = strchr(ptr, ',');
  292. if (comma == NULL)
  293. break;
  294. ptr = comma + 1;
  295. }
  296. comma = strchr(ptr, ',');
  297. if (comma == NULL)
  298. max = strlen(ptr);
  299. else
  300. max = comma - ptr;
  301. if (max > BUFSIZ)
  302. max = BUFSIZ;
  303. strncpy(buf, ptr, max);
  304. buf[max] = '\0';
  305. }
  306. info->Hs = 0;
  307. info->read_count = 0;
  308. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  309. info->Hs = ICARUS_REV3_HASH_TIME;
  310. info->read_count = ICARUS_READ_FAULT_COUNT_DEFAULT;
  311. info->timing_mode = MODE_SHORT;
  312. info->do_icarus_timing = true;
  313. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  314. info->Hs = ICARUS_REV3_HASH_TIME;
  315. info->read_count = ICARUS_READ_FAULT_COUNT_DEFAULT;
  316. info->timing_mode = MODE_LONG;
  317. info->do_icarus_timing = true;
  318. } else if ((Hs = atof(buf)) != 0) {
  319. info->Hs = Hs / NANOSEC;
  320. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  321. if ((eq = strchr(buf, '=')) != NULL)
  322. info->read_count = atoi(eq+1);
  323. if (info->read_count < 1)
  324. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  325. if (unlikely(info->read_count < 1))
  326. info->read_count = 1;
  327. info->timing_mode = MODE_VALUE;
  328. info->do_icarus_timing = false;
  329. } else {
  330. // Anything else in buf just uses DEFAULT mode
  331. info->Hs = ICARUS_REV3_HASH_TIME;
  332. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  333. if ((eq = strchr(buf, '=')) != NULL)
  334. info->read_count = atoi(eq+1);
  335. if (info->read_count < 1)
  336. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  337. info->timing_mode = MODE_DEFAULT;
  338. info->do_icarus_timing = false;
  339. }
  340. info->min_data_count = MIN_DATA_COUNT;
  341. applog(LOG_DEBUG, "Icarus: Init: %d mode=%s read_count=%d Hs=%e",
  342. icarus->device_id, timing_mode_str(info->timing_mode), info->read_count, info->Hs);
  343. }
  344. static bool icarus_detect_one(const char *devpath)
  345. {
  346. struct ICARUS_INFO *info;
  347. int fd;
  348. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  349. // N.B. golden_ob MUST take less time to calculate
  350. // than the timeout set in icarus_open()
  351. // This one takes ~0.53ms on Rev3 Icarus
  352. const char golden_ob[] =
  353. "4679ba4ec99876bf4bfe086082b40025"
  354. "4df6c356451471139a3afa71e48f544a"
  355. "00000000000000000000000000000000"
  356. "0000000087320b1a1426674f2fa722ce";
  357. const char golden_nonce[] = "000187a2";
  358. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  359. char *nonce_hex;
  360. applog(LOG_DEBUG, "Icarus Detect: Attempting to open %s", devpath);
  361. fd = icarus_open2(devpath, true);
  362. if (unlikely(fd == -1)) {
  363. applog(LOG_ERR, "Icarus Detect: Failed to open %s", devpath);
  364. return false;
  365. }
  366. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  367. icarus_write(fd, ob_bin, sizeof(ob_bin));
  368. memset(nonce_bin, 0, sizeof(nonce_bin));
  369. struct thr_info dummy = {
  370. .work_restart_fd = -1,
  371. };
  372. struct timeval tv_finish;
  373. icarus_gets(nonce_bin, fd, &tv_finish, &dummy, 1);
  374. icarus_close(fd);
  375. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  376. if (nonce_hex) {
  377. if (strncmp(nonce_hex, golden_nonce, 8)) {
  378. applog(LOG_ERR,
  379. "Icarus Detect: "
  380. "Test failed at %s: get %s, should: %s",
  381. devpath, nonce_hex, golden_nonce);
  382. free(nonce_hex);
  383. return false;
  384. }
  385. applog(LOG_DEBUG,
  386. "Icarus Detect: "
  387. "Test succeeded at %s: got %s",
  388. devpath, nonce_hex);
  389. free(nonce_hex);
  390. } else
  391. return false;
  392. /* We have a real Icarus! */
  393. struct cgpu_info *icarus;
  394. icarus = calloc(1, sizeof(struct cgpu_info));
  395. icarus->api = &icarus_api;
  396. icarus->device_path = strdup(devpath);
  397. icarus->threads = 1;
  398. add_cgpu(icarus);
  399. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  400. devpath, icarus->device_id);
  401. if (icarus_info[icarus->device_id] == NULL) {
  402. icarus_info[icarus->device_id] = (struct ICARUS_INFO *)malloc(sizeof(struct ICARUS_INFO));
  403. if (unlikely(!(icarus_info[icarus->device_id])))
  404. quit(1, "Failed to malloc ICARUS_INFO");
  405. }
  406. info = icarus_info[icarus->device_id];
  407. // Initialise everything to zero for a new device
  408. memset(info, 0, sizeof(struct ICARUS_INFO));
  409. set_timing_mode(icarus);
  410. return true;
  411. }
  412. static void icarus_detect()
  413. {
  414. serial_detect(icarus_api.dname, icarus_detect_one);
  415. }
  416. struct icarus_state {
  417. bool firstrun;
  418. struct timeval tv_workstart;
  419. struct timeval tv_workfinish;
  420. struct work last_work;
  421. bool changework;
  422. };
  423. static bool icarus_prepare(struct thr_info *thr)
  424. {
  425. struct cgpu_info *icarus = thr->cgpu;
  426. struct timeval now;
  427. int fd = icarus_open2(icarus->device_path, true);
  428. if (unlikely(-1 == fd)) {
  429. applog(LOG_ERR, "Failed to open Icarus on %s",
  430. icarus->device_path);
  431. return false;
  432. }
  433. icarus->device_fd = fd;
  434. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  435. gettimeofday(&now, NULL);
  436. get_datestamp(icarus->init, &now);
  437. struct icarus_state *state;
  438. thr->cgpu_data = state = calloc(1, sizeof(*state));
  439. state->firstrun = true;
  440. #ifdef HAVE_EPOLL
  441. int epollfd = epoll_create(2);
  442. if (epollfd != -1)
  443. {
  444. close(epollfd);
  445. thr->work_restart_fd = 0;
  446. }
  447. #endif
  448. return true;
  449. }
  450. static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  451. __maybe_unused uint64_t max_nonce)
  452. {
  453. struct cgpu_info *icarus;
  454. int fd;
  455. int ret, lret;
  456. struct ICARUS_INFO *info;
  457. unsigned char ob_bin[64] = {0}, nonce_bin[ICARUS_READ_SIZE] = {0};
  458. char *ob_hex;
  459. uint32_t nonce;
  460. uint64_t hash_count;
  461. struct timeval tv_start, elapsed;
  462. struct timeval tv_history_start, tv_history_finish;
  463. double Ti, Xi;
  464. int i;
  465. struct ICARUS_HISTORY *history0, *history;
  466. int count;
  467. double Hs, W, fullnonce;
  468. int read_count;
  469. uint64_t estimate_hashes;
  470. uint32_t values;
  471. uint64_t hash_count_range;
  472. elapsed.tv_sec = elapsed.tv_usec = 0;
  473. icarus = thr->cgpu;
  474. struct icarus_state *state = thr->cgpu_data;
  475. // Prepare the next work immediately
  476. memcpy(ob_bin, work->midstate, 32);
  477. memcpy(ob_bin + 52, work->data + 64, 12);
  478. rev(ob_bin, 32);
  479. rev(ob_bin + 52, 12);
  480. // Wait for the previous run's result
  481. fd = icarus->device_fd;
  482. info = icarus_info[icarus->device_id];
  483. if (!state->firstrun) {
  484. if (state->changework)
  485. state->changework = false;
  486. else
  487. {
  488. /* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
  489. lret = icarus_gets(nonce_bin, fd, &state->tv_workfinish, thr, info->read_count);
  490. if (lret && thr->work_restart) {
  491. // The prepared work is invalid, and the current work is abandoned
  492. // Go back to the main loop to get the next work, and stuff
  493. // Returning to the main loop will clear work_restart, so use a flag...
  494. state->changework = true;
  495. return 1;
  496. }
  497. }
  498. tv_start = state->tv_workstart;
  499. timeval_subtract(&elapsed, &state->tv_workfinish, &tv_start);
  500. }
  501. #ifndef WIN32
  502. tcflush(fd, TCOFLUSH);
  503. #endif
  504. gettimeofday(&state->tv_workstart, NULL);
  505. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  506. if (ret) {
  507. icarus_close(fd);
  508. return 0; /* This should never happen */
  509. }
  510. if (opt_debug) {
  511. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  512. if (ob_hex) {
  513. applog(LOG_DEBUG, "Icarus %d sent: %s",
  514. icarus->device_id, ob_hex);
  515. free(ob_hex);
  516. }
  517. }
  518. // Reopen the serial port to workaround a USB-host-chipset-specific issue with the Icarus's buggy USB-UART
  519. icarus_close(fd);
  520. fd = icarus_open(icarus->device_path);
  521. if (unlikely(-1 == fd)) {
  522. applog(LOG_ERR, "Failed to reopen Icarus on %s",
  523. icarus->device_path);
  524. return 0;
  525. }
  526. icarus->device_fd = fd;
  527. work->blk.nonce = 0xffffffff;
  528. if (state->firstrun) {
  529. state->firstrun = false;
  530. memcpy(&state->last_work, work, sizeof(state->last_work));
  531. return 1;
  532. }
  533. // OK, done starting Icarus's next job... now process the last run's result!
  534. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  535. // aborted before becoming idle, get new work
  536. if (nonce == 0 && lret) {
  537. memcpy(&state->last_work, work, sizeof(state->last_work));
  538. // ONLY up to just when it aborted
  539. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  540. estimate_hashes = ((double)(elapsed.tv_sec)
  541. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  542. // If some Serial-USB delay allowed the full nonce range to
  543. // complete it can't have done more than a full nonce
  544. if (unlikely(estimate_hashes > 0xffffffff))
  545. estimate_hashes = 0xffffffff;
  546. if (opt_debug) {
  547. applog(LOG_DEBUG, "Icarus %d no nonce = 0x%08llx hashes (%ld.%06lds)",
  548. icarus->device_id, estimate_hashes,
  549. elapsed.tv_sec, elapsed.tv_usec);
  550. }
  551. return estimate_hashes;
  552. }
  553. #if !defined (__BIG_ENDIAN__) && !defined(MIPSEB)
  554. nonce = swab32(nonce);
  555. #endif
  556. submit_nonce(thr, &state->last_work, nonce);
  557. memcpy(&state->last_work, work, sizeof(state->last_work));
  558. hash_count = (nonce & 0x7fffffff);
  559. if (hash_count++ == 0x7fffffff)
  560. hash_count = 0xffffffff;
  561. else
  562. hash_count <<= 1;
  563. if (opt_debug) {
  564. applog(LOG_DEBUG, "Icarus %d nonce = 0x%08x = 0x%08llx hashes (%ld.%06lds)",
  565. icarus->device_id, nonce, hash_count, elapsed.tv_sec, elapsed.tv_usec);
  566. }
  567. // ignore possible end condition values
  568. if (info->do_icarus_timing && (nonce & 0x7fffffff) > 0x000fffff && (nonce & 0x7fffffff) < 0x7ff00000) {
  569. gettimeofday(&tv_history_start, NULL);
  570. history0 = &(info->history[0]);
  571. if (history0->values == 0)
  572. timeradd(&tv_start, &history_sec, &(history0->finish));
  573. Ti = (double)(elapsed.tv_sec)
  574. + ((double)(elapsed.tv_usec))/((double)1000000)
  575. - ICARUS_READ_TIME;
  576. Xi = (double)hash_count;
  577. history0->sumXiTi += Xi * Ti;
  578. history0->sumXi += Xi;
  579. history0->sumTi += Ti;
  580. history0->sumXi2 += Xi * Xi;
  581. history0->values++;
  582. if (history0->hash_count_max < hash_count)
  583. history0->hash_count_max = hash_count;
  584. if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
  585. history0->hash_count_min = hash_count;
  586. if (history0->values >= info->min_data_count
  587. && timercmp(&tv_start, &(history0->finish), >)) {
  588. for (i = INFO_HISTORY; i > 0; i--)
  589. memcpy(&(info->history[i]),
  590. &(info->history[i-1]),
  591. sizeof(struct ICARUS_HISTORY));
  592. // Initialise history0 to zero for summary calculation
  593. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  594. // We just completed a history data set
  595. // So now recalc read_count based on the whole history thus we will
  596. // initially get more accurate until it completes INFO_HISTORY
  597. // total data sets
  598. count = 0;
  599. for (i = 1 ; i <= INFO_HISTORY; i++) {
  600. history = &(info->history[i]);
  601. if (history->values >= MIN_DATA_COUNT) {
  602. count++;
  603. history0->sumXiTi += history->sumXiTi;
  604. history0->sumXi += history->sumXi;
  605. history0->sumTi += history->sumTi;
  606. history0->sumXi2 += history->sumXi2;
  607. history0->values += history->values;
  608. if (history0->hash_count_max < history->hash_count_max)
  609. history0->hash_count_max = history->hash_count_max;
  610. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  611. history0->hash_count_min = history->hash_count_min;
  612. }
  613. }
  614. // All history data
  615. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  616. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  617. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  618. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  619. values = history0->values;
  620. // Initialise history0 to zero for next data set
  621. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  622. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  623. read_count = (int)(fullnonce * TIME_FACTOR) - 1;
  624. info->Hs = Hs;
  625. info->read_count = read_count;
  626. info->fullnonce = fullnonce;
  627. info->count = count;
  628. info->W = W;
  629. info->values = values;
  630. info->hash_count_range = hash_count_range;
  631. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  632. info->min_data_count *= 2;
  633. else if (info->timing_mode == MODE_SHORT)
  634. info->do_icarus_timing = false;
  635. // applog(LOG_WARNING, "Icarus %d Re-estimate: read_count=%d fullnonce=%fs history count=%d Hs=%e W=%e values=%d hash range=0x%08lx min data count=%u", icarus->device_id, read_count, fullnonce, count, Hs, W, values, hash_count_range, info->min_data_count);
  636. applog(LOG_WARNING, "Icarus %d Re-estimate: Hs=%e W=%e read_count=%d fullnonce=%.3fs",
  637. icarus->device_id, Hs, W, read_count, fullnonce);
  638. }
  639. info->history_count++;
  640. gettimeofday(&tv_history_finish, NULL);
  641. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  642. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  643. }
  644. return hash_count;
  645. }
  646. static json_t*
  647. icarus_perf_stats(struct cgpu_info *cgpu)
  648. {
  649. struct ICARUS_INFO *info = icarus_info[cgpu->device_id];
  650. json_t *ji = json_object();
  651. // Warning, access to these is not locked - but we don't really
  652. // care since hashing performance is way more important than
  653. // locking access to displaying API debug 'stats'
  654. json_object_set(ji, "read_count" , json_integer(info->read_count ));
  655. json_object_set(ji, "fullnonce" , json_real (info->fullnonce ));
  656. json_object_set(ji, "count" , json_integer(info->count ));
  657. json_object_set(ji, "Hs" , json_real (info->Hs ));
  658. json_object_set(ji, "W" , json_real (info->W ));
  659. json_object_set(ji, "total_values" , json_integer(info->values ));
  660. json_object_set(ji, "range" , json_integer(info->hash_count_range));
  661. json_object_set(ji, "history_count" , json_integer(info->history_count ));
  662. json_object_set(ji, "history_time" , json_real (
  663. (double)(info->history_time.tv_sec)
  664. + ((double)(info->history_time.tv_usec))/((double)1000000)
  665. ));
  666. json_object_set(ji, "min_data_count", json_integer(info->min_data_count));
  667. json_object_set(ji, "timing_values" , json_integer(info->history[0].values));
  668. return ji;
  669. }
  670. static void icarus_shutdown(struct thr_info *thr)
  671. {
  672. struct cgpu_info *icarus = thr->cgpu;
  673. icarus_close(icarus->device_fd);
  674. free(thr->cgpu_data);
  675. }
  676. struct device_api icarus_api = {
  677. .dname = "icarus",
  678. .name = "ICA",
  679. .api_detect = icarus_detect,
  680. .get_extra_device_perf_stats = icarus_perf_stats,
  681. .thread_prepare = icarus_prepare,
  682. .scanhash = icarus_scanhash,
  683. .thread_shutdown = icarus_shutdown,
  684. };