deviceapi.c 12 KB

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