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 #ifdef IOSHARK_MAIN
18*288bf522SAndroid Build Coastguard Worker const char *IO_op[] = {
19*288bf522SAndroid Build Coastguard Worker "LSEEK",
20*288bf522SAndroid Build Coastguard Worker "LLSEEK",
21*288bf522SAndroid Build Coastguard Worker "PREAD64",
22*288bf522SAndroid Build Coastguard Worker "PWRITE64",
23*288bf522SAndroid Build Coastguard Worker "READ",
24*288bf522SAndroid Build Coastguard Worker "WRITE",
25*288bf522SAndroid Build Coastguard Worker "MMAP",
26*288bf522SAndroid Build Coastguard Worker "MMAP2",
27*288bf522SAndroid Build Coastguard Worker "OPEN",
28*288bf522SAndroid Build Coastguard Worker "FSYNC",
29*288bf522SAndroid Build Coastguard Worker "FDATASYNC",
30*288bf522SAndroid Build Coastguard Worker "CLOSE",
31*288bf522SAndroid Build Coastguard Worker "MAPPED_PREAD",
32*288bf522SAndroid Build Coastguard Worker "MAPPED_PWRITE",
33*288bf522SAndroid Build Coastguard Worker "MAX_FILE_OP"
34*288bf522SAndroid Build Coastguard Worker };
35*288bf522SAndroid Build Coastguard Worker #endif
36*288bf522SAndroid Build Coastguard Worker
37*288bf522SAndroid Build Coastguard Worker #define MAX(A, B) ((A) > (B) ? (A) : (B))
38*288bf522SAndroid Build Coastguard Worker #define MIN(A, B) ((A) < (B) ? (A) : (B))
39*288bf522SAndroid Build Coastguard Worker
40*288bf522SAndroid Build Coastguard Worker #define MINBUFLEN (16*1024)
41*288bf522SAndroid Build Coastguard Worker
42*288bf522SAndroid Build Coastguard Worker #define FILE_DB_HASHSIZE 8192
43*288bf522SAndroid Build Coastguard Worker
44*288bf522SAndroid Build Coastguard Worker struct files_db_s {
45*288bf522SAndroid Build Coastguard Worker char *filename;
46*288bf522SAndroid Build Coastguard Worker int fileno;
47*288bf522SAndroid Build Coastguard Worker size_t size;
48*288bf522SAndroid Build Coastguard Worker int fd;
49*288bf522SAndroid Build Coastguard Worker int readonly;
50*288bf522SAndroid Build Coastguard Worker int debug_open_flags;
51*288bf522SAndroid Build Coastguard Worker struct files_db_s *next;
52*288bf522SAndroid Build Coastguard Worker };
53*288bf522SAndroid Build Coastguard Worker
54*288bf522SAndroid Build Coastguard Worker struct files_db_handle {
55*288bf522SAndroid Build Coastguard Worker struct files_db_s *files_db_buckets[FILE_DB_HASHSIZE];
56*288bf522SAndroid Build Coastguard Worker };
57*288bf522SAndroid Build Coastguard Worker
58*288bf522SAndroid Build Coastguard Worker struct IO_operation_s {
59*288bf522SAndroid Build Coastguard Worker char *IO_op;
60*288bf522SAndroid Build Coastguard Worker };
61*288bf522SAndroid Build Coastguard Worker
62*288bf522SAndroid Build Coastguard Worker struct rw_bytes_s {
63*288bf522SAndroid Build Coastguard Worker u_int64_t bytes_read;
64*288bf522SAndroid Build Coastguard Worker u_int64_t bytes_written;
65*288bf522SAndroid Build Coastguard Worker };
66*288bf522SAndroid Build Coastguard Worker
67*288bf522SAndroid Build Coastguard Worker static inline void
files_db_update_size(void * node,u_int64_t new_size)68*288bf522SAndroid Build Coastguard Worker files_db_update_size(void *node, u_int64_t new_size)
69*288bf522SAndroid Build Coastguard Worker {
70*288bf522SAndroid Build Coastguard Worker struct files_db_s *db_node = (struct files_db_s *)node;
71*288bf522SAndroid Build Coastguard Worker
72*288bf522SAndroid Build Coastguard Worker if (db_node->size < new_size)
73*288bf522SAndroid Build Coastguard Worker db_node->size = new_size;
74*288bf522SAndroid Build Coastguard Worker }
75*288bf522SAndroid Build Coastguard Worker
76*288bf522SAndroid Build Coastguard Worker static inline void
files_db_update_filename(void * node,char * filename)77*288bf522SAndroid Build Coastguard Worker files_db_update_filename(void *node, char *filename)
78*288bf522SAndroid Build Coastguard Worker {
79*288bf522SAndroid Build Coastguard Worker ((struct files_db_s *)node)->filename = strdup(filename);
80*288bf522SAndroid Build Coastguard Worker }
81*288bf522SAndroid Build Coastguard Worker
82*288bf522SAndroid Build Coastguard Worker static inline int
files_db_get_fileno(void * node)83*288bf522SAndroid Build Coastguard Worker files_db_get_fileno(void *node)
84*288bf522SAndroid Build Coastguard Worker {
85*288bf522SAndroid Build Coastguard Worker return (((struct files_db_s *)node)->fileno);
86*288bf522SAndroid Build Coastguard Worker }
87*288bf522SAndroid Build Coastguard Worker
88*288bf522SAndroid Build Coastguard Worker static inline int
files_db_get_fd(void * node)89*288bf522SAndroid Build Coastguard Worker files_db_get_fd(void *node)
90*288bf522SAndroid Build Coastguard Worker {
91*288bf522SAndroid Build Coastguard Worker return (((struct files_db_s *)node)->fd);
92*288bf522SAndroid Build Coastguard Worker }
93*288bf522SAndroid Build Coastguard Worker
94*288bf522SAndroid Build Coastguard Worker static inline char *
files_db_get_filename(void * node)95*288bf522SAndroid Build Coastguard Worker files_db_get_filename(void *node)
96*288bf522SAndroid Build Coastguard Worker {
97*288bf522SAndroid Build Coastguard Worker return (((struct files_db_s *)node)->filename);
98*288bf522SAndroid Build Coastguard Worker }
99*288bf522SAndroid Build Coastguard Worker
100*288bf522SAndroid Build Coastguard Worker static inline int
files_db_readonly(void * node)101*288bf522SAndroid Build Coastguard Worker files_db_readonly(void *node)
102*288bf522SAndroid Build Coastguard Worker {
103*288bf522SAndroid Build Coastguard Worker return (((struct files_db_s *)node)->readonly);
104*288bf522SAndroid Build Coastguard Worker }
105*288bf522SAndroid Build Coastguard Worker
106*288bf522SAndroid Build Coastguard Worker static inline u_int64_t
get_msecs(struct timeval * tv)107*288bf522SAndroid Build Coastguard Worker get_msecs(struct timeval *tv)
108*288bf522SAndroid Build Coastguard Worker {
109*288bf522SAndroid Build Coastguard Worker return ((tv->tv_sec * 1000) + (tv->tv_usec / 1000));
110*288bf522SAndroid Build Coastguard Worker }
111*288bf522SAndroid Build Coastguard Worker
112*288bf522SAndroid Build Coastguard Worker static inline u_int64_t
get_usecs(struct timeval * tv)113*288bf522SAndroid Build Coastguard Worker get_usecs(struct timeval *tv)
114*288bf522SAndroid Build Coastguard Worker {
115*288bf522SAndroid Build Coastguard Worker return (tv->tv_usec % 1000);
116*288bf522SAndroid Build Coastguard Worker }
117*288bf522SAndroid Build Coastguard Worker
118*288bf522SAndroid Build Coastguard Worker static inline void
update_delta_time(struct timeval * start,struct timeval * destination)119*288bf522SAndroid Build Coastguard Worker update_delta_time(struct timeval *start,
120*288bf522SAndroid Build Coastguard Worker struct timeval *destination)
121*288bf522SAndroid Build Coastguard Worker {
122*288bf522SAndroid Build Coastguard Worker struct timeval res, finish;
123*288bf522SAndroid Build Coastguard Worker
124*288bf522SAndroid Build Coastguard Worker (void)gettimeofday(&finish, (struct timezone *)NULL);
125*288bf522SAndroid Build Coastguard Worker timersub(&finish, start, &res);
126*288bf522SAndroid Build Coastguard Worker timeradd(destination, &res, &finish);
127*288bf522SAndroid Build Coastguard Worker *destination = finish;
128*288bf522SAndroid Build Coastguard Worker }
129*288bf522SAndroid Build Coastguard Worker
130*288bf522SAndroid Build Coastguard Worker void *files_db_create_handle(void);
131*288bf522SAndroid Build Coastguard Worker void *files_db_lookup_byfileno(void *handle, int fileno);
132*288bf522SAndroid Build Coastguard Worker void *files_db_add_byfileno(void *handle, int fileno, int readonly);
133*288bf522SAndroid Build Coastguard Worker void files_db_update_fd(void *node, int fd);
134*288bf522SAndroid Build Coastguard Worker void files_db_unlink_files(void *db_handle);
135*288bf522SAndroid Build Coastguard Worker void files_db_close_files(void *handle);
136*288bf522SAndroid Build Coastguard Worker void files_db_close_fd(void *node);
137*288bf522SAndroid Build Coastguard Worker void files_db_free_memory(void *handle);
138*288bf522SAndroid Build Coastguard Worker void create_file(char *path, size_t size,
139*288bf522SAndroid Build Coastguard Worker struct rw_bytes_s *rw_bytes);
140*288bf522SAndroid Build Coastguard Worker char *get_buf(char **buf, int *buflen, int len, int do_fill);
141*288bf522SAndroid Build Coastguard Worker void files_db_fsync_discard_files(void *handle);
142*288bf522SAndroid Build Coastguard Worker void print_op_stats(u_int64_t *op_counts);
143*288bf522SAndroid Build Coastguard Worker void print_bytes(char *desc, struct rw_bytes_s *rw_bytes);
144*288bf522SAndroid Build Coastguard Worker void ioshark_handle_mmap(void *db_node,
145*288bf522SAndroid Build Coastguard Worker struct ioshark_file_operation *file_op,
146*288bf522SAndroid Build Coastguard Worker char **bufp, int *buflen, u_int64_t *op_counts,
147*288bf522SAndroid Build Coastguard Worker struct rw_bytes_s *rw_bytes);
148*288bf522SAndroid Build Coastguard Worker void capture_util_state_before(void);
149*288bf522SAndroid Build Coastguard Worker void report_cpu_disk_util(void);
150*288bf522SAndroid Build Coastguard Worker
151*288bf522SAndroid Build Coastguard Worker char *get_ro_filename(int ix);
152*288bf522SAndroid Build Coastguard Worker void init_filename_cache(void);
153*288bf522SAndroid Build Coastguard Worker void free_filename_cache(void);
154*288bf522SAndroid Build Coastguard Worker int is_readonly_mount(char *filename, size_t size);
155*288bf522SAndroid Build Coastguard Worker
156*288bf522SAndroid Build Coastguard Worker int ioshark_read_header(FILE *fp, struct ioshark_header *header);
157*288bf522SAndroid Build Coastguard Worker int ioshark_read_file_state(FILE *fp, struct ioshark_file_state *state);
158*288bf522SAndroid Build Coastguard Worker int ioshark_read_file_op(FILE *fp, struct ioshark_file_operation *file_op);
159