GNUnet  0.10.x
Data Structures | Macros | Functions | Variables
gnunet-service-testbed_cache.c File Reference

testbed cache implementation More...

#include "gnunet-service-testbed.h"
Include dependency graph for gnunet-service-testbed_cache.c:

Go to the source code of this file.

Data Structures

struct  CacheEntry
 Cache entry. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "testbed-cache", __VA_ARGS__)
 Redefine LOG with a changed log component string. More...
 

Functions

static struct CacheEntrycache_lookup (unsigned int peer_id)
 Looks up in the cache and returns the entry. More...
 
static void free_entry (struct CacheEntry *entry)
 Free the resources occupied by a cache entry. More...
 
static struct CacheEntryadd_entry (unsigned int peer_id)
 Creates a new cache entry and then puts it into the cache's hashtable. More...
 
static int cache_clear_iterator (void *cls, uint32_t key, void *value)
 Iterator over hash map entries. More...
 
void GST_cache_clear ()
 Clear cache. More...
 
void GST_cache_init (unsigned int size)
 Initializes the cache. More...
 
const struct GNUNET_MessageHeaderGST_cache_lookup_hello (const unsigned int peer_id)
 Looks up in the hello cache and returns the HELLO of the given peer. More...
 
void GST_cache_add_hello (const unsigned int peer_id, const struct GNUNET_MessageHeader *hello)
 Caches the HELLO of the given peer. More...
 

Variables

static struct GNUNET_CONTAINER_MultiHashMap32cache
 Hashmap to maintain cache. More...
 
static struct CacheEntrycache_head
 DLL head for least recently used cache entries; least recently used cache items are at the head. More...
 
static struct CacheEntrycache_tail
 DLL tail for least recently used cache entries; recently used cache items are at the tail.The cache enties are added to this queue when their demand becomes zero. More...
 
static unsigned int cache_size
 Maximum number of elements to cache. More...
 

Detailed Description

testbed cache implementation

Author
Sree Harsha Totakura

Definition in file gnunet-service-testbed_cache.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "testbed-cache", __VA_ARGS__)

Redefine LOG with a changed log component string.

Definition at line 34 of file gnunet-service-testbed_cache.c.

Function Documentation

◆ cache_lookup()

static struct CacheEntry* cache_lookup ( unsigned int  peer_id)
static

Looks up in the cache and returns the entry.

Parameters
peer_idthe peer identity of the peer whose corresponding entry has to be looked up
Returns
the HELLO message; NULL if not found

Definition at line 100 of file gnunet-service-testbed_cache.c.

References GNUNET_assert, GNUNET_CONTAINER_DLL_insert_tail, GNUNET_CONTAINER_DLL_remove, and GNUNET_CONTAINER_multihashmap32_get().

Referenced by GST_cache_add_hello(), and GST_cache_lookup_hello().

101 {
102  struct CacheEntry *entry;
103 
104  GNUNET_assert (NULL != cache);
106  if (NULL == entry)
107  return NULL;
110  return entry;
111 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
static struct CacheEntry * cache_head
DLL head for least recently used cache entries; least recently used cache items are at the head...
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static char * peer_id
Option –peer.
Definition: gnunet-cadet.c:42
void * GNUNET_CONTAINER_multihashmap32_get(const struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key)
Given a key find a value in the map matching the key.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
static struct CacheEntry * cache_tail
DLL tail for least recently used cache entries; recently used cache items are at the tail...
static struct GNUNET_CONTAINER_MultiHashMap32 * cache
Hashmap to maintain cache.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_entry()

static void free_entry ( struct CacheEntry entry)
static

Free the resources occupied by a cache entry.

Parameters
entrythe cache entry to free

Definition at line 120 of file gnunet-service-testbed_cache.c.

References GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_free_non_null, and CacheEntry::hello.

Referenced by add_entry(), and cache_clear_iterator().

121 {
123  GNUNET_free_non_null (entry->hello);
124  GNUNET_free (entry);
125 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
static struct CacheEntry * cache_head
DLL head for least recently used cache entries; least recently used cache items are at the head...
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
static struct CacheEntry * cache_tail
DLL tail for least recently used cache entries; recently used cache items are at the tail...
struct GNUNET_MessageHeader * hello
The HELLO message.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ add_entry()

static struct CacheEntry* add_entry ( unsigned int  peer_id)
static

Creates a new cache entry and then puts it into the cache's hashtable.

Parameters
peer_idthe index of the peer to tag the newly created entry
Returns
the newly created entry

Definition at line 135 of file gnunet-service-testbed_cache.c.

References cache_head, cache_size, free_entry(), GNUNET_assert, GNUNET_CONTAINER_DLL_insert_tail, GNUNET_CONTAINER_multihashmap32_put(), GNUNET_CONTAINER_multihashmap32_remove(), GNUNET_CONTAINER_multihashmap32_size(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST, GNUNET_new, GNUNET_OK, and CacheEntry::peer_id.

Referenced by GST_cache_add_hello().

136 {
137  struct CacheEntry *entry;
138 
139  GNUNET_assert (NULL != cache);
141  {
142  /* remove the LRU head */
143  entry = cache_head;
146  entry->peer_id,
147  entry));
148  free_entry (entry);
149  }
150  entry = GNUNET_new (struct CacheEntry);
151  entry->peer_id = peer_id;
154  (uint32_t) peer_id,
155  entry,
158  return entry;
159 }
static struct CacheEntry * cache_head
DLL head for least recently used cache entries; least recently used cache items are at the head...
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static char * peer_id
Option –peer.
Definition: gnunet-cadet.c:42
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static void free_entry(struct CacheEntry *entry)
Free the resources occupied by a cache entry.
int GNUNET_CONTAINER_multihashmap32_put(struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
int GNUNET_CONTAINER_multihashmap32_remove(struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key, const void *value)
Remove the given key-value pair from the map.
, ' bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_...
unsigned int GNUNET_CONTAINER_multihashmap32_size(const struct GNUNET_CONTAINER_MultiHashMap32 *map)
Get the number of key-value pairs in the map.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
static struct CacheEntry * cache_tail
DLL tail for least recently used cache entries; recently used cache items are at the tail...
static struct GNUNET_CONTAINER_MultiHashMap32 * cache
Hashmap to maintain cache.
static unsigned int cache_size
Maximum number of elements to cache.
unsigned int peer_id
The id of the peer this entry corresponds to.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cache_clear_iterator()

static int cache_clear_iterator ( void *  cls,
uint32_t  key,
void *  value 
)
static

Iterator over hash map entries.

Parameters
clsclosure
keycurrent key
valuevalue in the hash map
Returns
GNUNET_YES if we should continue to iterate, GNUNET_NO if not.

Definition at line 173 of file gnunet-service-testbed_cache.c.

References free_entry(), GNUNET_assert, GNUNET_CONTAINER_multihashmap32_remove(), GNUNET_YES, and value.

Referenced by GST_cache_clear().

174 {
175  struct CacheEntry *entry = value;
176 
177  GNUNET_assert (NULL != entry);
180  free_entry (entry);
181  return GNUNET_YES;
182 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void free_entry(struct CacheEntry *entry)
Free the resources occupied by a cache entry.
int GNUNET_CONTAINER_multihashmap32_remove(struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key, const void *value)
Remove the given key-value pair from the map.
static char * value
Value of the record to add/remove.
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_CONTAINER_MultiHashMap32 * cache
Hashmap to maintain cache.
#define GNUNET_YES
Definition: gnunet_common.h:80
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GST_cache_clear()

void GST_cache_clear ( void  )

Clear cache.

Definition at line 189 of file gnunet-service-testbed_cache.c.

References cache_clear_iterator(), cache_size, GNUNET_assert, GNUNET_CONTAINER_multihashmap32_destroy(), GNUNET_CONTAINER_multihashmap32_iterate(), and GNUNET_CONTAINER_multihashmap32_size().

Referenced by shutdown_task().

190 {
191  if (NULL != cache)
192  {
196  cache = NULL;
197  }
198  cache_size = 0;
199  cache_head = NULL;
200  cache_tail = NULL;
201 }
static struct CacheEntry * cache_head
DLL head for least recently used cache entries; least recently used cache items are at the head...
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
unsigned int GNUNET_CONTAINER_multihashmap32_size(const struct GNUNET_CONTAINER_MultiHashMap32 *map)
Get the number of key-value pairs in the map.
static int cache_clear_iterator(void *cls, uint32_t key, void *value)
Iterator over hash map entries.
void GNUNET_CONTAINER_multihashmap32_destroy(struct GNUNET_CONTAINER_MultiHashMap32 *map)
Destroy a 32-bit key hash map.
static struct CacheEntry * cache_tail
DLL tail for least recently used cache entries; recently used cache items are at the tail...
int GNUNET_CONTAINER_multihashmap32_iterate(struct GNUNET_CONTAINER_MultiHashMap32 *map, GNUNET_CONTAINER_HashMapIterator32 it, void *it_cls)
Iterate over all entries in the map.
static struct GNUNET_CONTAINER_MultiHashMap32 * cache
Hashmap to maintain cache.
static unsigned int cache_size
Maximum number of elements to cache.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GST_cache_init()

void GST_cache_init ( unsigned int  size)

Initializes the cache.

Parameters
sizethe size of the cache

Definition at line 210 of file gnunet-service-testbed_cache.c.

References cache_size, GNUNET_CONTAINER_multihashmap32_create(), and size.

Referenced by testbed_run().

211 {
212  if (0 == size)
213  return;
214  cache_size = size;
216 }
struct GNUNET_CONTAINER_MultiHashMap32 * GNUNET_CONTAINER_multihashmap32_create(unsigned int len)
Create a 32-bit key multi hash map.
static unsigned int size
Size of the "table".
Definition: peer.c:67
static struct GNUNET_CONTAINER_MultiHashMap32 * cache
Hashmap to maintain cache.
static unsigned int cache_size
Maximum number of elements to cache.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GST_cache_lookup_hello()

const struct GNUNET_MessageHeader* GST_cache_lookup_hello ( const unsigned int  peer_id)

Looks up in the hello cache and returns the HELLO of the given peer.

Parameters
peer_idthe index of the peer whose HELLO has to be looked up
Returns
the HELLO message; NULL if not found

Definition at line 226 of file gnunet-service-testbed_cache.c.

References cache_lookup(), CacheEntry::hello, and LOG_DEBUG.

Referenced by occ_cache_get_handle_core_cb().

227 {
228  struct CacheEntry *entry;
229 
230  LOG_DEBUG ("Looking up HELLO for peer %u\n", peer_id);
231  if (NULL == cache)
232  {
233  LOG_DEBUG ("Caching disabled\n");
234  return NULL;
235  }
236  entry = cache_lookup (peer_id);
237  if (NULL == entry)
238  return NULL;
239  if (NULL != entry->hello)
240  LOG_DEBUG ("HELLO found for peer %u\n", peer_id);
241  return entry->hello;
242 }
static char * peer_id
Option –peer.
Definition: gnunet-cadet.c:42
static struct GNUNET_CONTAINER_MultiHashMap32 * cache
Hashmap to maintain cache.
struct GNUNET_MessageHeader * hello
The HELLO message.
static struct CacheEntry * cache_lookup(unsigned int peer_id)
Looks up in the cache and returns the entry.
#define LOG_DEBUG(msg)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GST_cache_add_hello()

void GST_cache_add_hello ( const unsigned int  peer_id,
const struct GNUNET_MessageHeader hello 
)

Caches the HELLO of the given peer.

Updates the HELLO if it was already cached before

Parameters
peer_idthe peer identity of the peer whose HELLO has to be cached
hellothe HELLO message

Definition at line 253 of file gnunet-service-testbed_cache.c.

References add_entry(), cache_lookup(), GNUNET_copy_message(), GNUNET_free_non_null, and CacheEntry::hello.

Referenced by hello_update_cb().

255 {
256  struct CacheEntry *entry;
257 
258  if (NULL == cache)
259  return;
260  entry = cache_lookup (peer_id);
261  if (NULL == entry)
262  entry = add_entry (peer_id);
263  GNUNET_free_non_null (entry->hello);
264  entry->hello = GNUNET_copy_message (hello);
265 }
static struct CacheEntry * add_entry(unsigned int peer_id)
Creates a new cache entry and then puts it into the cache's hashtable.
static char * peer_id
Option –peer.
Definition: gnunet-cadet.c:42
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
static struct GNUNET_CONTAINER_MultiHashMap32 * cache
Hashmap to maintain cache.
struct GNUNET_MessageHeader * hello
The HELLO message.
static struct CacheEntry * cache_lookup(unsigned int peer_id)
Looks up in the cache and returns the entry.
struct GNUNET_MessageHeader * GNUNET_copy_message(const struct GNUNET_MessageHeader *msg)
Create a copy of the given message.
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ cache

struct GNUNET_CONTAINER_MultiHashMap32* cache
static

Hashmap to maintain cache.

Definition at line 68 of file gnunet-service-testbed_cache.c.

Referenced by GNUNET_OS_get_libexec_binary_path(), and GNUNET_OS_get_suid_binary_path().

◆ cache_head

struct CacheEntry* cache_head
static

DLL head for least recently used cache entries; least recently used cache items are at the head.

The cache enties are added to this queue when their demand becomes zero. They are removed from the queue when they are needed by any operation.

Definition at line 76 of file gnunet-service-testbed_cache.c.

Referenced by add_entry().

◆ cache_tail

struct CacheEntry* cache_tail
static

DLL tail for least recently used cache entries; recently used cache items are at the tail.The cache enties are added to this queue when their demand becomes zero.

They are removed from the queue when they are needed by any operation.

Definition at line 84 of file gnunet-service-testbed_cache.c.

◆ cache_size

unsigned int cache_size
static

Maximum number of elements to cache.

Definition at line 89 of file gnunet-service-testbed_cache.c.

Referenced by add_entry(), GST_cache_clear(), and GST_cache_init().