odb.h

Go to the documentation of this file.
00001 
00013 #ifndef ODB_HASH_H
00014 #define ODB_HASH_H
00015 
00016 #include <stddef.h>
00017 #include <stdint.h>
00018 
00019 #include "op_list.h"
00020 
00022 typedef uint64_t odb_key_t;
00024 typedef unsigned int odb_value_t;
00026 typedef unsigned int odb_index_t;
00028 typedef odb_index_t odb_node_nr_t;
00030 typedef odb_index_t odb_hash_mask_t;
00031 
00032 /* there is (bucket factor * nr node) entry in hash table, this can seem
00033  * excessive but hash coding eip don't give a good distributions and our
00034  * goal is to get a O(1) amortized insert time. bucket factor must be a
00035  * power of two. FIXME: see big comment in odb_hash_add_node, you must
00036  * re-enable zeroing hash table if BUCKET_FACTOR > 2 (roughly exact, you
00037  * want to read the comment in odb_hash_add_node() if you tune this define)
00038  */
00039 #define BUCKET_FACTOR 1
00040 
00042 typedef struct {
00043     odb_key_t key;          
00044     odb_value_t value;      
00045     odb_index_t next;       
00046 } odb_node_t;
00047 
00052 typedef struct {
00053     odb_node_nr_t size;     
00054     odb_node_nr_t current_size; 
00055     int padding[6];         
00056 } odb_descr_t;
00057 
00075 typedef struct odb_data {
00076     odb_node_t * node_base;     
00077     odb_index_t * hash_base;    
00078     odb_descr_t * descr;        
00079     odb_hash_mask_t hash_mask;  
00080     unsigned int sizeof_header; 
00081     unsigned int offset_node;   
00082     void * base_memory;     
00083     int fd;             
00084     char * filename;                
00085     int ref_count;                  
00086     struct list_head list;          
00087 } odb_data_t;
00088 
00089 typedef struct {
00090     odb_data_t * data;
00091 } odb_t;
00092 
00093 #ifdef __cplusplus
00094 extern "C" {
00095 #endif
00096 
00097 /* db_manage.c */
00098 
00100 enum odb_rw {
00101     ODB_RDONLY = 0, 
00102     ODB_RDWR = 1    
00103 };
00104 
00109 void odb_init(odb_t * odb);
00110 
00123 int odb_open(odb_t * odb, char const * filename,
00124              enum odb_rw rw, size_t sizeof_header);
00125 
00127 void odb_close(odb_t * odb);
00128 
00130 int odb_open_count(odb_t const * odb);
00131 
00133 void * odb_get_data(odb_t * odb);
00134 
00136 void odb_sync(odb_t const * odb);
00137 
00152 int odb_grow_hashtable(odb_data_t * data);
00156 static __inline void odb_commit_reservation(odb_data_t * data)
00157 {
00158     ++data->descr->current_size;
00159 }
00160 
00162 #define ODB_NODE_NR_INVALID ((odb_node_nr_t)-1)
00163 
00164 /* db_debug.c */
00166 int odb_check_hash(odb_t const * odb);
00167 
00168 /* db_stat.c */
00169 typedef struct odb_hash_stat_t odb_hash_stat_t;
00170 odb_hash_stat_t * odb_hash_stat(odb_t const * odb);
00171 void odb_hash_display_stat(odb_hash_stat_t const * stats);
00172 void odb_hash_free_stat(odb_hash_stat_t * stats);
00173 
00174 /* db_insert.c */
00181 int odb_update_node(odb_t * odb, odb_key_t key);
00182 
00195 int odb_update_node_with_offset(odb_t * odb, 
00196                 odb_key_t key, 
00197                 unsigned long int offset);
00198 
00203 int odb_add_node(odb_t * odb, odb_key_t key, odb_value_t value);
00204 
00205 /* db_travel.c */
00218 odb_node_t * odb_get_iterator(odb_t const * odb, odb_node_nr_t * nr);
00219 
00220 static __inline unsigned int
00221 odb_do_hash(odb_data_t const * data, odb_key_t value)
00222 {
00223     /* FIXME: better hash for eip value, needs to instrument code
00224      * and do a lot of tests ... */
00225     /* trying to combine high order bits his a no-op: inside a binary image
00226      * high order bits don't vary a lot, hash table start with 7 bits mask
00227      * so this hash coding use bits 0-7, 8-15. Hash table is stored in
00228      * files avoiding to rebuilding them at profiling re-start so
00229      * on changing do_hash() change the file format!
00230      */
00231     uint32_t temp = (value >> 32) ^ value;
00232     return ((temp << 0) ^ (temp >> 8)) & data->hash_mask;
00233 }
00234 
00235 #ifdef __cplusplus
00236 }
00237 #endif
00238 
00239 #endif /* !ODB_H */

Generated on 8 Nov 2012 for Oprofile by  doxygen 1.6.1