deviceapi.c 12 KB

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