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/types.h>
19*288bf522SAndroid Build Coastguard Worker #include <unistd.h>
20*288bf522SAndroid Build Coastguard Worker
21*288bf522SAndroid Build Coastguard Worker #include "ioshark.h"
22*288bf522SAndroid Build Coastguard Worker
23*288bf522SAndroid Build Coastguard Worker #define FILE_DB_HASHSIZE 8192
24*288bf522SAndroid Build Coastguard Worker
25*288bf522SAndroid Build Coastguard Worker struct files_db_s {
26*288bf522SAndroid Build Coastguard Worker char *filename;
27*288bf522SAndroid Build Coastguard Worker int fileno;
28*288bf522SAndroid Build Coastguard Worker struct files_db_s *next;
29*288bf522SAndroid Build Coastguard Worker size_t size;
30*288bf522SAndroid Build Coastguard Worker int global_filename_ix;
31*288bf522SAndroid Build Coastguard Worker };
32*288bf522SAndroid Build Coastguard Worker
33*288bf522SAndroid Build Coastguard Worker /* Lifted from Wikipedia Jenkins Hash function page */
34*288bf522SAndroid Build Coastguard Worker static inline u_int32_t
jenkins_one_at_a_time_hash(char * key,size_t len)35*288bf522SAndroid Build Coastguard Worker jenkins_one_at_a_time_hash(char *key, size_t len)
36*288bf522SAndroid Build Coastguard Worker {
37*288bf522SAndroid Build Coastguard Worker u_int32_t hash, i;
38*288bf522SAndroid Build Coastguard Worker
39*288bf522SAndroid Build Coastguard Worker for(hash = i = 0; i < len; ++i) {
40*288bf522SAndroid Build Coastguard Worker hash += key[i];
41*288bf522SAndroid Build Coastguard Worker hash += (hash << 10);
42*288bf522SAndroid Build Coastguard Worker hash ^= (hash >> 6);
43*288bf522SAndroid Build Coastguard Worker }
44*288bf522SAndroid Build Coastguard Worker hash += (hash << 3);
45*288bf522SAndroid Build Coastguard Worker hash ^= (hash >> 11);
46*288bf522SAndroid Build Coastguard Worker hash += (hash << 15);
47*288bf522SAndroid Build Coastguard Worker return hash;
48*288bf522SAndroid Build Coastguard Worker }
49*288bf522SAndroid Build Coastguard Worker
50*288bf522SAndroid Build Coastguard Worker static inline void
files_db_update_size(void * node,u_int64_t new_size)51*288bf522SAndroid Build Coastguard Worker files_db_update_size(void *node, u_int64_t new_size)
52*288bf522SAndroid Build Coastguard Worker {
53*288bf522SAndroid Build Coastguard Worker struct files_db_s *db_node = (struct files_db_s *)node;
54*288bf522SAndroid Build Coastguard Worker
55*288bf522SAndroid Build Coastguard Worker if (db_node->size < new_size)
56*288bf522SAndroid Build Coastguard Worker db_node->size = new_size;
57*288bf522SAndroid Build Coastguard Worker }
58*288bf522SAndroid Build Coastguard Worker
59*288bf522SAndroid Build Coastguard Worker static inline void
files_db_add_to_size(void * node,u_int64_t size_incr)60*288bf522SAndroid Build Coastguard Worker files_db_add_to_size(void *node, u_int64_t size_incr)
61*288bf522SAndroid Build Coastguard Worker {
62*288bf522SAndroid Build Coastguard Worker ((struct files_db_s *)node)->size += size_incr;
63*288bf522SAndroid Build Coastguard Worker }
64*288bf522SAndroid Build Coastguard Worker
65*288bf522SAndroid Build Coastguard Worker static inline int
files_db_get_fileno(void * node)66*288bf522SAndroid Build Coastguard Worker files_db_get_fileno(void *node)
67*288bf522SAndroid Build Coastguard Worker {
68*288bf522SAndroid Build Coastguard Worker return (((struct files_db_s *)node)->fileno);
69*288bf522SAndroid Build Coastguard Worker }
70*288bf522SAndroid Build Coastguard Worker
71*288bf522SAndroid Build Coastguard Worker static inline char *
files_db_get_filename(void * node)72*288bf522SAndroid Build Coastguard Worker files_db_get_filename(void *node)
73*288bf522SAndroid Build Coastguard Worker {
74*288bf522SAndroid Build Coastguard Worker return (((struct files_db_s *)node)->filename);
75*288bf522SAndroid Build Coastguard Worker }
76*288bf522SAndroid Build Coastguard Worker
77*288bf522SAndroid Build Coastguard Worker void *files_db_create_handle(void);
78*288bf522SAndroid Build Coastguard Worker void files_db_write_objects(FILE *fp);
79*288bf522SAndroid Build Coastguard Worker void *files_db_add(char *filename);
80*288bf522SAndroid Build Coastguard Worker void *files_db_lookup(char *filename);
81*288bf522SAndroid Build Coastguard Worker int files_db_get_total_obj(void);
82*288bf522SAndroid Build Coastguard Worker void init_filename_cache(void);
83*288bf522SAndroid Build Coastguard Worker void store_filename_cache(void);
84*288bf522SAndroid Build Coastguard Worker
85*288bf522SAndroid Build Coastguard Worker int ioshark_write_header(FILE *fp, struct ioshark_header *header);
86*288bf522SAndroid Build Coastguard Worker int ioshark_write_file_state(FILE *fp, struct ioshark_file_state *state);
87*288bf522SAndroid Build Coastguard Worker int ioshark_write_file_op(FILE *fp, struct ioshark_file_operation *file_op);
88