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
18const 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
44struct 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
54struct files_db_handle {
55	struct files_db_s *files_db_buckets[FILE_DB_HASHSIZE];
56};
57
58struct IO_operation_s {
59	char *IO_op;
60};
61
62struct rw_bytes_s {
63	u_int64_t bytes_read;
64	u_int64_t bytes_written;
65};
66
67static inline void
68files_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
76static inline void
77files_db_update_filename(void *node, char *filename)
78{
79	((struct files_db_s *)node)->filename = strdup(filename);
80}
81
82static inline int
83files_db_get_fileno(void *node)
84{
85	return (((struct files_db_s *)node)->fileno);
86}
87
88static inline int
89files_db_get_fd(void *node)
90{
91	return (((struct files_db_s *)node)->fd);
92}
93
94static inline char *
95files_db_get_filename(void *node)
96{
97	return (((struct files_db_s *)node)->filename);
98}
99
100static inline int
101files_db_readonly(void *node)
102{
103	return (((struct files_db_s *)node)->readonly);
104}
105
106static inline u_int64_t
107get_msecs(struct timeval *tv)
108{
109	return ((tv->tv_sec * 1000) + (tv->tv_usec / 1000));
110}
111
112static inline u_int64_t
113get_usecs(struct timeval *tv)
114{
115	return (tv->tv_usec % 1000);
116}
117
118static inline void
119update_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
130void *files_db_create_handle(void);
131void *files_db_lookup_byfileno(void *handle, int fileno);
132void *files_db_add_byfileno(void *handle, int fileno, int readonly);
133void files_db_update_fd(void *node, int fd);
134void files_db_unlink_files(void *db_handle);
135void files_db_close_files(void *handle);
136void files_db_close_fd(void *node);
137void files_db_free_memory(void *handle);
138void create_file(char *path, size_t size,
139		 struct rw_bytes_s *rw_bytes);
140char *get_buf(char **buf, int *buflen, int len, int do_fill);
141void files_db_fsync_discard_files(void *handle);
142void print_op_stats(u_int64_t *op_counts);
143void print_bytes(char *desc, struct rw_bytes_s *rw_bytes);
144void 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);
148void capture_util_state_before(void);
149void report_cpu_disk_util(void);
150
151char *get_ro_filename(int ix);
152void init_filename_cache(void);
153void free_filename_cache(void);
154int is_readonly_mount(char *filename, size_t size);
155