-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_read.c
209 lines (153 loc) · 3.63 KB
/
file_read.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
/*
Small IO Test Programm
GD, Sep 26, 2019
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/param.h>
#include <stdint.h>
#include <string.h>
double deltaT( struct timeval *tv1, struct timeval *tv2)
{
/* return time diff between two timevals as double */
double dt = 1. * ( tv2->tv_sec - tv1->tv_sec ) +
( tv2->tv_usec - tv1->tv_usec ) / 1e6 ;
return dt;
}
double gaus_rnd()
{
/* simple gauss approx with sum of 12 rands */
int i;
double r = 0;
for ( i=0; i<12; i++ ) {
r += (((double) random() )/ RAND_MAX) - 0.5 ;
}
return r ;
}
void usage()
{
printf("%s [-B:M:f: -o outfile] file-name\n", "file_read");
exit(1);
}
int main( int argc, char** argv )
{
char *file;
size_t bufsize = 1024L*128L;
unsigned char * bufptr;
int c, i, fd, fdo;
long mr;
long long ntot = 0, ntoto = 0, maxread = 0;
ssize_t nin, nout;
/* for getopt */
extern char *optarg;
extern int optind;
double ttot;
int output = 0, debug = 0;
if (argc < 2) {
usage();
}
struct timeval tvs, tv, tvnow;
struct timezone tz;
/* for random access read */
double skip_fraction = 0., frnd;
struct stat fsb;
off_t fsize = 0, sret = 0;
/* rnd gen seed */
unsigned int seed;
seed = getpid();
srandom(seed);
const size_t flenmx = 200;
char ofile[flenmx];
ofile[0]='\0';
while( (c = getopt(argc, argv, "Do:hB:M:f:?")) != EOF) {
switch(c) {
case 'D' :
debug = 1;
break;
case 'B' :
bufsize = atol(optarg);
break;
case 'M': /* max size to read in MB */
mr = atol(optarg);
maxread = (uint64_t) mr * 1024L * 1024L;
break;
case 'o' :
output = 1;
strncpy(ofile,optarg,flenmx);
break;
case 'f':
skip_fraction = atof(optarg);
if ( skip_fraction < 1. ) {
skip_fraction = 1./(1.-skip_fraction);
}
break;
case 'h':
case '?':
usage();
}
}
if ( ( argc - optind) != 1) {
usage();
}
file = argv[optind];
bufptr = malloc( bufsize );
gettimeofday(&tvs, &tz);
fd = open(file, O_RDONLY);
if ( fd < 0 ) {
printf("Error opening file %s \n", file);
return(2);
}
if ( output ) {
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
fdo = open( ofile, O_WRONLY | O_CREAT | O_TRUNC, mode);
if ( fdo < 0 ) {
printf("Error opening file %s %d\n", ofile, fdo);
return(3);
}
}
if ( skip_fraction > 0 ) {
fstat( fd, &fsb );
fsize = fsb.st_size;
if ( debug ) {
printf ( "Filesize %ld %s\n", fsize, file );
}
}
int nit = 0;
while ( 1 ) {
nit ++;
nin = read( fd, bufptr, bufsize ) ;
ntot += nin;
if ( output ) {
nout = write( fdo, bufptr, nin ) ;
ntoto += nout;
}
if ( debug ) printf("%d %ld %lld\n", nit, nin, ntot );
if ( nin < bufsize || (maxread > 0 && ntot > maxread) ) {
break;
}
if ( skip_fraction > 0 ) {
/* current pos */
sret = lseek( fd, (off_t) 0, SEEK_CUR );
frnd = bufsize * skip_fraction * (2. + gaus_rnd() );
if ( sret + frnd < 0 ) frnd = 0;
sret = lseek( fd, sret + frnd, SEEK_SET );
if ( debug ) printf("lseek set to %ld\n", sret );
if ( sret >= fsize ) {
break;
}
}
}
close(fd);
if ( output ) close(fdo);
gettimeofday(&tv, &tz);
ttot = deltaT ( &tvs, &tv);
printf("%lld %lld %8.4f %8.2f %ld %s %s\n", ntot, ntoto, ttot,
ntot/(1024.*1024.*ttot), 0L, file, ofile);
free( bufptr );
return(0);
}