-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeker.c
88 lines (72 loc) · 1.69 KB
/
seeker.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
#define _LARGEFILE64_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <signal.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#define BLOCKSIZE 512
#define TIMEOUT 20
int count;
time_t start;
void done()
{
time_t end;
time(&end);
if (end < start + TIMEOUT) {
printf(".");
alarm(1);
return;
}
if (count) {
printf(".\nResults: %d seeks/second, %.2f ms random access time\n",
count / TIMEOUT, 1000.0 * TIMEOUT / count);
}
exit(EXIT_SUCCESS);
}
void handle(const char *string, int error)
{
if (error) {
perror(string);
exit(EXIT_FAILURE);
}
}
int main(int argc, char **argv)
{
char buffer[BLOCKSIZE];
int fd, retval;
unsigned long numblocks;
off64_t offset;
setvbuf(stdout, NULL, _IONBF, 0);
printf("Test disk read random access time\n");
if (argc != 2) {
printf("Usage: seeker <raw disk device>\n");
exit(EXIT_SUCCESS);
}
fd = open(argv[1], O_RDONLY);
handle("open", fd < 0);
retval = ioctl(fd, BLKGETSIZE, &numblocks);
handle("ioctl", retval == -1);
if (BLOCKSIZE*numblocks < 10*1024*1024*1024)
printf("Benchmarking %s [%lu MiB], wait %d seconds ",
argv[1], numblocks*BLOCKSIZE/1024/1024, TIMEOUT);
else
printf("Benchmarking %s [%lu GiB], wait %d seconds ",
argv[1], numblocks*BLOCKSIZE/1024/1024/1024, TIMEOUT);
time(&start);
srand(start);
signal(SIGALRM, &done);
alarm(1);
for (;;) {
offset = (off64_t) numblocks * random() / RAND_MAX;
retval = lseek64(fd, BLOCKSIZE * offset, SEEK_SET);
handle("lseek64", retval == (off64_t) -1);
retval = read(fd, buffer, BLOCKSIZE);
handle("read", retval < 0);
count++;
}
/* notreached */
}