deviceapi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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 "fpgautils.h"
  27. #include "logging.h"
  28. #include "miner.h"
  29. #include "util.h"
  30. bool hashes_done(struct thr_info *thr, int64_t hashes, struct timeval *tvp_hashes, uint32_t *max_nonce)
  31. {
  32. struct cgpu_info *cgpu = thr->cgpu;
  33. const long cycle = opt_log_interval / 5 ? : 1;
  34. if (unlikely(hashes == -1)) {
  35. time_t now = time(NULL);
  36. if (difftime(now, cgpu->device_last_not_well) > 1.)
  37. dev_error(cgpu, REASON_THREAD_ZERO_HASH);
  38. if (thr->scanhash_working && opt_restart) {
  39. applog(LOG_ERR, "%"PRIpreprv" failure, attempting to reinitialize", cgpu->proc_repr);
  40. thr->scanhash_working = false;
  41. cgpu->reinit_backoff = 5.2734375;
  42. hashes = 0;
  43. } else {
  44. applog(LOG_ERR, "%"PRIpreprv" failure, disabling!", cgpu->proc_repr);
  45. cgpu->deven = DEV_RECOVER_ERR;
  46. return false;
  47. }
  48. }
  49. else
  50. thr->scanhash_working = true;
  51. thr->hashes_done += hashes;
  52. if (hashes > cgpu->max_hashes)
  53. cgpu->max_hashes = hashes;
  54. timeradd(&thr->tv_hashes_done, tvp_hashes, &thr->tv_hashes_done);
  55. // max_nonce management (optional)
  56. if (unlikely((long)thr->tv_hashes_done.tv_sec < cycle)) {
  57. int mult;
  58. if (likely(!max_nonce || *max_nonce == 0xffffffff))
  59. return true;
  60. mult = 1000000 / ((thr->tv_hashes_done.tv_usec + 0x400) / 0x400) + 0x10;
  61. mult *= cycle;
  62. if (*max_nonce > (0xffffffff * 0x400) / mult)
  63. *max_nonce = 0xffffffff;
  64. else
  65. *max_nonce = (*max_nonce * mult) / 0x400;
  66. } else if (unlikely(thr->tv_hashes_done.tv_sec > cycle) && max_nonce)
  67. *max_nonce = *max_nonce * cycle / thr->tv_hashes_done.tv_sec;
  68. else if (unlikely(thr->tv_hashes_done.tv_usec > 100000) && max_nonce)
  69. *max_nonce = *max_nonce * 0x400 / (((cycle * 1000000) + thr->tv_hashes_done.tv_usec) / (cycle * 1000000 / 0x400));
  70. hashmeter2(thr);
  71. return true;
  72. }
  73. /* A generic wait function for threads that poll that will wait a specified
  74. * time tdiff waiting on a work restart request. Returns zero if the condition
  75. * was met (work restart requested) or ETIMEDOUT if not.
  76. */
  77. int restart_wait(struct thr_info *thr, unsigned int mstime)
  78. {
  79. struct timeval tv_timer, tv_now, tv_timeout;
  80. fd_set rfds;
  81. SOCKETTYPE wrn = thr->work_restart_notifier[0];
  82. int rv;
  83. if (unlikely(thr->work_restart_notifier[1] == INVSOCK))
  84. {
  85. // This is a bug!
  86. applog(LOG_ERR, "%"PRIpreprv": restart_wait called without a work_restart_notifier", thr->cgpu->proc_repr);
  87. nmsleep(mstime);
  88. return (thr->work_restart ? 0 : ETIMEDOUT);
  89. }
  90. gettimeofday(&tv_now, NULL);
  91. timer_set_delay(&tv_timer, &tv_now, mstime * 1000);
  92. while (true)
  93. {
  94. FD_ZERO(&rfds);
  95. FD_SET(wrn, &rfds);
  96. tv_timeout = tv_timer;
  97. rv = select(wrn + 1, &rfds, NULL, NULL, select_timeout(&tv_timeout, &tv_now));
  98. if (rv == 0)
  99. return ETIMEDOUT;
  100. if (rv > 0)
  101. {
  102. if (thr->work_restart)
  103. return 0;
  104. notifier_read(thr->work_restart_notifier);
  105. }
  106. gettimeofday(&tv_now, NULL);
  107. }
  108. }
  109. static
  110. struct work *get_and_prepare_work(struct thr_info *thr)
  111. {
  112. struct cgpu_info *proc = thr->cgpu;
  113. struct device_drv *api = proc->drv;
  114. struct work *work;
  115. work = get_work(thr);
  116. if (!work)
  117. return NULL;
  118. if (api->prepare_work && !api->prepare_work(thr, work)) {
  119. free_work(work);
  120. applog(LOG_ERR, "%"PRIpreprv": Work prepare failed, disabling!", proc->proc_repr);
  121. proc->deven = DEV_RECOVER_ERR;
  122. return NULL;
  123. }
  124. return work;
  125. }
  126. // Miner loop to manage a single processor (with possibly multiple threads per processor)
  127. void minerloop_scanhash(struct thr_info *mythr)
  128. {
  129. struct cgpu_info *cgpu = mythr->cgpu;
  130. struct device_drv *api = cgpu->drv;
  131. struct timeval tv_start, tv_end;
  132. struct timeval tv_hashes, tv_worktime;
  133. uint32_t max_nonce = api->can_limit_work ? api->can_limit_work(mythr) : 0xffffffff;
  134. int64_t hashes;
  135. struct work *work;
  136. const bool primary = (!mythr->device_thread) || mythr->primary_thread;
  137. #ifdef HAVE_PTHREAD_CANCEL
  138. pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
  139. #endif
  140. while (likely(!cgpu->shutdown)) {
  141. mythr->work_restart = false;
  142. request_work(mythr);
  143. work = get_and_prepare_work(mythr);
  144. if (!work)
  145. break;
  146. gettimeofday(&(work->tv_work_start), NULL);
  147. do {
  148. thread_reportin(mythr);
  149. /* Only allow the mining thread to be cancelled when
  150. * it is not in the driver code. */
  151. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  152. gettimeofday(&tv_start, NULL);
  153. hashes = api->scanhash(mythr, work, work->blk.nonce + max_nonce);
  154. gettimeofday(&tv_end, NULL);
  155. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  156. pthread_testcancel();
  157. thread_reportin(mythr);
  158. timersub(&tv_end, &tv_start, &tv_hashes);
  159. if (!hashes_done(mythr, hashes, &tv_hashes, api->can_limit_work ? &max_nonce : NULL))
  160. goto disabled;
  161. if (unlikely(mythr->work_restart)) {
  162. /* Apart from device_thread 0, we stagger the
  163. * starting of every next thread to try and get
  164. * all devices busy before worrying about
  165. * getting work for their extra threads */
  166. if (!primary) {
  167. struct timespec rgtp;
  168. rgtp.tv_sec = 0;
  169. rgtp.tv_nsec = 250 * mythr->device_thread * 1000000;
  170. nanosleep(&rgtp, NULL);
  171. }
  172. break;
  173. }
  174. if (unlikely(mythr->pause || cgpu->deven != DEV_ENABLED))
  175. disabled:
  176. mt_disable(mythr);
  177. timersub(&tv_end, &work->tv_work_start, &tv_worktime);
  178. } while (!abandon_work(work, &tv_worktime, cgpu->max_hashes));
  179. free_work(work);
  180. }
  181. }
  182. bool do_job_prepare(struct thr_info *mythr, struct timeval *tvp_now)
  183. {
  184. struct cgpu_info *proc = mythr->cgpu;
  185. struct device_drv *api = proc->drv;
  186. struct timeval tv_worktime;
  187. mythr->tv_morework.tv_sec = -1;
  188. mythr->_job_transition_in_progress = true;
  189. if (mythr->work)
  190. timersub(tvp_now, &mythr->work->tv_work_start, &tv_worktime);
  191. if ((!mythr->work) || abandon_work(mythr->work, &tv_worktime, proc->max_hashes))
  192. {
  193. mythr->work_restart = false;
  194. request_work(mythr);
  195. // FIXME: Allow get_work to return NULL to retry on notification
  196. if (mythr->next_work)
  197. free_work(mythr->next_work);
  198. mythr->next_work = get_and_prepare_work(mythr);
  199. if (!mythr->next_work)
  200. return false;
  201. mythr->starting_next_work = true;
  202. api->job_prepare(mythr, mythr->next_work, mythr->_max_nonce);
  203. }
  204. else
  205. {
  206. mythr->starting_next_work = false;
  207. api->job_prepare(mythr, mythr->work, mythr->_max_nonce);
  208. }
  209. job_prepare_complete(mythr);
  210. return true;
  211. }
  212. void job_prepare_complete(struct thr_info *mythr)
  213. {
  214. if (unlikely(mythr->busy_state == TBS_GETTING_RESULTS))
  215. return;
  216. if (mythr->work)
  217. {
  218. if (true /* TODO: job is near complete */ || unlikely(mythr->work_restart))
  219. do_get_results(mythr, true);
  220. else
  221. {} // TODO: Set a timer to call do_get_results when job is near complete
  222. }
  223. else // no job currently running
  224. do_job_start(mythr);
  225. }
  226. void do_get_results(struct thr_info *mythr, bool proceed_with_new_job)
  227. {
  228. struct cgpu_info *proc = mythr->cgpu;
  229. struct device_drv *api = proc->drv;
  230. struct work *work = mythr->work;
  231. mythr->_job_transition_in_progress = true;
  232. mythr->tv_results_jobstart = mythr->tv_jobstart;
  233. mythr->_proceed_with_new_job = proceed_with_new_job;
  234. if (api->job_get_results)
  235. api->job_get_results(mythr, work);
  236. else
  237. job_results_fetched(mythr);
  238. }
  239. void job_results_fetched(struct thr_info *mythr)
  240. {
  241. if (mythr->_proceed_with_new_job)
  242. do_job_start(mythr);
  243. else
  244. {
  245. struct timeval tv_now;
  246. gettimeofday(&tv_now, NULL);
  247. do_process_results(mythr, &tv_now, mythr->prev_work, true);
  248. }
  249. }
  250. void do_job_start(struct thr_info *mythr)
  251. {
  252. struct cgpu_info *proc = mythr->cgpu;
  253. struct device_drv *api = proc->drv;
  254. thread_reportin(mythr);
  255. api->job_start(mythr);
  256. }
  257. void mt_job_transition(struct thr_info *mythr)
  258. {
  259. struct timeval tv_now;
  260. gettimeofday(&tv_now, NULL);
  261. if (mythr->starting_next_work)
  262. {
  263. mythr->next_work->tv_work_start = tv_now;
  264. if (mythr->prev_work)
  265. free_work(mythr->prev_work);
  266. mythr->prev_work = mythr->work;
  267. mythr->work = mythr->next_work;
  268. mythr->next_work = NULL;
  269. }
  270. mythr->tv_jobstart = tv_now;
  271. mythr->_job_transition_in_progress = false;
  272. }
  273. void job_start_complete(struct thr_info *mythr)
  274. {
  275. struct timeval tv_now;
  276. gettimeofday(&tv_now, NULL);
  277. do_process_results(mythr, &tv_now, mythr->prev_work, false);
  278. }
  279. void job_start_abort(struct thr_info *mythr, bool failure)
  280. {
  281. struct cgpu_info *proc = mythr->cgpu;
  282. if (failure)
  283. proc->deven = DEV_RECOVER_ERR;
  284. mythr->work = NULL;
  285. mythr->_job_transition_in_progress = false;
  286. }
  287. bool do_process_results(struct thr_info *mythr, struct timeval *tvp_now, struct work *work, bool stopping)
  288. {
  289. struct cgpu_info *proc = mythr->cgpu;
  290. struct device_drv *api = proc->drv;
  291. struct timeval tv_hashes;
  292. int64_t hashes = 0;
  293. if (api->job_process_results)
  294. hashes = api->job_process_results(mythr, work, stopping);
  295. thread_reportin(mythr);
  296. if (hashes)
  297. {
  298. timersub(tvp_now, &mythr->tv_results_jobstart, &tv_hashes);
  299. if (!hashes_done(mythr, hashes, &tv_hashes, api->can_limit_work ? &mythr->_max_nonce : NULL))
  300. return false;
  301. }
  302. return true;
  303. }
  304. static
  305. void do_notifier_select(struct thr_info *thr, struct timeval *tvp_timeout)
  306. {
  307. struct cgpu_info *cgpu = thr->cgpu;
  308. struct timeval tv_now;
  309. int maxfd;
  310. fd_set rfds;
  311. gettimeofday(&tv_now, NULL);
  312. FD_ZERO(&rfds);
  313. FD_SET(thr->notifier[0], &rfds);
  314. maxfd = thr->notifier[0];
  315. FD_SET(thr->work_restart_notifier[0], &rfds);
  316. set_maxfd(&maxfd, thr->work_restart_notifier[0]);
  317. if (thr->mutex_request[1] != INVSOCK)
  318. {
  319. FD_SET(thr->mutex_request[0], &rfds);
  320. set_maxfd(&maxfd, thr->mutex_request[0]);
  321. }
  322. if (select(maxfd + 1, &rfds, NULL, NULL, select_timeout(tvp_timeout, &tv_now)) < 0)
  323. return;
  324. if (thr->mutex_request[1] != INVSOCK && FD_ISSET(thr->mutex_request[0], &rfds))
  325. {
  326. // FIXME: This can only handle one request at a time!
  327. pthread_mutex_t *mutexp = &cgpu->device_mutex;
  328. notifier_read(thr->mutex_request);
  329. mutex_lock(mutexp);
  330. pthread_cond_signal(&cgpu->device_cond);
  331. pthread_cond_wait(&cgpu->device_cond, mutexp);
  332. mutex_unlock(mutexp);
  333. }
  334. if (FD_ISSET(thr->notifier[0], &rfds)) {
  335. notifier_read(thr->notifier);
  336. }
  337. if (FD_ISSET(thr->work_restart_notifier[0], &rfds))
  338. notifier_read(thr->work_restart_notifier);
  339. }
  340. void minerloop_async(struct thr_info *mythr)
  341. {
  342. struct thr_info *thr = mythr;
  343. struct cgpu_info *cgpu = mythr->cgpu;
  344. struct device_drv *api = cgpu->drv;
  345. struct timeval tv_now;
  346. struct timeval tv_timeout;
  347. struct cgpu_info *proc;
  348. bool is_running, should_be_running;
  349. if (mythr->work_restart_notifier[1] == -1)
  350. notifier_init(mythr->work_restart_notifier);
  351. while (likely(!cgpu->shutdown)) {
  352. tv_timeout.tv_sec = -1;
  353. gettimeofday(&tv_now, NULL);
  354. for (proc = cgpu; proc; proc = proc->next_proc)
  355. {
  356. mythr = proc->thr[0];
  357. // Nothing should happen while we're starting a job
  358. if (unlikely(mythr->busy_state == TBS_STARTING_JOB))
  359. goto defer_events;
  360. is_running = mythr->work;
  361. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  362. if (should_be_running)
  363. {
  364. if (unlikely(!(is_running || mythr->_job_transition_in_progress)))
  365. {
  366. mt_disable_finish(mythr);
  367. goto djp;
  368. }
  369. if (unlikely(mythr->work_restart))
  370. goto djp;
  371. }
  372. else // ! should_be_running
  373. {
  374. if (unlikely(is_running && !mythr->_job_transition_in_progress))
  375. {
  376. disabled: ;
  377. mythr->tv_morework.tv_sec = -1;
  378. if (mythr->busy_state != TBS_GETTING_RESULTS)
  379. do_get_results(mythr, false);
  380. else
  381. // Avoid starting job when pending result fetch completes
  382. mythr->_proceed_with_new_job = false;
  383. }
  384. }
  385. if (timer_passed(&mythr->tv_morework, &tv_now))
  386. {
  387. djp: ;
  388. if (!do_job_prepare(mythr, &tv_now))
  389. goto disabled;
  390. }
  391. defer_events:
  392. if (timer_passed(&mythr->tv_poll, &tv_now))
  393. api->poll(mythr);
  394. reduce_timeout_to(&tv_timeout, &mythr->tv_morework);
  395. reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
  396. }
  397. do_notifier_select(thr, &tv_timeout);
  398. }
  399. }
  400. static
  401. void do_queue_flush(struct thr_info *mythr)
  402. {
  403. struct cgpu_info *proc = mythr->cgpu;
  404. struct device_drv *api = proc->drv;
  405. api->queue_flush(mythr);
  406. if (mythr->next_work)
  407. {
  408. free_work(mythr->next_work);
  409. mythr->next_work = NULL;
  410. }
  411. }
  412. void minerloop_queue(struct thr_info *thr)
  413. {
  414. struct thr_info *mythr;
  415. struct cgpu_info *cgpu = thr->cgpu;
  416. struct device_drv *api = cgpu->drv;
  417. struct timeval tv_now;
  418. struct timeval tv_timeout;
  419. struct cgpu_info *proc;
  420. bool should_be_running;
  421. struct work *work;
  422. if (thr->work_restart_notifier[1] == -1)
  423. notifier_init(thr->work_restart_notifier);
  424. while (likely(!cgpu->shutdown)) {
  425. tv_timeout.tv_sec = -1;
  426. gettimeofday(&tv_now, NULL);
  427. for (proc = cgpu; proc; proc = proc->next_proc)
  428. {
  429. mythr = proc->thr[0];
  430. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  431. redo:
  432. if (should_be_running)
  433. {
  434. if (unlikely(!mythr->_last_sbr_state))
  435. {
  436. mt_disable_finish(mythr);
  437. mythr->_last_sbr_state = should_be_running;
  438. }
  439. if (unlikely(mythr->work_restart))
  440. {
  441. mythr->work_restart = false;
  442. do_queue_flush(mythr);
  443. }
  444. while (!mythr->queue_full)
  445. {
  446. if (mythr->next_work)
  447. {
  448. work = mythr->next_work;
  449. mythr->next_work = NULL;
  450. }
  451. else
  452. {
  453. request_work(mythr);
  454. // FIXME: Allow get_work to return NULL to retry on notification
  455. work = get_and_prepare_work(mythr);
  456. }
  457. if (!work)
  458. break;
  459. if (!api->queue_append(mythr, work))
  460. mythr->next_work = work;
  461. }
  462. }
  463. else
  464. if (unlikely(mythr->_last_sbr_state))
  465. {
  466. mythr->_last_sbr_state = should_be_running;
  467. do_queue_flush(mythr);
  468. }
  469. if (timer_passed(&mythr->tv_poll, &tv_now))
  470. api->poll(mythr);
  471. should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
  472. if (should_be_running && !mythr->queue_full)
  473. goto redo;
  474. reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
  475. }
  476. do_notifier_select(thr, &tv_timeout);
  477. }
  478. }
  479. void *miner_thread(void *userdata)
  480. {
  481. struct thr_info *mythr = userdata;
  482. struct cgpu_info *cgpu = mythr->cgpu;
  483. struct device_drv *drv = cgpu->drv;
  484. pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  485. char threadname[20];
  486. snprintf(threadname, 20, "miner_%s", cgpu->proc_repr_ns);
  487. RenameThread(threadname);
  488. if (drv->thread_init && !drv->thread_init(mythr)) {
  489. dev_error(cgpu, REASON_THREAD_FAIL_INIT);
  490. for (struct cgpu_info *slave = cgpu->next_proc; slave && !slave->threads; slave = slave->next_proc)
  491. dev_error(slave, REASON_THREAD_FAIL_INIT);
  492. __thr_being_msg(LOG_ERR, mythr, "failure, exiting");
  493. goto out;
  494. }
  495. thread_reportout(mythr);
  496. applog(LOG_DEBUG, "Popping ping in miner thread");
  497. notifier_read(mythr->notifier); // Wait for a notification to start
  498. cgtime(&cgpu->cgminer_stats.start_tv);
  499. if (drv->minerloop)
  500. drv->minerloop(mythr);
  501. else
  502. minerloop_scanhash(mythr);
  503. __thr_being_msg(LOG_NOTICE, mythr, "shutting down");
  504. out: ;
  505. struct cgpu_info *proc = cgpu;
  506. do
  507. {
  508. proc->deven = DEV_DISABLED;
  509. proc->status = LIFE_DEAD2;
  510. }
  511. while ( (proc = proc->next_proc) && !proc->threads);
  512. mythr->getwork = 0;
  513. mythr->has_pth = false;
  514. nmsleep(1000);
  515. if (drv->thread_shutdown)
  516. drv->thread_shutdown(mythr);
  517. notifier_destroy(mythr->notifier);
  518. return NULL;
  519. }
  520. bool add_cgpu(struct cgpu_info *cgpu)
  521. {
  522. int lpcount;
  523. renumber_cgpu(cgpu);
  524. if (!cgpu->procs)
  525. cgpu->procs = 1;
  526. lpcount = cgpu->procs;
  527. cgpu->device = cgpu;
  528. cgpu->dev_repr = malloc(6);
  529. sprintf(cgpu->dev_repr, "%s%2u", cgpu->drv->name, cgpu->device_id % 100);
  530. cgpu->dev_repr_ns = malloc(6);
  531. sprintf(cgpu->dev_repr_ns, "%s%u", cgpu->drv->name, cgpu->device_id % 100);
  532. strcpy(cgpu->proc_repr, cgpu->dev_repr);
  533. sprintf(cgpu->proc_repr_ns, "%s%u", cgpu->drv->name, cgpu->device_id);
  534. cgpu->dev_manufacturer = maybe_strdup(detectone_meta_info.manufacturer);
  535. cgpu->dev_product = maybe_strdup(detectone_meta_info.product);
  536. cgpu->dev_serial = maybe_strdup(detectone_meta_info.serial);
  537. devices_new = realloc(devices_new, sizeof(struct cgpu_info *) * (total_devices_new + lpcount + 1));
  538. devices_new[total_devices_new++] = cgpu;
  539. if (lpcount > 1)
  540. {
  541. int ns;
  542. int tpp = cgpu->threads / lpcount;
  543. struct cgpu_info **nlp_p, *slave;
  544. const bool manylp = (lpcount > 26);
  545. const char *as = (manylp ? "aa" : "a");
  546. // Note, strcpy instead of assigning a byte to get the \0 too
  547. strcpy(&cgpu->proc_repr[5], as);
  548. ns = strlen(cgpu->proc_repr_ns);
  549. strcpy(&cgpu->proc_repr_ns[ns], as);
  550. nlp_p = &cgpu->next_proc;
  551. for (int i = 1; i < lpcount; ++i)
  552. {
  553. slave = malloc(sizeof(*slave));
  554. *slave = *cgpu;
  555. slave->proc_id = i;
  556. if (manylp)
  557. {
  558. slave->proc_repr[5] += i / 26;
  559. slave->proc_repr[6] += i % 26;
  560. slave->proc_repr_ns[ns ] += i / 26;
  561. slave->proc_repr_ns[ns + 1] += i % 26;
  562. }
  563. else
  564. {
  565. slave->proc_repr[5] += i;
  566. slave->proc_repr_ns[ns] += i;
  567. }
  568. slave->threads = tpp;
  569. devices_new[total_devices_new++] = slave;
  570. *nlp_p = slave;
  571. nlp_p = &slave->next_proc;
  572. }
  573. *nlp_p = NULL;
  574. cgpu->proc_id = 0;
  575. cgpu->threads -= (tpp * (lpcount - 1));
  576. }
  577. cgpu->last_device_valid_work = time(NULL);
  578. return true;
  579. }