driver-icarus.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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 <limits.h>
  31. #include <pthread.h>
  32. #include <stdio.h>
  33. #include <sys/time.h>
  34. #include <sys/types.h>
  35. #include <dirent.h>
  36. #include <unistd.h>
  37. #ifndef WIN32
  38. #include <termios.h>
  39. #include <sys/stat.h>
  40. #include <fcntl.h>
  41. #ifndef O_CLOEXEC
  42. #define O_CLOEXEC 0
  43. #endif
  44. #else
  45. #include <windows.h>
  46. #include <io.h>
  47. #endif
  48. #include "elist.h"
  49. #include "fpgautils.h"
  50. #include "miner.h"
  51. // The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h
  52. #define ICARUS_IO_SPEED 115200
  53. // The size of a successful nonce read
  54. #define ICARUS_READ_SIZE 4
  55. // Ensure the sizes are correct for the Serial read
  56. #if (ICARUS_READ_SIZE != 4)
  57. #error ICARUS_READ_SIZE must be 4
  58. #endif
  59. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  60. ASSERT1(sizeof(uint32_t) == 4);
  61. #define ICARUS_READ_TIME ((double)ICARUS_READ_SIZE * (double)8.0 / (double)ICARUS_IO_SPEED)
  62. // Fraction of a second, USB timeout is measured in
  63. // i.e. 10 means 1/10 of a second
  64. #define TIME_FACTOR 10
  65. // It's 10 per second, thus value = 10/TIME_FACTOR =
  66. #define ICARUS_READ_FAULT_DECISECONDS 1
  67. // In timing mode: Default starting value until an estimate can be obtained
  68. // 5 seconds allows for up to a ~840MH/s device
  69. #define ICARUS_READ_COUNT_TIMING (5 * TIME_FACTOR)
  70. // For a standard Icarus REV3 (to 5 places)
  71. // Since this rounds up a the last digit - it is a slight overestimate
  72. // Thus the hash rate will be a VERY slight underestimate
  73. // (by a lot less than the displayed accuracy)
  74. #define ICARUS_REV3_HASH_TIME 0.0000000026316
  75. #define NANOSEC 1000000000.0
  76. // Icarus Rev3 doesn't send a completion message when it finishes
  77. // the full nonce range, so to avoid being idle we must abort the
  78. // work (by starting a new work) shortly before it finishes
  79. //
  80. // Thus we need to estimate 2 things:
  81. // 1) How many hashes were done if the work was aborted
  82. // 2) How high can the timeout be before the Icarus is idle,
  83. // to minimise the number of work started
  84. // We set 2) to 'the calculated estimate' - 1
  85. // to ensure the estimate ends before idle
  86. //
  87. // The simple calculation used is:
  88. // Tn = Total time in seconds to calculate n hashes
  89. // Hs = seconds per hash
  90. // Xn = number of hashes
  91. // W = code overhead per work
  92. //
  93. // Rough but reasonable estimate:
  94. // Tn = Hs * Xn + W (of the form y = mx + b)
  95. //
  96. // Thus:
  97. // Line of best fit (using least squares)
  98. //
  99. // Hs = (n*Sum(XiTi)-Sum(Xi)*Sum(Ti))/(n*Sum(Xi^2)-Sum(Xi)^2)
  100. // W = Sum(Ti)/n - (Hs*Sum(Xi))/n
  101. //
  102. // N.B. W is less when aborting work since we aren't waiting for the reply
  103. // to be transferred back (ICARUS_READ_TIME)
  104. // Calculating the hashes aborted at n seconds is thus just n/Hs
  105. // (though this is still a slight overestimate due to code delays)
  106. //
  107. // Both below must be exceeded to complete a set of data
  108. // Minimum how long after the first, the last data point must be
  109. #define HISTORY_SEC 60
  110. // Minimum how many points a single ICARUS_HISTORY should have
  111. #define MIN_DATA_COUNT 5
  112. // The value above used is doubled each history until it exceeds:
  113. #define MAX_MIN_DATA_COUNT 100
  114. static struct timeval history_sec = { HISTORY_SEC, 0 };
  115. // Store the last INFO_HISTORY data sets
  116. // [0] = current data, not yet ready to be included as an estimate
  117. // Each new data set throws the last old set off the end thus
  118. // keeping a ongoing average of recent data
  119. #define INFO_HISTORY 10
  120. struct ICARUS_HISTORY {
  121. struct timeval finish;
  122. double sumXiTi;
  123. double sumXi;
  124. double sumTi;
  125. double sumXi2;
  126. uint32_t values;
  127. uint32_t hash_count_min;
  128. uint32_t hash_count_max;
  129. };
  130. enum timing_mode { MODE_DEFAULT, MODE_SHORT, MODE_LONG, MODE_VALUE };
  131. static const char *MODE_DEFAULT_STR = "default";
  132. static const char *MODE_SHORT_STR = "short";
  133. static const char *MODE_LONG_STR = "long";
  134. static const char *MODE_VALUE_STR = "value";
  135. static const char *MODE_UNKNOWN_STR = "unknown";
  136. struct ICARUS_INFO {
  137. // time to calculate the golden_ob
  138. uint64_t golden_hashes;
  139. struct timeval golden_tv;
  140. struct ICARUS_HISTORY history[INFO_HISTORY+1];
  141. uint32_t min_data_count;
  142. // seconds per Hash
  143. double Hs;
  144. int read_count;
  145. enum timing_mode timing_mode;
  146. bool do_icarus_timing;
  147. double fullnonce;
  148. int count;
  149. double W;
  150. uint32_t values;
  151. uint64_t hash_count_range;
  152. // Determine the cost of history processing
  153. // (which will only affect W)
  154. uint64_t history_count;
  155. struct timeval history_time;
  156. };
  157. // One for each possible device
  158. static struct ICARUS_INFO **icarus_info;
  159. struct device_api icarus_api;
  160. static void rev(unsigned char *s, size_t l)
  161. {
  162. size_t i, j;
  163. unsigned char t;
  164. for (i = 0, j = l - 1; i < j; i++, j--) {
  165. t = s[i];
  166. s[i] = s[j];
  167. s[j] = t;
  168. }
  169. }
  170. #define icarus_open2(devpath, purge) serial_open(devpath, 115200, ICARUS_READ_FAULT_DECISECONDS, purge)
  171. #define icarus_open(devpath) icarus_open2(devpath, false)
  172. static int icarus_gets(unsigned char *buf, int fd, struct timeval *tv_finish, struct thr_info *thr, int read_count)
  173. {
  174. ssize_t ret = 0;
  175. int rc = 0;
  176. int read_amount = ICARUS_READ_SIZE;
  177. bool first = true;
  178. // Read reply 1 byte at a time to get earliest tv_finish
  179. while (true) {
  180. ret = read(fd, buf, 1);
  181. if (first)
  182. gettimeofday(tv_finish, NULL);
  183. if (ret >= read_amount)
  184. return 0;
  185. if (ret > 0) {
  186. buf += ret;
  187. read_amount -= ret;
  188. first = false;
  189. continue;
  190. }
  191. rc++;
  192. if (rc >= read_count) {
  193. if (opt_debug) {
  194. applog(LOG_DEBUG,
  195. "Icarus Read: No data in %.2f seconds",
  196. (float)rc/(float)TIME_FACTOR);
  197. }
  198. return 1;
  199. }
  200. if (thr->work_restart) {
  201. if (opt_debug) {
  202. applog(LOG_DEBUG,
  203. "Icarus Read: Work restart at %.2f seconds",
  204. (float)(rc)/(float)TIME_FACTOR);
  205. }
  206. return 1;
  207. }
  208. }
  209. }
  210. static int icarus_write(int fd, const void *buf, size_t bufLen)
  211. {
  212. size_t ret;
  213. ret = write(fd, buf, bufLen);
  214. if (unlikely(ret != bufLen))
  215. return 1;
  216. return 0;
  217. }
  218. #define icarus_close(fd) close(fd)
  219. static const char *timing_mode_str(enum timing_mode timing_mode)
  220. {
  221. switch(timing_mode) {
  222. case MODE_DEFAULT:
  223. return MODE_DEFAULT_STR;
  224. case MODE_SHORT:
  225. return MODE_SHORT_STR;
  226. case MODE_LONG:
  227. return MODE_LONG_STR;
  228. case MODE_VALUE:
  229. return MODE_VALUE_STR;
  230. default:
  231. return MODE_UNKNOWN_STR;
  232. }
  233. }
  234. static void set_timing_mode(struct cgpu_info *icarus)
  235. {
  236. struct ICARUS_INFO *info = icarus_info[icarus->device_id];
  237. double Hs;
  238. char buf[BUFSIZ+1];
  239. char *ptr, *comma, *eq;
  240. size_t max;
  241. int i;
  242. if (opt_icarus_timing == NULL)
  243. buf[0] = '\0';
  244. else {
  245. ptr = opt_icarus_timing;
  246. for (i = 0; i < icarus->device_id; i++) {
  247. comma = strchr(ptr, ',');
  248. if (comma == NULL)
  249. break;
  250. ptr = comma + 1;
  251. }
  252. comma = strchr(ptr, ',');
  253. if (comma == NULL)
  254. max = strlen(ptr);
  255. else
  256. max = comma - ptr;
  257. if (max > BUFSIZ)
  258. max = BUFSIZ;
  259. strncpy(buf, ptr, max);
  260. buf[max] = '\0';
  261. }
  262. info->Hs = 0;
  263. info->read_count = 0;
  264. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  265. info->Hs = ICARUS_REV3_HASH_TIME;
  266. info->read_count = ICARUS_READ_COUNT_TIMING;
  267. info->timing_mode = MODE_SHORT;
  268. info->do_icarus_timing = true;
  269. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  270. info->Hs = ICARUS_REV3_HASH_TIME;
  271. info->read_count = ICARUS_READ_COUNT_TIMING;
  272. info->timing_mode = MODE_LONG;
  273. info->do_icarus_timing = true;
  274. } else if ((Hs = atof(buf)) != 0) {
  275. info->Hs = Hs / NANOSEC;
  276. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  277. if ((eq = strchr(buf, '=')) != NULL)
  278. info->read_count = atoi(eq+1);
  279. if (info->read_count < 1)
  280. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  281. if (unlikely(info->read_count < 1))
  282. info->read_count = 1;
  283. info->timing_mode = MODE_VALUE;
  284. info->do_icarus_timing = false;
  285. } else {
  286. // Anything else in buf just uses DEFAULT mode
  287. info->Hs = ICARUS_REV3_HASH_TIME;
  288. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  289. if ((eq = strchr(buf, '=')) != NULL)
  290. info->read_count = atoi(eq+1);
  291. if (info->read_count < 1)
  292. info->read_count = (int)(info->fullnonce * TIME_FACTOR) - 1;
  293. info->timing_mode = MODE_DEFAULT;
  294. info->do_icarus_timing = false;
  295. }
  296. info->min_data_count = MIN_DATA_COUNT;
  297. applog(LOG_DEBUG, "Icarus: Init: %d mode=%s read_count=%d Hs=%e",
  298. icarus->device_id, timing_mode_str(info->timing_mode), info->read_count, info->Hs);
  299. }
  300. static bool icarus_detect_one(const char *devpath)
  301. {
  302. struct ICARUS_INFO *info;
  303. struct timeval tv_start, tv_finish;
  304. int fd;
  305. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  306. // N.B. golden_ob MUST take less time to calculate
  307. // than the timeout set in icarus_open()
  308. // This one takes ~0.53ms on Rev3 Icarus
  309. const char golden_ob[] =
  310. "4679ba4ec99876bf4bfe086082b40025"
  311. "4df6c356451471139a3afa71e48f544a"
  312. "00000000000000000000000000000000"
  313. "0000000087320b1a1426674f2fa722ce";
  314. const char golden_nonce[] = "000187a2";
  315. const uint32_t golden_nonce_val = 0x000187a2;
  316. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  317. char *nonce_hex;
  318. applog(LOG_DEBUG, "Icarus Detect: Attempting to open %s", devpath);
  319. fd = icarus_open2(devpath, true);
  320. if (unlikely(fd == -1)) {
  321. applog(LOG_ERR, "Icarus Detect: Failed to open %s", devpath);
  322. return false;
  323. }
  324. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  325. icarus_write(fd, ob_bin, sizeof(ob_bin));
  326. gettimeofday(&tv_start, NULL);
  327. memset(nonce_bin, 0, sizeof(nonce_bin));
  328. struct thr_info dummy = {
  329. .work_restart = false,
  330. };
  331. icarus_gets(nonce_bin, fd, &tv_finish, &dummy, 1);
  332. icarus_close(fd);
  333. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  334. if (nonce_hex) {
  335. if (strncmp(nonce_hex, golden_nonce, 8)) {
  336. applog(LOG_ERR,
  337. "Icarus Detect: "
  338. "Test failed at %s: get %s, should: %s",
  339. devpath, nonce_hex, golden_nonce);
  340. free(nonce_hex);
  341. return false;
  342. }
  343. applog(LOG_DEBUG,
  344. "Icarus Detect: "
  345. "Test succeeded at %s: got %s",
  346. devpath, nonce_hex);
  347. free(nonce_hex);
  348. } else
  349. return false;
  350. /* We have a real Icarus! */
  351. struct cgpu_info *icarus;
  352. icarus = calloc(1, sizeof(struct cgpu_info));
  353. icarus->api = &icarus_api;
  354. icarus->device_path = strdup(devpath);
  355. icarus->threads = 1;
  356. add_cgpu(icarus);
  357. icarus_info = realloc(icarus_info, sizeof(struct ICARUS_INFO *) * (total_devices + 1));
  358. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  359. devpath, icarus->device_id);
  360. // Since we are adding a new device on the end it needs to always be allocated
  361. icarus_info[icarus->device_id] = (struct ICARUS_INFO *)malloc(sizeof(struct ICARUS_INFO));
  362. if (unlikely(!(icarus_info[icarus->device_id])))
  363. quit(1, "Failed to malloc ICARUS_INFO");
  364. info = icarus_info[icarus->device_id];
  365. // Initialise everything to zero for a new device
  366. memset(info, 0, sizeof(struct ICARUS_INFO));
  367. info->golden_hashes = (golden_nonce_val & 0x7fffffff) << 1;
  368. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  369. set_timing_mode(icarus);
  370. return true;
  371. }
  372. static void icarus_detect()
  373. {
  374. serial_detect(icarus_api.dname, icarus_detect_one);
  375. }
  376. static bool icarus_prepare(struct thr_info *thr)
  377. {
  378. struct cgpu_info *icarus = thr->cgpu;
  379. struct timeval now;
  380. int fd = icarus_open(icarus->device_path);
  381. if (unlikely(-1 == fd)) {
  382. applog(LOG_ERR, "Failed to open Icarus on %s",
  383. icarus->device_path);
  384. return false;
  385. }
  386. icarus->device_fd = fd;
  387. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  388. gettimeofday(&now, NULL);
  389. get_datestamp(icarus->init, &now);
  390. return true;
  391. }
  392. static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  393. __maybe_unused int64_t max_nonce)
  394. {
  395. struct cgpu_info *icarus;
  396. int fd;
  397. int ret;
  398. struct ICARUS_INFO *info;
  399. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  400. char *ob_hex;
  401. uint32_t nonce;
  402. int64_t hash_count;
  403. struct timeval tv_start, tv_finish, elapsed;
  404. struct timeval tv_history_start, tv_history_finish;
  405. double Ti, Xi;
  406. int i;
  407. struct ICARUS_HISTORY *history0, *history;
  408. int count;
  409. double Hs, W, fullnonce;
  410. int read_count;
  411. int64_t estimate_hashes;
  412. uint32_t values;
  413. int64_t hash_count_range;
  414. elapsed.tv_sec = elapsed.tv_usec = 0;
  415. icarus = thr->cgpu;
  416. fd = icarus->device_fd;
  417. memset(ob_bin, 0, sizeof(ob_bin));
  418. memcpy(ob_bin, work->midstate, 32);
  419. memcpy(ob_bin + 52, work->data + 64, 12);
  420. rev(ob_bin, 32);
  421. rev(ob_bin + 52, 12);
  422. #ifndef WIN32
  423. tcflush(fd, TCOFLUSH);
  424. #endif
  425. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  426. if (ret)
  427. return -1; /* This should never happen */
  428. gettimeofday(&tv_start, NULL);
  429. if (opt_debug) {
  430. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  431. if (ob_hex) {
  432. applog(LOG_DEBUG, "Icarus %d sent: %s",
  433. icarus->device_id, ob_hex);
  434. free(ob_hex);
  435. }
  436. }
  437. /* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
  438. memset(nonce_bin, 0, sizeof(nonce_bin));
  439. info = icarus_info[icarus->device_id];
  440. ret = icarus_gets(nonce_bin, fd, &tv_finish, thr, info->read_count);
  441. work->blk.nonce = 0xffffffff;
  442. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  443. // aborted before becoming idle, get new work
  444. if (nonce == 0 && ret) {
  445. timersub(&tv_finish, &tv_start, &elapsed);
  446. // ONLY up to just when it aborted
  447. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  448. estimate_hashes = ((double)(elapsed.tv_sec)
  449. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  450. // If some Serial-USB delay allowed the full nonce range to
  451. // complete it can't have done more than a full nonce
  452. if (unlikely(estimate_hashes > 0xffffffff))
  453. estimate_hashes = 0xffffffff;
  454. if (opt_debug) {
  455. applog(LOG_DEBUG, "Icarus %d no nonce = 0x%08llx hashes (%ld.%06lds)",
  456. icarus->device_id, estimate_hashes,
  457. elapsed.tv_sec, elapsed.tv_usec);
  458. }
  459. return estimate_hashes;
  460. }
  461. #if !defined (__BIG_ENDIAN__) && !defined(MIPSEB)
  462. nonce = swab32(nonce);
  463. #endif
  464. submit_nonce(thr, work, nonce);
  465. hash_count = (nonce & 0x7fffffff);
  466. if (hash_count++ == 0x7fffffff)
  467. hash_count = 0xffffffff;
  468. else
  469. hash_count <<= 1;
  470. if (opt_debug || info->do_icarus_timing)
  471. timersub(&tv_finish, &tv_start, &elapsed);
  472. if (opt_debug) {
  473. applog(LOG_DEBUG, "Icarus %d nonce = 0x%08x = 0x%08llx hashes (%ld.%06lds)",
  474. icarus->device_id, nonce, hash_count, elapsed.tv_sec, elapsed.tv_usec);
  475. }
  476. // ignore possible end condition values
  477. if (info->do_icarus_timing && (nonce & 0x7fffffff) > 0x000fffff && (nonce & 0x7fffffff) < 0x7ff00000) {
  478. gettimeofday(&tv_history_start, NULL);
  479. history0 = &(info->history[0]);
  480. if (history0->values == 0)
  481. timeradd(&tv_start, &history_sec, &(history0->finish));
  482. Ti = (double)(elapsed.tv_sec)
  483. + ((double)(elapsed.tv_usec))/((double)1000000)
  484. - ICARUS_READ_TIME;
  485. Xi = (double)hash_count;
  486. history0->sumXiTi += Xi * Ti;
  487. history0->sumXi += Xi;
  488. history0->sumTi += Ti;
  489. history0->sumXi2 += Xi * Xi;
  490. history0->values++;
  491. if (history0->hash_count_max < hash_count)
  492. history0->hash_count_max = hash_count;
  493. if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
  494. history0->hash_count_min = hash_count;
  495. if (history0->values >= info->min_data_count
  496. && timercmp(&tv_start, &(history0->finish), >)) {
  497. for (i = INFO_HISTORY; i > 0; i--)
  498. memcpy(&(info->history[i]),
  499. &(info->history[i-1]),
  500. sizeof(struct ICARUS_HISTORY));
  501. // Initialise history0 to zero for summary calculation
  502. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  503. // We just completed a history data set
  504. // So now recalc read_count based on the whole history thus we will
  505. // initially get more accurate until it completes INFO_HISTORY
  506. // total data sets
  507. count = 0;
  508. for (i = 1 ; i <= INFO_HISTORY; i++) {
  509. history = &(info->history[i]);
  510. if (history->values >= MIN_DATA_COUNT) {
  511. count++;
  512. history0->sumXiTi += history->sumXiTi;
  513. history0->sumXi += history->sumXi;
  514. history0->sumTi += history->sumTi;
  515. history0->sumXi2 += history->sumXi2;
  516. history0->values += history->values;
  517. if (history0->hash_count_max < history->hash_count_max)
  518. history0->hash_count_max = history->hash_count_max;
  519. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  520. history0->hash_count_min = history->hash_count_min;
  521. }
  522. }
  523. // All history data
  524. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  525. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  526. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  527. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  528. values = history0->values;
  529. // Initialise history0 to zero for next data set
  530. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  531. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  532. read_count = (int)(fullnonce * TIME_FACTOR) - 1;
  533. info->Hs = Hs;
  534. info->read_count = read_count;
  535. info->fullnonce = fullnonce;
  536. info->count = count;
  537. info->W = W;
  538. info->values = values;
  539. info->hash_count_range = hash_count_range;
  540. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  541. info->min_data_count *= 2;
  542. else if (info->timing_mode == MODE_SHORT)
  543. info->do_icarus_timing = false;
  544. // 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);
  545. applog(LOG_WARNING, "Icarus %d Re-estimate: Hs=%e W=%e read_count=%d fullnonce=%.3fs",
  546. icarus->device_id, Hs, W, read_count, fullnonce);
  547. }
  548. info->history_count++;
  549. gettimeofday(&tv_history_finish, NULL);
  550. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  551. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  552. }
  553. return hash_count;
  554. }
  555. static struct api_data *icarus_api_stats(struct cgpu_info *cgpu)
  556. {
  557. struct api_data *root = NULL;
  558. struct ICARUS_INFO *info = icarus_info[cgpu->device_id];
  559. // Warning, access to these is not locked - but we don't really
  560. // care since hashing performance is way more important than
  561. // locking access to displaying API debug 'stats'
  562. // If locking becomes an issue for any of them, use copy_data=true also
  563. root = api_add_int(root, "read_count", &(info->read_count), false);
  564. root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
  565. root = api_add_int(root, "count", &(info->count), false);
  566. root = api_add_hs(root, "Hs", &(info->Hs), false);
  567. root = api_add_double(root, "W", &(info->W), false);
  568. root = api_add_uint(root, "total_values", &(info->values), false);
  569. root = api_add_uint64(root, "range", &(info->hash_count_range), false);
  570. root = api_add_uint64(root, "history_count", &(info->history_count), false);
  571. root = api_add_timeval(root, "history_time", &(info->history_time), false);
  572. root = api_add_uint(root, "min_data_count", &(info->min_data_count), false);
  573. root = api_add_uint(root, "timing_values", &(info->history[0].values), false);
  574. root = api_add_const(root, "timing_mode", timing_mode_str(info->timing_mode), false);
  575. root = api_add_bool(root, "is_timing", &(info->do_icarus_timing), false);
  576. return root;
  577. }
  578. static void icarus_shutdown(struct thr_info *thr)
  579. {
  580. struct cgpu_info *icarus = thr->cgpu;
  581. icarus_close(icarus->device_fd);
  582. }
  583. struct device_api icarus_api = {
  584. .dname = "icarus",
  585. .name = "ICA",
  586. .api_detect = icarus_detect,
  587. .get_api_stats = icarus_api_stats,
  588. .thread_prepare = icarus_prepare,
  589. .scanhash = icarus_scanhash,
  590. .thread_shutdown = icarus_shutdown,
  591. };