1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifdef IOSHARK_MAIN
18 const char *IO_op[] = {
19 "LSEEK",
20 "LLSEEK",
21 "PREAD64",
22 "PWRITE64",
23 "READ",
24 "WRITE",
25 "MMAP",
26 "MMAP2",
27 "OPEN",
28 "FSYNC",
29 "FDATASYNC",
30 "CLOSE",
31 "MAPPED_PREAD",
32 "MAPPED_PWRITE",
33 "MAX_FILE_OP"
34 };
35 #endif
36
37 #define MAX(A, B) ((A) > (B) ? (A) : (B))
38 #define MIN(A, B) ((A) < (B) ? (A) : (B))
39
40 #define MINBUFLEN (16*1024)
41
42 #define FILE_DB_HASHSIZE 8192
43
44 struct files_db_s {
45 char *filename;
46 int fileno;
47 size_t size;
48 int fd;
49 int readonly;
50 int debug_open_flags;
51 struct files_db_s *next;
52 };
53
54 struct files_db_handle {
55 struct files_db_s *files_db_buckets[FILE_DB_HASHSIZE];
56 };
57
58 struct IO_operation_s {
59 char *IO_op;
60 };
61
62 struct rw_bytes_s {
63 u_int64_t bytes_read;
64 u_int64_t bytes_written;
65 };
66
67 static inline void
files_db_update_size(void * node,u_int64_t new_size)68 files_db_update_size(void *node, u_int64_t new_size)
69 {
70 struct files_db_s *db_node = (struct files_db_s *)node;
71
72 if (db_node->size < new_size)
73 db_node->size = new_size;
74 }
75
76 static inline void
files_db_update_filename(void * node,char * filename)77 files_db_update_filename(void *node, char *filename)
78 {
79 ((struct files_db_s *)node)->filename = strdup(filename);
80 }
81
82 static inline int
files_db_get_fileno(void * node)83 files_db_get_fileno(void *node)
84 {
85 return (((struct files_db_s *)node)->fileno);
86 }
87
88 static inline int
files_db_get_fd(void * node)89 files_db_get_fd(void *node)
90 {
91 return (((struct files_db_s *)node)->fd);
92 }
93
94 static inline char *
files_db_get_filename(void * node)95 files_db_get_filename(void *node)
96 {
97 return (((struct files_db_s *)node)->filename);
98 }
99
100 static inline int
files_db_readonly(void * node)101 files_db_readonly(void *node)
102 {
103 return (((struct files_db_s *)node)->readonly);
104 }
105
106 static inline u_int64_t
get_msecs(struct timeval * tv)107 get_msecs(struct timeval *tv)
108 {
109 return ((tv->tv_sec * 1000) + (tv->tv_usec / 1000));
110 }
111
112 static inline u_int64_t
get_usecs(struct timeval * tv)113 get_usecs(struct timeval *tv)
114 {
115 return (tv->tv_usec % 1000);
116 }
117
118 static inline void
update_delta_time(struct timeval * start,struct timeval * destination)119 update_delta_time(struct timeval *start,
120 struct timeval *destination)
121 {
122 struct timeval res, finish;
123
124 (void)gettimeofday(&finish, (struct timezone *)NULL);
125 timersub(&finish, start, &res);
126 timeradd(destination, &res, &finish);
127 *destination = finish;
128 }
129
130 void *files_db_create_handle(void);
131 void *files_db_lookup_byfileno(void *handle, int fileno);
132 void *files_db_add_byfileno(void *handle, int fileno, int readonly);
133 void files_db_update_fd(void *node, int fd);
134 void files_db_unlink_files(void *db_handle);
135 void files_db_close_files(void *handle);
136 void files_db_close_fd(void *node);
137 void files_db_free_memory(void *handle);
138 void create_file(char *path, size_t size,
139 struct rw_bytes_s *rw_bytes);
140 char *get_buf(char **buf, int *buflen, int len, int do_fill);
141 void files_db_fsync_discard_files(void *handle);
142 void print_op_stats(u_int64_t *op_counts);
143 void print_bytes(char *desc, struct rw_bytes_s *rw_bytes);
144 void ioshark_handle_mmap(void *db_node,
145 struct ioshark_file_operation *file_op,
146 char **bufp, int *buflen, u_int64_t *op_counts,
147 struct rw_bytes_s *rw_bytes);
148 void capture_util_state_before(void);
149 void report_cpu_disk_util(void);
150
151 char *get_ro_filename(int ix);
152 void init_filename_cache(void);
153 void free_filename_cache(void);
154 int is_readonly_mount(char *filename, size_t size);
155
156 int ioshark_read_header(FILE *fp, struct ioshark_header *header);
157 int ioshark_read_file_state(FILE *fp, struct ioshark_file_state *state);
158 int ioshark_read_file_op(FILE *fp, struct ioshark_file_operation *file_op);
159