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
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <fcntl.h>
#include <string.h>
#include <assert.h>
#include "ioshark.h"
#include "ioshark_bench.h"
/*
* The purpose of this code is to convert mmap() calls into
* a mix of (semi)-random reads and writes.
* PROT_READ => 4KB/8KB/16KB random reads.
* PROT_WRITE => adds 4KB random writes.
*/
extern char *progname;
#define IOSHARK_MAX_MMAP_IOLEN (16*1024)
#define MMAP_ENTS 16
struct mmap_io_ent_tab_s {
off_t offset;
size_t len;
};
struct mmap_io_ent_s {
int num_entries;
struct mmap_io_ent_tab_s table[MMAP_ENTS + 1];
size_t resid;
};
static void
setup_mmap_io_state(struct mmap_io_ent_s *mio,
size_t total_len, off_t offset)
{
int slice;
memset(mio, 0, sizeof(struct mmap_io_ent_s));
mio->resid = total_len;
slice = MAX(IOSHARK_MAX_MMAP_IOLEN,
total_len / MMAP_ENTS);
while (total_len > 0) {
assert(mio->num_entries < MMAP_ENTS + 1);
mio->table[mio->num_entries].offset = offset;
mio->table[mio->num_entries].len =
MIN((u_int64_t)total_len, (u_int64_t)slice);
total_len -= mio->table[mio->num_entries].len;
offset += mio->table[mio->num_entries].len;
mio->num_entries++;
}
}
static size_t
mmap_getnext_off_len(struct mmap_io_ent_s *mio,
off_t *offset)
{
int i;
int find_rand_index[MMAP_ENTS + 1];
int rand_index_len = 0;
size_t iolength;
if (mio->resid == 0)
return 0;
/* Pick a slot with residual length > 0 at random first */
for (i = 0 ; i < MMAP_ENTS + 1 ; i++) {
if (mio->table[i].len > 0)
find_rand_index[rand_index_len++] = i;
}
i = find_rand_index[rand() % rand_index_len];
/* Randomize iolength 4K-16K */
iolength = ((rand() % 4) + 1) * 4096;
iolength = MIN(mio->table[i].len, iolength);
*offset = mio->table[i].offset;
mio->table[i].offset += iolength;
mio->table[i].len -= iolength;
mio->resid -= iolength;
return iolength;
}
static void
mmap_do_io(void *db_node, int prot, off_t offset, size_t len,
char **bufp, int *buflen, u_int64_t *op_counts,
struct rw_bytes_s *rw_bytes)
{
char *p;
int ret;
if (!(prot & IOSHARK_PROT_WRITE)) {
/* Only preads */
p = get_buf(bufp, buflen, len, 0);
ret = pread(files_db_get_fd(db_node),
p, len, offset);
rw_bytes->bytes_read += len;
if (ret < 0) {
fprintf(stderr,
"%s: mapped pread(%s %zu %lu) error fd=%d %s\n",
progname, files_db_get_filename(db_node),
len, offset, files_db_get_fd(db_node),
strerror(errno));
exit(EXIT_FAILURE);
}
op_counts[IOSHARK_MAPPED_PREAD]++;
} else {
/* 50-50 R/W */
if ((rand() % 2) == 1) {
p = get_buf(bufp, buflen, len, 1);
ret = pwrite(files_db_get_fd(db_node),
p, len, offset);
rw_bytes->bytes_written += len;
if (ret < 0) {
#if 0
fprintf(stderr,
"%s: mapped pwrite failed, file unwriteable ? open_flags=%x\n",
progname,
fcntl(files_db_get_fd(db_node), F_GETFL));
exit(EXIT_FAILURE);
#endif
} else
op_counts[IOSHARK_MAPPED_PWRITE]++;
} else {
p = get_buf(bufp, buflen, len, 0);
ret = pread(files_db_get_fd(db_node),
p, len, offset);
rw_bytes->bytes_read += len;
if (ret < 0) {
fprintf(stderr,
"%s: mapped pread(%s %zu %lu) error fd=%d %s\n",
progname, files_db_get_filename(db_node),
len,
offset, files_db_get_fd(db_node),
strerror(errno));
exit(EXIT_FAILURE);
}
op_counts[IOSHARK_MAPPED_PREAD]++;
}
}
}
void
ioshark_handle_mmap(void *db_node,
struct ioshark_file_operation *file_op,
char **bufp, int *buflen, u_int64_t *op_counts,
struct rw_bytes_s *rw_bytes)
{
off_t offset = file_op->mmap_offset;
size_t len = file_op->mmap_len;
int prot = file_op->mmap_prot;
struct mmap_io_ent_s mio;
struct stat statbuf;
if (fstat(files_db_get_fd(db_node), &statbuf) < 0) {
fprintf(stderr,
"%s: fstat failure %s\n",
__func__, strerror(errno));
exit(EXIT_FAILURE);
}
/*
* The size of the file better accomodate offset + len
* Else there is an issue with pre-creation
*/
assert(offset + len <= statbuf.st_size);
if (len <= IOSHARK_MAX_MMAP_IOLEN) {
mmap_do_io(db_node, prot, offset, len,
bufp, buflen, op_counts,
rw_bytes);
return;
}
setup_mmap_io_state(&mio, len, offset);
assert(mio.num_entries > 0);
while ((len = mmap_getnext_off_len(&mio, &offset))) {
assert((offset + len) <=
(file_op->mmap_offset + file_op->mmap_len));
mmap_do_io(db_node, prot, offset, len, bufp, buflen,
op_counts, rw_bytes);
}
}
|