deviceapi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. if (mythr->next_work)
  175. free_work(mythr->next_work);
  176. mythr->next_work = get_work(mythr);
  177. if (api->prepare_work && !api->prepare_work(mythr, mythr->next_work)) {
  178. applog(LOG_ERR, "%"PRIpreprv": Work prepare failed, disabling!", proc->proc_repr);
  179. proc->deven = DEV_RECOVER_ERR;
  180. return false;
  181. }
  182. mythr->starting_next_work = true;
  183. api->job_prepare(mythr, mythr->next_work, mythr->_max_nonce);
  184. }
  185. else
  186. {
  187. mythr->starting_next_work = false;
  188. api->job_prepare(mythr, mythr->work, mythr->_max_nonce);
  189. }
  190. job_prepare_complete(mythr);
  191. return true;
  192. }
  193. void job_prepare_complete(struct thr_info *mythr)
  194. {
  195. if (unlikely(mythr->busy_state == TBS_GETTING_RESULTS))
  196. return;
  197. if (mythr->work)
  198. {
  199. if (true /* TODO: job is near complete */ || unlikely(mythr->work_restart))
  200. do_get_results(mythr, true);
  201. else
  202. {} // TODO: Set a timer to call do_get_results when job is near complete
  203. }
  204. else // no job currently running
  205. do_job_start(mythr);
  206. }
  207. void do_get_results(struct thr_info *mythr, bool proceed_with_new_job)
  208. {
  209. struct cgpu_info *proc = mythr->cgpu;
  210. const struct device_api *api = proc->api;
  211. struct work *work = mythr->work;
  212. mythr->_job_transition_in_progress = true;
  213. mythr->tv_results_jobstart = mythr->tv_jobstart;
  214. mythr->_proceed_with_new_job = proceed_with_new_job;
  215. if (api->job_get_results)
  216. api->job_get_results(mythr, work);
  217. else
  218. job_results_fetched(mythr);
  219. }
  220. void job_results_fetched(struct thr_info *mythr)
  221. {
  222. if (mythr->_proceed_with_new_job)
  223. do_job_start(mythr);
  224. else
  225. {
  226. struct timeval tv_now;
  227. gettimeofday(&tv_now, NULL);
  228. do_process_results(mythr, &tv_now, mythr->prev_work, true);
  229. }
  230. }
  231. void do_job_start(struct thr_info *mythr)
  232. {
  233. struct cgpu_info *proc = mythr->cgpu;
  234. const struct device_api *api = proc->api;
  235. thread_reportin(mythr);
  236. api->job_start(mythr);
  237. }
  238. void mt_job_transition(struct thr_info *mythr)
  239. {
  240. struct timeval tv_now;
  241. gettimeofday(&tv_now, NULL);
  242. if (mythr->starting_next_work)
  243. {
  244. mythr->next_work->tv_work_start = tv_now;
  245. if (mythr->prev_work)
  246. free_work(mythr->prev_work);
  247. mythr->prev_work = mythr->work;
  248. mythr->work = mythr->next_work;
  249. mythr->next_work = NULL;
  250. }
  251. mythr->tv_jobstart = tv_now;
  252. mythr->_job_transition_in_progress = false;
  253. }
  254. void job_start_complete(struct thr_info *mythr)
  255. {
  256. struct timeval tv_now;
  257. gettimeofday(&tv_now, NULL);
  258. do_process_results(mythr, &tv_now, mythr->prev_work, false);
  259. }
  260. void job_start_abort(struct thr_info *mythr, bool failure)
  261. {
  262. struct cgpu_info *proc = mythr->cgpu;
  263. if (failure)
  264. proc->deven = DEV_RECOVER_ERR;
  265. mythr->work = NULL;
  266. mythr->_job_transition_in_progress = false;
  267. }
  268. bool do_process_results(struct thr_info *mythr, struct timeval *tvp_now, struct work *work, bool stopping)
  269. {
  270. struct cgpu_info *proc = mythr->cgpu;
  271. const struct device_api *api = proc->api;
  272. struct timeval tv_hashes;
  273. int64_t hashes = 0;
  274. if (api->job_process_results)
  275. hashes = api->job_process_results(mythr, work, stopping);
  276. thread_reportin(mythr);
  277. if (hashes)
  278. {
  279. timersub(tvp_now, &mythr->tv_results_jobstart, &tv_hashes);
  280. if (!hashes_done(mythr, hashes, &tv_hashes, api->can_limit_work ? &mythr->_max_nonce : NULL))
  281. return false;
  282. }
  283. return true;
  284. }
  285. void minerloop_async(struct thr_info *mythr)
  286. {
  287. struct thr_info *thr = mythr;
  288. struct cgpu_info *cgpu = mythr->cgpu;
  289. const struct device_api *api = cgpu->api;
  290. struct timeval tv_now;
  291. struct timeval tv_timeout;
  292. struct cgpu_info *proc;
  293. int maxfd;
  294. fd_set rfds;
  295. bool is_running, should_be_running;
  296. if (mythr->work_restart_notifier[1] == -1)
  297. notifier_init(mythr->work_restart_notifier);
  298. while (1) {
  299. tv_timeout.tv_sec = -1;
  300. gettimeofday(&tv_now, NULL);
  301. for (proc = cgpu; proc; proc = proc->next_proc)
  302. {
  303. mythr = proc->thr[0];
  304. // Nothing should happen while we're starting a job
  305. if (unlikely(mythr->busy_state == TBS_STARTING_JOB))
  306. goto defer_events;
  307. is_running = mythr->work;
  308. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  309. if (should_be_running)
  310. {
  311. if (unlikely(!(is_running || mythr->_job_transition_in_progress)))
  312. {
  313. mt_disable_finish(mythr);
  314. goto djp;
  315. }
  316. if (unlikely(mythr->work_restart))
  317. goto djp;
  318. }
  319. else // ! should_be_running
  320. {
  321. if (unlikely(is_running && !mythr->_job_transition_in_progress))
  322. {
  323. disabled: ;
  324. mythr->tv_morework.tv_sec = -1;
  325. if (mythr->busy_state != TBS_GETTING_RESULTS)
  326. do_get_results(mythr, false);
  327. else
  328. // Avoid starting job when pending result fetch completes
  329. mythr->_proceed_with_new_job = false;
  330. }
  331. }
  332. if (timer_passed(&mythr->tv_morework, &tv_now))
  333. {
  334. djp: ;
  335. if (!do_job_prepare(mythr, &tv_now))
  336. goto disabled;
  337. }
  338. defer_events:
  339. if (timer_passed(&mythr->tv_poll, &tv_now))
  340. api->poll(mythr);
  341. reduce_timeout_to(&tv_timeout, &mythr->tv_morework);
  342. reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
  343. }
  344. mythr = thr;
  345. gettimeofday(&tv_now, NULL);
  346. FD_ZERO(&rfds);
  347. FD_SET(mythr->notifier[0], &rfds);
  348. maxfd = mythr->notifier[0];
  349. FD_SET(mythr->work_restart_notifier[0], &rfds);
  350. set_maxfd(&maxfd, mythr->work_restart_notifier[0]);
  351. if (thr->mutex_request[1] != INVSOCK)
  352. {
  353. FD_SET(thr->mutex_request[0], &rfds);
  354. set_maxfd(&maxfd, thr->mutex_request[0]);
  355. }
  356. if (select(maxfd + 1, &rfds, NULL, NULL, select_timeout(&tv_timeout, &tv_now)) < 0)
  357. continue;
  358. if (thr->mutex_request[1] != INVSOCK && FD_ISSET(thr->mutex_request[0], &rfds))
  359. {
  360. // FIXME: This can only handle one request at a time!
  361. pthread_mutex_t *mutexp = &cgpu->device_mutex;
  362. notifier_read(thr->mutex_request);
  363. mutex_lock(mutexp);
  364. pthread_cond_signal(&cgpu->device_cond);
  365. pthread_cond_wait(&cgpu->device_cond, mutexp);
  366. mutex_unlock(mutexp);
  367. }
  368. if (FD_ISSET(mythr->notifier[0], &rfds)) {
  369. notifier_read(mythr->notifier);
  370. }
  371. if (FD_ISSET(mythr->work_restart_notifier[0], &rfds))
  372. notifier_read(mythr->work_restart_notifier);
  373. }
  374. }
  375. void *miner_thread(void *userdata)
  376. {
  377. struct thr_info *mythr = userdata;
  378. const int thr_id = mythr->id;
  379. struct cgpu_info *cgpu = mythr->cgpu;
  380. const struct device_api *api = cgpu->api;
  381. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  382. char threadname[20];
  383. snprintf(threadname, 20, "miner_%s", cgpu->proc_repr_ns);
  384. RenameThread(threadname);
  385. if (api->thread_init && !api->thread_init(mythr)) {
  386. dev_error(cgpu, REASON_THREAD_FAIL_INIT);
  387. for (struct cgpu_info *slave = cgpu->next_proc; slave && !slave->threads; slave = slave->next_proc)
  388. dev_error(slave, REASON_THREAD_FAIL_INIT);
  389. goto out;
  390. }
  391. thread_reportout(mythr);
  392. applog(LOG_DEBUG, "Popping ping in miner thread");
  393. notifier_read(mythr->notifier); // Wait for a notification to start
  394. if (api->minerloop)
  395. api->minerloop(mythr);
  396. else
  397. minerloop_scanhash(mythr);
  398. out:
  399. if (api->thread_shutdown)
  400. api->thread_shutdown(mythr);
  401. thread_reportin(mythr);
  402. applog(LOG_ERR, "Thread %d failure, exiting", thr_id);
  403. notifier_destroy(mythr->notifier);
  404. return NULL;
  405. }
  406. bool add_cgpu(struct cgpu_info*cgpu)
  407. {
  408. int lpcount;
  409. renumber_cgpu(cgpu);
  410. if (!cgpu->procs)
  411. cgpu->procs = 1;
  412. lpcount = cgpu->procs;
  413. cgpu->device = cgpu;
  414. cgpu->dev_repr = malloc(6);
  415. sprintf(cgpu->dev_repr, "%s%2u", cgpu->api->name, cgpu->device_id % 100);
  416. cgpu->dev_repr_ns = malloc(6);
  417. sprintf(cgpu->dev_repr_ns, "%s%u", cgpu->api->name, cgpu->device_id % 100);
  418. strcpy(cgpu->proc_repr, cgpu->dev_repr);
  419. sprintf(cgpu->proc_repr_ns, "%s%u", cgpu->api->name, cgpu->device_id);
  420. devices = realloc(devices, sizeof(struct cgpu_info *) * (total_devices + lpcount + 1));
  421. devices[total_devices++] = cgpu;
  422. if (lpcount > 1)
  423. {
  424. int ns;
  425. int tpp = cgpu->threads / lpcount;
  426. struct cgpu_info **nlp_p, *slave;
  427. // Note, strcpy instead of assigning a byte to get the \0 too
  428. strcpy(&cgpu->proc_repr[5], "a");
  429. ns = strlen(cgpu->proc_repr_ns);
  430. strcpy(&cgpu->proc_repr_ns[ns], "a");
  431. nlp_p = &cgpu->next_proc;
  432. for (int i = 1; i < lpcount; ++i)
  433. {
  434. slave = malloc(sizeof(*slave));
  435. *slave = *cgpu;
  436. slave->proc_id = i;
  437. slave->proc_repr[5] += i;
  438. slave->proc_repr_ns[ns] += i;
  439. slave->threads = tpp;
  440. devices[total_devices++] = slave;
  441. *nlp_p = slave;
  442. nlp_p = &slave->next_proc;
  443. }
  444. *nlp_p = NULL;
  445. cgpu->proc_id = 0;
  446. cgpu->threads -= (tpp * (lpcount - 1));
  447. }
  448. return true;
  449. }