driver-icarus.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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[MAX_DEVICES];
  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, int thr_id, 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_id >= 0 && work_restart[thr_id].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. fd = icarus_open2(devpath, true);
  319. if (unlikely(fd == -1)) {
  320. applog(LOG_ERR, "Icarus Detect: Failed to open %s", devpath);
  321. return false;
  322. }
  323. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  324. icarus_write(fd, ob_bin, sizeof(ob_bin));
  325. gettimeofday(&tv_start, NULL);
  326. memset(nonce_bin, 0, sizeof(nonce_bin));
  327. icarus_gets(nonce_bin, fd, &tv_finish, -1, 1);
  328. icarus_close(fd);
  329. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  330. if (nonce_hex) {
  331. if (strncmp(nonce_hex, golden_nonce, 8)) {
  332. applog(LOG_ERR,
  333. "Icarus Detect: "
  334. "Test failed at %s: get %s, should: %s",
  335. devpath, nonce_hex, golden_nonce);
  336. free(nonce_hex);
  337. return false;
  338. }
  339. applog(LOG_DEBUG,
  340. "Icarus Detect: "
  341. "Test succeeded at %s: got %s",
  342. devpath, nonce_hex);
  343. free(nonce_hex);
  344. } else
  345. return false;
  346. /* We have a real Icarus! */
  347. struct cgpu_info *icarus;
  348. icarus = calloc(1, sizeof(struct cgpu_info));
  349. icarus->api = &icarus_api;
  350. icarus->device_path = strdup(devpath);
  351. icarus->threads = 1;
  352. add_cgpu(icarus);
  353. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  354. devpath, icarus->device_id);
  355. if (icarus_info[icarus->device_id] == NULL) {
  356. icarus_info[icarus->device_id] = (struct ICARUS_INFO *)malloc(sizeof(struct ICARUS_INFO));
  357. if (unlikely(!(icarus_info[icarus->device_id])))
  358. quit(1, "Failed to malloc ICARUS_INFO");
  359. }
  360. info = icarus_info[icarus->device_id];
  361. // Initialise everything to zero for a new device
  362. memset(info, 0, sizeof(struct ICARUS_INFO));
  363. info->golden_hashes = (golden_nonce_val & 0x7fffffff) << 1;
  364. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  365. set_timing_mode(icarus);
  366. return true;
  367. }
  368. static void icarus_detect()
  369. {
  370. serial_detect("icarus", icarus_detect_one);
  371. }
  372. static bool icarus_prepare(struct thr_info *thr)
  373. {
  374. struct cgpu_info *icarus = thr->cgpu;
  375. struct timeval now;
  376. int fd = icarus_open(icarus->device_path);
  377. if (unlikely(-1 == fd)) {
  378. applog(LOG_ERR, "Failed to open Icarus on %s",
  379. icarus->device_path);
  380. return false;
  381. }
  382. icarus->device_fd = fd;
  383. applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
  384. gettimeofday(&now, NULL);
  385. get_datestamp(icarus->init, &now);
  386. return true;
  387. }
  388. static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  389. __maybe_unused uint64_t max_nonce)
  390. {
  391. const int thr_id = thr->id;
  392. struct cgpu_info *icarus;
  393. int fd;
  394. int ret;
  395. struct ICARUS_INFO *info;
  396. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  397. char *ob_hex;
  398. uint32_t nonce;
  399. uint64_t hash_count;
  400. struct timeval tv_start, tv_finish, elapsed;
  401. struct timeval tv_history_start, tv_history_finish;
  402. double Ti, Xi;
  403. int i;
  404. struct ICARUS_HISTORY *history0, *history;
  405. int count;
  406. double Hs, W, fullnonce;
  407. int read_count;
  408. uint64_t estimate_hashes;
  409. uint32_t values;
  410. uint64_t hash_count_range;
  411. elapsed.tv_sec = elapsed.tv_usec = 0;
  412. icarus = thr->cgpu;
  413. fd = icarus->device_fd;
  414. memset(ob_bin, 0, sizeof(ob_bin));
  415. memcpy(ob_bin, work->midstate, 32);
  416. memcpy(ob_bin + 52, work->data + 64, 12);
  417. rev(ob_bin, 32);
  418. rev(ob_bin + 52, 12);
  419. #ifndef WIN32
  420. tcflush(fd, TCOFLUSH);
  421. #endif
  422. ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
  423. if (ret)
  424. return 0; /* This should never happen */
  425. gettimeofday(&tv_start, NULL);
  426. if (opt_debug) {
  427. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  428. if (ob_hex) {
  429. applog(LOG_DEBUG, "Icarus %d sent: %s",
  430. icarus->device_id, ob_hex);
  431. free(ob_hex);
  432. }
  433. }
  434. /* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
  435. memset(nonce_bin, 0, sizeof(nonce_bin));
  436. info = icarus_info[icarus->device_id];
  437. ret = icarus_gets(nonce_bin, fd, &tv_finish, thr_id, info->read_count);
  438. work->blk.nonce = 0xffffffff;
  439. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  440. // aborted before becoming idle, get new work
  441. if (nonce == 0 && ret) {
  442. timersub(&tv_finish, &tv_start, &elapsed);
  443. // ONLY up to just when it aborted
  444. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  445. estimate_hashes = ((double)(elapsed.tv_sec)
  446. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  447. // If some Serial-USB delay allowed the full nonce range to
  448. // complete it can't have done more than a full nonce
  449. if (unlikely(estimate_hashes > 0xffffffff))
  450. estimate_hashes = 0xffffffff;
  451. if (opt_debug) {
  452. applog(LOG_DEBUG, "Icarus %d no nonce = 0x%08llx hashes (%ld.%06lds)",
  453. icarus->device_id, estimate_hashes,
  454. elapsed.tv_sec, elapsed.tv_usec);
  455. }
  456. return estimate_hashes;
  457. }
  458. #if !defined (__BIG_ENDIAN__) && !defined(MIPSEB)
  459. nonce = swab32(nonce);
  460. #endif
  461. submit_nonce(thr, work, nonce);
  462. hash_count = (nonce & 0x7fffffff);
  463. if (hash_count++ == 0x7fffffff)
  464. hash_count = 0xffffffff;
  465. else
  466. hash_count <<= 1;
  467. if (opt_debug || info->do_icarus_timing)
  468. timersub(&tv_finish, &tv_start, &elapsed);
  469. if (opt_debug) {
  470. applog(LOG_DEBUG, "Icarus %d nonce = 0x%08x = 0x%08llx hashes (%ld.%06lds)",
  471. icarus->device_id, nonce, hash_count, elapsed.tv_sec, elapsed.tv_usec);
  472. }
  473. // ignore possible end condition values
  474. if (info->do_icarus_timing && (nonce & 0x7fffffff) > 0x000fffff && (nonce & 0x7fffffff) < 0x7ff00000) {
  475. gettimeofday(&tv_history_start, NULL);
  476. history0 = &(info->history[0]);
  477. if (history0->values == 0)
  478. timeradd(&tv_start, &history_sec, &(history0->finish));
  479. Ti = (double)(elapsed.tv_sec)
  480. + ((double)(elapsed.tv_usec))/((double)1000000)
  481. - ICARUS_READ_TIME;
  482. Xi = (double)hash_count;
  483. history0->sumXiTi += Xi * Ti;
  484. history0->sumXi += Xi;
  485. history0->sumTi += Ti;
  486. history0->sumXi2 += Xi * Xi;
  487. history0->values++;
  488. if (history0->hash_count_max < hash_count)
  489. history0->hash_count_max = hash_count;
  490. if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
  491. history0->hash_count_min = hash_count;
  492. if (history0->values >= info->min_data_count
  493. && timercmp(&tv_start, &(history0->finish), >)) {
  494. for (i = INFO_HISTORY; i > 0; i--)
  495. memcpy(&(info->history[i]),
  496. &(info->history[i-1]),
  497. sizeof(struct ICARUS_HISTORY));
  498. // Initialise history0 to zero for summary calculation
  499. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  500. // We just completed a history data set
  501. // So now recalc read_count based on the whole history thus we will
  502. // initially get more accurate until it completes INFO_HISTORY
  503. // total data sets
  504. count = 0;
  505. for (i = 1 ; i <= INFO_HISTORY; i++) {
  506. history = &(info->history[i]);
  507. if (history->values >= MIN_DATA_COUNT) {
  508. count++;
  509. history0->sumXiTi += history->sumXiTi;
  510. history0->sumXi += history->sumXi;
  511. history0->sumTi += history->sumTi;
  512. history0->sumXi2 += history->sumXi2;
  513. history0->values += history->values;
  514. if (history0->hash_count_max < history->hash_count_max)
  515. history0->hash_count_max = history->hash_count_max;
  516. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  517. history0->hash_count_min = history->hash_count_min;
  518. }
  519. }
  520. // All history data
  521. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  522. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  523. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  524. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  525. values = history0->values;
  526. // Initialise history0 to zero for next data set
  527. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  528. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  529. read_count = (int)(fullnonce * TIME_FACTOR) - 1;
  530. info->Hs = Hs;
  531. info->read_count = read_count;
  532. info->fullnonce = fullnonce;
  533. info->count = count;
  534. info->W = W;
  535. info->values = values;
  536. info->hash_count_range = hash_count_range;
  537. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  538. info->min_data_count *= 2;
  539. else if (info->timing_mode == MODE_SHORT)
  540. info->do_icarus_timing = false;
  541. // 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);
  542. applog(LOG_WARNING, "Icarus %d Re-estimate: Hs=%e W=%e read_count=%d fullnonce=%.3fs",
  543. icarus->device_id, Hs, W, read_count, fullnonce);
  544. }
  545. info->history_count++;
  546. gettimeofday(&tv_history_finish, NULL);
  547. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  548. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  549. }
  550. return hash_count;
  551. }
  552. static void icarus_api_stats(char *buf, struct cgpu_info *cgpu, bool isjson)
  553. {
  554. struct ICARUS_INFO *info = icarus_info[cgpu->device_id];
  555. // Warning, access to these is not locked - but we don't really
  556. // care since hashing performance is way more important than
  557. // locking access to displaying API debug 'stats'
  558. sprintf(buf, isjson
  559. ? "\"read_count\":%d,\"fullnonce\":%f,\"count\":%d,\"Hs\":%.15f,\"W\":%f,\"total_values\":%u,\"range\":%"PRIu64",\"history_count\":%"PRIu64",\"history_time\":%f,\"min_data_count\":%u,\"timing_values\":%u"
  560. : "read_count=%d,fullnonce=%f,count=%d,Hs=%.15f,W=%f,total_values=%u,range=%"PRIu64",history_count=%"PRIu64",history_time=%f,min_data_count=%u,timing_values=%u",
  561. info->read_count, info->fullnonce,
  562. info->count, info->Hs, info->W,
  563. info->values, info->hash_count_range,
  564. info->history_count,
  565. (double)(info->history_time.tv_sec)
  566. + ((double)(info->history_time.tv_usec))/((double)1000000),
  567. info->min_data_count, info->history[0].values);
  568. }
  569. static void icarus_shutdown(struct thr_info *thr)
  570. {
  571. struct cgpu_info *icarus = thr->cgpu;
  572. icarus_close(icarus->device_fd);
  573. }
  574. struct device_api icarus_api = {
  575. .dname = "icarus",
  576. .name = "ICA",
  577. .api_detect = icarus_detect,
  578. .get_api_stats = icarus_api_stats,
  579. .thread_prepare = icarus_prepare,
  580. .scanhash = icarus_scanhash,
  581. .thread_shutdown = icarus_shutdown,
  582. };