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 <fcntl.h>
18*288bf522SAndroid Build Coastguard Worker #include <stdio.h>
19*288bf522SAndroid Build Coastguard Worker #include <sys/types.h>
20*288bf522SAndroid Build Coastguard Worker #include <unistd.h>
21*288bf522SAndroid Build Coastguard Worker #include <stdlib.h>
22*288bf522SAndroid Build Coastguard Worker #include <string.h>
23*288bf522SAndroid Build Coastguard Worker #include <sys/stat.h>
24*288bf522SAndroid Build Coastguard Worker #include <errno.h>
25*288bf522SAndroid Build Coastguard Worker #include "ioshark.h"
26*288bf522SAndroid Build Coastguard Worker #include "compile_ioshark.h"
27*288bf522SAndroid Build Coastguard Worker #include <endian.h>
28*288bf522SAndroid Build Coastguard Worker
29*288bf522SAndroid Build Coastguard Worker extern char *progname;
30*288bf522SAndroid Build Coastguard Worker
31*288bf522SAndroid Build Coastguard Worker static struct files_db_s *files_db_buckets[FILE_DB_HASHSIZE];
32*288bf522SAndroid Build Coastguard Worker static int current_fileno = 1;
33*288bf522SAndroid Build Coastguard Worker static int num_objects = 0;
34*288bf522SAndroid Build Coastguard Worker
35*288bf522SAndroid Build Coastguard Worker static int filename_cache_lookup(char *filename);
36*288bf522SAndroid Build Coastguard Worker
37*288bf522SAndroid Build Coastguard Worker void
files_db_write_objects(FILE * fp)38*288bf522SAndroid Build Coastguard Worker files_db_write_objects(FILE *fp)
39*288bf522SAndroid Build Coastguard Worker {
40*288bf522SAndroid Build Coastguard Worker int i;
41*288bf522SAndroid Build Coastguard Worker struct ioshark_file_state st;
42*288bf522SAndroid Build Coastguard Worker
43*288bf522SAndroid Build Coastguard Worker for (i = 0 ; i < FILE_DB_HASHSIZE ; i++) {
44*288bf522SAndroid Build Coastguard Worker struct files_db_s *db_node, *s;
45*288bf522SAndroid Build Coastguard Worker
46*288bf522SAndroid Build Coastguard Worker db_node = files_db_buckets[i];
47*288bf522SAndroid Build Coastguard Worker while (db_node != NULL) {
48*288bf522SAndroid Build Coastguard Worker st.fileno = db_node->fileno;
49*288bf522SAndroid Build Coastguard Worker st.size = db_node->size;
50*288bf522SAndroid Build Coastguard Worker st.global_filename_ix =
51*288bf522SAndroid Build Coastguard Worker db_node->global_filename_ix;
52*288bf522SAndroid Build Coastguard Worker if (ioshark_write_file_state(fp, &st) != 1) {
53*288bf522SAndroid Build Coastguard Worker fprintf(stderr,
54*288bf522SAndroid Build Coastguard Worker "%s Write error trace.outfile\n",
55*288bf522SAndroid Build Coastguard Worker progname);
56*288bf522SAndroid Build Coastguard Worker exit(EXIT_FAILURE);
57*288bf522SAndroid Build Coastguard Worker }
58*288bf522SAndroid Build Coastguard Worker s = db_node;
59*288bf522SAndroid Build Coastguard Worker db_node = db_node->next;
60*288bf522SAndroid Build Coastguard Worker free(s->filename);
61*288bf522SAndroid Build Coastguard Worker free(s);
62*288bf522SAndroid Build Coastguard Worker }
63*288bf522SAndroid Build Coastguard Worker }
64*288bf522SAndroid Build Coastguard Worker }
65*288bf522SAndroid Build Coastguard Worker
files_db_lookup(char * pathname)66*288bf522SAndroid Build Coastguard Worker void *files_db_lookup(char *pathname)
67*288bf522SAndroid Build Coastguard Worker {
68*288bf522SAndroid Build Coastguard Worker u_int32_t hash;
69*288bf522SAndroid Build Coastguard Worker struct files_db_s *db_node;
70*288bf522SAndroid Build Coastguard Worker
71*288bf522SAndroid Build Coastguard Worker hash = jenkins_one_at_a_time_hash(pathname, strlen(pathname));
72*288bf522SAndroid Build Coastguard Worker hash %= FILE_DB_HASHSIZE;
73*288bf522SAndroid Build Coastguard Worker db_node = files_db_buckets[hash];
74*288bf522SAndroid Build Coastguard Worker while (db_node != NULL) {
75*288bf522SAndroid Build Coastguard Worker if (strcmp(db_node->filename, pathname) == 0)
76*288bf522SAndroid Build Coastguard Worker break;
77*288bf522SAndroid Build Coastguard Worker db_node = db_node->next;
78*288bf522SAndroid Build Coastguard Worker }
79*288bf522SAndroid Build Coastguard Worker return db_node;
80*288bf522SAndroid Build Coastguard Worker }
81*288bf522SAndroid Build Coastguard Worker
files_db_add(char * filename)82*288bf522SAndroid Build Coastguard Worker void *files_db_add(char *filename)
83*288bf522SAndroid Build Coastguard Worker {
84*288bf522SAndroid Build Coastguard Worker u_int32_t hash;
85*288bf522SAndroid Build Coastguard Worker struct files_db_s *db_node;
86*288bf522SAndroid Build Coastguard Worker
87*288bf522SAndroid Build Coastguard Worker if ((db_node = files_db_lookup(filename)))
88*288bf522SAndroid Build Coastguard Worker return db_node;
89*288bf522SAndroid Build Coastguard Worker hash = jenkins_one_at_a_time_hash(filename, strlen(filename));
90*288bf522SAndroid Build Coastguard Worker hash %= FILE_DB_HASHSIZE;
91*288bf522SAndroid Build Coastguard Worker db_node = malloc(sizeof(struct files_db_s));
92*288bf522SAndroid Build Coastguard Worker db_node->filename = strdup(filename);
93*288bf522SAndroid Build Coastguard Worker db_node->global_filename_ix =
94*288bf522SAndroid Build Coastguard Worker filename_cache_lookup(filename);
95*288bf522SAndroid Build Coastguard Worker db_node->fileno = current_fileno++;
96*288bf522SAndroid Build Coastguard Worker db_node->next = files_db_buckets[hash];
97*288bf522SAndroid Build Coastguard Worker db_node->size = 0;
98*288bf522SAndroid Build Coastguard Worker files_db_buckets[hash] = db_node;
99*288bf522SAndroid Build Coastguard Worker num_objects++;
100*288bf522SAndroid Build Coastguard Worker return db_node;
101*288bf522SAndroid Build Coastguard Worker }
102*288bf522SAndroid Build Coastguard Worker
103*288bf522SAndroid Build Coastguard Worker int
files_db_get_total_obj(void)104*288bf522SAndroid Build Coastguard Worker files_db_get_total_obj(void)
105*288bf522SAndroid Build Coastguard Worker {
106*288bf522SAndroid Build Coastguard Worker return num_objects;
107*288bf522SAndroid Build Coastguard Worker }
108*288bf522SAndroid Build Coastguard Worker
109*288bf522SAndroid Build Coastguard Worker static struct ioshark_filename_struct *filename_cache;
110*288bf522SAndroid Build Coastguard Worker static int filename_cache_num_entries;
111*288bf522SAndroid Build Coastguard Worker static int filename_cache_size;
112*288bf522SAndroid Build Coastguard Worker
113*288bf522SAndroid Build Coastguard Worker void
init_filename_cache(void)114*288bf522SAndroid Build Coastguard Worker init_filename_cache(void)
115*288bf522SAndroid Build Coastguard Worker {
116*288bf522SAndroid Build Coastguard Worker static FILE *filename_cache_fp;
117*288bf522SAndroid Build Coastguard Worker struct stat st;
118*288bf522SAndroid Build Coastguard Worker int file_exists = 1;
119*288bf522SAndroid Build Coastguard Worker
120*288bf522SAndroid Build Coastguard Worker if (stat("ioshark_filenames", &st) < 0) {
121*288bf522SAndroid Build Coastguard Worker if (errno != ENOENT) {
122*288bf522SAndroid Build Coastguard Worker fprintf(stderr, "%s Can't stat ioshark_filenames file\n",
123*288bf522SAndroid Build Coastguard Worker progname);
124*288bf522SAndroid Build Coastguard Worker exit(EXIT_FAILURE);
125*288bf522SAndroid Build Coastguard Worker } else {
126*288bf522SAndroid Build Coastguard Worker file_exists = 0;
127*288bf522SAndroid Build Coastguard Worker filename_cache_num_entries = 0;
128*288bf522SAndroid Build Coastguard Worker }
129*288bf522SAndroid Build Coastguard Worker } else {
130*288bf522SAndroid Build Coastguard Worker filename_cache_num_entries = st.st_size /
131*288bf522SAndroid Build Coastguard Worker sizeof(struct ioshark_filename_struct);
132*288bf522SAndroid Build Coastguard Worker }
133*288bf522SAndroid Build Coastguard Worker if (file_exists) {
134*288bf522SAndroid Build Coastguard Worker filename_cache_fp = fopen("ioshark_filenames", "r");
135*288bf522SAndroid Build Coastguard Worker if (filename_cache_fp == NULL) {
136*288bf522SAndroid Build Coastguard Worker fprintf(stderr, "%s Cannot open ioshark_filenames file\n",
137*288bf522SAndroid Build Coastguard Worker progname);
138*288bf522SAndroid Build Coastguard Worker exit(EXIT_FAILURE);
139*288bf522SAndroid Build Coastguard Worker }
140*288bf522SAndroid Build Coastguard Worker }
141*288bf522SAndroid Build Coastguard Worker /* Preallocate a fixed size of entries */
142*288bf522SAndroid Build Coastguard Worker filename_cache_size = filename_cache_num_entries + 1024;
143*288bf522SAndroid Build Coastguard Worker filename_cache = calloc(filename_cache_size,
144*288bf522SAndroid Build Coastguard Worker sizeof(struct ioshark_filename_struct));
145*288bf522SAndroid Build Coastguard Worker if (filename_cache == NULL) {
146*288bf522SAndroid Build Coastguard Worker fprintf(stderr, "%s Can't allocate memory - this is fatal\n",
147*288bf522SAndroid Build Coastguard Worker __func__);
148*288bf522SAndroid Build Coastguard Worker exit(EXIT_FAILURE);
149*288bf522SAndroid Build Coastguard Worker }
150*288bf522SAndroid Build Coastguard Worker if (fread(filename_cache,
151*288bf522SAndroid Build Coastguard Worker sizeof(struct ioshark_filename_struct),
152*288bf522SAndroid Build Coastguard Worker filename_cache_num_entries,
153*288bf522SAndroid Build Coastguard Worker filename_cache_fp) != (size_t)filename_cache_num_entries) {
154*288bf522SAndroid Build Coastguard Worker fprintf(stderr, "%s Can't read ioshark_filenames file\n",
155*288bf522SAndroid Build Coastguard Worker progname);
156*288bf522SAndroid Build Coastguard Worker exit(EXIT_FAILURE);
157*288bf522SAndroid Build Coastguard Worker }
158*288bf522SAndroid Build Coastguard Worker if (file_exists)
159*288bf522SAndroid Build Coastguard Worker fclose(filename_cache_fp);
160*288bf522SAndroid Build Coastguard Worker }
161*288bf522SAndroid Build Coastguard Worker
162*288bf522SAndroid Build Coastguard Worker static int
filename_cache_lookup(char * filename)163*288bf522SAndroid Build Coastguard Worker filename_cache_lookup(char *filename)
164*288bf522SAndroid Build Coastguard Worker {
165*288bf522SAndroid Build Coastguard Worker int ret;
166*288bf522SAndroid Build Coastguard Worker int i;
167*288bf522SAndroid Build Coastguard Worker
168*288bf522SAndroid Build Coastguard Worker for (i = 0 ; i < filename_cache_num_entries ; i++) {
169*288bf522SAndroid Build Coastguard Worker if (strcmp(filename_cache[i].path, filename) == 0)
170*288bf522SAndroid Build Coastguard Worker return i;
171*288bf522SAndroid Build Coastguard Worker }
172*288bf522SAndroid Build Coastguard Worker if (filename_cache_num_entries >= filename_cache_size) {
173*288bf522SAndroid Build Coastguard Worker int newsize;
174*288bf522SAndroid Build Coastguard Worker
175*288bf522SAndroid Build Coastguard Worker /* reallocate the filename cache up first */
176*288bf522SAndroid Build Coastguard Worker filename_cache_size += 1024;
177*288bf522SAndroid Build Coastguard Worker newsize = filename_cache_size *
178*288bf522SAndroid Build Coastguard Worker sizeof(struct ioshark_filename_struct);
179*288bf522SAndroid Build Coastguard Worker filename_cache = realloc(filename_cache, newsize);
180*288bf522SAndroid Build Coastguard Worker if (filename_cache == NULL) {
181*288bf522SAndroid Build Coastguard Worker fprintf(stderr,
182*288bf522SAndroid Build Coastguard Worker "%s Can't allocate memory - this is fatal\n",
183*288bf522SAndroid Build Coastguard Worker __func__);
184*288bf522SAndroid Build Coastguard Worker exit(EXIT_FAILURE);
185*288bf522SAndroid Build Coastguard Worker }
186*288bf522SAndroid Build Coastguard Worker }
187*288bf522SAndroid Build Coastguard Worker strcpy(filename_cache[filename_cache_num_entries].path,
188*288bf522SAndroid Build Coastguard Worker filename);
189*288bf522SAndroid Build Coastguard Worker ret = filename_cache_num_entries;
190*288bf522SAndroid Build Coastguard Worker filename_cache_num_entries++;
191*288bf522SAndroid Build Coastguard Worker return ret;
192*288bf522SAndroid Build Coastguard Worker }
193*288bf522SAndroid Build Coastguard Worker
194*288bf522SAndroid Build Coastguard Worker void
store_filename_cache(void)195*288bf522SAndroid Build Coastguard Worker store_filename_cache(void)
196*288bf522SAndroid Build Coastguard Worker {
197*288bf522SAndroid Build Coastguard Worker static FILE *filename_cache_fp;
198*288bf522SAndroid Build Coastguard Worker
199*288bf522SAndroid Build Coastguard Worker filename_cache_fp = fopen("ioshark_filenames", "w+");
200*288bf522SAndroid Build Coastguard Worker if (filename_cache_fp == NULL) {
201*288bf522SAndroid Build Coastguard Worker fprintf(stderr, "%s Cannot open ioshark_filenames file\n",
202*288bf522SAndroid Build Coastguard Worker progname);
203*288bf522SAndroid Build Coastguard Worker exit(EXIT_FAILURE);
204*288bf522SAndroid Build Coastguard Worker }
205*288bf522SAndroid Build Coastguard Worker if (fwrite(filename_cache,
206*288bf522SAndroid Build Coastguard Worker sizeof(struct ioshark_filename_struct),
207*288bf522SAndroid Build Coastguard Worker filename_cache_num_entries,
208*288bf522SAndroid Build Coastguard Worker filename_cache_fp) != (size_t)filename_cache_num_entries) {
209*288bf522SAndroid Build Coastguard Worker fprintf(stderr, "%s Can't read ioshark_filenames file\n",
210*288bf522SAndroid Build Coastguard Worker progname);
211*288bf522SAndroid Build Coastguard Worker exit(EXIT_FAILURE);
212*288bf522SAndroid Build Coastguard Worker }
213*288bf522SAndroid Build Coastguard Worker fclose(filename_cache_fp);
214*288bf522SAndroid Build Coastguard Worker free(filename_cache);
215*288bf522SAndroid Build Coastguard Worker }
216*288bf522SAndroid Build Coastguard Worker
217*288bf522SAndroid Build Coastguard Worker int
ioshark_write_header(FILE * fp,struct ioshark_header * header)218*288bf522SAndroid Build Coastguard Worker ioshark_write_header(FILE *fp, struct ioshark_header *header)
219*288bf522SAndroid Build Coastguard Worker {
220*288bf522SAndroid Build Coastguard Worker header->version = htobe64(header->version);
221*288bf522SAndroid Build Coastguard Worker header->num_files = htobe64(header->num_files);
222*288bf522SAndroid Build Coastguard Worker header->num_io_operations = htobe64(header->num_io_operations);
223*288bf522SAndroid Build Coastguard Worker return fwrite(header, sizeof(struct ioshark_header), 1, fp);
224*288bf522SAndroid Build Coastguard Worker }
225*288bf522SAndroid Build Coastguard Worker
226*288bf522SAndroid Build Coastguard Worker int
ioshark_write_file_state(FILE * fp,struct ioshark_file_state * state)227*288bf522SAndroid Build Coastguard Worker ioshark_write_file_state(FILE *fp, struct ioshark_file_state *state)
228*288bf522SAndroid Build Coastguard Worker {
229*288bf522SAndroid Build Coastguard Worker state->fileno = htobe64(state->fileno);
230*288bf522SAndroid Build Coastguard Worker state->size = htobe64(state->size);
231*288bf522SAndroid Build Coastguard Worker state->global_filename_ix = htobe64(state->global_filename_ix);
232*288bf522SAndroid Build Coastguard Worker return fwrite(state, sizeof(struct ioshark_file_state), 1, fp);
233*288bf522SAndroid Build Coastguard Worker }
234*288bf522SAndroid Build Coastguard Worker
235*288bf522SAndroid Build Coastguard Worker int
ioshark_write_file_op(FILE * fp,struct ioshark_file_operation * file_op)236*288bf522SAndroid Build Coastguard Worker ioshark_write_file_op(FILE *fp, struct ioshark_file_operation *file_op)
237*288bf522SAndroid Build Coastguard Worker {
238*288bf522SAndroid Build Coastguard Worker enum file_op op = file_op->ioshark_io_op;
239*288bf522SAndroid Build Coastguard Worker
240*288bf522SAndroid Build Coastguard Worker file_op->delta_us = htobe64(file_op->delta_us);
241*288bf522SAndroid Build Coastguard Worker file_op->op_union.enum_size = htobe32(file_op->op_union.enum_size);
242*288bf522SAndroid Build Coastguard Worker file_op->fileno = htobe64(file_op->fileno);
243*288bf522SAndroid Build Coastguard Worker switch (op) {
244*288bf522SAndroid Build Coastguard Worker case IOSHARK_LSEEK:
245*288bf522SAndroid Build Coastguard Worker case IOSHARK_LLSEEK:
246*288bf522SAndroid Build Coastguard Worker file_op->lseek_offset = htobe64(file_op->lseek_offset);
247*288bf522SAndroid Build Coastguard Worker file_op->lseek_action = htobe32(file_op->lseek_action);
248*288bf522SAndroid Build Coastguard Worker break;
249*288bf522SAndroid Build Coastguard Worker case IOSHARK_PREAD64:
250*288bf522SAndroid Build Coastguard Worker case IOSHARK_PWRITE64:
251*288bf522SAndroid Build Coastguard Worker file_op->prw_offset = htobe64(file_op->prw_offset);
252*288bf522SAndroid Build Coastguard Worker file_op->prw_len = htobe64(file_op->prw_len);
253*288bf522SAndroid Build Coastguard Worker break;
254*288bf522SAndroid Build Coastguard Worker case IOSHARK_READ:
255*288bf522SAndroid Build Coastguard Worker case IOSHARK_WRITE:
256*288bf522SAndroid Build Coastguard Worker file_op->rw_len = htobe64(file_op->rw_len);
257*288bf522SAndroid Build Coastguard Worker break;
258*288bf522SAndroid Build Coastguard Worker case IOSHARK_MMAP:
259*288bf522SAndroid Build Coastguard Worker case IOSHARK_MMAP2:
260*288bf522SAndroid Build Coastguard Worker file_op->mmap_offset = htobe64(file_op->mmap_offset);
261*288bf522SAndroid Build Coastguard Worker file_op->mmap_len = htobe64(file_op->mmap_len);
262*288bf522SAndroid Build Coastguard Worker file_op->mmap_prot = htobe32(file_op->mmap_prot);
263*288bf522SAndroid Build Coastguard Worker break;
264*288bf522SAndroid Build Coastguard Worker case IOSHARK_OPEN:
265*288bf522SAndroid Build Coastguard Worker file_op->open_flags = htobe32(file_op->open_flags);
266*288bf522SAndroid Build Coastguard Worker file_op->open_mode = htobe32(file_op->open_mode);
267*288bf522SAndroid Build Coastguard Worker break;
268*288bf522SAndroid Build Coastguard Worker case IOSHARK_FSYNC:
269*288bf522SAndroid Build Coastguard Worker case IOSHARK_FDATASYNC:
270*288bf522SAndroid Build Coastguard Worker break;
271*288bf522SAndroid Build Coastguard Worker case IOSHARK_CLOSE:
272*288bf522SAndroid Build Coastguard Worker break;
273*288bf522SAndroid Build Coastguard Worker default:
274*288bf522SAndroid Build Coastguard Worker fprintf(stderr, "%s: unknown FILE_OP %d\n",
275*288bf522SAndroid Build Coastguard Worker progname, op);
276*288bf522SAndroid Build Coastguard Worker exit(EXIT_FAILURE);
277*288bf522SAndroid Build Coastguard Worker break;
278*288bf522SAndroid Build Coastguard Worker }
279*288bf522SAndroid Build Coastguard Worker return fwrite(file_op, sizeof(struct ioshark_file_operation), 1, fp);
280*288bf522SAndroid Build Coastguard Worker }
281