-
Notifications
You must be signed in to change notification settings - Fork 15
/
dtmmap.c
530 lines (483 loc) · 15.3 KB
/
dtmmap.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/****************************************************************************
* *
* COPYRIGHT (c) 1988 - 2021 *
* This Software Provided *
* By *
* Robin's Nest Software Inc. *
* *
* Permission to use, copy, modify, distribute and sell this software and *
* its documentation for any purpose and without fee is hereby granted, *
* provided that the above copyright notice appear in all copies and that *
* both that copyright notice and this permission notice appear in the *
* supporting documentation, and that the name of the author not be used *
* in advertising or publicity pertaining to distribution of the software *
* without specific, written prior permission. *
* *
* THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, *
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN *
* NO EVENT SHALL HE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL *
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR *
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS *
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF *
* THIS SOFTWARE. *
* *
****************************************************************************/
/*
* Module: dtmmap.c
* Author: Robin T. Miller
* Date: September 4, 1993
*
* Description:
* Functions to do memory mapped I/O for 'dt' program.
*
* Modification History:
*
* June 20th, 2013 by Robin T Miller
* Mostly a rewrite for multithreaded IO, so starting with new history!
*/
#if defined(MMAP)
#include "dt.h"
#include <limits.h>
#include <sys/mman.h>
#include <sys/stat.h>
#if !defined(MAP_FILE)
# define MAP_FILE 0
#endif /* !defined(MAP_FILE) */
/*
* Forward References:
*/
void reference_data (u_char *buffer, size_t count);
/*
* Declare the memory mapped test functions.
*/
struct dtfuncs mmap_funcs = {
/* tf_open, tf_close, tf_initialize, */
open_file, close_file, nofunc,
/* tf_start_test, tf_end_test, */
mmap_file, nofunc,
/* tf_read_file, tf_read_data, tf_cancel_reads, */
read_file, mmap_read_data, nofunc,
/* tf_write_file, tf_write_data, tf_cancel_writes, */
write_file, mmap_write_data, nofunc,
/* tf_flush_data, tf_verify_data, tf_reopen_file, */
mmap_flush, verify_data, mmap_reopen_file,
/* tf_startup, tf_cleanup, tf_validate_opts */
nofunc, nofunc, mmap_validate_opts
};
/************************************************************************
* *
* mmap_file() Memory map the input or output file. *
* *
* Inputs: dip = The device information pointer. *
* *
* Return Value: *
* Returns 0 / -1 = SUCESS / FAILURE. *
* *
************************************************************************/
int
mmap_file (struct dinfo *dip)
{
int fd = dip->di_fd;
int status = SUCCESS;
/*
* For memory mapped I/O, map the file to a buffer.
*/
if (dip->di_mode == READ_MODE) {
dip->di_mmap_buffer = mmap(NULL, dip->di_data_limit,
PROT_READ, (MAP_FILE|MAP_PRIVATE), fd, (off_t) 0);
} else { /* Output file */
/*
* Set the output file to the specified limit before
* memory mapping the file.
*/
status = ftruncate(fd, dip->di_data_limit);
if (status == FAILURE) {
ReportErrorInfo(dip, dip->di_dname, os_get_error(), "ftruncate", TRUNCATE_OP, True);
return(status);
}
dip->di_mmap_buffer = mmap(NULL, dip->di_data_limit,
(PROT_READ|PROT_WRITE),
(MAP_FILE|MAP_SHARED), fd, (off_t) 0);
}
if (dip->di_mmap_buffer == (u_char *) -1) {
ReportErrorInfo(dip, dip->di_dname, os_get_error(), "mmap", MMAP_OP, True);
status = FAILURE;
} else {
dip->di_mmap_bufptr = dip->di_mmap_buffer;
}
/*
* File positioning options currently ignored... maybe later.
*/
//dip->di_mmap_bufptr += dip->di_file_position;
return (status);
}
/************************************************************************
* *
* mmap_flush() Flush memory map file data to permanent storage. *
* *
* Inputs: dip = The device information pointer. *
* *
* Return Value: *
* Returns 0 / -1 = SUCCESS / FAILURE. *
* *
************************************************************************/
int
mmap_flush (struct dinfo *dip)
{
int status = SUCCESS;
/*
* Sync out modified pages and invalid the address range to
* force them to be obtained from the file system during the
* read pass.
*/
if (dip->di_mode == WRITE_MODE) {
if (dip->di_noprog_flag && optiming_table[MSYNC_OP].opt_timing_flag) {
dip->di_optype = MSYNC_OP;
dip->di_initiated_time = time((time_t *)0);
}
status = msync(dip->di_mmap_buffer, dip->di_dbytes_written, MS_INVALIDATE);
if (dip->di_noprog_flag) {
dip->di_optype = NONE_OP;
dip->di_initiated_time = (time_t) 0;
}
if (status == FAILURE) {
ReportErrorInfo(dip, dip->di_dname, os_get_error(), "msync", MSYNC_OP, True);
}
}
return(status);
}
/************************************************************************
* *
* mmap_reopen_file() - Reopen memory mapped input or output file. *
* *
* Inputs: dip = The device information pointer. *
* oflags = The device/file open flags. *
* *
* Return Value: *
* Returns 0 / -1 = SUCESS / FAILURE. *
* *
************************************************************************/
int
mmap_reopen_file (struct dinfo *dip, int oflags)
{
/*
* For memory mapped files, remove the mappings before closing
* the file.
*/
if (dip->di_mmap_flag) {
if (munmap(dip->di_mmap_buffer, dip->di_data_limit) == FAILURE) {
ReportErrorInfo(dip, dip->di_dname, os_get_error(), "munmap", MUNMAP_OP, True);
return (FAILURE);
}
dip->di_mmap_bufptr = dip->di_mmap_buffer = (u_char *) 0;
}
return ( reopen_file(dip, oflags) );
}
/************************************************************************
* *
* mmap_validate_opts() - Validate Memory Mapped Test Options. *
* *
* Description: *
* This function verifies the options specified for memory mapped *
* file testing are valid. *
* *
* Inputs: dip = The device information pointer. *
* *
* Return Value: *
* Returns SUCESS / FAILURE = Valid / Invalid Options. *
* *
************************************************************************/
int
mmap_validate_opts (struct dinfo *dip)
{
int status = SUCCESS;
/*
* For memory mapped I/O, ensure the user specified a limit, and
* that the block size is a multiple of the page size (a MUST!).
*/
if (dip->di_mmap_flag) {
if (dip->di_data_limit == INFINITY) {
Fprintf(dip, "You must specify a data limit for memory mapped I/O.\n");
status = FAILURE;
} else if (dip->di_block_size % page_size) {
Fprintf(dip,
"Please specify a block size modulo of the page size (%d).\n", page_size);
status = FAILURE;
} else if (dip->di_aio_flag) {
Fprintf(dip, "Cannot enable async I/O with memory mapped I/O.\n");
status = FAILURE;
} else {
status = validate_opts(dip);
}
}
return(status);
}
/************************************************************************
* *
* mmap_read_data() - Read and optionally verify memory mapped data. *
* *
* Inputs: dip = The device information pointer. *
* *
* Outputs: Returns SUCCESS/FAILURE = Ok/Error. *
* *
************************************************************************/
int
mmap_read_data (struct dinfo *dip)
{
ssize_t count;
size_t bsize, dsize;
int status = SUCCESS;
struct dtfuncs *dtf = dip->di_funcs;
u_int32 lba;
/*
* For variable length records, initialize to minimum record size.
*/
if (dip->di_min_size) {
if (dip->di_variable_flag) {
dsize = get_variable (dip);
} else {
dsize = dip->di_min_size;
}
} else {
dsize = dip->di_block_size;
}
/*
* Now read and optionally verify the input records.
*/
while ( (dip->di_error_count < dip->di_error_limit) &&
(dip->di_fbytes_read < dip->di_data_limit) &&
(dip->di_records_read < dip->di_record_limit) ) {
PAUSE_THREAD(dip);
if ( THREAD_TERMINATING(dip) ) break;
if (dip->di_terminating) break;
if ( dip->di_max_data && (dip->di_maxdata_read >= dip->di_max_data) ) {
break;
}
if (dip->di_read_delay) { /* Optional read delay. */
mySleep(dip, dip->di_read_delay);
}
/*
* If data limit was specified, ensure we don't exceed it.
*/
if ( (dip->di_fbytes_read + dsize) > dip->di_data_limit) {
bsize = (dip->di_data_limit - dip->di_fbytes_read);
if (dip->di_debug_flag) {
Printf (dip, "Reading partial record of %d bytes...\n", bsize);
}
} else {
bsize = dsize;
}
count = bsize; /* Paged in by system. */
lba = make_lbdata (dip, dip->di_offset);
if ((dip->di_io_mode == TEST_MODE) && dip->di_compare_flag) {
if (dip->di_iot_pattern) {
lba = init_iotdata (dip, dip->di_pattern_buffer, count, lba, dip->di_lbdata_size);
}
}
/*
* Stop reading when end of file is reached.
*/
if (count == (ssize_t) 0) { /* Pseudo end of file. */
if (dip->di_debug_flag) {
Printf (dip, "End of memory mapped file detected...\n");
}
dip->di_end_of_file = True;
exit_status = END_OF_FILE;
break;
} else {
dip->di_dbytes_read += count;
dip->di_fbytes_read += count;
if ((status = check_read (dip, count, bsize)) == FAILURE) {
break;
}
}
if ((size_t)count == dsize) {
dip->di_full_reads++;
} else {
dip->di_partial_reads++;
}
/*
* Verify the data (unless disabled).
*/
if (dip->di_compare_flag) {
status = (*dtf->tf_verify_data)(dip, dip->di_mmap_bufptr, count, dip->di_pattern, &lba, False);
} else {
/*
* Must reference the data to get it paged in.
*/
reference_data (dip->di_mmap_bufptr, count);
}
dip->di_offset += count;
dip->di_mmap_bufptr += count;
/*
* For variable length records, adjust the next record size.
*/
if (dip->di_min_size) {
if (dip->di_variable_flag) {
dsize = get_variable (dip);
} else {
dsize += dip->di_incr_count;
if (dsize > dip->di_max_size) dsize = dip->di_min_size;
}
}
if ( (dip->di_fbytes_read >= dip->di_data_limit) ||
(++dip->di_records_read >= dip->di_record_limit) ) {
break;
}
#ifdef notdef
/*
* Can't do this right now... if it's not mapped via mmap(), you'll
* get a "Segmentation Fault" and core dump. Need more logic...
*/
if (dip->di_step_offset) dip->di_mmap_bufptr += dip->di_step_offset;
#endif
}
return (status);
}
/************************************************************************
* *
* reference_data() - Reference Data of Memory Mapped File. *
* *
* Description: *
* This function simply references each data byte to force pages *
* to be mapped in by the system (memory mapped file I/O). *
* *
* Inputs: buffer = Data buffer to reference. *
* count = Number of bytes to reference. *
* *
* Return Value: *
* Void. *
* *
************************************************************************/
void
reference_data (u_char *buffer, size_t count)
{
size_t i = (size_t) 0;
u_char *bptr = buffer;
static volatile u_char data;
while (i++ < count) {
data = *bptr++;
}
}
/************************************************************************
* *
* mman_write_data() - Write data to memory mapped output file. *
* *
* Inputs: dip = The device information pointer. *
* *
* Outputs: Returns SUCCESS/FAILURE = Ok/Error. *
* *
************************************************************************/
int
mmap_write_data (struct dinfo *dip)
{
ssize_t count;
size_t bsize, dsize;
int status = SUCCESS;
u_int32 lba;
/*
* For variable length records, initialize to minimum record size.
*/
if (dip->di_min_size) {
dsize = dip->di_min_size;
} else {
dsize = dip->di_block_size;
}
/*
* Now write the specifed number of records.
*/
while ( (dip->di_fbytes_written < dip->di_data_limit) &&
(dip->di_records_written < dip->di_record_limit) ) {
PAUSE_THREAD(dip);
if ( THREAD_TERMINATING(dip) ) break;
if (dip->di_terminating) break;
if ( dip->di_max_data && (dip->di_maxdata_written >= dip->di_max_data) ) {
break;
}
if (dip->di_write_delay) { /* Optional write delay */
mySleep(dip, dip->di_write_delay);
}
/*
* If data limit was specified, ensure we don't exceed it.
*/
if ( (dip->di_fbytes_written + dsize) > dip->di_data_limit) {
bsize = (dip->di_data_limit - dip->di_fbytes_written);
if (dip->di_debug_flag) {
Printf (dip, "Writing partial record of %d bytes...\n",
bsize);
}
} else {
bsize = dsize;
}
count = bsize;
lba = make_lbdata (dip, dip->di_offset);
if ((dip->di_io_mode == TEST_MODE) && dip->di_compare_flag) {
if (dip->di_iot_pattern) {
lba = init_iotdata (dip, dip->di_mmap_bufptr, count, lba, dip->di_lbdata_size);
} else {
fill_buffer (dip, dip->di_mmap_bufptr, count, dip->di_pattern);
}
}
/*
* Initialize the logical block data (if enabled).
*/
if (dip->di_lbdata_flag && dip->di_lbdata_size && !dip->di_iot_pattern) {
lba = init_lbdata (dip, dip->di_mmap_bufptr, count, lba, dip->di_lbdata_size);
}
#if defined(TIMESTAMP)
/*
* If timestamps are enabled, initialize buffer accordingly.
*/
if (dip->di_timestamp_flag) {
init_timestamp(dip, dip->di_mmap_bufptr, count, dip->di_lbdata_size);
}
#endif /* defined(TIMESTAMP) */
dip->di_offset += count;
dip->di_mmap_bufptr += count;
dip->di_dbytes_written += count;
dip->di_fbytes_written += count;
/*
* Stop writing when end of file is reached.
*/
if (count == (ssize_t) 0) { /* Pseudo end of file. */
if (dip->di_debug_flag) {
Printf (dip, "End of memory mapped file reached...\n");
}
dip->di_end_of_file = True;
exit_status = END_OF_FILE;
break;
}
if ((status = check_write (dip, count, bsize, dip->di_offset)) == FAILURE) {
break;
} else {
if ((size_t)count == dsize) {
dip->di_full_writes++;
} else {
dip->di_partial_writes++;
}
}
/*
* For variable length records, adjust the next record size.
*/
if (dip->di_min_size) {
dsize += dip->di_incr_count;
if (dsize > dip->di_max_size) dsize = dip->di_min_size;
}
++dip->di_records_written;
if ( dip->di_fsync_frequency && ((dip->di_records_written % dip->di_fsync_frequency) == 0) ) {
status = (*dip->di_funcs->tf_flush_data)(dip);
if ( (status == FAILURE) && (dip->di_error_count >= dip->di_error_limit) ) {
return (status);
}
}
#ifdef notdef
/*
* Can't do this right now... if it's not mapped via mmap(), you'll
* get a "Segmentation Fault" and core dump. Need more logic...
*/
if (dip->di_step_offset) dip->di_mmap_bufptr += dip->di_step_offset;
#endif
}
return (status);
}
#endif /* defined(MMAP) */