forked from luke-jr/bfgminer
-
Notifications
You must be signed in to change notification settings - Fork 10
/
driver-cpu.c
880 lines (738 loc) · 20.9 KB
/
driver-cpu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
/*
* Copyright 2011-2012 Con Kolivas
* Copyright 2011-2013 Luke Dashjr
* Copyright 2010 Jeff Garzik
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version. See COPYING for more details.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifndef WIN32
#include <sys/wait.h>
#include <sys/resource.h>
#endif
#include <libgen.h>
#include "compat.h"
#include "deviceapi.h"
#include "miner.h"
#include "logging.h"
#include "util.h"
#include "driver-cpu.h"
#if defined(unix)
#include <errno.h>
#include <fcntl.h>
#endif
BFG_REGISTER_DRIVER(cpu_drv)
#if defined(__linux) && defined(CPU_ZERO) /* Linux specific policy and affinity management */
#include <sched.h>
static inline void drop_policy(void)
{
struct sched_param param;
#ifdef SCHED_BATCH
#ifdef SCHED_IDLE
if (unlikely(sched_setscheduler(0, SCHED_IDLE, ¶m) == -1))
#endif
sched_setscheduler(0, SCHED_BATCH, ¶m);
#endif
}
static inline void affine_to_cpu(int id, int cpu)
{
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(cpu, &set);
sched_setaffinity(0, sizeof(&set), &set);
applog(LOG_INFO, "Binding cpu mining thread %d to cpu %d", id, cpu);
}
#else
static inline void drop_policy(void)
{
}
static inline void affine_to_cpu(int __maybe_unused id, int __maybe_unused cpu)
{
}
#endif
/* TODO: resolve externals */
extern char *set_int_range(const char *arg, int *i, int min, int max);
extern int dev_from_id(int thr_id);
/* chipset-optimized hash functions */
extern bool ScanHash_4WaySSE2(struct thr_info*, const unsigned char *pmidstate,
unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
const unsigned char *ptarget,
uint32_t max_nonce, uint32_t *last_nonce, uint32_t nonce);
extern bool ScanHash_altivec_4way(struct thr_info*, const unsigned char *pmidstate,
unsigned char *pdata,
unsigned char *phash1, unsigned char *phash,
const unsigned char *ptarget,
uint32_t max_nonce, uint32_t *last_nonce, uint32_t nonce);
extern bool scanhash_via(struct thr_info*, const unsigned char *pmidstate,
unsigned char *pdata,
unsigned char *phash1, unsigned char *phash,
const unsigned char *target,
uint32_t max_nonce, uint32_t *last_nonce, uint32_t n);
extern bool scanhash_c(struct thr_info*, const unsigned char *midstate, unsigned char *data,
unsigned char *hash1, unsigned char *hash,
const unsigned char *target,
uint32_t max_nonce, uint32_t *last_nonce, uint32_t n);
extern bool scanhash_cryptopp(struct thr_info*, const unsigned char *midstate,unsigned char *data,
unsigned char *hash1, unsigned char *hash,
const unsigned char *target,
uint32_t max_nonce, uint32_t *last_nonce, uint32_t n);
extern bool scanhash_asm32(struct thr_info*, const unsigned char *midstate,unsigned char *data,
unsigned char *hash1, unsigned char *hash,
const unsigned char *target,
uint32_t max_nonce, uint32_t *last_nonce, uint32_t nonce);
extern bool scanhash_sse2_64(struct thr_info*, const unsigned char *pmidstate, unsigned char *pdata,
unsigned char *phash1, unsigned char *phash,
const unsigned char *ptarget,
uint32_t max_nonce, uint32_t *last_nonce,
uint32_t nonce);
extern bool scanhash_sse4_64(struct thr_info*, const unsigned char *pmidstate, unsigned char *pdata,
unsigned char *phash1, unsigned char *phash,
const unsigned char *ptarget,
uint32_t max_nonce, uint32_t *last_nonce,
uint32_t nonce);
extern bool scanhash_sse2_32(struct thr_info*, const unsigned char *pmidstate, unsigned char *pdata,
unsigned char *phash1, unsigned char *phash,
const unsigned char *ptarget,
uint32_t max_nonce, uint32_t *last_nonce,
uint32_t nonce);
extern bool scanhash_scrypt(struct thr_info *, const unsigned char *pmidstate, unsigned char *pdata, unsigned char *phash1, unsigned char __maybe_unused *phash, const unsigned char *ptarget, uint32_t max_nonce, uint32_t *last_nonce, uint32_t nonce);
#ifdef WANT_CPUMINE
static size_t max_name_len = 0;
static char *name_spaces_pad = NULL;
const char *algo_names[] = {
[ALGO_C] = "c",
#ifdef WANT_SSE2_4WAY
[ALGO_4WAY] = "4way",
#endif
#ifdef WANT_VIA_PADLOCK
[ALGO_VIA] = "via",
#endif
[ALGO_CRYPTOPP] = "cryptopp",
#ifdef WANT_CRYPTOPP_ASM32
[ALGO_CRYPTOPP_ASM32] = "cryptopp_asm32",
#endif
#ifdef WANT_X8632_SSE2
[ALGO_SSE2_32] = "sse2_32",
#endif
#ifdef WANT_X8664_SSE2
[ALGO_SSE2_64] = "sse2_64",
#endif
#ifdef WANT_X8664_SSE4
[ALGO_SSE4_64] = "sse4_64",
#endif
#ifdef WANT_ALTIVEC_4WAY
[ALGO_ALTIVEC_4WAY] = "altivec_4way",
#endif
#ifdef WANT_SCRYPT
[ALGO_SCRYPT] = "scrypt",
#endif
[ALGO_FASTAUTO] = "fastauto",
[ALGO_AUTO] = "auto",
};
static const sha256_func sha256_funcs[] = {
[ALGO_C] = (sha256_func)scanhash_c,
#ifdef WANT_SSE2_4WAY
[ALGO_4WAY] = (sha256_func)ScanHash_4WaySSE2,
#endif
#ifdef WANT_ALTIVEC_4WAY
[ALGO_ALTIVEC_4WAY] = (sha256_func) ScanHash_altivec_4way,
#endif
#ifdef WANT_VIA_PADLOCK
[ALGO_VIA] = (sha256_func)scanhash_via,
#endif
[ALGO_CRYPTOPP] = (sha256_func)scanhash_cryptopp,
#ifdef WANT_CRYPTOPP_ASM32
[ALGO_CRYPTOPP_ASM32] = (sha256_func)scanhash_asm32,
#endif
#ifdef WANT_X8632_SSE2
[ALGO_SSE2_32] = (sha256_func)scanhash_sse2_32,
#endif
#ifdef WANT_X8664_SSE2
[ALGO_SSE2_64] = (sha256_func)scanhash_sse2_64,
#endif
#ifdef WANT_X8664_SSE4
[ALGO_SSE4_64] = (sha256_func)scanhash_sse4_64,
#endif
#ifdef WANT_SCRYPT
[ALGO_SCRYPT] = (sha256_func)scanhash_scrypt
#endif
};
#endif
#ifdef WANT_CPUMINE
enum sha256_algos opt_algo = ALGO_FASTAUTO;
static bool forced_n_threads;
#endif
static const uint32_t hash1_init[] = {
0,0,0,0,0,0,0,0,
0x80000000,
0,0,0,0,0,0,
0x100,
};
#ifdef WANT_CPUMINE
// Algo benchmark, crash-prone, system independent stage
double bench_algo_stage3(
enum sha256_algos algo
)
{
struct work work __attribute__((aligned(128)));
unsigned char hash1[64];
get_benchmark_work(&work);
static struct thr_info dummy;
struct timeval end;
struct timeval start;
uint32_t max_nonce = opt_algo == ALGO_FASTAUTO ? (1<<8) : (1<<22);
uint32_t last_nonce = 0;
memcpy(&hash1[0], &hash1_init[0], sizeof(hash1));
timer_set_now(&start);
{
sha256_func func = sha256_funcs[algo];
(*func)(
&dummy,
work.midstate,
work.data,
hash1,
work.hash,
work.target,
max_nonce,
&last_nonce,
0
);
}
timer_set_now(&end);
uint64_t usec_end = ((uint64_t)end.tv_sec)*1000*1000 + end.tv_usec;
uint64_t usec_start = ((uint64_t)start.tv_sec)*1000*1000 + start.tv_usec;
uint64_t usec_elapsed = usec_end - usec_start;
double rate = -1.0;
if (0<usec_elapsed) {
rate = (1.0*(last_nonce+1))/usec_elapsed;
}
return rate;
}
#if defined(unix)
// Change non-blocking status on a file descriptor
static void set_non_blocking(
int fd,
int yes
)
{
int flags = fcntl(fd, F_GETFL, 0);
if (flags<0) {
perror("fcntl(GET) failed");
exit(1);
}
flags = yes ? (flags|O_NONBLOCK) : (flags&~O_NONBLOCK);
int r = fcntl(fd, F_SETFL, flags);
if (r<0) {
perror("fcntl(SET) failed");
exit(1);
}
}
#endif // defined(unix)
// Algo benchmark, crash-safe, system-dependent stage
static double bench_algo_stage2(
enum sha256_algos algo
)
{
// Here, the gig is to safely run a piece of code that potentially
// crashes. Unfortunately, the Right Way (tm) to do this is rather
// heavily platform dependent :(
double rate = -1.23457;
#if defined(unix)
// Make a pipe: [readFD, writeFD]
int pfd[2];
int r = pipe(pfd);
if (r<0) {
perror("pipe - failed to create pipe for --algo auto");
exit(1);
}
// Make pipe non blocking
set_non_blocking(pfd[0], 1);
set_non_blocking(pfd[1], 1);
// Don't allow a crashing child to kill the main process
sighandler_t sr0 = signal(SIGPIPE, SIG_IGN);
sighandler_t sr1 = signal(SIGPIPE, SIG_IGN);
if (SIG_ERR==sr0 || SIG_ERR==sr1) {
perror("signal - failed to edit signal mask for --algo auto");
exit(1);
}
// Fork a child to do the actual benchmarking
pid_t child_pid = fork();
if (child_pid<0) {
perror("fork - failed to create a child process for --algo auto");
exit(1);
}
// Do the dangerous work in the child, knowing we might crash
if (0==child_pid) {
// TODO: some umask trickery to prevent coredumps
// Benchmark this algorithm
double r = bench_algo_stage3(algo);
// We survived, send result to parent and bail
int loop_count = 0;
while (1) {
ssize_t bytes_written = write(pfd[1], &r, sizeof(r));
int try_again = (0==bytes_written || (bytes_written<0 && EAGAIN==errno));
int success = (sizeof(r)==(size_t)bytes_written);
if (success)
break;
if (!try_again) {
perror("write - child failed to write benchmark result to pipe");
exit(1);
}
if (5<loop_count) {
applog(LOG_ERR, "child tried %d times to communicate with parent, giving up", loop_count);
exit(1);
}
++loop_count;
sleep(1);
}
exit(0);
}
// Parent waits for a result from child
int loop_count = 0;
while (1) {
// Wait for child to die
int status;
int r = waitpid(child_pid, &status, WNOHANG);
if ((child_pid==r) || (r<0 && ECHILD==errno)) {
// Child died somehow. Grab result and bail
double tmp;
ssize_t bytes_read = read(pfd[0], &tmp, sizeof(tmp));
if (sizeof(tmp)==(size_t)bytes_read)
rate = tmp;
break;
} else if (r<0) {
perror("bench_algo: waitpid failed. giving up.");
exit(1);
}
// Give up on child after a ~60s
if (60<loop_count) {
kill(child_pid, SIGKILL);
waitpid(child_pid, &status, 0);
break;
}
// Wait a bit longer
++loop_count;
sleep(1);
}
// Close pipe
r = close(pfd[0]);
if (r<0) {
perror("close - failed to close read end of pipe for --algo auto");
exit(1);
}
r = close(pfd[1]);
if (r<0) {
perror("close - failed to close read end of pipe for --algo auto");
exit(1);
}
#elif defined(WIN32)
// Get handle to current exe
HINSTANCE module = GetModuleHandle(0);
if (!module) {
applog(LOG_ERR, "failed to retrieve module handle");
exit(1);
}
// Create a unique name
char unique_name[33];
snprintf(
unique_name,
sizeof(unique_name)-1,
"bfgminer-%p",
(void*)module
);
// Create and init a chunked of shared memory
HANDLE map_handle = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security attributes
PAGE_READWRITE, // read/write access
0, // size: high 32-bits
4096, // size: low 32-bits
unique_name // name of map object
);
if (NULL==map_handle) {
applog(LOG_ERR, "could not create shared memory");
exit(1);
}
void *shared_mem = MapViewOfFile(
map_handle, // object to map view of
FILE_MAP_WRITE, // read/write access
0, // high offset: map from
0, // low offset: beginning
0 // default: map entire file
);
if (NULL==shared_mem) {
applog(LOG_ERR, "could not map shared memory");
exit(1);
}
SetEnvironmentVariable("BFGMINER_SHARED_MEM", unique_name);
CopyMemory(shared_mem, &rate, sizeof(rate));
// Get path to current exe
char cmd_line[256 + MAX_PATH];
const size_t n = sizeof(cmd_line)-200;
DWORD size = GetModuleFileName(module, cmd_line, n);
if (0==size) {
applog(LOG_ERR, "failed to retrieve module path");
exit(1);
}
// Construct new command line based on that
char buf[0x20];
snprintf(buf, sizeof(buf), "%d", algo);
SetEnvironmentVariable("BFGMINER_BENCH_ALGO", buf);
// Launch a debug copy of BFGMiner
STARTUPINFO startup_info;
PROCESS_INFORMATION process_info;
ZeroMemory(&startup_info, sizeof(startup_info));
ZeroMemory(&process_info, sizeof(process_info));
startup_info.cb = sizeof(startup_info);
BOOL ok = CreateProcess(
NULL, // No module name (use command line)
cmd_line, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
DEBUG_ONLY_THIS_PROCESS,// We're going to debug the child
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&startup_info, // Pointer to STARTUPINFO structure
&process_info // Pointer to PROCESS_INFORMATION structure
);
if (!ok) {
applog(LOG_ERR, "CreateProcess failed with error %ld\n", (long)GetLastError() );
exit(1);
}
// Debug the child (only clean way to catch exceptions)
while (1) {
// Wait for child to do something
DEBUG_EVENT debug_event;
ZeroMemory(&debug_event, sizeof(debug_event));
BOOL ok = WaitForDebugEvent(&debug_event, 60 * 1000);
if (!ok)
break;
// Decide if event is "normal"
int go_on =
CREATE_PROCESS_DEBUG_EVENT== debug_event.dwDebugEventCode ||
CREATE_THREAD_DEBUG_EVENT == debug_event.dwDebugEventCode ||
EXIT_THREAD_DEBUG_EVENT == debug_event.dwDebugEventCode ||
EXCEPTION_DEBUG_EVENT == debug_event.dwDebugEventCode ||
LOAD_DLL_DEBUG_EVENT == debug_event.dwDebugEventCode ||
OUTPUT_DEBUG_STRING_EVENT == debug_event.dwDebugEventCode ||
UNLOAD_DLL_DEBUG_EVENT == debug_event.dwDebugEventCode;
if (!go_on)
break;
// Some exceptions are also "normal", apparently.
if (EXCEPTION_DEBUG_EVENT== debug_event.dwDebugEventCode) {
int go_on =
EXCEPTION_BREAKPOINT== debug_event.u.Exception.ExceptionRecord.ExceptionCode;
if (!go_on)
break;
}
// If nothing unexpected happened, let child proceed
ContinueDebugEvent(
debug_event.dwProcessId,
debug_event.dwThreadId,
DBG_CONTINUE
);
}
// Clean up child process
TerminateProcess(process_info.hProcess, 1);
CloseHandle(process_info.hProcess);
CloseHandle(process_info.hThread);
// Reap return value and cleanup
CopyMemory(&rate, shared_mem, sizeof(rate));
(void)UnmapViewOfFile(shared_mem);
(void)CloseHandle(map_handle);
#else
// Not linux, not unix, not WIN32 ... do our best
rate = bench_algo_stage3(algo);
#endif // defined(unix)
// Done
return rate;
}
static void bench_algo(
double *best_rate,
enum sha256_algos *best_algo,
enum sha256_algos algo
)
{
size_t n = max_name_len - strlen(algo_names[algo]);
memset(name_spaces_pad, ' ', n);
name_spaces_pad[n] = 0;
applog(
LOG_ERR,
"\"%s\"%s : benchmarking algorithm ...",
algo_names[algo],
name_spaces_pad
);
double rate = bench_algo_stage2(algo);
if (rate<0.0) {
applog(
LOG_ERR,
"\"%s\"%s : algorithm fails on this platform",
algo_names[algo],
name_spaces_pad
);
} else {
applog(
LOG_ERR,
"\"%s\"%s : algorithm runs at %.5f MH/s",
algo_names[algo],
name_spaces_pad,
rate
);
if (*best_rate<rate) {
*best_rate = rate;
*best_algo = algo;
}
}
}
// Figure out the longest algorithm name
void init_max_name_len()
{
size_t i;
size_t nb_names = sizeof(algo_names)/sizeof(algo_names[0]);
for (i=0; i<nb_names; ++i) {
const char *p = algo_names[i];
size_t name_len = p ? strlen(p) : 0;
if (max_name_len<name_len)
max_name_len = name_len;
}
name_spaces_pad = (char*) malloc(max_name_len+16);
if (0==name_spaces_pad) {
perror("malloc failed");
exit(1);
}
}
// Pick the fastest CPU hasher
static enum sha256_algos pick_fastest_algo()
{
double best_rate = -1.0;
enum sha256_algos best_algo = 0;
applog(LOG_ERR, "benchmarking all sha256 algorithms ...");
bench_algo(&best_rate, &best_algo, ALGO_C);
#if defined(WANT_SSE2_4WAY)
bench_algo(&best_rate, &best_algo, ALGO_4WAY);
#endif
#if defined(WANT_VIA_PADLOCK)
bench_algo(&best_rate, &best_algo, ALGO_VIA);
#endif
bench_algo(&best_rate, &best_algo, ALGO_CRYPTOPP);
#if defined(WANT_CRYPTOPP_ASM32)
bench_algo(&best_rate, &best_algo, ALGO_CRYPTOPP_ASM32);
#endif
#if defined(WANT_X8632_SSE2)
bench_algo(&best_rate, &best_algo, ALGO_SSE2_32);
#endif
#if defined(WANT_X8664_SSE2)
bench_algo(&best_rate, &best_algo, ALGO_SSE2_64);
#endif
#if defined(WANT_X8664_SSE4)
bench_algo(&best_rate, &best_algo, ALGO_SSE4_64);
#endif
#if defined(WANT_ALTIVEC_4WAY)
bench_algo(&best_rate, &best_algo, ALGO_ALTIVEC_4WAY);
#endif
size_t n = max_name_len - strlen(algo_names[best_algo]);
memset(name_spaces_pad, ' ', n);
name_spaces_pad[n] = 0;
applog(
LOG_ERR,
"\"%s\"%s : is fastest algorithm at %.5f MH/s",
algo_names[best_algo],
name_spaces_pad,
best_rate
);
return best_algo;
}
/* FIXME: Use asprintf for better errors. */
char *set_algo(const char *arg, enum sha256_algos *algo)
{
enum sha256_algos i;
if (opt_scrypt)
return "Can only use scrypt algorithm";
for (i = 0; i < ARRAY_SIZE(algo_names); i++) {
if (algo_names[i] && !strcmp(arg, algo_names[i])) {
*algo = i;
return NULL;
}
}
return "Unknown algorithm";
}
#ifdef WANT_SCRYPT
void set_scrypt_algo(enum sha256_algos *algo)
{
*algo = ALGO_SCRYPT;
}
#endif
void show_algo(char buf[OPT_SHOW_LEN], const enum sha256_algos *algo)
{
strncpy(buf, algo_names[*algo], OPT_SHOW_LEN);
}
#endif
#ifdef WANT_CPUMINE
char *force_nthreads_int(const char *arg, int *i)
{
forced_n_threads = true;
return set_int_range(arg, i, 0, 9999);
}
#endif
#ifdef WANT_CPUMINE
static int cpu_autodetect()
{
RUNONCE(0);
int i;
// Reckon number of cores in the box
#if defined(WIN32)
{
DWORD_PTR system_am;
DWORD_PTR process_am;
BOOL ok = GetProcessAffinityMask(
GetCurrentProcess(),
&system_am,
&process_am
);
if (!ok) {
applog(LOG_ERR, "couldn't figure out number of processors :(");
num_processors = 1;
} else {
size_t n = 32;
num_processors = 0;
while (n--)
if (process_am & (1<<n))
++num_processors;
}
}
#elif defined(_SC_NPROCESSORS_ONLN)
num_processors = sysconf(_SC_NPROCESSORS_ONLN);
#elif defined(HW_NCPU)
int req[] = { CTL_HW, HW_NCPU };
size_t len = sizeof(num_processors);
v = sysctl(req, 2, &num_processors, &len, NULL, 0);
#else
num_processors = 1;
#endif /* !WIN32 */
if (opt_n_threads < 0 || !forced_n_threads) {
opt_n_threads = num_processors;
}
if (num_processors < 1)
return 0;
cpus = calloc(opt_n_threads, sizeof(struct cgpu_info));
if (unlikely(!cpus))
quit(1, "Failed to calloc cpus");
for (i = 0; i < opt_n_threads; ++i) {
struct cgpu_info *cgpu;
cgpu = &cpus[i];
cgpu->drv = &cpu_drv;
cgpu->deven = DEV_ENABLED;
cgpu->threads = 1;
cgpu->kname = algo_names[opt_algo];
add_cgpu(cgpu);
}
return opt_n_threads;
}
static void cpu_detect()
{
noserial_detect_manual(&cpu_drv, cpu_autodetect);
}
static pthread_mutex_t cpualgo_lock;
static bool cpu_thread_prepare(struct thr_info *thr)
{
struct cgpu_info *cgpu = thr->cgpu;
if (!(cgpu->device_id || thr->device_thread || cgpu->proc_id))
mutex_init(&cpualgo_lock);
thread_reportin(thr);
return true;
}
static uint64_t cpu_can_limit_work(struct thr_info __maybe_unused *thr)
{
return 0xffff;
}
static bool cpu_thread_init(struct thr_info *thr)
{
const int thr_id = thr->id;
struct cgpu_info *cgpu = thr->cgpu;
mutex_lock(&cpualgo_lock);
switch (opt_algo)
{
case ALGO_AUTO:
case ALGO_FASTAUTO:
opt_algo = pick_fastest_algo();
default:
break;
}
mutex_unlock(&cpualgo_lock);
cgpu->kname = algo_names[opt_algo];
if (opt_algo == ALGO_SCRYPT)
cgpu->min_nonce_diff = 1./0x10000;
/* Set worker threads to nice 19 and then preferentially to SCHED_IDLE
* and if that fails, then SCHED_BATCH. No need for this to be an
* error if it fails */
setpriority(PRIO_PROCESS, 0, 19);
drop_policy();
/* Cpu affinity only makes sense if the number of threads is a multiple
* of the number of CPUs */
if (!(opt_n_threads % num_processors))
affine_to_cpu(dev_from_id(thr_id), dev_from_id(thr_id) % num_processors);
return true;
}
static int64_t cpu_scanhash(struct thr_info *thr, struct work *work, int64_t max_nonce)
{
unsigned char hash1[64];
uint32_t first_nonce = work->blk.nonce;
uint32_t last_nonce;
bool rc;
memcpy(&hash1[0], &hash1_init[0], sizeof(hash1));
CPUSearch:
last_nonce = first_nonce;
rc = false;
/* scan nonces for a proof-of-work hash */
{
sha256_func func = sha256_funcs[opt_algo];
rc = (*func)(
thr,
work->midstate,
work->data,
hash1,
work->hash,
work->target,
max_nonce,
&last_nonce,
work->blk.nonce
);
}
/* if nonce found, submit work */
if (unlikely(rc)) {
applog(LOG_DEBUG, "%"PRIpreprv" found something?", thr->cgpu->proc_repr);
submit_nonce(thr, work, le32toh(*(uint32_t*)&work->data[76]));
work->blk.nonce = last_nonce + 1;
goto CPUSearch;
}
else
if (unlikely(last_nonce == first_nonce))
return 0;
work->blk.nonce = last_nonce + 1;
return last_nonce - first_nonce + 1;
}
struct device_drv cpu_drv = {
.dname = "cpu",
.name = "CPU",
.probe_priority = 120,
.supported_algos = POW_SHA256D | POW_SCRYPT,
.drv_detect = cpu_detect,
.thread_prepare = cpu_thread_prepare,
.can_limit_work = cpu_can_limit_work,
.thread_init = cpu_thread_init,
.scanhash = cpu_scanhash,
};
#endif