forked from pooler/cpuminer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpu-miner.c
308 lines (262 loc) · 7.57 KB
/
cpu-miner.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
/*
* Copyright 2010 Jeff Garzik
* Copyright 2012-2017 pooler
* Copyright 2022-2024 antonilol
*
* 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 2 of the License, or (at your option)
* any later version. See COPYING for more details.
*/
#include "cpuminer-config.h"
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#ifdef WIN32
#include <windows.h>
#else
#include <sys/resource.h>
#if HAVE_SYS_SYSCTL_H
#include <sys/types.h>
#if HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#include <sys/sysctl.h>
#endif
#endif
#include "compat.h"
#include "miner.h"
#define PROGRAM_NAME "minerd"
#define LP_SCANTIME 60
#ifdef __linux /* Linux specific policy and affinity management */
#include <sched.h>
static inline void drop_policy(void) {
struct sched_param param;
param.sched_priority = 0;
#ifdef SCHED_IDLE
if (unlikely(sched_setscheduler(0, SCHED_IDLE, ¶m) == -1))
#endif
#ifdef SCHED_BATCH
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);
}
#elif defined(__FreeBSD__) /* FreeBSD specific policy and affinity management */
#include <sys/cpuset.h>
static inline void drop_policy(void) {}
static inline void affine_to_cpu(int id, int cpu) {
cpuset_t set;
CPU_ZERO(&set);
CPU_SET(cpu, &set);
cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpuset_t), &set);
}
#else
static inline void drop_policy(void) {}
static inline void affine_to_cpu(int id, int cpu) {}
#endif
static const uint32_t nonces = 16000000;
static uint32_t blkheader[19];
static uint32_t target[8] = {0};
static int num_processors;
static pthread_mutex_t stats_lock;
static double *thr_hashrates;
static void *miner_thread(void *userdata) {
int thr_id = *(int *)userdata;
uint32_t work[32] = {0};
for (int i = 0; i < 19; i++) {
work[i] = blkheader[i];
}
work[19] = 0xffffffffU / num_processors * thr_id;
work[20] = 0x80000000;
work[31] = 640;
uint32_t end_nonce = 0xffffffffU / num_processors * (thr_id + 1);
int i;
/* 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 (num_processors > 1) {
affine_to_cpu(thr_id, thr_id);
}
while (work[19] < end_nonce) {
unsigned long hashes_done;
struct timeval tv_start, tv_end, diff;
uint32_t step = end_nonce - work[19];
if (step > nonces) {
step = nonces;
}
hashes_done = 0;
gettimeofday(&tv_start, NULL);
/* scan nonces for a proof-of-work hash */
if (scanhash_sha256d(thr_id, work, target, work[19] + step, &hashes_done)) {
uint32_t h[20];
for (uint8_t i = 0; i < 20; i++) {
h[i] = swab32(work[i]);
}
char header_hex[161];
bin2hex(header_hex, (unsigned char *) h, 80);
header_hex[160]='\0';
printf("%s", header_hex);
exit(0);
}
/* record scanhash elapsed time */
gettimeofday(&tv_end, NULL);
timeval_subtract(&diff, &tv_end, &tv_start);
if (diff.tv_usec || diff.tv_sec) {
pthread_mutex_lock(&stats_lock);
thr_hashrates[thr_id] =
hashes_done / (diff.tv_sec + 1e-6 * diff.tv_usec);
pthread_mutex_unlock(&stats_lock);
}
if (thr_id == num_processors - 1) {
double hashrate = 0.;
pthread_mutex_lock(&stats_lock);
for (i = 0; i < num_processors && thr_hashrates[i]; i++)
hashrate += thr_hashrates[i];
pthread_mutex_unlock(&stats_lock);
if (i == num_processors) {
if (hashrate >= 1e9) {
fprintf(stderr, " Hashrate: %.0f MH/s \r", 1e-6 * hashrate);
} else {
fprintf(stderr, " Hashrate: %.2f MH/s \r", 1e-6 * hashrate);
}
}
}
}
fprintf(stderr, "Refreshing work \n");
exit(0);
return NULL;
}
static void show_version(void) {
fprintf(stderr, PACKAGE_STRING "\n built on " __DATE__ "\n features:"
#if defined(USE_ASM) && defined(__i386__)
" i386"
#endif
#if defined(USE_ASM) && defined(__x86_64__)
" x86_64"
" PHE"
#endif
#if defined(USE_ASM) && (defined(__i386__) || defined(__x86_64__))
" SSE2"
#endif
#if defined(__x86_64__) && defined(USE_AVX)
" AVX"
#endif
#if defined(__x86_64__) && defined(USE_AVX2)
" AVX2"
#endif
#if defined(__x86_64__) && defined(USE_XOP)
" XOP"
#endif
#if defined(USE_ASM) && defined(__arm__) && defined(__APCS_32__)
" ARM"
#if defined(__ARM_ARCH_5E__) || defined(__ARM_ARCH_5TE__) || \
defined(__ARM_ARCH_5TEJ__) || defined(__ARM_ARCH_6__) || \
defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || \
defined(__ARM_ARCH_6M__) || defined(__ARM_ARCH_6T2__) || \
defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || \
defined(__ARM_ARCH_7__) || \
defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || \
defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__)
" ARMv5E"
#endif
#if defined(__ARM_NEON__)
" NEON"
#endif
#endif
#if defined(USE_ASM) && (defined(__powerpc__) || defined(__ppc__) || defined(__PPC__))
" PowerPC"
#if defined(__ALTIVEC__)
" AltiVec"
#endif
#endif
"\n");
}
int main(int argc, char *argv[]) {
if (argc > 2 && !strcmp(argv[2], "info")) {
show_version();
}
if (argc < 2) {
fprintf(stderr, "no blockheader argument\n");
return 1;
}
size_t len = strlen(argv[1]);
if (len != (80 - 4) * 2 && len != 80 * 2) {
fprintf(stderr, "invalid length\n");
return 1;
}
for (int i = 0; i < 19; i++) {
char n[9];
memcpy(n, argv[1] + i * 8, 8);
n[8] = '\0';
blkheader[i] = strtoul(n, NULL, 16);
}
const uint32_t nCompact = swab32(blkheader[18]);
// https://github.com/bitcoin/bitcoin/blob/fa99e108e778b5169b3de2ce557af68f1fe0ac0b/src/arith_uint256.cpp#L203-L221
const int nSize = nCompact >> 24;
const uint32_t nWord = nCompact & 0x007fffff;
if (nWord != 0 && (nCompact & 0x00800000) != 0) {
fprintf(stderr, "bits negative");
return 1;
}
if (nWord != 0 && ((nSize > 34) || (nWord > 0xff && nSize > 33) || (nWord > 0xffff && nSize > 32))) {
fprintf(stderr, "bits overflow");
return 1;
}
memcpy((unsigned char *) target + nSize - 3, &nWord, 3);
int i;
pthread_mutex_init(&stats_lock, NULL);
#if defined(WIN32)
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
num_processors = sysinfo.dwNumberOfProcessors;
#elif defined(_SC_NPROCESSORS_ONLN)
num_processors = sysconf(_SC_NPROCESSORS_ONLN);
#elif defined(CTL_HW) && defined(HW_NCPU)
int req[] = { CTL_HW, HW_NCPU };
size_t len = sizeof(num_processors);
sysctl(req, 2, &num_processors, &len, NULL, 0);
#else
num_processors = 1;
#endif
if (num_processors < 1) {
num_processors = 1;
}
int *thr_ids = malloc(num_processors * sizeof(int));
pthread_t *thr_handles = malloc(num_processors * sizeof(pthread_t));
thr_hashrates = calloc(num_processors, sizeof(double));
if (!thr_ids || !thr_handles || !thr_hashrates) {
return 1;
}
/* start mining threads */
for (i = 0; i < num_processors; i++) {
int *userdata = &thr_ids[i];
*userdata = i;
if (unlikely(pthread_create(&thr_handles[i], NULL, miner_thread, userdata))) {
fprintf(stderr, "creating thread %d failed\n", i);
return 1;
}
}
fprintf(stderr, "%d miner threads started\n", num_processors);
/* main loop - simply wait for miner threads to exit */
for (i = 0; i < num_processors; i++) {
pthread_join(thr_handles[i], NULL);
}
free(thr_ids);
free(thr_handles);
free(thr_hashrates);
return 0;
}