deviceapi.c 12 KB

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