deviceapi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. static
  109. struct work *get_and_prepare_work(struct thr_info *thr)
  110. {
  111. struct cgpu_info *proc = thr->cgpu;
  112. const struct device_api *api = proc->api;
  113. struct work *work;
  114. work = get_work(thr);
  115. if (!work)
  116. return NULL;
  117. if (api->prepare_work && !api->prepare_work(thr, work)) {
  118. free_work(work);
  119. applog(LOG_ERR, "%"PRIpreprv": Work prepare failed, disabling!", proc->proc_repr);
  120. proc->deven = DEV_RECOVER_ERR;
  121. return NULL;
  122. }
  123. return work;
  124. }
  125. // Miner loop to manage a single processor (with possibly multiple threads per processor)
  126. void minerloop_scanhash(struct thr_info *mythr)
  127. {
  128. struct cgpu_info *cgpu = mythr->cgpu;
  129. const struct device_api *api = cgpu->api;
  130. struct timeval tv_start, tv_end;
  131. struct timeval tv_hashes, tv_worktime;
  132. uint32_t max_nonce = api->can_limit_work ? api->can_limit_work(mythr) : 0xffffffff;
  133. int64_t hashes;
  134. struct work *work;
  135. const bool primary = (!mythr->device_thread) || mythr->primary_thread;
  136. while (1) {
  137. mythr->work_restart = false;
  138. request_work(mythr);
  139. work = get_and_prepare_work(mythr);
  140. if (!work)
  141. break;
  142. gettimeofday(&(work->tv_work_start), NULL);
  143. do {
  144. thread_reportin(mythr);
  145. gettimeofday(&tv_start, NULL);
  146. hashes = api->scanhash(mythr, work, work->blk.nonce + max_nonce);
  147. gettimeofday(&tv_end, NULL);
  148. thread_reportin(mythr);
  149. timersub(&tv_end, &tv_start, &tv_hashes);
  150. if (!hashes_done(mythr, hashes, &tv_hashes, api->can_limit_work ? &max_nonce : NULL))
  151. goto disabled;
  152. if (unlikely(mythr->work_restart)) {
  153. /* Apart from device_thread 0, we stagger the
  154. * starting of every next thread to try and get
  155. * all devices busy before worrying about
  156. * getting work for their extra threads */
  157. if (!primary) {
  158. struct timespec rgtp;
  159. rgtp.tv_sec = 0;
  160. rgtp.tv_nsec = 250 * mythr->device_thread * 1000000;
  161. nanosleep(&rgtp, NULL);
  162. }
  163. break;
  164. }
  165. if (unlikely(mythr->pause || cgpu->deven != DEV_ENABLED))
  166. disabled:
  167. mt_disable(mythr);
  168. timersub(&tv_end, &work->tv_work_start, &tv_worktime);
  169. } while (!abandon_work(work, &tv_worktime, cgpu->max_hashes));
  170. free_work(work);
  171. }
  172. }
  173. bool do_job_prepare(struct thr_info *mythr, struct timeval *tvp_now)
  174. {
  175. struct cgpu_info *proc = mythr->cgpu;
  176. const struct device_api *api = proc->api;
  177. struct timeval tv_worktime;
  178. mythr->tv_morework.tv_sec = -1;
  179. mythr->_job_transition_in_progress = true;
  180. if (mythr->work)
  181. timersub(tvp_now, &mythr->work->tv_work_start, &tv_worktime);
  182. if ((!mythr->work) || abandon_work(mythr->work, &tv_worktime, proc->max_hashes))
  183. {
  184. mythr->work_restart = false;
  185. request_work(mythr);
  186. // FIXME: Allow get_work to return NULL to retry on notification
  187. if (mythr->next_work)
  188. free_work(mythr->next_work);
  189. mythr->next_work = get_and_prepare_work(mythr);
  190. if (!mythr->next_work)
  191. return false;
  192. mythr->starting_next_work = true;
  193. api->job_prepare(mythr, mythr->next_work, mythr->_max_nonce);
  194. }
  195. else
  196. {
  197. mythr->starting_next_work = false;
  198. api->job_prepare(mythr, mythr->work, mythr->_max_nonce);
  199. }
  200. job_prepare_complete(mythr);
  201. return true;
  202. }
  203. void job_prepare_complete(struct thr_info *mythr)
  204. {
  205. if (unlikely(mythr->busy_state == TBS_GETTING_RESULTS))
  206. return;
  207. if (mythr->work)
  208. {
  209. if (true /* TODO: job is near complete */ || unlikely(mythr->work_restart))
  210. do_get_results(mythr, true);
  211. else
  212. {} // TODO: Set a timer to call do_get_results when job is near complete
  213. }
  214. else // no job currently running
  215. do_job_start(mythr);
  216. }
  217. void do_get_results(struct thr_info *mythr, bool proceed_with_new_job)
  218. {
  219. struct cgpu_info *proc = mythr->cgpu;
  220. const struct device_api *api = proc->api;
  221. struct work *work = mythr->work;
  222. mythr->_job_transition_in_progress = true;
  223. mythr->tv_results_jobstart = mythr->tv_jobstart;
  224. mythr->_proceed_with_new_job = proceed_with_new_job;
  225. if (api->job_get_results)
  226. api->job_get_results(mythr, work);
  227. else
  228. job_results_fetched(mythr);
  229. }
  230. void job_results_fetched(struct thr_info *mythr)
  231. {
  232. if (mythr->_proceed_with_new_job)
  233. do_job_start(mythr);
  234. else
  235. {
  236. struct timeval tv_now;
  237. gettimeofday(&tv_now, NULL);
  238. do_process_results(mythr, &tv_now, mythr->prev_work, true);
  239. }
  240. }
  241. void do_job_start(struct thr_info *mythr)
  242. {
  243. struct cgpu_info *proc = mythr->cgpu;
  244. const struct device_api *api = proc->api;
  245. thread_reportin(mythr);
  246. api->job_start(mythr);
  247. }
  248. void mt_job_transition(struct thr_info *mythr)
  249. {
  250. struct timeval tv_now;
  251. gettimeofday(&tv_now, NULL);
  252. if (mythr->starting_next_work)
  253. {
  254. mythr->next_work->tv_work_start = tv_now;
  255. if (mythr->prev_work)
  256. free_work(mythr->prev_work);
  257. mythr->prev_work = mythr->work;
  258. mythr->work = mythr->next_work;
  259. mythr->next_work = NULL;
  260. }
  261. mythr->tv_jobstart = tv_now;
  262. mythr->_job_transition_in_progress = false;
  263. }
  264. void job_start_complete(struct thr_info *mythr)
  265. {
  266. struct timeval tv_now;
  267. gettimeofday(&tv_now, NULL);
  268. do_process_results(mythr, &tv_now, mythr->prev_work, false);
  269. }
  270. void job_start_abort(struct thr_info *mythr, bool failure)
  271. {
  272. struct cgpu_info *proc = mythr->cgpu;
  273. if (failure)
  274. proc->deven = DEV_RECOVER_ERR;
  275. mythr->work = NULL;
  276. mythr->_job_transition_in_progress = false;
  277. }
  278. bool do_process_results(struct thr_info *mythr, struct timeval *tvp_now, struct work *work, bool stopping)
  279. {
  280. struct cgpu_info *proc = mythr->cgpu;
  281. const struct device_api *api = proc->api;
  282. struct timeval tv_hashes;
  283. int64_t hashes = 0;
  284. if (api->job_process_results)
  285. hashes = api->job_process_results(mythr, work, stopping);
  286. thread_reportin(mythr);
  287. if (hashes)
  288. {
  289. timersub(tvp_now, &mythr->tv_results_jobstart, &tv_hashes);
  290. if (!hashes_done(mythr, hashes, &tv_hashes, api->can_limit_work ? &mythr->_max_nonce : NULL))
  291. return false;
  292. }
  293. return true;
  294. }
  295. static
  296. void do_notifier_select(struct thr_info *thr, struct timeval *tvp_timeout)
  297. {
  298. struct cgpu_info *cgpu = thr->cgpu;
  299. struct timeval tv_now;
  300. int maxfd;
  301. fd_set rfds;
  302. gettimeofday(&tv_now, NULL);
  303. FD_ZERO(&rfds);
  304. FD_SET(thr->notifier[0], &rfds);
  305. maxfd = thr->notifier[0];
  306. FD_SET(thr->work_restart_notifier[0], &rfds);
  307. set_maxfd(&maxfd, thr->work_restart_notifier[0]);
  308. if (thr->mutex_request[1] != INVSOCK)
  309. {
  310. FD_SET(thr->mutex_request[0], &rfds);
  311. set_maxfd(&maxfd, thr->mutex_request[0]);
  312. }
  313. if (select(maxfd + 1, &rfds, NULL, NULL, select_timeout(tvp_timeout, &tv_now)) < 0)
  314. return;
  315. if (thr->mutex_request[1] != INVSOCK && FD_ISSET(thr->mutex_request[0], &rfds))
  316. {
  317. // FIXME: This can only handle one request at a time!
  318. pthread_mutex_t *mutexp = &cgpu->device_mutex;
  319. notifier_read(thr->mutex_request);
  320. mutex_lock(mutexp);
  321. pthread_cond_signal(&cgpu->device_cond);
  322. pthread_cond_wait(&cgpu->device_cond, mutexp);
  323. mutex_unlock(mutexp);
  324. }
  325. if (FD_ISSET(thr->notifier[0], &rfds)) {
  326. notifier_read(thr->notifier);
  327. }
  328. if (FD_ISSET(thr->work_restart_notifier[0], &rfds))
  329. notifier_read(thr->work_restart_notifier);
  330. }
  331. void minerloop_async(struct thr_info *mythr)
  332. {
  333. struct thr_info *thr = mythr;
  334. struct cgpu_info *cgpu = mythr->cgpu;
  335. const struct device_api *api = cgpu->api;
  336. struct timeval tv_now;
  337. struct timeval tv_timeout;
  338. struct cgpu_info *proc;
  339. bool is_running, should_be_running;
  340. if (mythr->work_restart_notifier[1] == -1)
  341. notifier_init(mythr->work_restart_notifier);
  342. while (1) {
  343. tv_timeout.tv_sec = -1;
  344. gettimeofday(&tv_now, NULL);
  345. for (proc = cgpu; proc; proc = proc->next_proc)
  346. {
  347. mythr = proc->thr[0];
  348. // Nothing should happen while we're starting a job
  349. if (unlikely(mythr->busy_state == TBS_STARTING_JOB))
  350. goto defer_events;
  351. is_running = mythr->work;
  352. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  353. if (should_be_running)
  354. {
  355. if (unlikely(!(is_running || mythr->_job_transition_in_progress)))
  356. {
  357. mt_disable_finish(mythr);
  358. goto djp;
  359. }
  360. if (unlikely(mythr->work_restart))
  361. goto djp;
  362. }
  363. else // ! should_be_running
  364. {
  365. if (unlikely(is_running && !mythr->_job_transition_in_progress))
  366. {
  367. disabled: ;
  368. mythr->tv_morework.tv_sec = -1;
  369. if (mythr->busy_state != TBS_GETTING_RESULTS)
  370. do_get_results(mythr, false);
  371. else
  372. // Avoid starting job when pending result fetch completes
  373. mythr->_proceed_with_new_job = false;
  374. }
  375. }
  376. if (timer_passed(&mythr->tv_morework, &tv_now))
  377. {
  378. djp: ;
  379. if (!do_job_prepare(mythr, &tv_now))
  380. goto disabled;
  381. }
  382. defer_events:
  383. if (timer_passed(&mythr->tv_poll, &tv_now))
  384. api->poll(mythr);
  385. reduce_timeout_to(&tv_timeout, &mythr->tv_morework);
  386. reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
  387. }
  388. do_notifier_select(thr, &tv_timeout);
  389. }
  390. }
  391. static
  392. void do_queue_flush(struct thr_info *mythr)
  393. {
  394. struct cgpu_info *proc = mythr->cgpu;
  395. const struct device_api *api = proc->api;
  396. api->queue_flush(mythr);
  397. if (mythr->next_work)
  398. {
  399. free_work(mythr->next_work);
  400. mythr->next_work = NULL;
  401. }
  402. }
  403. void minerloop_queue(struct thr_info *thr)
  404. {
  405. struct thr_info *mythr;
  406. struct cgpu_info *cgpu = thr->cgpu;
  407. const struct device_api *api = cgpu->api;
  408. struct timeval tv_now;
  409. struct timeval tv_timeout;
  410. struct cgpu_info *proc;
  411. bool should_be_running;
  412. struct work *work;
  413. if (thr->work_restart_notifier[1] == -1)
  414. notifier_init(thr->work_restart_notifier);
  415. while (1) {
  416. tv_timeout.tv_sec = -1;
  417. gettimeofday(&tv_now, NULL);
  418. for (proc = cgpu; proc; proc = proc->next_proc)
  419. {
  420. mythr = proc->thr[0];
  421. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  422. redo:
  423. if (should_be_running)
  424. {
  425. if (unlikely(!mythr->_last_sbr_state))
  426. {
  427. mt_disable_finish(mythr);
  428. mythr->_last_sbr_state = should_be_running;
  429. }
  430. if (unlikely(mythr->work_restart))
  431. {
  432. mythr->work_restart = false;
  433. do_queue_flush(mythr);
  434. }
  435. while (!mythr->queue_full)
  436. {
  437. if (mythr->next_work)
  438. {
  439. work = mythr->next_work;
  440. mythr->next_work = NULL;
  441. }
  442. else
  443. {
  444. request_work(mythr);
  445. // FIXME: Allow get_work to return NULL to retry on notification
  446. work = get_and_prepare_work(mythr);
  447. }
  448. if (!work)
  449. break;
  450. if (!api->queue_append(mythr, work))
  451. mythr->next_work = work;
  452. }
  453. }
  454. else
  455. if (unlikely(mythr->_last_sbr_state))
  456. {
  457. mythr->_last_sbr_state = should_be_running;
  458. do_queue_flush(mythr);
  459. }
  460. if (timer_passed(&mythr->tv_poll, &tv_now))
  461. api->poll(mythr);
  462. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  463. if (should_be_running && !mythr->queue_full)
  464. goto redo;
  465. reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
  466. }
  467. do_notifier_select(thr, &tv_timeout);
  468. }
  469. }
  470. void *miner_thread(void *userdata)
  471. {
  472. struct thr_info *mythr = userdata;
  473. const int thr_id = mythr->id;
  474. struct cgpu_info *cgpu = mythr->cgpu;
  475. const struct device_api *api = cgpu->api;
  476. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  477. char threadname[20];
  478. snprintf(threadname, 20, "miner_%s", cgpu->proc_repr_ns);
  479. RenameThread(threadname);
  480. if (api->thread_init && !api->thread_init(mythr)) {
  481. dev_error(cgpu, REASON_THREAD_FAIL_INIT);
  482. for (struct cgpu_info *slave = cgpu->next_proc; slave && !slave->threads; slave = slave->next_proc)
  483. dev_error(slave, REASON_THREAD_FAIL_INIT);
  484. goto out;
  485. }
  486. thread_reportout(mythr);
  487. applog(LOG_DEBUG, "Popping ping in miner thread");
  488. notifier_read(mythr->notifier); // Wait for a notification to start
  489. gettimeofday(&cgpu->cgminer_stats.start_tv, NULL);
  490. if (api->minerloop)
  491. api->minerloop(mythr);
  492. else
  493. minerloop_scanhash(mythr);
  494. out:
  495. if (api->thread_shutdown)
  496. api->thread_shutdown(mythr);
  497. thread_reportin(mythr);
  498. applog(LOG_ERR, "Thread %d failure, exiting", thr_id);
  499. notifier_destroy(mythr->notifier);
  500. return NULL;
  501. }
  502. bool add_cgpu(struct cgpu_info*cgpu)
  503. {
  504. int lpcount;
  505. renumber_cgpu(cgpu);
  506. if (!cgpu->procs)
  507. cgpu->procs = 1;
  508. lpcount = cgpu->procs;
  509. cgpu->device = cgpu;
  510. cgpu->dev_repr = malloc(6);
  511. sprintf(cgpu->dev_repr, "%s%2u", cgpu->api->name, cgpu->device_id % 100);
  512. cgpu->dev_repr_ns = malloc(6);
  513. sprintf(cgpu->dev_repr_ns, "%s%u", cgpu->api->name, cgpu->device_id % 100);
  514. strcpy(cgpu->proc_repr, cgpu->dev_repr);
  515. sprintf(cgpu->proc_repr_ns, "%s%u", cgpu->api->name, cgpu->device_id);
  516. devices = realloc(devices, sizeof(struct cgpu_info *) * (total_devices + lpcount + 1));
  517. devices[total_devices++] = cgpu;
  518. if (lpcount > 1)
  519. {
  520. int ns;
  521. int tpp = cgpu->threads / lpcount;
  522. struct cgpu_info **nlp_p, *slave;
  523. // Note, strcpy instead of assigning a byte to get the \0 too
  524. strcpy(&cgpu->proc_repr[5], "a");
  525. ns = strlen(cgpu->proc_repr_ns);
  526. strcpy(&cgpu->proc_repr_ns[ns], "a");
  527. nlp_p = &cgpu->next_proc;
  528. for (int i = 1; i < lpcount; ++i)
  529. {
  530. slave = malloc(sizeof(*slave));
  531. *slave = *cgpu;
  532. slave->proc_id = i;
  533. slave->proc_repr[5] += i;
  534. slave->proc_repr_ns[ns] += i;
  535. slave->threads = tpp;
  536. devices[total_devices++] = slave;
  537. *nlp_p = slave;
  538. nlp_p = &slave->next_proc;
  539. }
  540. *nlp_p = NULL;
  541. cgpu->proc_id = 0;
  542. cgpu->threads -= (tpp * (lpcount - 1));
  543. }
  544. return true;
  545. }