-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfrw.c
165 lines (152 loc) · 2.83 KB
/
frw.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
#define _XOPEN_SOURCE 500
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <time.h>
#include <signal.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#define PAGE_SZ 4096
void
timeit(char *msg)
{
char *t_msg = "timeit:";
static struct timespec ts1, ts2, *ts;
static int first_in = 0;
first_in = !first_in;
if(first_in)
ts = &ts1;
else
ts = &ts2;
clock_gettime(CLOCK_MONOTONIC, ts);
if(!first_in)
{
if(!msg) msg=t_msg;
printf("%d:%s: time delta msecs=%ld\n", getpid(), msg, (ts2.tv_sec - ts1.tv_sec)*1000 + (ts2.tv_nsec - ts1.tv_nsec)/1000000);
}
}
void
read_fd(int fd, int max_pages, int rand_flag)
{
char page_buf[PAGE_SZ];
int i;
int offset = random();
lseek(fd, offset, random()%2?SEEK_SET:SEEK_END);
for(i=0; i<max_pages; i++)
{
if(rand_flag)
{
if(pread(fd, page_buf, sizeof(page_buf), (random()%max_pages)*PAGE_SZ)<0)
{
perror("pread:");
}
}
else
{
if(read(fd, page_buf, sizeof(page_buf))<0)
{
perror("read:");
}
}
}
}
void
write_fd(int fd, int max_pages, int rand_flag)
{
char page_buf[PAGE_SZ];
int i;
memset(page_buf, random()%255, sizeof(page_buf));
lseek(fd, 0, SEEK_SET);
for(i=0; i<max_pages; i++)
{
if(rand_flag)
{
if(pwrite(fd, page_buf, sizeof(page_buf), (random()%max_pages)*PAGE_SZ)<0);
{
perror("pwrite:");
}
}
else
{
if(write(fd, page_buf, sizeof(page_buf))<0)
{
perror("write:");
}
}
}
}
int slam_fd;
void
sigterm_hdlr(int signo)
{
printf("Quitting\n");
close(slam_fd);
_exit(0);
}
int
main(int argc, char *argv[])
{
char **argp = ++argv;
char *fname;
char *op;
int max_pages;
int rest_secs;
int iteration = 0;
#define NARGS 4
if(argc < NARGS + 1)
{
printf("frw <fname> <op=r/w> <max_pages> <rest_secs>\n");
printf("op = r - read , w - random, s - fsync write\n");
return -2;
}
fname = *argp++;
op = *argp++;
sscanf(*argp++, "%d", &max_pages);
sscanf(*argp++, "%d", &rest_secs);
printf("fname = %s op=%s max_pages = %d rest_secs = %d\n", fname, op, max_pages, rest_secs);
if(op[0] == 'r')
{
slam_fd = open(fname, O_RDWR);
}
else
{
slam_fd = open(fname, O_WRONLY| O_TRUNC);
if(slam_fd < 0 )
slam_fd = creat(fname, 0666);
}
if(slam_fd < 0)
{
perror("open:");
return -2;
}
signal(SIGINT, sigterm_hdlr);
signal(SIGTERM, sigterm_hdlr);
while(1)
{
timeit(NULL);
if(op[0] == 'r')
{
read_fd(slam_fd, max_pages, op[1]=='r');
timeit("read");
}
else
{
write_fd(slam_fd, max_pages, op[1]=='r');
if(op[0] == 's')
{
fsync(slam_fd);
timeit("write fsync");
}
else
{
timeit("write");
}
}
printf("%d:iter=%d\n", getpid(), iteration++);
sleep(rest_secs);
}
return 0;
}