aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/gnunet-service-testbed_cache.c
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2013-10-28 14:15:30 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2013-10-28 14:15:30 +0000
commit93ddb2f4a73f7867dc9cc850cccb6c1ab8d23fd0 (patch)
tree242ab4ed50964d6794edccccea65fca2afa375d1 /src/testbed/gnunet-service-testbed_cache.c
parent39dceb2cfe324e3f1b44958e3cf4cb936ab76881 (diff)
downloadgnunet-93ddb2f4a73f7867dc9cc850cccb6c1ab8d23fd0.tar.gz
gnunet-93ddb2f4a73f7867dc9cc850cccb6c1ab8d23fd0.zip
- cleanup; move to hashmap32
Diffstat (limited to 'src/testbed/gnunet-service-testbed_cache.c')
-rw-r--r--src/testbed/gnunet-service-testbed_cache.c123
1 files changed, 51 insertions, 72 deletions
diff --git a/src/testbed/gnunet-service-testbed_cache.c b/src/testbed/gnunet-service-testbed_cache.c
index 9f2b15579..69f820a30 100644
--- a/src/testbed/gnunet-service-testbed_cache.c
+++ b/src/testbed/gnunet-service-testbed_cache.c
@@ -36,13 +36,6 @@
36 36
37 37
38/** 38/**
39 * Time to expire a cache entry
40 */
41#define CACHE_EXPIRY \
42 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
43
44
45/**
46 * Cache entry 39 * Cache entry
47 */ 40 */
48struct CacheEntry 41struct CacheEntry
@@ -58,18 +51,6 @@ struct CacheEntry
58 struct CacheEntry *prev; 51 struct CacheEntry *prev;
59 52
60 /** 53 /**
61 * The peer identity of this peer. Will be set upon opening a connection to
62 * the peers CORE service. Will be NULL until then and after the CORE
63 * connection is closed
64 */
65 struct GNUNET_PeerIdentity *peer_identity;
66
67 /**
68 * The key for this entry
69 */
70 struct GNUNET_HashCode key;
71
72 /**
73 * The HELLO message 54 * The HELLO message
74 */ 55 */
75 struct GNUNET_MessageHeader *hello; 56 struct GNUNET_MessageHeader *hello;
@@ -78,18 +59,13 @@ struct CacheEntry
78 * The id of the peer this entry corresponds to 59 * The id of the peer this entry corresponds to
79 */ 60 */
80 unsigned int peer_id; 61 unsigned int peer_id;
81
82 /**
83 * Is this entry in LRU cache queue?
84 */
85 unsigned int in_lru;
86}; 62};
87 63
88 64
89/** 65/**
90 * Hashmap to maintain cache 66 * Hashmap to maintain cache
91 */ 67 */
92static struct GNUNET_CONTAINER_MultiHashMap *cache; 68static struct GNUNET_CONTAINER_MultiHashMap32 *cache;
93 69
94/** 70/**
95 * DLL head for least recently used cache entries; least recently used 71 * DLL head for least recently used cache entries; least recently used
@@ -97,7 +73,7 @@ static struct GNUNET_CONTAINER_MultiHashMap *cache;
97 * their demand becomes zero. They are removed from the queue when they are 73 * their demand becomes zero. They are removed from the queue when they are
98 * needed by any operation. 74 * needed by any operation.
99 */ 75 */
100static struct CacheEntry *lru_cache_head; 76static struct CacheEntry *cache_head;
101 77
102/** 78/**
103 * DLL tail for least recently used cache entries; recently used cache 79 * DLL tail for least recently used cache entries; recently used cache
@@ -105,20 +81,10 @@ static struct CacheEntry *lru_cache_head;
105 * their demand becomes zero. They are removed from the queue when they are 81 * their demand becomes zero. They are removed from the queue when they are
106 * needed by any operation. 82 * needed by any operation.
107 */ 83 */
108static struct CacheEntry *lru_cache_tail; 84static struct CacheEntry *cache_tail;
109 85
110/** 86/**
111 * the size of the LRU queue 87 * Maximum number of elements to cache
112 */
113static unsigned int lru_cache_size;
114
115/**
116 * the threshold size for the LRU queue
117 */
118static unsigned int lru_cache_threshold_size;
119
120/**
121 * The total number of elements in cache
122 */ 88 */
123static unsigned int cache_size; 89static unsigned int cache_size;
124 90
@@ -131,13 +97,16 @@ static unsigned int cache_size;
131 * @return the HELLO message; NULL if not found 97 * @return the HELLO message; NULL if not found
132 */ 98 */
133static struct CacheEntry * 99static struct CacheEntry *
134cache_lookup (const struct GNUNET_HashCode *key) 100cache_lookup (unsigned int peer_id)
135{ 101{
136 struct CacheEntry *entry; 102 struct CacheEntry *entry;
137 103
138 if (NULL == cache) 104 GNUNET_assert (NULL != cache);
105 entry = GNUNET_CONTAINER_multihashmap32_get (cache, peer_id);
106 if (NULL == entry)
139 return NULL; 107 return NULL;
140 entry = GNUNET_CONTAINER_multihashmap_get (cache, key); 108 GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, entry);
109 GNUNET_CONTAINER_DLL_insert_tail (cache_head, cache_tail, entry);
141 return entry; 110 return entry;
142} 111}
143 112
@@ -145,22 +114,32 @@ cache_lookup (const struct GNUNET_HashCode *key)
145/** 114/**
146 * Creates a new cache entry and then puts it into the cache's hashtable. 115 * Creates a new cache entry and then puts it into the cache's hashtable.
147 * 116 *
148 * @param key the hash code to use for inserting the newly created entry
149 * @param peer_id the index of the peer to tag the newly created entry 117 * @param peer_id the index of the peer to tag the newly created entry
150 * @return the newly created entry 118 * @return the newly created entry
151 */ 119 */
152static struct CacheEntry * 120static struct CacheEntry *
153add_entry (const struct GNUNET_HashCode *key, unsigned int peer_id) 121add_entry (unsigned int peer_id)
154{ 122{
155 struct CacheEntry *entry; 123 struct CacheEntry *entry;
156 124
125 GNUNET_assert (NULL != cache);
126 if (cache_size == GNUNET_CONTAINER_multihashmap32_size (cache))
127 {
128 /* remove the LRU head */
129 entry = cache_head;
130 GNUNET_assert (GNUNET_OK ==
131 GNUNET_CONTAINER_multihashmap32_remove (cache, (uint32_t)
132 entry->peer_id,
133 entry));
134 }
157 entry = GNUNET_malloc (sizeof (struct CacheEntry)); 135 entry = GNUNET_malloc (sizeof (struct CacheEntry));
158 entry->peer_id = peer_id; 136 entry->peer_id = peer_id;
159 memcpy (&entry->key, key, sizeof (struct GNUNET_HashCode));
160 GNUNET_assert (GNUNET_OK == 137 GNUNET_assert (GNUNET_OK ==
161 GNUNET_CONTAINER_multihashmap_put (cache, &entry->key, entry, 138 GNUNET_CONTAINER_multihashmap32_put (cache,
162 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)); 139 (uint32_t) peer_id,
163 cache_size++; 140 entry,
141 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
142 GNUNET_CONTAINER_DLL_insert_tail (cache_head, cache_tail, entry);
164 return entry; 143 return entry;
165} 144}
166 145
@@ -169,22 +148,20 @@ add_entry (const struct GNUNET_HashCode *key, unsigned int peer_id)
169 * Iterator over hash map entries. 148 * Iterator over hash map entries.
170 * 149 *
171 * @param cls closure 150 * @param cls closure
172 * @param key current key code 151 * @param key current key
173 * @param value value in the hash map 152 * @param value value in the hash map
174 * @return GNUNET_YES if we should continue to 153 * @return GNUNET_YES if we should continue to
175 * iterate, 154 * iterate,
176 * GNUNET_NO if not. 155 * GNUNET_NO if not.
177 */ 156 */
178static int 157static int
179cache_clear_iterator (void *cls, const struct GNUNET_HashCode *key, void *value) 158cache_clear_iterator (void *cls, uint32_t key, void *value)
180{ 159{
181 struct CacheEntry *entry = value; 160 struct CacheEntry *entry = value;
182 static unsigned int ncleared;
183 161
184 GNUNET_assert (NULL != entry); 162 GNUNET_assert (NULL != entry);
185 LOG_DEBUG ("Clearing entry %u of %u\n", ++ncleared, cache_size);
186 GNUNET_assert (GNUNET_YES == 163 GNUNET_assert (GNUNET_YES ==
187 GNUNET_CONTAINER_multihashmap_remove (cache, key, value)); 164 GNUNET_CONTAINER_multihashmap32_remove (cache, key, value));
188 GNUNET_free_non_null (entry->hello); 165 GNUNET_free_non_null (entry->hello);
189 GNUNET_free (entry); 166 GNUNET_free (entry);
190 return GNUNET_YES; 167 return GNUNET_YES;
@@ -197,15 +174,16 @@ cache_clear_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
197void 174void
198GST_cache_clear () 175GST_cache_clear ()
199{ 176{
200 GNUNET_CONTAINER_multihashmap_iterate (cache, &cache_clear_iterator, NULL); 177 if (NULL != cache)
201 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (cache)); 178 {
202 GNUNET_CONTAINER_multihashmap_destroy (cache); 179 GNUNET_CONTAINER_multihashmap32_iterate (cache, &cache_clear_iterator, NULL);
203 cache = NULL; 180 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (cache));
204 lru_cache_size = 0; 181 GNUNET_CONTAINER_multihashmap32_destroy (cache);
205 lru_cache_threshold_size = 0; 182 cache = NULL;
183 }
206 cache_size = 0; 184 cache_size = 0;
207 lru_cache_head = NULL; 185 cache_head = NULL;
208 lru_cache_tail = NULL; 186 cache_tail = NULL;
209} 187}
210 188
211 189
@@ -219,10 +197,8 @@ GST_cache_init (unsigned int size)
219{ 197{
220 if (0 == size) 198 if (0 == size)
221 return; 199 return;
222 lru_cache_threshold_size = size; 200 cache_size = size;
223 if (size > 1) 201 cache = GNUNET_CONTAINER_multihashmap32_create (cache_size);
224 size = size / 2;
225 cache = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_YES);
226} 202}
227 203
228 204
@@ -236,11 +212,14 @@ const struct GNUNET_MessageHeader *
236GST_cache_lookup_hello (const unsigned int peer_id) 212GST_cache_lookup_hello (const unsigned int peer_id)
237{ 213{
238 struct CacheEntry *entry; 214 struct CacheEntry *entry;
239 struct GNUNET_HashCode key;
240 215
241 LOG_DEBUG ("Looking up HELLO for peer %u\n", peer_id); 216 LOG_DEBUG ("Looking up HELLO for peer %u\n", peer_id);
242 GNUNET_CRYPTO_hash (&peer_id, sizeof (peer_id), &key); 217 if (NULL == cache)
243 entry = cache_lookup (&key); 218 {
219 LOG_DEBUG ("Caching disabled\n");
220 return NULL;
221 }
222 entry = cache_lookup (peer_id);
244 if (NULL == entry) 223 if (NULL == entry)
245 return NULL; 224 return NULL;
246 if (NULL != entry->hello) 225 if (NULL != entry->hello)
@@ -261,12 +240,12 @@ GST_cache_add_hello (const unsigned int peer_id,
261 const struct GNUNET_MessageHeader *hello) 240 const struct GNUNET_MessageHeader *hello)
262{ 241{
263 struct CacheEntry *entry; 242 struct CacheEntry *entry;
264 struct GNUNET_HashCode key;
265 243
266 GNUNET_CRYPTO_hash (&peer_id, sizeof (peer_id), &key); 244 if (NULL == cache)
267 entry = GNUNET_CONTAINER_multihashmap_get (cache, &key); 245 return;
246 entry = cache_lookup (peer_id);
268 if (NULL == entry) 247 if (NULL == entry)
269 entry = add_entry (&key, peer_id); 248 entry = add_entry (peer_id);
270 GNUNET_free_non_null (entry->hello); 249 GNUNET_free_non_null (entry->hello);
271 entry->hello = GNUNET_copy_message (hello); 250 entry->hello = GNUNET_copy_message (hello);
272} 251}