-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdwipe.c
101 lines (86 loc) · 2.01 KB
/
rdwipe.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
#define _POSIX_C_SOURCE 2
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MB 1024*1024
#define BUF_64MB 64*MB
void* rdrand_thread(void*);
typedef struct {
int64_t* buffer;
size_t start;
size_t end;
} thread_data;
int main(int argc, char* argv[]) {
size_t bufsize = BUF_64MB;
size_t cpu_count = sysconf(_SC_NPROCESSORS_ONLN);
int out_fd = STDOUT_FILENO;
char c;
while ((c = getopt(argc, argv, "n:b:d:")) != -1 ) {
switch (c) {
case 'n':
//Thread count
cpu_count = (int)strtol(optarg, NULL, 10);
break;
case 'b':
//Buffer size
bufsize = (int)strtol(optarg, NULL, 10)*MB;
break;
case 'd':
//Device
if (optarg[0] != '-') {
out_fd = open(optarg, O_WRONLY);
}
break;
}
}
pthread_t threads[cpu_count];
int problem = 0;
int64_t* buffer = malloc(bufsize * sizeof(char));
while (1) {
size_t thread_size = bufsize / 8 / cpu_count;
for (size_t i = 0; i < cpu_count; i++) {
thread_data* args = malloc(sizeof(thread_data));
args->buffer = buffer;
args->start = i * thread_size;
args->end = ((i + 1) * thread_size) - 1;
pthread_create(&threads[i], NULL, rdrand_thread, args);
}
for (size_t i = 0; i < cpu_count; i++) {
pthread_join(threads[i], NULL);
}
if (write(out_fd, (char*)buffer, bufsize) != 0) {
int err = errno;
if (err == EPIPE || err == ENOSPC) {
//EPIPE: no problem, reading piped process ended...
//ENOSPC: no problem, disk is full
break;
} else if (err != 0) {
fprintf(stderr, "Error: %s\n", strerror(err));
problem = 1;
break;
}
}
}
free(buffer);
return problem;
}
void* rdrand_thread(void* data) {
thread_data args = *(thread_data*)data;
free(data);
for (size_t i = args.start; i < args.end; i++) {
int64_t random;
__asm__ (
"rdrand %0;"
: "=r" (random)
);
args.buffer[i] = random;
}
pthread_exit(NULL);
}