2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright (c) 2017 by Delphix. All rights reserved.
18 #include "file_common.h"
21 * The following sample was derived from real-world data
22 * of a production Oracle database.
24 static const uint64_t size_distribution
[] = {
45 static uint64_t distribution_n
;
47 static uint8_t randbuf
[BLOCKSZ
];
50 rwc_pwrite(int fd
, const void *buf
, size_t nbytes
, off_t offset
)
52 size_t nleft
= nbytes
;
55 nwrite
= pwrite(fd
, buf
, nbytes
, offset
);
63 (void) fprintf(stderr
, "warning: pwrite: "
64 "wrote %zu out of %zu bytes\n",
65 (nbytes
- nleft
), nbytes
);
72 uint64_t rv
= lrand48() % distribution_n
;
77 i
< sizeof (size_distribution
) / sizeof (size_distribution
[0]);
79 sum
+= size_distribution
[i
];
84 memcpy(buf
, randbuf
, BLOCKSZ
);
86 memset(buf
, 0, BLOCKSZ
- 10);
88 memset(buf
, 0, BLOCKSZ
- i
* 512 + 256);
89 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
90 ((uint32_t *)buf
)[0] = lrand48();
96 (void) puts("usage: randwritecomp [-s] file [nwrites]");
101 sequential_writes(int fd
, char *buf
, uint64_t nblocks
, int64_t n
)
103 for (int64_t i
= 0; n
== -1 || i
< n
; i
++) {
106 static uint64_t j
= 0;
108 j
= lrand48() % nblocks
;
109 rwc_pwrite(fd
, buf
, BLOCKSZ
, j
* BLOCKSZ
);
117 random_writes(int fd
, char *buf
, uint64_t nblocks
, int64_t n
)
119 for (int64_t i
= 0; n
== -1 || i
< n
; i
++) {
121 rwc_pwrite(fd
, buf
, BLOCKSZ
, (lrand48() % nblocks
) * BLOCKSZ
);
126 main(int argc
, char *argv
[])
129 char *filename
= NULL
;
140 if (strcmp("-s", argv
[0]) == 0) {
152 n
= strtoull(argv
[0], NULL
, 0);
154 fd
= open(filename
, O_RDWR
|O_CREAT
, 0666);
156 (void) fprintf(stderr
, "open(%s) failed: %s\n", filename
,
160 err
= fstat(fd
, &ss
);
162 (void) fprintf(stderr
,
163 "error: fstat returned error code %d\n", err
);
167 nblocks
= ss
.st_size
/ BLOCKSZ
;
169 (void) fprintf(stderr
, "error: "
170 "file is too small (min allowed size is %d bytes)\n",
176 for (int i
= 0; i
< BLOCKSZ
; i
++)
177 randbuf
[i
] = lrand48();
181 i
< sizeof (size_distribution
) / sizeof (size_distribution
[0]);
183 distribution_n
+= size_distribution
[i
];
187 sequential_writes(fd
, buf
, nblocks
, n
);
189 random_writes(fd
, buf
, nblocks
, n
);