-
Notifications
You must be signed in to change notification settings - Fork 0
/
dprofiler.c
491 lines (433 loc) · 13.2 KB
/
dprofiler.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
* --------------------------
* Disk Throughput Profiler
* --------------------------
* dprofiler.c
* -------------
* Copyright 2011-2012 (c) Felipe Franciosi <[email protected]>
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Read the README file for the changelog and information on how to
* compile and use this program.
*/
// Global definitions (don't mess with those)
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#define IO_OP_INFO 0
#define IO_OP_READ 1
#define IO_OP_WRITE 2
#define MT_PROGNAME "Disk Throughput Profiler"
#ifdef CLOCK_MONOTONIC_RAW
#define MT_CLOCK CLOCK_MONOTONIC_RAW
#else
#define MT_CLOCK CLOCK_MONOTONIC
#endif
// Header files
#include <stdio.h> // printf, perror, snprintf, fopen, fputs, fclose
#include <stdlib.h> // posix_memalign, free
#include <string.h> // memset
#include <inttypes.h> // PRI*
#include <sys/types.h> // open, lseek64, getpid
#include <sys/stat.h> // open
#include <fcntl.h> // open
#include <unistd.h> // close, lseek64, getpid, getpagesize, read
#include <time.h> // clock_gettime
// Global default definitions
#define MT_DATAFILE "dprofiler" // Default File prefix for data output
#define MT_BUFSIZE 1024*1024 // Default 1MB i/o operations
#define MT_WZERO 1 // Default write behaviour
#define MT_OSYNC 0 // Default usage of O_SYNC flag upon open()
#define MT_MBSPR 0 // Default output is 'us' and not MB/s
#define MT_IO_OP IO_OP_INFO // Default IO_OP to INFO
// Auxiliary functions
void usage(char *argv0){
// Local variables
int i; // Temporary integer
// Print help
for (i=0; i<strlen(MT_PROGNAME); i++) fprintf(stderr, "-");
fprintf(stderr, "\n%s\n", MT_PROGNAME);
for (i=0; i<strlen(MT_PROGNAME); i++) fprintf(stderr, "-");
fprintf(stderr, "\nUsage: %s < -r | -w | -i > < -d dev_name >\n", argv0);
fprintf(stderr, " [ -hmv[v] ] [ -b <buf_size> ] [ -g <grp_size> ] [ -o <datafile> ]\n");
fprintf(stderr, " -r | -w | -i Read from, write to or simply print info of the device.\n");
fprintf(stderr, " (default is -i)\n");
fprintf(stderr, " -d dev_name Specify block device to operate on.\n");
fprintf(stderr, " !!WARNING!!\n");
fprintf(stderr, " When using -w, as the device will be overwritten.\n");
fprintf(stderr, " -h Print this help message and quit.\n");
// fprintf(stderr, " -z When writing, write only zeros instead of random data (default is random data).\n");
fprintf(stderr, " -m Output in MB/s instead of us.\n");
fprintf(stderr, " -v Increase verbose level (may be used multiple times).\n");
fprintf(stderr, " -b buf_size Specify different buffer size.\n");
fprintf(stderr, " (in bytes, default is %d)\n", MT_BUFSIZE);
fprintf(stderr, " -g grp_size Group measurements and average results accordingly.\n");
fprintf(stderr, " (default is 1)\n");
fprintf(stderr, " -o datafile Specify output datafile name.");
fprintf(stderr, " (default=%s<pid>.dat)\n", MT_DATAFILE);
fprintf(stderr, " -y Open device with O_SYNC (see open(2) man page).\n");
}
// Main function
int main(int argc, char **argv){
// Local variables
// IO related
char *buf = NULL; // IO Buffer
off_t bufsize = 0; // IO Buffer size
char io_op = -1; // IO Operation
char wzero = -1; // Write zeros instead of random data
char osync = -1; // Open device with O_SYNC
char mbspr = -1; // Output format (MB/s or us)
// Datafile related
FILE *datafp = NULL; // Data file file pointer
char *datafn = NULL; // Data file file name
// Block device related
int bdevfd = -1; // Block device file descriptor
char *bdevfn = NULL; // Block device file name
int bdflags; // Block device open flags
off_t bdevsize; // Block device size
// System related
int psize; // System pagesize
// Summary related
ssize_t bytesioed; // Number of bytes io'ed (each io)
u_int64_t count = 0; // Number of bytes io'ed (accumulated)
u_int32_t gsize = 1; // Group size for outputs
u_int32_t gcount; // Group counter
u_int64_t gstart; // Group start address
u_int64_t timeacc; // Time accumulator for grouping
struct timespec st1; // Start time
struct timespec st2; // Finish time
// General
int err = 0; // Return error code
int verbose = 0; // Verbose level
int i,j; // Temporary integers
// Fetch arguments
while ((i = getopt(argc, argv, "rwid:hmzvb:g:o:y")) != -1){
switch (i){
case 'r':
// Set io_op to read, if unset
if (io_op != -1){
fprintf(stderr, "%s: Invalid argument \"-r\", operation mode already set.\n", argv[0]);
err = 1;
goto out;
}
io_op = IO_OP_READ;
break;
case 'w':
// Set io_op to write, if unset
if (io_op != -1){
fprintf(stderr, "%s: Invalid argument \"-w\", operation mode already set.\n", argv[0]);
err = 1;
goto out;
}
io_op = IO_OP_WRITE;
break;
case 'i':
// Set io_op to info, if unset
if (io_op != -1){
fprintf(stderr, "%s: Invalid argument \"-i\", operation mode already set.\n", argv[0]);
err = 1;
goto out;
}
io_op = IO_OP_INFO;
break;
case 'd':
// Set a block device
if (bdevfn){
fprintf(stderr, "%s: Invalid argument \"-d\", device already set.\n", argv[0]);
err = 1;
goto out;
}
if (asprintf(&bdevfn, "%s", optarg) == -1){
perror("asprintf");
fprintf(stderr, "%s: Error allocating memory for block device name.\n", argv[0]);
bdevfn = NULL;
err = 1;
goto out;
}
break;
case 'h':
// Print help
usage(argv[0]);
goto out;
case 'm':
// Set output format
if (mbspr != -1){
fprintf(stderr, "%s: Invalid argument \"-m\", already set to print in MB/s.\n", argv[0]);
err =1;
goto out;
}
mbspr = 1;
break;
case 'z':
// Set write to zeros
if (wzero == 1){
fprintf(stderr, "%s: Invalid argument \"-z\", already set to write zeros.\n", argv[0]);
err = 1;
goto out;
}
wzero = 1;
break;
case 'v':
// Increase verbose level
verbose++;
break;
case 'b':
// Set a different buffer size
if (bufsize > 0){
fprintf(stderr, "%s: Invalid argument \"-b\", buffer already set.\n", argv[0]);
err = 1;
goto out;
}
bufsize = atoi(optarg);
if (bufsize == 0){
fprintf(stderr, "%s: Invalid value for \"-b\". Buffer must be greater than zero.\n", argv[0]);
err = 1;
goto out;
}
break;
case 'g':
// Set group size
gsize = atoi(optarg);
if (gsize == 0){
gsize = 1;
}
break;
case 'o':
// Set datafile name
if (datafn != NULL){
fprintf(stderr, "%s: Invalid argument \"-o\", data file already specified.\n", argv[0]);
err = 1;
goto out;
}
if ((datafn = (char *)malloc(strlen(optarg)+1)) == NULL){
perror("malloc");
fprintf(stderr, "Error reallocating memory for data file name.\n");
err = 1;
goto out;
}
sprintf(datafn, "%s", optarg);
break;
case 'y':
// Set usage of O_SYNC
if (osync != -1){
fprintf(stderr, "%s: Invalid argument \"-y\", usage of O_SYNC already set.\n", argv[0]);
err = 1;
goto out;
}
osync = 1;
break;
}
}
// Verify if operation mode was set
if (io_op == -1){
io_op = MT_IO_OP;
}
// Verify if a block device has been specified
if (bdevfn == NULL){
fprintf(stderr, "%s: Error, you must specify a block device with \"-d\"\n\n", argv[0]);
usage(argv[0]);
err = 1;
goto out;
}
// If wzero has been turned on, check if writing (double check if the user knows what he is doing)
if ((wzero == 1) && (io_op != IO_OP_WRITE)){
fprintf(stderr, "%s: Error, \"-z\" can only be used with \"-w\".\n\n", argv[0]);
usage(argv[0]);
err = 1;
goto out;
}
// Set datafn to default if it hasn't been specified
if (datafn == NULL){
if (asprintf(&datafn, "%s%d.dat", MT_DATAFILE, getpid()) == -1){
perror("asprintf");
fprintf(stderr, "%s: Error allocating memory for data file name.\n", argv[0]);
datafn = NULL;
err = 1;
goto out;
}
}
// Set buffer size to default if it hasn't been specified
if (bufsize == 0){
bufsize = MT_BUFSIZE;
}
// Set SYNC flag to default if it hasn't been specified
if (osync == -1){
osync = MT_OSYNC;
}
// Set write behaviour to default if it hasn't been specified
if (wzero == -1){
wzero = MT_WZERO;
}
// Set default output behaviour
if (mbspr == -1){
mbspr = MT_MBSPR;
}
// Open block device
bdflags = O_RDWR | O_DIRECT | O_LARGEFILE;
if (osync){
bdflags |= O_SYNC;
}
if ((bdevfd = open(bdevfn, bdflags, 0)) < 0){
perror("open");
fprintf(stderr, "%s: Error opening block device \"%s\".\n", argv[0], bdevfn);
err = 1;
goto out;
}
// Move the pointer to the end of the block device (fetchs block device size)
if ((bdevsize = lseek(bdevfd, 0, SEEK_END)) < 0){
perror("lseek");
fprintf(stderr, "%s: Error repositioning offset to eof.\n", argv[0]);
err = 1;
goto out;
}
// Move the pointer back to the beginning of the block device
if (lseek(bdevfd, 0, SEEK_SET) < 0){
perror("lseek");
fprintf(stderr, "%s: Error repositioning offset to start.\n", argv[0]);
err = 1;
goto out;
}
// Fetch system pagesize
psize = getpagesize();
// Allocates rbuf according to the systems page size
if (posix_memalign((void **)&(buf), psize, bufsize) != 0){
//perror("posix_memalign"); // posix_memalign doesn't set errno.
fprintf(stderr, "%s: Error malloc'ing aligned buf, %"PRIu64" bytes long.\n", argv[0], bufsize);
err = 1;
goto out;
}
// Open datafile
if ((io_op != IO_OP_INFO) && ((datafp = fopen(datafn, "w")) == NULL)){
perror("fopen");
fprintf(stderr, "%s: Error opening datafile \"%s\" for writting.\n", argv[0], datafn);
err = 1;
goto out;
}
// Print verbose stuff
if (verbose || io_op == IO_OP_INFO){
for (j=0; j<strlen(MT_PROGNAME); j++) fprintf(stderr, "-");
fprintf(stderr, "\n%s\n", MT_PROGNAME);
for (j=0; j<strlen(MT_PROGNAME); j++) fprintf(stderr, "-");
fprintf(stderr, "\nDevice \"%s\" has %"PRIu64" bytes\n", bdevfn, bdevsize);
fprintf(stderr, "System pagesize is %d bytes long.\n", psize);
fprintf(stderr, "Got aligned buffer at %p.\n", buf);
fprintf(stderr, "Buffer is %"PRIu64" bytes long.\n", bufsize);
fprintf(stderr, "Grouped at every %d outputs.\n", gsize);
fprintf(stderr, "Datafile is \"%s\".\n", datafn);
fprintf(stderr, "------------------------------------------\n");
fflush(stderr);
}
// End here if IO_OP_INFO
if (io_op == IO_OP_INFO){
goto out;
}
// Initialize loop
bytesioed = 1;
gcount = gsize;
gstart = 0;
timeacc = 0;
// IO device until its end
while(bytesioed > 0){
// Prepare buffer
#if 0
switch(io_op){
case IO_OP_WRITE:
// Decide what to write
if (wzero){
// Use only zeros
memset(buf, 0, bufsize);
}else{
// Use random data
memset(buf, 0, bufsize);
// FIXME: I'm just doing zeros here for now.
}
break;
case IO_OP_READ:
// Reset read buffer
memset(buf, 0, bufsize);
break;
}
#else
memset(buf, 0, bufsize);
#endif
// Perform IO op
if (io_op == IO_OP_WRITE){
// Start the clock
clock_gettime(MT_CLOCK, &st1);
// Write
bytesioed = write(bdevfd, buf, bufsize);
// Stop the clock
clock_gettime(MT_CLOCK, &st2);
}else{
// Start the clock
clock_gettime(MT_CLOCK, &st1);
// Read
bytesioed = read(bdevfd, buf, bufsize);
// Stop the clock
clock_gettime(MT_CLOCK, &st2);
}
// Increase the counter
count += bytesioed;
// Accumulate time difference
timeacc += (((long long unsigned)st2.tv_sec*1000000)+((long long unsigned)st2.tv_nsec/1000))-
(((long long unsigned)st1.tv_sec*1000000)+((long long unsigned)st1.tv_nsec/1000));
// Print it whenever group is complete
if (--gcount == 0){
// Write to data file
if (mbspr == 1){
sprintf(buf, "%013"PRIu64" %f\n", gstart, (double)(gsize*bufsize)/(double)timeacc);
}else{
sprintf(buf, "%013"PRIu64" %"PRIu64"\n", gstart, timeacc);
}
fputs(buf, datafp);
fflush(datafp);
// If verbose level is 2, also print on stdout
if (verbose > 1)
{
fprintf(stdout, "%s", buf);
fflush(stdout);
}
// Reset group counter
gcount = gsize;
// Reset group start address
gstart = count;
// Reset time accumulator
timeacc = 0;
}
}
out:
// Free various buffers
if (buf){
// I/O Buffer
free(buf);
}
if (bdevfn){
// Block device file name
free(bdevfn);
}
if (datafn){
// Data file name
free(datafn);
}
// Close data file pointer
if (datafp){
fclose(datafp);
}
// Close block device descriptor
if (bdevfd != -1){
close(bdevfd);
}
// Return
return(err);
}