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
|
/* rawspeed 1.15 - Measure speed of a device. Author: Kees J. Bot
* 26 Apr 1992
*/
#define nil 0
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#if !__minix
#include <sys/time.h>
#endif
#include <fcntl.h>
#include <limits.h>
#include <sys/stat.h>
#define SECTOR_SIZE 512
#define BLK_MAX_SECTORS (sizeof(int) == 2 ? 32 : 64)
#define CHR_MAX_SECTORS (sizeof(int) == 2 ? 63 : 512)
#define ABS_MAX_SECTORS (INT_MAX / SECTOR_SIZE)
#define USEC (!__minix || __minix_vmd)
/* Any good random number generator around? */
#if __minix_vmd || __linux
#define rand random
#define srand srandom
#endif
#if __sun && __svr4__
#define rand lrand48
#define srand srand48
#endif
void report(const char *label)
{
fprintf(stderr, "rawspeed: %s: %s\n", label, strerror(errno));
}
void fatal(const char *label)
{
report(label);
exit(1);
}
void usage(void)
{
fprintf(stderr,
"Usage: rawspeed [-u unit] [-m max] [-t seconds] [-c] [-r limit] device\n");
fprintf(stderr,
" -u unit = best sector multiple (default 2)\n");
fprintf(stderr,
" -m max = read multiples of unit upto 'max' (default %d raw, %d file)\n",
CHR_MAX_SECTORS, BLK_MAX_SECTORS);
fprintf(stderr,
" -t seconds = time to run test (default 10)\n");
fprintf(stderr,
" -c = cache test: rewind after each read or write of max size\n");
fprintf(stderr,
" -r limit = random seeks upto sector 'limit' before reading or writing\n");
exit(1);
}
int done= 0;
void timeout(int sig)
{
done= 1;
}
int main(int argc, char **argv)
{
int i, fd, n= 0, unit= -1, max= -1, cache= 0;
int size, seconds= 10;
long tenthsec;
#if USEC
struct timeval start_time, end_time;
struct timezone dummy;
#else
time_t start_time;
#endif
off_t nbytes= 0, wbytes= 0, randlimit= 0;
char *device, *chunk;
struct stat st;
off_t nseeks= 0;
for (i= 1; i < argc && argv[i][0] == '-' && argv[i][1] != 0; i++) {
char *opt;
if (argv[i][1] == '-' && argv[i][2] == 0) { i++; break; }
for (opt= argv[i]+1; *opt != 0; opt++) {
switch (*opt) {
case 'w':
if (i == argc) usage();
wbytes= atol(argv[++i]) * 1024;
if (wbytes <= 0) usage();
break;
case 'm':
if (i == argc) usage();
max= atoi(argv[++i]);
if (max <= 0 || max > ABS_MAX_SECTORS)
usage();
break;
case 'u':
if (i == argc) usage();
unit= atoi(argv[++i]);
if (unit <= 0 || unit > ABS_MAX_SECTORS)
usage();
break;
case 't':
if (i == argc) usage();
seconds= atoi(argv[++i]);
if (seconds <= 0) usage();
break;
case 'c':
cache= 1;
break;
case 'r':
if (i == argc) usage();
randlimit= atol(argv[++i]);
if (randlimit <= 0) usage();
break;
default:
usage();
}
}
}
if (i != argc - 1) usage();
if (strcmp(argv[i], "-") == 0) {
fd= wbytes == 0 ? 0 : 1;
device= "";
} else {
device= argv[i];
if ((fd= open(device,
wbytes == 0 ? O_RDONLY : O_WRONLY | O_CREAT, 0666)) < 0)
fatal(device);
}
if (max < 0) {
if (fstat(fd, &st) >= 0 && S_ISCHR(st.st_mode))
max= CHR_MAX_SECTORS;
else
max= BLK_MAX_SECTORS;
}
if (unit < 0) unit= max > 1 ? 2 : 1;
unit*= max / unit;
if (unit == 0) usage();
size= unit * SECTOR_SIZE;
randlimit/= unit;
if ((chunk= malloc((size_t) size)) == nil) {
fprintf(stderr, "rawspeed: can't grab %d bytes: %s\n",
size, strerror(errno));
exit(1);
}
/* Touch the pages to get real memory sending other processes to swap.
*/
memset((void *) chunk, 0, (size_t) size);
/* Clean the cache. */
sync();
signal(SIGALRM, timeout);
signal(SIGINT, timeout);
#if USEC
gettimeofday(&start_time, &dummy);
if (randlimit != 0) srand((int) (start_time.tv_sec & INT_MAX));
#else
start_time= time((time_t *) nil);
if (randlimit != 0) srand((int) (start_time & INT_MAX));
#endif
alarm(seconds);
if (wbytes > 0) {
while (!done && (n= write(fd, chunk, size)) > 0
&& (nbytes+= n) < wbytes) {
if (cache && lseek(fd, (off_t) 0, SEEK_SET) == -1)
fatal(device);
if (randlimit != 0) {
if (lseek(fd, (off_t)
(rand() % randlimit * size),
SEEK_SET) == -1)
fatal(device);
nseeks++;
}
}
sync();
} else {
while (!done && (n= read(fd, chunk, size)) > 0) {
nbytes+= n;
if (cache && lseek(fd, (off_t) 0, SEEK_SET) == -1)
fatal(device);
if (randlimit != 0) {
if (lseek(fd, (off_t)
(rand() % randlimit * size),
SEEK_SET) == -1)
fatal(device);
nseeks++;
}
}
}
#if USEC
gettimeofday(&end_time, &dummy);
tenthsec= (end_time.tv_sec - start_time.tv_sec) * 10
+ (end_time.tv_usec - start_time.tv_usec) / 100000;
#else
tenthsec= (time((time_t *) 0) - start_time) * 10;
#endif
if (n < 0 && errno == EINTR) n= 0;
if (n < 0) report(device);
if (nbytes > 0) {
off_t kBpts;
fprintf(stderr, "%lld kB / %ld.%ld s = ",
(nbytes + 512) / 1024,
tenthsec / 10, tenthsec % 10);
if (tenthsec < 5)
fprintf(stderr, "infinite\n");
else {
if (nbytes > LONG_MAX / 100) {
seconds = (tenthsec + 5) / 10;
kBpts= (nbytes + 512L * seconds)
/ (1024L * seconds);
fprintf(stderr, "%lld kB/s\n", kBpts);
} else {
kBpts= (100 * nbytes + 512L * tenthsec)
/ (1024L * tenthsec);
fprintf(stderr, "%lld.%d kB/s\n",
kBpts/10, (int)(kBpts%10));
}
}
}
if (randlimit != 0 && tenthsec >= 5) {
int rpm, disc= 0;
off_t tenthms;
tenthms= (tenthsec * 1000 + nseeks/2) / nseeks;
fprintf(stderr,
"%lld seeks / %ld.%ld s = %lld seeks/s = %lld.%d ms/seek\n",
nseeks, tenthsec / 10, tenthsec % 10,
(nseeks * 10 + tenthsec/2) / tenthsec,
tenthms / 10, (int)(tenthms % 10));
for (rpm= 3600; rpm <= 7200; rpm+= 1800) {
int rotms = (10000L / 2 * 60 + rpm/2) / rpm;
if (tenthms <= rotms) continue;
if (!disc) {
fprintf(stderr,
"discarding av. rotational delay:\n ");
disc= 1;
} else {
fprintf(stderr, ", ");
}
fprintf(stderr, "%lld.%d ms (%d rpm)",
(tenthms - rotms) / 10,
(int)((tenthms - rotms) % 10),
rpm);
}
if (disc) fputc('\n', stdout);
}
return n < 0 ? 1 : 0;
}
|