1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker *
4*288bf522SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker *
8*288bf522SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker *
10*288bf522SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker */
16*288bf522SAndroid Build Coastguard Worker
17*288bf522SAndroid Build Coastguard Worker #include <stdio.h>
18*288bf522SAndroid Build Coastguard Worker #include <sys/time.h>
19*288bf522SAndroid Build Coastguard Worker #include <sys/types.h>
20*288bf522SAndroid Build Coastguard Worker #include <unistd.h>
21*288bf522SAndroid Build Coastguard Worker #include <sys/syscall.h>
22*288bf522SAndroid Build Coastguard Worker #include <stdlib.h>
23*288bf522SAndroid Build Coastguard Worker #include <string.h>
24*288bf522SAndroid Build Coastguard Worker #include <pthread.h>
25*288bf522SAndroid Build Coastguard Worker #include <sys/stat.h>
26*288bf522SAndroid Build Coastguard Worker #include <errno.h>
27*288bf522SAndroid Build Coastguard Worker #include <fcntl.h>
28*288bf522SAndroid Build Coastguard Worker #include <string.h>
29*288bf522SAndroid Build Coastguard Worker #include <assert.h>
30*288bf522SAndroid Build Coastguard Worker #include "ioshark.h"
31*288bf522SAndroid Build Coastguard Worker #include "ioshark_bench.h"
32*288bf522SAndroid Build Coastguard Worker
33*288bf522SAndroid Build Coastguard Worker /*
34*288bf522SAndroid Build Coastguard Worker * The purpose of this code is to convert mmap() calls into
35*288bf522SAndroid Build Coastguard Worker * a mix of (semi)-random reads and writes.
36*288bf522SAndroid Build Coastguard Worker * PROT_READ => 4KB/8KB/16KB random reads.
37*288bf522SAndroid Build Coastguard Worker * PROT_WRITE => adds 4KB random writes.
38*288bf522SAndroid Build Coastguard Worker */
39*288bf522SAndroid Build Coastguard Worker
40*288bf522SAndroid Build Coastguard Worker extern char *progname;
41*288bf522SAndroid Build Coastguard Worker
42*288bf522SAndroid Build Coastguard Worker #define IOSHARK_MAX_MMAP_IOLEN (16*1024)
43*288bf522SAndroid Build Coastguard Worker
44*288bf522SAndroid Build Coastguard Worker #define MMAP_ENTS 16
45*288bf522SAndroid Build Coastguard Worker
46*288bf522SAndroid Build Coastguard Worker struct mmap_io_ent_tab_s {
47*288bf522SAndroid Build Coastguard Worker off_t offset;
48*288bf522SAndroid Build Coastguard Worker size_t len;
49*288bf522SAndroid Build Coastguard Worker };
50*288bf522SAndroid Build Coastguard Worker
51*288bf522SAndroid Build Coastguard Worker struct mmap_io_ent_s {
52*288bf522SAndroid Build Coastguard Worker int num_entries;
53*288bf522SAndroid Build Coastguard Worker struct mmap_io_ent_tab_s table[MMAP_ENTS + 1];
54*288bf522SAndroid Build Coastguard Worker size_t resid;
55*288bf522SAndroid Build Coastguard Worker };
56*288bf522SAndroid Build Coastguard Worker
57*288bf522SAndroid Build Coastguard Worker static void
setup_mmap_io_state(struct mmap_io_ent_s * mio,size_t total_len,off_t offset)58*288bf522SAndroid Build Coastguard Worker setup_mmap_io_state(struct mmap_io_ent_s *mio,
59*288bf522SAndroid Build Coastguard Worker size_t total_len, off_t offset)
60*288bf522SAndroid Build Coastguard Worker {
61*288bf522SAndroid Build Coastguard Worker int slice;
62*288bf522SAndroid Build Coastguard Worker
63*288bf522SAndroid Build Coastguard Worker memset(mio, 0, sizeof(struct mmap_io_ent_s));
64*288bf522SAndroid Build Coastguard Worker mio->resid = total_len;
65*288bf522SAndroid Build Coastguard Worker slice = MAX(IOSHARK_MAX_MMAP_IOLEN,
66*288bf522SAndroid Build Coastguard Worker total_len / MMAP_ENTS);
67*288bf522SAndroid Build Coastguard Worker while (total_len > 0) {
68*288bf522SAndroid Build Coastguard Worker assert(mio->num_entries < MMAP_ENTS + 1);
69*288bf522SAndroid Build Coastguard Worker mio->table[mio->num_entries].offset = offset;
70*288bf522SAndroid Build Coastguard Worker mio->table[mio->num_entries].len =
71*288bf522SAndroid Build Coastguard Worker MIN((u_int64_t)total_len, (u_int64_t)slice);
72*288bf522SAndroid Build Coastguard Worker total_len -= mio->table[mio->num_entries].len;
73*288bf522SAndroid Build Coastguard Worker offset += mio->table[mio->num_entries].len;
74*288bf522SAndroid Build Coastguard Worker mio->num_entries++;
75*288bf522SAndroid Build Coastguard Worker }
76*288bf522SAndroid Build Coastguard Worker }
77*288bf522SAndroid Build Coastguard Worker
78*288bf522SAndroid Build Coastguard Worker static size_t
mmap_getnext_off_len(struct mmap_io_ent_s * mio,off_t * offset)79*288bf522SAndroid Build Coastguard Worker mmap_getnext_off_len(struct mmap_io_ent_s *mio,
80*288bf522SAndroid Build Coastguard Worker off_t *offset)
81*288bf522SAndroid Build Coastguard Worker {
82*288bf522SAndroid Build Coastguard Worker int i;
83*288bf522SAndroid Build Coastguard Worker int find_rand_index[MMAP_ENTS + 1];
84*288bf522SAndroid Build Coastguard Worker int rand_index_len = 0;
85*288bf522SAndroid Build Coastguard Worker size_t iolength;
86*288bf522SAndroid Build Coastguard Worker
87*288bf522SAndroid Build Coastguard Worker if (mio->resid == 0)
88*288bf522SAndroid Build Coastguard Worker return 0;
89*288bf522SAndroid Build Coastguard Worker /* Pick a slot with residual length > 0 at random first */
90*288bf522SAndroid Build Coastguard Worker for (i = 0 ; i < MMAP_ENTS + 1 ; i++) {
91*288bf522SAndroid Build Coastguard Worker if (mio->table[i].len > 0)
92*288bf522SAndroid Build Coastguard Worker find_rand_index[rand_index_len++] = i;
93*288bf522SAndroid Build Coastguard Worker }
94*288bf522SAndroid Build Coastguard Worker i = find_rand_index[rand() % rand_index_len];
95*288bf522SAndroid Build Coastguard Worker /* Randomize iolength 4K-16K */
96*288bf522SAndroid Build Coastguard Worker iolength = ((rand() % 4) + 1) * 4096;
97*288bf522SAndroid Build Coastguard Worker iolength = MIN(mio->table[i].len, iolength);
98*288bf522SAndroid Build Coastguard Worker *offset = mio->table[i].offset;
99*288bf522SAndroid Build Coastguard Worker mio->table[i].offset += iolength;
100*288bf522SAndroid Build Coastguard Worker mio->table[i].len -= iolength;
101*288bf522SAndroid Build Coastguard Worker mio->resid -= iolength;
102*288bf522SAndroid Build Coastguard Worker return iolength;
103*288bf522SAndroid Build Coastguard Worker }
104*288bf522SAndroid Build Coastguard Worker
105*288bf522SAndroid Build Coastguard Worker 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)106*288bf522SAndroid Build Coastguard Worker mmap_do_io(void *db_node, int prot, off_t offset, size_t len,
107*288bf522SAndroid Build Coastguard Worker char **bufp, int *buflen, u_int64_t *op_counts,
108*288bf522SAndroid Build Coastguard Worker struct rw_bytes_s *rw_bytes)
109*288bf522SAndroid Build Coastguard Worker {
110*288bf522SAndroid Build Coastguard Worker char *p;
111*288bf522SAndroid Build Coastguard Worker int ret;
112*288bf522SAndroid Build Coastguard Worker
113*288bf522SAndroid Build Coastguard Worker if (!(prot & IOSHARK_PROT_WRITE)) {
114*288bf522SAndroid Build Coastguard Worker /* Only preads */
115*288bf522SAndroid Build Coastguard Worker p = get_buf(bufp, buflen, len, 0);
116*288bf522SAndroid Build Coastguard Worker ret = pread(files_db_get_fd(db_node),
117*288bf522SAndroid Build Coastguard Worker p, len, offset);
118*288bf522SAndroid Build Coastguard Worker rw_bytes->bytes_read += len;
119*288bf522SAndroid Build Coastguard Worker if (ret < 0) {
120*288bf522SAndroid Build Coastguard Worker fprintf(stderr,
121*288bf522SAndroid Build Coastguard Worker "%s: mapped pread(%s %zu %lu) error fd=%d %s\n",
122*288bf522SAndroid Build Coastguard Worker progname, files_db_get_filename(db_node),
123*288bf522SAndroid Build Coastguard Worker len, offset, files_db_get_fd(db_node),
124*288bf522SAndroid Build Coastguard Worker strerror(errno));
125*288bf522SAndroid Build Coastguard Worker exit(EXIT_FAILURE);
126*288bf522SAndroid Build Coastguard Worker }
127*288bf522SAndroid Build Coastguard Worker op_counts[IOSHARK_MAPPED_PREAD]++;
128*288bf522SAndroid Build Coastguard Worker } else {
129*288bf522SAndroid Build Coastguard Worker /* 50-50 R/W */
130*288bf522SAndroid Build Coastguard Worker if ((rand() % 2) == 1) {
131*288bf522SAndroid Build Coastguard Worker p = get_buf(bufp, buflen, len, 1);
132*288bf522SAndroid Build Coastguard Worker ret = pwrite(files_db_get_fd(db_node),
133*288bf522SAndroid Build Coastguard Worker p, len, offset);
134*288bf522SAndroid Build Coastguard Worker rw_bytes->bytes_written += len;
135*288bf522SAndroid Build Coastguard Worker if (ret < 0) {
136*288bf522SAndroid Build Coastguard Worker #if 0
137*288bf522SAndroid Build Coastguard Worker fprintf(stderr,
138*288bf522SAndroid Build Coastguard Worker "%s: mapped pwrite failed, file unwriteable ? open_flags=%x\n",
139*288bf522SAndroid Build Coastguard Worker progname,
140*288bf522SAndroid Build Coastguard Worker fcntl(files_db_get_fd(db_node), F_GETFL));
141*288bf522SAndroid Build Coastguard Worker exit(EXIT_FAILURE);
142*288bf522SAndroid Build Coastguard Worker #endif
143*288bf522SAndroid Build Coastguard Worker } else
144*288bf522SAndroid Build Coastguard Worker op_counts[IOSHARK_MAPPED_PWRITE]++;
145*288bf522SAndroid Build Coastguard Worker } else {
146*288bf522SAndroid Build Coastguard Worker p = get_buf(bufp, buflen, len, 0);
147*288bf522SAndroid Build Coastguard Worker ret = pread(files_db_get_fd(db_node),
148*288bf522SAndroid Build Coastguard Worker p, len, offset);
149*288bf522SAndroid Build Coastguard Worker rw_bytes->bytes_read += len;
150*288bf522SAndroid Build Coastguard Worker if (ret < 0) {
151*288bf522SAndroid Build Coastguard Worker fprintf(stderr,
152*288bf522SAndroid Build Coastguard Worker "%s: mapped pread(%s %zu %lu) error fd=%d %s\n",
153*288bf522SAndroid Build Coastguard Worker progname, files_db_get_filename(db_node),
154*288bf522SAndroid Build Coastguard Worker len,
155*288bf522SAndroid Build Coastguard Worker offset, files_db_get_fd(db_node),
156*288bf522SAndroid Build Coastguard Worker strerror(errno));
157*288bf522SAndroid Build Coastguard Worker exit(EXIT_FAILURE);
158*288bf522SAndroid Build Coastguard Worker }
159*288bf522SAndroid Build Coastguard Worker op_counts[IOSHARK_MAPPED_PREAD]++;
160*288bf522SAndroid Build Coastguard Worker }
161*288bf522SAndroid Build Coastguard Worker }
162*288bf522SAndroid Build Coastguard Worker }
163*288bf522SAndroid Build Coastguard Worker
164*288bf522SAndroid Build Coastguard Worker 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)165*288bf522SAndroid Build Coastguard Worker ioshark_handle_mmap(void *db_node,
166*288bf522SAndroid Build Coastguard Worker struct ioshark_file_operation *file_op,
167*288bf522SAndroid Build Coastguard Worker char **bufp, int *buflen, u_int64_t *op_counts,
168*288bf522SAndroid Build Coastguard Worker struct rw_bytes_s *rw_bytes)
169*288bf522SAndroid Build Coastguard Worker {
170*288bf522SAndroid Build Coastguard Worker off_t offset = file_op->mmap_offset;
171*288bf522SAndroid Build Coastguard Worker size_t len = file_op->mmap_len;
172*288bf522SAndroid Build Coastguard Worker int prot = file_op->mmap_prot;
173*288bf522SAndroid Build Coastguard Worker struct mmap_io_ent_s mio;
174*288bf522SAndroid Build Coastguard Worker struct stat statbuf;
175*288bf522SAndroid Build Coastguard Worker
176*288bf522SAndroid Build Coastguard Worker if (fstat(files_db_get_fd(db_node), &statbuf) < 0) {
177*288bf522SAndroid Build Coastguard Worker fprintf(stderr,
178*288bf522SAndroid Build Coastguard Worker "%s: fstat failure %s\n",
179*288bf522SAndroid Build Coastguard Worker __func__, strerror(errno));
180*288bf522SAndroid Build Coastguard Worker exit(EXIT_FAILURE);
181*288bf522SAndroid Build Coastguard Worker }
182*288bf522SAndroid Build Coastguard Worker /*
183*288bf522SAndroid Build Coastguard Worker * The size of the file better accommodate offset + len
184*288bf522SAndroid Build Coastguard Worker * Else there is an issue with pre-creation
185*288bf522SAndroid Build Coastguard Worker */
186*288bf522SAndroid Build Coastguard Worker assert(offset + len <= statbuf.st_size);
187*288bf522SAndroid Build Coastguard Worker if (len <= IOSHARK_MAX_MMAP_IOLEN) {
188*288bf522SAndroid Build Coastguard Worker mmap_do_io(db_node, prot, offset, len,
189*288bf522SAndroid Build Coastguard Worker bufp, buflen, op_counts,
190*288bf522SAndroid Build Coastguard Worker rw_bytes);
191*288bf522SAndroid Build Coastguard Worker return;
192*288bf522SAndroid Build Coastguard Worker }
193*288bf522SAndroid Build Coastguard Worker setup_mmap_io_state(&mio, len, offset);
194*288bf522SAndroid Build Coastguard Worker assert(mio.num_entries > 0);
195*288bf522SAndroid Build Coastguard Worker while ((len = mmap_getnext_off_len(&mio, &offset))) {
196*288bf522SAndroid Build Coastguard Worker assert((offset + len) <=
197*288bf522SAndroid Build Coastguard Worker (file_op->mmap_offset + file_op->mmap_len));
198*288bf522SAndroid Build Coastguard Worker mmap_do_io(db_node, prot, offset, len, bufp, buflen,
199*288bf522SAndroid Build Coastguard Worker op_counts, rw_bytes);
200*288bf522SAndroid Build Coastguard Worker }
201*288bf522SAndroid Build Coastguard Worker }
202