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.
17 * The following is defined so the source can use
18 * lrand48() and srand48().
20 #define __EXTENSIONS__
22 #include "../file_common.h"
25 * The following sample was derived from real-world data
26 * of a production Oracle database.
28 static uint64_t size_distribution
[] = {
49 static uint64_t distribution_n
;
51 static uint8_t randbuf
[BLOCKSZ
];
54 rwc_pwrite(int fd
, const void *buf
, size_t nbytes
, off_t offset
)
56 size_t nleft
= nbytes
;
59 nwrite
= pwrite(fd
, buf
, nbytes
, offset
);
67 (void) fprintf(stderr
, "warning: pwrite: "
68 "wrote %zu out of %zu bytes\n",
69 (nbytes
- nleft
), nbytes
);
76 uint64_t rv
= lrand48() % distribution_n
;
81 i
< sizeof (size_distribution
) / sizeof (size_distribution
[0]);
83 sum
+= size_distribution
[i
];
88 bcopy(randbuf
, buf
, BLOCKSZ
);
90 bzero(buf
, BLOCKSZ
- 10);
92 bzero(buf
, BLOCKSZ
- i
* 512 + 256);
93 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
94 ((uint32_t *)buf
)[0] = lrand48();
100 (void) printf("usage: ");
101 (void) printf("randwritecomp <file> [-s] [nwrites]\n");
106 sequential_writes(int fd
, char *buf
, uint64_t nblocks
, int64_t n
)
108 for (int64_t i
= 0; n
== -1 || i
< n
; i
++) {
111 static uint64_t j
= 0;
113 j
= lrand48() % nblocks
;
114 rwc_pwrite(fd
, buf
, BLOCKSZ
, j
* BLOCKSZ
);
122 random_writes(int fd
, char *buf
, uint64_t nblocks
, int64_t n
)
124 for (int64_t i
= 0; n
== -1 || i
< n
; i
++) {
126 rwc_pwrite(fd
, buf
, BLOCKSZ
, (lrand48() % nblocks
) * BLOCKSZ
);
131 main(int argc
, char *argv
[])
134 char *filename
= NULL
;
145 if (strcmp("-s", argv
[0]) == 0) {
157 n
= strtoull(argv
[0], NULL
, 0);
159 fd
= open(filename
, O_RDWR
|O_CREAT
, 0666);
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
);