deviceapi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 (mythr->work)
  151. {
  152. if (true /* TODO: job is near complete */ || unlikely(mythr->work_restart))
  153. do_get_results(mythr, true);
  154. else
  155. {} // TODO: Set a timer to call do_get_results when job is near complete
  156. }
  157. else // no job currently running
  158. do_job_start(mythr);
  159. }
  160. void do_get_results(struct thr_info *mythr, bool proceed_with_new_job)
  161. {
  162. struct cgpu_info *proc = mythr->cgpu;
  163. const struct device_api *api = proc->api;
  164. struct work *work = mythr->work;
  165. mythr->_job_transition_in_progress = true;
  166. mythr->tv_results_jobstart = mythr->tv_jobstart;
  167. mythr->_proceed_with_new_job = proceed_with_new_job;
  168. if (api->job_get_results)
  169. api->job_get_results(mythr, work);
  170. else
  171. job_results_fetched(mythr);
  172. }
  173. void job_results_fetched(struct thr_info *mythr)
  174. {
  175. if (mythr->_proceed_with_new_job)
  176. do_job_start(mythr);
  177. else
  178. {
  179. struct timeval tv_now;
  180. gettimeofday(&tv_now, NULL);
  181. do_process_results(mythr, &tv_now, mythr->prev_work, true);
  182. }
  183. }
  184. void do_job_start(struct thr_info *mythr)
  185. {
  186. struct cgpu_info *proc = mythr->cgpu;
  187. const struct device_api *api = proc->api;
  188. thread_reportin(mythr);
  189. api->job_start(mythr);
  190. }
  191. void mt_job_transition(struct thr_info *mythr)
  192. {
  193. struct timeval tv_now;
  194. gettimeofday(&tv_now, NULL);
  195. if (mythr->starting_next_work)
  196. {
  197. mythr->next_work->tv_work_start = tv_now;
  198. if (mythr->prev_work)
  199. free_work(mythr->prev_work);
  200. mythr->prev_work = mythr->work;
  201. mythr->work = mythr->next_work;
  202. mythr->next_work = NULL;
  203. }
  204. mythr->tv_jobstart = tv_now;
  205. mythr->_job_transition_in_progress = false;
  206. }
  207. void job_start_complete(struct thr_info *mythr)
  208. {
  209. struct timeval tv_now;
  210. gettimeofday(&tv_now, NULL);
  211. if (!do_process_results(mythr, &tv_now, mythr->prev_work, false))
  212. {
  213. struct cgpu_info *proc = mythr->cgpu;
  214. proc->deven = DEV_RECOVER_ERR;
  215. mythr->_job_transition_in_progress = false;
  216. }
  217. }
  218. bool do_process_results(struct thr_info *mythr, struct timeval *tvp_now, struct work *work, bool stopping)
  219. {
  220. struct cgpu_info *proc = mythr->cgpu;
  221. const struct device_api *api = proc->api;
  222. struct timeval tv_hashes;
  223. int64_t hashes = 0;
  224. if (api->job_process_results)
  225. hashes = api->job_process_results(mythr, work, stopping);
  226. thread_reportin(mythr);
  227. if (hashes)
  228. {
  229. timersub(tvp_now, &mythr->tv_results_jobstart, &tv_hashes);
  230. if (!hashes_done(mythr, hashes, &tv_hashes, api->can_limit_work ? &mythr->_max_nonce : NULL))
  231. return false;
  232. }
  233. return true;
  234. }
  235. void minerloop_async(struct thr_info *mythr)
  236. {
  237. const int thr_id = mythr->id;
  238. struct cgpu_info *cgpu = mythr->cgpu;
  239. const struct device_api *api = cgpu->api;
  240. struct timeval tv_now;
  241. struct timeval tv_timeout;
  242. struct work *work;
  243. const bool primary = (!mythr->device_thread) || mythr->primary_thread;
  244. struct cgpu_info *proc;
  245. // NOTE: prev_job_work/new_job_work are distinct from mythr->prev_work/next_work (job v work boundary)
  246. struct work *prev_job_work;
  247. int proc_thr_no;
  248. int maxfd;
  249. fd_set rfds;
  250. bool is_running, should_be_running;
  251. while (1) {
  252. tv_timeout.tv_sec = -1;
  253. gettimeofday(&tv_now, NULL);
  254. for (proc = cgpu; proc; proc = proc->next_proc)
  255. {
  256. mythr = proc->thr[0];
  257. is_running = mythr->work;
  258. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  259. if (should_be_running)
  260. {
  261. if (unlikely(!(is_running || mythr->_job_transition_in_progress)))
  262. {
  263. mt_disable_finish(mythr);
  264. goto djp;
  265. }
  266. if (unlikely(mythr->work_restart))
  267. goto djp;
  268. }
  269. else // ! should_be_running
  270. {
  271. if (unlikely(is_running && !mythr->_job_transition_in_progress))
  272. {
  273. disabled: ;
  274. mythr->tv_morework.tv_sec = -1;
  275. do_get_results(mythr, false);
  276. }
  277. }
  278. if (timer_passed(&mythr->tv_morework, &tv_now))
  279. {
  280. djp: ;
  281. if (!do_job_prepare(mythr, &tv_now))
  282. goto disabled;
  283. }
  284. if (timer_passed(&mythr->tv_poll, &tv_now))
  285. api->poll(mythr);
  286. reduce_timeout_to(&tv_timeout, &mythr->tv_morework);
  287. reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
  288. }
  289. gettimeofday(&tv_now, NULL);
  290. // FIXME: break select on work restart
  291. FD_ZERO(&rfds);
  292. FD_SET(mythr->notifier[0], &rfds);
  293. maxfd = mythr->notifier[0];
  294. if (select(maxfd + 1, &rfds, NULL, NULL, select_timeout(&tv_timeout, &tv_now)) < 0)
  295. continue;
  296. if (FD_ISSET(mythr->notifier[0], &rfds)) {
  297. notifier_read(mythr->notifier);
  298. }
  299. }
  300. }
  301. void *miner_thread(void *userdata)
  302. {
  303. struct thr_info *mythr = userdata;
  304. const int thr_id = mythr->id;
  305. struct cgpu_info *cgpu = mythr->cgpu;
  306. const struct device_api *api = cgpu->api;
  307. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  308. char threadname[20];
  309. snprintf(threadname, 20, "miner_%s", cgpu->proc_repr_ns);
  310. RenameThread(threadname);
  311. if (api->thread_init && !api->thread_init(mythr)) {
  312. dev_error(cgpu, REASON_THREAD_FAIL_INIT);
  313. for (struct cgpu_info *slave = cgpu->next_proc; slave && !slave->threads; slave = slave->next_proc)
  314. dev_error(slave, REASON_THREAD_FAIL_INIT);
  315. goto out;
  316. }
  317. thread_reportout(mythr);
  318. applog(LOG_DEBUG, "Popping ping in miner thread");
  319. notifier_read(mythr->notifier); // Wait for a notification to start
  320. if (api->minerloop)
  321. api->minerloop(mythr);
  322. else
  323. minerloop_scanhash(mythr);
  324. out:
  325. if (api->thread_shutdown)
  326. api->thread_shutdown(mythr);
  327. thread_reportin(mythr);
  328. applog(LOG_ERR, "Thread %d failure, exiting", thr_id);
  329. notifier_destroy(mythr->notifier);
  330. return NULL;
  331. }
  332. bool add_cgpu(struct cgpu_info*cgpu)
  333. {
  334. int lpcount;
  335. renumber_cgpu(cgpu);
  336. if (!cgpu->procs)
  337. cgpu->procs = 1;
  338. lpcount = cgpu->procs;
  339. cgpu->device = cgpu;
  340. cgpu->dev_repr = malloc(6);
  341. sprintf(cgpu->dev_repr, "%s%2u", cgpu->api->name, cgpu->device_id % 100);
  342. cgpu->dev_repr_ns = malloc(6);
  343. sprintf(cgpu->dev_repr_ns, "%s%u", cgpu->api->name, cgpu->device_id % 100);
  344. strcpy(cgpu->proc_repr, cgpu->dev_repr);
  345. sprintf(cgpu->proc_repr_ns, "%s%u", cgpu->api->name, cgpu->device_id);
  346. devices = realloc(devices, sizeof(struct cgpu_info *) * (total_devices + lpcount + 1));
  347. devices[total_devices++] = cgpu;
  348. if (lpcount > 1)
  349. {
  350. int ns;
  351. int tpp = cgpu->threads / lpcount;
  352. struct cgpu_info **nlp_p, *slave;
  353. // Note, strcpy instead of assigning a byte to get the \0 too
  354. strcpy(&cgpu->proc_repr[5], "a");
  355. ns = strlen(cgpu->proc_repr_ns);
  356. strcpy(&cgpu->proc_repr_ns[ns], "a");
  357. nlp_p = &cgpu->next_proc;
  358. for (int i = 1; i < lpcount; ++i)
  359. {
  360. slave = malloc(sizeof(*slave));
  361. *slave = *cgpu;
  362. slave->proc_id = i;
  363. slave->proc_repr[5] += i;
  364. slave->proc_repr_ns[ns] += i;
  365. slave->threads = tpp;
  366. devices[total_devices++] = slave;
  367. *nlp_p = slave;
  368. nlp_p = &slave->next_proc;
  369. }
  370. *nlp_p = NULL;
  371. cgpu->proc_id = 0;
  372. cgpu->threads -= (tpp * (lpcount - 1));
  373. }
  374. return true;
  375. }