deviceapi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Copyright 2011-2013 Luke Dashjr
  3. * Copyright 2011-2012 Con Kolivas
  4. * Copyright 2012-2013 Andrew Smith
  5. * Copyright 2010 Jeff Garzik
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 3 of the License, or (at your option)
  10. * any later version. See COPYING for more details.
  11. */
  12. #include "config.h"
  13. #ifdef WIN32
  14. #include <winsock2.h>
  15. #else
  16. #include <sys/select.h>
  17. #endif
  18. #include <stdbool.h>
  19. #include <stdint.h>
  20. #include <sys/time.h>
  21. #include <sys/types.h>
  22. #include <time.h>
  23. #include <unistd.h>
  24. #include "compat.h"
  25. #include "deviceapi.h"
  26. #include "logging.h"
  27. #include "miner.h"
  28. #include "util.h"
  29. bool hashes_done(struct thr_info *thr, int64_t hashes, struct timeval *tvp_hashes, uint32_t *max_nonce)
  30. {
  31. struct cgpu_info *cgpu = thr->cgpu;
  32. const long cycle = opt_log_interval / 5 ? : 1;
  33. if (unlikely(hashes == -1)) {
  34. time_t now = time(NULL);
  35. if (difftime(now, cgpu->device_last_not_well) > 1.)
  36. dev_error(cgpu, REASON_THREAD_ZERO_HASH);
  37. if (thr->scanhash_working && opt_restart) {
  38. applog(LOG_ERR, "%"PRIpreprv" failure, attempting to reinitialize", cgpu->proc_repr);
  39. thr->scanhash_working = false;
  40. cgpu->reinit_backoff = 5.2734375;
  41. hashes = 0;
  42. } else {
  43. applog(LOG_ERR, "%"PRIpreprv" failure, disabling!", cgpu->proc_repr);
  44. cgpu->deven = DEV_RECOVER_ERR;
  45. return false;
  46. }
  47. }
  48. else
  49. thr->scanhash_working = true;
  50. thr->hashes_done += hashes;
  51. if (hashes > cgpu->max_hashes)
  52. cgpu->max_hashes = hashes;
  53. timeradd(&thr->tv_hashes_done, tvp_hashes, &thr->tv_hashes_done);
  54. // max_nonce management (optional)
  55. if (unlikely((long)thr->tv_hashes_done.tv_sec < cycle)) {
  56. int mult;
  57. if (likely(!max_nonce || *max_nonce == 0xffffffff))
  58. return true;
  59. mult = 1000000 / ((thr->tv_hashes_done.tv_usec + 0x400) / 0x400) + 0x10;
  60. mult *= cycle;
  61. if (*max_nonce > (0xffffffff * 0x400) / mult)
  62. *max_nonce = 0xffffffff;
  63. else
  64. *max_nonce = (*max_nonce * mult) / 0x400;
  65. } else if (unlikely(thr->tv_hashes_done.tv_sec > cycle) && max_nonce)
  66. *max_nonce = *max_nonce * cycle / thr->tv_hashes_done.tv_sec;
  67. else if (unlikely(thr->tv_hashes_done.tv_usec > 100000) && max_nonce)
  68. *max_nonce = *max_nonce * 0x400 / (((cycle * 1000000) + thr->tv_hashes_done.tv_usec) / (cycle * 1000000 / 0x400));
  69. hashmeter2(thr);
  70. return true;
  71. }
  72. /* A generic wait function for threads that poll that will wait a specified
  73. * time tdiff waiting on a work restart request. Returns zero if the condition
  74. * was met (work restart requested) or ETIMEDOUT if not.
  75. */
  76. int restart_wait(struct thr_info *thr, unsigned int mstime)
  77. {
  78. struct timeval tv_timer, tv_now, tv_timeout;
  79. fd_set rfds;
  80. SOCKETTYPE wrn = thr->work_restart_notifier[0];
  81. int rv;
  82. if (unlikely(thr->work_restart_notifier[1] == INVSOCK))
  83. {
  84. // This is a bug!
  85. applog(LOG_ERR, "%"PRIpreprv": restart_wait called without a work_restart_notifier", thr->cgpu->proc_repr);
  86. nmsleep(mstime);
  87. return (thr->work_restart ? 0 : ETIMEDOUT);
  88. }
  89. gettimeofday(&tv_now, NULL);
  90. timer_set_delay(&tv_timer, &tv_now, mstime * 1000);
  91. while (true)
  92. {
  93. FD_ZERO(&rfds);
  94. FD_SET(wrn, &rfds);
  95. tv_timeout = tv_timer;
  96. rv = select(wrn + 1, &rfds, NULL, NULL, select_timeout(&tv_timeout, &tv_now));
  97. if (rv == 0)
  98. return ETIMEDOUT;
  99. if (rv > 0)
  100. {
  101. if (thr->work_restart)
  102. return 0;
  103. notifier_read(thr->work_restart_notifier);
  104. }
  105. gettimeofday(&tv_now, NULL);
  106. }
  107. }
  108. // Miner loop to manage a single processor (with possibly multiple threads per processor)
  109. void minerloop_scanhash(struct thr_info *mythr)
  110. {
  111. const int thr_id = mythr->id;
  112. struct cgpu_info *cgpu = mythr->cgpu;
  113. const struct device_api *api = cgpu->api;
  114. struct timeval tv_start, tv_end;
  115. struct timeval tv_hashes, tv_worktime;
  116. uint32_t max_nonce = api->can_limit_work ? api->can_limit_work(mythr) : 0xffffffff;
  117. int64_t hashes;
  118. struct work *work;
  119. const bool primary = (!mythr->device_thread) || mythr->primary_thread;
  120. while (1) {
  121. mythr->work_restart = false;
  122. request_work(mythr);
  123. work = get_work(mythr);
  124. if (api->prepare_work && !api->prepare_work(mythr, work)) {
  125. applog(LOG_ERR, "work prepare failed, exiting "
  126. "mining thread %d", thr_id);
  127. break;
  128. }
  129. gettimeofday(&(work->tv_work_start), NULL);
  130. do {
  131. thread_reportin(mythr);
  132. gettimeofday(&tv_start, NULL);
  133. hashes = api->scanhash(mythr, work, work->blk.nonce + max_nonce);
  134. gettimeofday(&tv_end, NULL);
  135. thread_reportin(mythr);
  136. timersub(&tv_end, &tv_start, &tv_hashes);
  137. if (!hashes_done(mythr, hashes, &tv_hashes, api->can_limit_work ? &max_nonce : NULL))
  138. goto disabled;
  139. if (unlikely(mythr->work_restart)) {
  140. /* Apart from device_thread 0, we stagger the
  141. * starting of every next thread to try and get
  142. * all devices busy before worrying about
  143. * getting work for their extra threads */
  144. if (!primary) {
  145. struct timespec rgtp;
  146. rgtp.tv_sec = 0;
  147. rgtp.tv_nsec = 250 * mythr->device_thread * 1000000;
  148. nanosleep(&rgtp, NULL);
  149. }
  150. break;
  151. }
  152. if (unlikely(mythr->pause || cgpu->deven != DEV_ENABLED))
  153. disabled:
  154. mt_disable(mythr);
  155. timersub(&tv_end, &work->tv_work_start, &tv_worktime);
  156. } while (!abandon_work(work, &tv_worktime, cgpu->max_hashes));
  157. free_work(work);
  158. }
  159. }
  160. bool do_job_prepare(struct thr_info *mythr, struct timeval *tvp_now)
  161. {
  162. struct cgpu_info *proc = mythr->cgpu;
  163. const struct device_api *api = proc->api;
  164. struct timeval tv_worktime;
  165. mythr->tv_morework.tv_sec = -1;
  166. mythr->_job_transition_in_progress = true;
  167. if (mythr->work)
  168. timersub(tvp_now, &mythr->work->tv_work_start, &tv_worktime);
  169. if ((!mythr->work) || abandon_work(mythr->work, &tv_worktime, proc->max_hashes))
  170. {
  171. mythr->work_restart = false;
  172. request_work(mythr);
  173. // FIXME: Allow get_work to return NULL to retry on notification
  174. mythr->next_work = get_work(mythr);
  175. if (api->prepare_work && !api->prepare_work(mythr, mythr->next_work)) {
  176. applog(LOG_ERR, "%"PRIpreprv": Work prepare failed, disabling!", proc->proc_repr);
  177. proc->deven = DEV_RECOVER_ERR;
  178. return false;
  179. }
  180. mythr->starting_next_work = true;
  181. api->job_prepare(mythr, mythr->next_work, mythr->_max_nonce);
  182. }
  183. else
  184. {
  185. mythr->starting_next_work = false;
  186. api->job_prepare(mythr, mythr->work, mythr->_max_nonce);
  187. }
  188. job_prepare_complete(mythr);
  189. return true;
  190. }
  191. void job_prepare_complete(struct thr_info *mythr)
  192. {
  193. if (mythr->work)
  194. {
  195. if (true /* TODO: job is near complete */ || unlikely(mythr->work_restart))
  196. do_get_results(mythr, true);
  197. else
  198. {} // TODO: Set a timer to call do_get_results when job is near complete
  199. }
  200. else // no job currently running
  201. do_job_start(mythr);
  202. }
  203. void do_get_results(struct thr_info *mythr, bool proceed_with_new_job)
  204. {
  205. struct cgpu_info *proc = mythr->cgpu;
  206. const struct device_api *api = proc->api;
  207. struct work *work = mythr->work;
  208. mythr->_job_transition_in_progress = true;
  209. mythr->tv_results_jobstart = mythr->tv_jobstart;
  210. mythr->_proceed_with_new_job = proceed_with_new_job;
  211. if (api->job_get_results)
  212. api->job_get_results(mythr, work);
  213. else
  214. job_results_fetched(mythr);
  215. }
  216. void job_results_fetched(struct thr_info *mythr)
  217. {
  218. if (mythr->_proceed_with_new_job)
  219. do_job_start(mythr);
  220. else
  221. {
  222. struct timeval tv_now;
  223. gettimeofday(&tv_now, NULL);
  224. do_process_results(mythr, &tv_now, mythr->prev_work, true);
  225. }
  226. }
  227. void do_job_start(struct thr_info *mythr)
  228. {
  229. struct cgpu_info *proc = mythr->cgpu;
  230. const struct device_api *api = proc->api;
  231. thread_reportin(mythr);
  232. api->job_start(mythr);
  233. }
  234. void mt_job_transition(struct thr_info *mythr)
  235. {
  236. struct timeval tv_now;
  237. gettimeofday(&tv_now, NULL);
  238. if (mythr->starting_next_work)
  239. {
  240. mythr->next_work->tv_work_start = tv_now;
  241. if (mythr->prev_work)
  242. free_work(mythr->prev_work);
  243. mythr->prev_work = mythr->work;
  244. mythr->work = mythr->next_work;
  245. mythr->next_work = NULL;
  246. }
  247. mythr->tv_jobstart = tv_now;
  248. mythr->_job_transition_in_progress = false;
  249. }
  250. void job_start_complete(struct thr_info *mythr)
  251. {
  252. struct timeval tv_now;
  253. gettimeofday(&tv_now, NULL);
  254. do_process_results(mythr, &tv_now, mythr->prev_work, false);
  255. }
  256. void job_start_abort(struct thr_info *mythr, bool failure)
  257. {
  258. struct cgpu_info *proc = mythr->cgpu;
  259. if (failure)
  260. proc->deven = DEV_RECOVER_ERR;
  261. mythr->work = NULL;
  262. mythr->_job_transition_in_progress = false;
  263. }
  264. bool do_process_results(struct thr_info *mythr, struct timeval *tvp_now, struct work *work, bool stopping)
  265. {
  266. struct cgpu_info *proc = mythr->cgpu;
  267. const struct device_api *api = proc->api;
  268. struct timeval tv_hashes;
  269. int64_t hashes = 0;
  270. if (api->job_process_results)
  271. hashes = api->job_process_results(mythr, work, stopping);
  272. thread_reportin(mythr);
  273. if (hashes)
  274. {
  275. timersub(tvp_now, &mythr->tv_results_jobstart, &tv_hashes);
  276. if (!hashes_done(mythr, hashes, &tv_hashes, api->can_limit_work ? &mythr->_max_nonce : NULL))
  277. return false;
  278. }
  279. return true;
  280. }
  281. void minerloop_async(struct thr_info *mythr)
  282. {
  283. struct cgpu_info *cgpu = mythr->cgpu;
  284. const struct device_api *api = cgpu->api;
  285. struct timeval tv_now;
  286. struct timeval tv_timeout;
  287. struct cgpu_info *proc;
  288. int maxfd;
  289. fd_set rfds;
  290. bool is_running, should_be_running;
  291. if (mythr->work_restart_notifier[1] == -1)
  292. notifier_init(mythr->work_restart_notifier);
  293. while (1) {
  294. tv_timeout.tv_sec = -1;
  295. gettimeofday(&tv_now, NULL);
  296. for (proc = cgpu; proc; proc = proc->next_proc)
  297. {
  298. mythr = proc->thr[0];
  299. is_running = mythr->work;
  300. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  301. if (should_be_running)
  302. {
  303. if (unlikely(!(is_running || mythr->_job_transition_in_progress)))
  304. {
  305. mt_disable_finish(mythr);
  306. goto djp;
  307. }
  308. if (unlikely(mythr->work_restart))
  309. goto djp;
  310. }
  311. else // ! should_be_running
  312. {
  313. if (unlikely(is_running && !mythr->_job_transition_in_progress))
  314. {
  315. disabled: ;
  316. mythr->tv_morework.tv_sec = -1;
  317. do_get_results(mythr, false);
  318. }
  319. }
  320. if (timer_passed(&mythr->tv_morework, &tv_now))
  321. {
  322. djp: ;
  323. if (!do_job_prepare(mythr, &tv_now))
  324. goto disabled;
  325. }
  326. if (timer_passed(&mythr->tv_poll, &tv_now))
  327. api->poll(mythr);
  328. reduce_timeout_to(&tv_timeout, &mythr->tv_morework);
  329. reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
  330. }
  331. gettimeofday(&tv_now, NULL);
  332. FD_ZERO(&rfds);
  333. FD_SET(mythr->notifier[0], &rfds);
  334. maxfd = mythr->notifier[0];
  335. FD_SET(mythr->work_restart_notifier[0], &rfds);
  336. set_maxfd(&maxfd, mythr->work_restart_notifier[0]);
  337. if (select(maxfd + 1, &rfds, NULL, NULL, select_timeout(&tv_timeout, &tv_now)) < 0)
  338. continue;
  339. if (FD_ISSET(mythr->notifier[0], &rfds)) {
  340. notifier_read(mythr->notifier);
  341. }
  342. if (FD_ISSET(mythr->work_restart_notifier[0], &rfds))
  343. notifier_read(mythr->work_restart_notifier);
  344. }
  345. }
  346. void *miner_thread(void *userdata)
  347. {
  348. struct thr_info *mythr = userdata;
  349. const int thr_id = mythr->id;
  350. struct cgpu_info *cgpu = mythr->cgpu;
  351. const struct device_api *api = cgpu->api;
  352. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  353. char threadname[20];
  354. snprintf(threadname, 20, "miner_%s", cgpu->proc_repr_ns);
  355. RenameThread(threadname);
  356. if (api->thread_init && !api->thread_init(mythr)) {
  357. dev_error(cgpu, REASON_THREAD_FAIL_INIT);
  358. for (struct cgpu_info *slave = cgpu->next_proc; slave && !slave->threads; slave = slave->next_proc)
  359. dev_error(slave, REASON_THREAD_FAIL_INIT);
  360. goto out;
  361. }
  362. thread_reportout(mythr);
  363. applog(LOG_DEBUG, "Popping ping in miner thread");
  364. notifier_read(mythr->notifier); // Wait for a notification to start
  365. if (api->minerloop)
  366. api->minerloop(mythr);
  367. else
  368. minerloop_scanhash(mythr);
  369. out:
  370. if (api->thread_shutdown)
  371. api->thread_shutdown(mythr);
  372. thread_reportin(mythr);
  373. applog(LOG_ERR, "Thread %d failure, exiting", thr_id);
  374. notifier_destroy(mythr->notifier);
  375. return NULL;
  376. }
  377. bool add_cgpu(struct cgpu_info*cgpu)
  378. {
  379. int lpcount;
  380. renumber_cgpu(cgpu);
  381. if (!cgpu->procs)
  382. cgpu->procs = 1;
  383. lpcount = cgpu->procs;
  384. cgpu->device = cgpu;
  385. cgpu->dev_repr = malloc(6);
  386. sprintf(cgpu->dev_repr, "%s%2u", cgpu->api->name, cgpu->device_id % 100);
  387. cgpu->dev_repr_ns = malloc(6);
  388. sprintf(cgpu->dev_repr_ns, "%s%u", cgpu->api->name, cgpu->device_id % 100);
  389. strcpy(cgpu->proc_repr, cgpu->dev_repr);
  390. sprintf(cgpu->proc_repr_ns, "%s%u", cgpu->api->name, cgpu->device_id);
  391. devices = realloc(devices, sizeof(struct cgpu_info *) * (total_devices + lpcount + 1));
  392. devices[total_devices++] = cgpu;
  393. if (lpcount > 1)
  394. {
  395. int ns;
  396. int tpp = cgpu->threads / lpcount;
  397. struct cgpu_info **nlp_p, *slave;
  398. // Note, strcpy instead of assigning a byte to get the \0 too
  399. strcpy(&cgpu->proc_repr[5], "a");
  400. ns = strlen(cgpu->proc_repr_ns);
  401. strcpy(&cgpu->proc_repr_ns[ns], "a");
  402. nlp_p = &cgpu->next_proc;
  403. for (int i = 1; i < lpcount; ++i)
  404. {
  405. slave = malloc(sizeof(*slave));
  406. *slave = *cgpu;
  407. slave->proc_id = i;
  408. slave->proc_repr[5] += i;
  409. slave->proc_repr_ns[ns] += i;
  410. slave->threads = tpp;
  411. devices[total_devices++] = slave;
  412. *nlp_p = slave;
  413. nlp_p = &slave->next_proc;
  414. }
  415. *nlp_p = NULL;
  416. cgpu->proc_id = 0;
  417. cgpu->threads -= (tpp * (lpcount - 1));
  418. }
  419. return true;
  420. }