diff options
author | Enrico Loparco (eloparco) <eloparco@cisco.com> | 2022-09-08 12:18:09 +0000 |
---|---|---|
committer | Enrico Loparco (eloparco) <eloparco@cisco.com> | 2022-09-12 13:23:29 +0000 |
commit | 6ee6b65ef03f8a479cccb2ae04e6b156f309f045 (patch) | |
tree | ea59d0e048c53c40490bc7290eda09ccad954423 /hicn-light/src/hicn/core | |
parent | cb6f5724b85e51295498a39144ed4ccce114ad53 (diff) |
feat(slab): add slab allocator to store hashtables' keys
Ref: HICN-777
Signed-off-by: Enrico Loparco (eloparco) <eloparco@cisco.com>
Change-Id: Ibbd5c5e73cfd2f6adf757f7248dff8a933515d21
Diffstat (limited to 'hicn-light/src/hicn/core')
-rw-r--r-- | hicn-light/src/hicn/core/connection_table.c | 38 | ||||
-rw-r--r-- | hicn-light/src/hicn/core/connection_table.h | 3 | ||||
-rw-r--r-- | hicn-light/src/hicn/core/forwarder.c | 11 | ||||
-rw-r--r-- | hicn-light/src/hicn/core/listener_table.c | 38 | ||||
-rw-r--r-- | hicn-light/src/hicn/core/listener_table.h | 3 | ||||
-rw-r--r-- | hicn-light/src/hicn/core/packet_cache.c | 61 | ||||
-rw-r--r-- | hicn-light/src/hicn/core/packet_cache.h | 14 |
7 files changed, 95 insertions, 73 deletions
diff --git a/hicn-light/src/hicn/core/connection_table.c b/hicn-light/src/hicn/core/connection_table.c index 7bc0e2f4c..a6b114c01 100644 --- a/hicn-light/src/hicn/core/connection_table.c +++ b/hicn-light/src/hicn/core/connection_table.c @@ -19,6 +19,7 @@ */ #include <hicn/util/log.h> +#include <hicn/util/sstrncpy.h> #include "connection.h" #include "connection_table.h" @@ -27,6 +28,10 @@ */ #define DEFAULT_CONNECTION_TABLE_SIZE 64 +typedef struct { + char name[SYMBOLIC_NAME_LEN]; +} name_key_t; + connection_table_t *_connection_table_create(size_t init_size, size_t max_size) { if (init_size == 0) init_size = DEFAULT_CONNECTION_TABLE_SIZE; @@ -38,7 +43,9 @@ connection_table_t *_connection_table_create(size_t init_size, /* Initialize indices */ table->id_by_pair = kh_init_ct_pair(); + table->pair_keys = slab_create(address_pair_t, SLAB_INIT_SIZE); table->id_by_name = kh_init_ct_name(); + table->name_keys = slab_create(name_key_t, SLAB_INIT_SIZE); /* * We start by allocating a reasonably-sized pool, as this will eventually @@ -50,26 +57,20 @@ connection_table_t *_connection_table_create(size_t init_size, } void connection_table_free(connection_table_t *table) { - const char *k_name; - const address_pair_t *k_pair; - unsigned v_conn_id; + unsigned conn_id; + kh_foreach_value(table->id_by_name, conn_id, { + connection_t *connection = connection_table_get_by_id(table, conn_id); + const char *name = connection_get_name(connection); - connection_t *connection; - const char *name; - kh_foreach(table->id_by_name, k_name, v_conn_id, { - connection = connection_table_get_by_id(table, v_conn_id); - name = connection_get_name(connection); INFO("Removing connection %s [%d]", name, connection->fd); connection_finalize(connection); }); - (void)v_conn_id; - kh_foreach(table->id_by_name, k_name, v_conn_id, { free((char *)k_name); }); - kh_foreach(table->id_by_pair, k_pair, v_conn_id, - { free((address_pair_t *)k_pair); }); - kh_destroy_ct_pair(table->id_by_pair); + slab_free(table->pair_keys); kh_destroy_ct_name(table->id_by_name); + slab_free(table->name_keys); + pool_free(table->connections); free(table); } @@ -93,12 +94,15 @@ connection_t *connection_table_allocate(const connection_table_t *table, int rc; // Add in name hash table - khiter_t k = kh_put_ct_name(table->id_by_name, strdup(name), &rc); + name_key_t *name_copy = slab_get(name_key_t, table->name_keys); + strcpy_s(name_copy->name, sizeof(name_key_t), name); + + khiter_t k = kh_put_ct_name(table->id_by_name, name_copy->name, &rc); assert(rc == KH_ADDED || rc == KH_RESET); kh_value(table->id_by_name, k) = (unsigned int)id; // Add in pair hash table - address_pair_t *pair_copy = (address_pair_t *)malloc(sizeof(address_pair_t)); + address_pair_t *pair_copy = slab_get(address_pair_t, table->pair_keys); memcpy(pair_copy, pair, sizeof(address_pair_t)); k = kh_put_ct_pair(table->id_by_pair, pair_copy, &rc); @@ -125,14 +129,14 @@ void connection_table_deallocate(const connection_table_t *table, // Remove from name hash table khiter_t k = kh_get_ct_name(table->id_by_name, name); assert(k != kh_end(table->id_by_name)); - free((char *)kh_key(table->id_by_name, k)); kh_del_ct_name(table->id_by_name, k); + slab_put(table->name_keys, kh_key(table->id_by_name, k)); // Remove from pair hash table k = kh_get_ct_pair(table->id_by_pair, pair); assert(k != kh_end(table->id_by_pair)); - free((address_pair_t *)kh_key(table->id_by_pair, k)); kh_del_ct_pair(table->id_by_pair, k); + slab_put(table->pair_keys, kh_key(table->id_by_pair, k)); assert(kh_size(table->id_by_name) == kh_size(table->id_by_pair)); pool_put(table->connections, conn); diff --git a/hicn-light/src/hicn/core/connection_table.h b/hicn-light/src/hicn/core/connection_table.h index 566443d93..dcfb1e1dd 100644 --- a/hicn-light/src/hicn/core/connection_table.h +++ b/hicn-light/src/hicn/core/connection_table.h @@ -32,6 +32,7 @@ #include <hicn/util/khash.h> #include <hicn/util/hash.h> +#include <hicn/util/slab.h> #include "address_pair.h" #include "connection.h" #include <hicn/util/pool.h> @@ -48,7 +49,9 @@ typedef struct { size_t max_size; kh_ct_pair_t *id_by_pair; + slab_t *pair_keys; kh_ct_name_t *id_by_name; + slab_t *name_keys; connection_t *connections; // pool } connection_table_t; diff --git a/hicn-light/src/hicn/core/forwarder.c b/hicn-light/src/hicn/core/forwarder.c index 72bb22118..d148f845d 100644 --- a/hicn-light/src/hicn/core/forwarder.c +++ b/hicn-light/src/hicn/core/forwarder.c @@ -805,7 +805,7 @@ int _forwarder_forward_aggregated_interest( assert(msgbuf_id_is_valid(msgbuf_id) && msgbuf_get_type(msgbuf) == HICN_PACKET_TYPE_INTEREST); - bool ret = -1; + int ret = -1; fib_entry_t *fib_entry = fib_match_msgbuf(forwarder->fib, msgbuf); if (!fib_entry) goto END; @@ -843,9 +843,8 @@ int _forwarder_forward_aggregated_interest( BITMAP_SIZE * sizeof(hicn_uword)); size_t suffix_index = 0; // Position of suffix in initial manifest - interest_manifest_header_t *manifest; + interest_manifest_header_t *manifest = NULL; size_t payload_size; - int ret; while (suffix_index < total_suffixes) { // If more than one sub-manifest, // clone original interest manifest and update suffix @@ -869,9 +868,9 @@ int _forwarder_forward_aggregated_interest( // Update manifest bitmap in current msgbuf - ret = + int ret = _forwarder_get_interest_manifest(msgbuf, &manifest, &payload_size); - assert(ret == 0); + _ASSERT(ret == 0); memcpy(manifest->request_bitmap, curr_bitmap, BITMAP_SIZE * sizeof(hicn_uword)); WITH_TRACE({ @@ -1088,7 +1087,7 @@ static ssize_t forwarder_process_interest(forwarder_t *forwarder, pkt_cache_save_suffixes_for_prefix( forwarder->pkt_cache, hicn_name_get_prefix(msgbuf_get_name(msgbuf))); - if (!int_manifest_header) + if (ret == -1) return forwarder_process_single_interest(forwarder, msgbuf_pool, msgbuf, msgbuf_id); return forwarder_process_aggregated_interest(forwarder, int_manifest_header, diff --git a/hicn-light/src/hicn/core/listener_table.c b/hicn-light/src/hicn/core/listener_table.c index c130399a5..4a0f74a13 100644 --- a/hicn-light/src/hicn/core/listener_table.c +++ b/hicn-light/src/hicn/core/listener_table.c @@ -19,6 +19,7 @@ */ #include <hicn/util/log.h> +#include <hicn/util/sstrncpy.h> #include "listener_table.h" #include "listener.h" @@ -27,6 +28,10 @@ */ #define DEFAULT_LISTENER_TABLE_SIZE 64 +typedef struct { + char name[SYMBOLIC_NAME_LEN]; +} name_key_t; + listener_table_t *_listener_table_create(size_t init_size, size_t max_size) { if (init_size == 0) init_size = DEFAULT_LISTENER_TABLE_SIZE; @@ -37,7 +42,9 @@ listener_table_t *_listener_table_create(size_t init_size, size_t max_size) { /* Initialize indices */ table->id_by_name = kh_init_lt_name(); + table->name_keys = slab_create(name_key_t, SLAB_INIT_SIZE); table->id_by_key = kh_init_lt_key(); + table->listener_keys = slab_create(listener_key_t, SLAB_INIT_SIZE); /* * We start by allocating a reasonably-sized pool, as this will eventually @@ -49,25 +56,19 @@ listener_table_t *_listener_table_create(size_t init_size, size_t max_size) { } void listener_table_free(listener_table_t *table) { - const char *k_name; - const listener_key_t *k_key; - unsigned v; - - listener_t *listener; - const char *name; - kh_foreach(table->id_by_key, k_key, v, { - listener = listener_table_get_by_id(table, v); - name = listener_get_name(listener); + unsigned listener_id; + kh_foreach_value(table->id_by_key, listener_id, { + listener_t *listener = listener_table_get_by_id(table, listener_id); + const char *name = listener_get_name(listener); INFO("Removing listener %s [%d]", name, listener->fd); listener_finalize(listener); }); - (void)v; - kh_foreach(table->id_by_name, k_name, v, { free((char *)k_name); }); - kh_foreach(table->id_by_key, k_key, v, { free((listener_key_t *)k_key); }); - kh_destroy_lt_name(table->id_by_name); + slab_free(table->name_keys); kh_destroy_lt_key(table->id_by_key); + slab_free(table->listener_keys); + pool_free(table->listeners); free(table); } @@ -83,12 +84,15 @@ listener_t *listener_table_allocate(const listener_table_t *table, int rc; // Add in name hash table - khiter_t k = kh_put_lt_name(table->id_by_name, strdup(name), &rc); + name_key_t *name_copy = slab_get(name_key_t, table->name_keys); + strcpy_s(name_copy->name, sizeof(name_key_t), name); + + khiter_t k = kh_put_lt_name(table->id_by_name, name_copy->name, &rc); assert(rc == KH_ADDED || rc == KH_RESET); kh_value(table->id_by_name, k) = (unsigned int)id; // Add in key hash table - listener_key_t *key_copy = (listener_key_t *)malloc(sizeof(listener_key_t)); + listener_key_t *key_copy = slab_get(listener_key_t, table->listener_keys); memcpy(key_copy, key, sizeof(listener_key_t)); k = kh_put_lt_key(table->id_by_key, key_copy, &rc); @@ -107,14 +111,14 @@ void listener_table_deallocate(const listener_table_t *table, // Remove from name hash table khiter_t k = kh_get_lt_name(table->id_by_name, name); assert(k != kh_end(table->id_by_name)); - free((char *)kh_key(table->id_by_name, k)); kh_del_lt_name(table->id_by_name, k); + slab_put(table->name_keys, kh_key(table->id_by_name, k)); // Remove from key hash table k = kh_get_lt_key(table->id_by_key, key); assert(k != kh_end(table->id_by_key)); - free((listener_key_t *)kh_key(table->id_by_key, k)); kh_del_lt_key(table->id_by_key, k); + slab_put(table->listener_keys, kh_key(table->id_by_key, k)); assert(kh_size(table->id_by_name) == kh_size(table->id_by_key)); pool_put(table->listeners, listener); diff --git a/hicn-light/src/hicn/core/listener_table.h b/hicn-light/src/hicn/core/listener_table.h index 27c5daa04..062d1748e 100644 --- a/hicn-light/src/hicn/core/listener_table.h +++ b/hicn-light/src/hicn/core/listener_table.h @@ -32,6 +32,7 @@ #include <hicn/util/khash.h> #include <hicn/util/hash.h> +#include <hicn/util/slab.h> #include "address.h" #include "listener.h" #include <hicn/util/pool.h> @@ -48,7 +49,9 @@ typedef struct { size_t max_size; kh_lt_key_t *id_by_key; + slab_t *listener_keys; kh_lt_name_t *id_by_name; + slab_t *name_keys; listener_t *listeners; // pool } listener_table_t; diff --git a/hicn-light/src/hicn/core/packet_cache.c b/hicn-light/src/hicn/core/packet_cache.c index 9d0b041c3..7fa18b48f 100644 --- a/hicn-light/src/hicn/core/packet_cache.c +++ b/hicn-light/src/hicn/core/packet_cache.c @@ -84,13 +84,9 @@ const char *_pkt_cache_verdict_str[] = { * Free the two level packet cache structure (helper) */ void _prefix_map_free(kh_pkt_cache_prefix_t *prefix_to_suffixes) { - const hicn_name_prefix_t *key; - kh_pkt_cache_suffix_t *value; - kh_foreach(prefix_to_suffixes, key, value, { - //(void)key; - free((hicn_name_prefix_t *)key); - kh_destroy_pkt_cache_suffix(value); - }); + kh_pkt_cache_suffix_t *suffix; + kh_foreach_value(prefix_to_suffixes, suffix, + { kh_destroy_pkt_cache_suffix(suffix); }); kh_destroy_pkt_cache_prefix(prefix_to_suffixes); } @@ -99,7 +95,7 @@ void _prefix_map_free(kh_pkt_cache_prefix_t *prefix_to_suffixes) { */ kh_pkt_cache_suffix_t *_get_suffixes(kh_pkt_cache_prefix_t *prefix_to_suffixes, const hicn_name_prefix_t *prefix, - bool create) { + bool create, slab_t *prefix_keys) { khiter_t k = kh_get_pkt_cache_prefix(prefix_to_suffixes, prefix); /* Return suffixes if found... */ @@ -114,8 +110,8 @@ kh_pkt_cache_suffix_t *_get_suffixes(kh_pkt_cache_prefix_t *prefix_to_suffixes, */ kh_pkt_cache_suffix_t *suffixes = kh_init_pkt_cache_suffix(); - hicn_name_prefix_t *prefix_copy = malloc(sizeof(hicn_name_prefix_t)); - *prefix_copy = *prefix; + hicn_name_prefix_t *prefix_copy = slab_get(hicn_name_prefix_t, prefix_keys); + memcpy(prefix_copy, prefix, sizeof(hicn_name_prefix_t)); int rc; k = kh_put_pkt_cache_prefix(prefix_to_suffixes, prefix_copy, &rc); @@ -129,8 +125,9 @@ kh_pkt_cache_suffix_t *_get_suffixes(kh_pkt_cache_prefix_t *prefix_to_suffixes, */ void _remove_suffix(kh_pkt_cache_prefix_t *prefixes, const hicn_name_prefix_t *prefix, - const hicn_name_suffix_t suffix) { - kh_pkt_cache_suffix_t *suffixes = _get_suffixes(prefixes, prefix, false); + const hicn_name_suffix_t suffix, slab_t *prefix_keys) { + kh_pkt_cache_suffix_t *suffixes = + _get_suffixes(prefixes, prefix, false, prefix_keys); assert(suffixes != NULL); khiter_t k = kh_get_pkt_cache_suffix(suffixes, suffix); @@ -159,8 +156,10 @@ void __add_suffix(kh_pkt_cache_suffix_t *suffixes, hicn_name_suffix_t suffix, */ void _add_suffix(kh_pkt_cache_prefix_t *prefixes, const hicn_name_prefix_t *prefix, - const hicn_name_suffix_t suffix, unsigned val) { - kh_pkt_cache_suffix_t *suffixes = _get_suffixes(prefixes, prefix, true); + const hicn_name_suffix_t suffix, unsigned val, + slab_t *prefix_keys) { + kh_pkt_cache_suffix_t *suffixes = + _get_suffixes(prefixes, prefix, true, prefix_keys); assert(suffixes != NULL); __add_suffix(suffixes, suffix, val); @@ -184,9 +183,10 @@ unsigned __get_suffix(kh_pkt_cache_suffix_t *suffixes, unsigned _get_suffix(kh_pkt_cache_prefix_t *prefixes, const hicn_name_prefix_t *prefix, - hicn_name_suffix_t suffix) { + hicn_name_suffix_t suffix, slab_t *prefix_keys) { /* create is false as this function is always called by lookup */ - kh_pkt_cache_suffix_t *suffixes = _get_suffixes(prefixes, prefix, false); + kh_pkt_cache_suffix_t *suffixes = + _get_suffixes(prefixes, prefix, false, prefix_keys); if (!suffixes) { return HICN_INVALID_SUFFIX; } @@ -197,11 +197,11 @@ unsigned _get_suffix(kh_pkt_cache_prefix_t *prefixes, * Lookup in both the first and second levels of the packet cache (helper) */ unsigned _get_suffix_from_name(kh_pkt_cache_prefix_t *prefixes, - const hicn_name_t *name) { + const hicn_name_t *name, slab_t *prefix_keys) { const hicn_name_prefix_t *prefix = hicn_name_get_prefix(name); const hicn_name_suffix_t suffix = hicn_name_get_suffix(name); - return _get_suffix(prefixes, prefix, suffix); + return _get_suffix(prefixes, prefix, suffix, prefix_keys); } void pkt_cache_save_suffixes_for_prefix(pkt_cache_t *pkt_cache, @@ -216,8 +216,9 @@ void pkt_cache_save_suffixes_for_prefix(pkt_cache_t *pkt_cache, // Update cached prefix information pkt_cache->cached_prefix = *prefix; pkt_cache->cached_suffixes = - _get_suffixes(pkt_cache->prefix_to_suffixes, prefix, true); // XXX - // + _get_suffixes(pkt_cache->prefix_to_suffixes, prefix, true, + pkt_cache->prefix_keys); // XXX + // } void pkt_cache_reset_suffixes_for_prefix(pkt_cache_t *pkt_cache) { @@ -237,6 +238,7 @@ pkt_cache_t *pkt_cache_create(size_t cs_size) { if (!pkt_cache->cs) return NULL; pkt_cache->prefix_to_suffixes = kh_init_pkt_cache_prefix(); + pkt_cache->prefix_keys = slab_create(hicn_name_prefix_t, SLAB_INIT_SIZE); pool_init(pkt_cache->entries, DEFAULT_PKT_CACHE_SIZE, 0); pkt_cache->cached_prefix = HICN_NAME_PREFIX_EMPTY; @@ -250,6 +252,7 @@ void pkt_cache_free(pkt_cache_t *pkt_cache) { // Free prefix hash table and pool _prefix_map_free(pkt_cache->prefix_to_suffixes); + slab_free(pkt_cache->prefix_keys); pool_free(pkt_cache->entries); // Free PIT and CS @@ -261,8 +264,10 @@ void pkt_cache_free(pkt_cache_t *pkt_cache) { kh_pkt_cache_suffix_t *pkt_cache_get_suffixes(const pkt_cache_t *pkt_cache, const hicn_name_prefix_t *prefix, - bool create) { - return _get_suffixes(pkt_cache->prefix_to_suffixes, prefix, create); + bool create, + slab_t *prefix_keys) { + return _get_suffixes(pkt_cache->prefix_to_suffixes, prefix, create, + prefix_keys); } pkt_cache_entry_t *pkt_cache_allocate(pkt_cache_t *pkt_cache) { @@ -286,7 +291,8 @@ void pkt_cache_add_to_index(const pkt_cache_t *pkt_cache, (unsigned int)id); } else { _add_suffix(pkt_cache->prefix_to_suffixes, hicn_name_get_prefix(name), - hicn_name_get_suffix(name), (unsigned int)id); + hicn_name_get_suffix(name), (unsigned int)id, + pkt_cache->prefix_keys); } } @@ -296,7 +302,7 @@ void pkt_cache_add_to_index(const pkt_cache_t *pkt_cache, void pkt_cache_remove_from_index(const pkt_cache_t *pkt_cache, const hicn_name_t *name) { _remove_suffix(pkt_cache->prefix_to_suffixes, hicn_name_get_prefix(name), - hicn_name_get_suffix(name)); + hicn_name_get_suffix(name), pkt_cache->prefix_keys); // TODO #if 0 @@ -321,7 +327,8 @@ pkt_cache_entry_t *pkt_cache_lookup(pkt_cache_t *pkt_cache, index = __get_suffix(pkt_cache->cached_suffixes, hicn_name_get_suffix(name)); } else { - index = _get_suffix_from_name(pkt_cache->prefix_to_suffixes, name); + index = _get_suffix_from_name(pkt_cache->prefix_to_suffixes, name, + pkt_cache->prefix_keys); } if (index == HICN_INVALID_SUFFIX) { @@ -364,7 +371,7 @@ void pkt_cache_cs_remove_entry(pkt_cache_t *pkt_cache, pkt_cache_entry_t *entry, // XXX const hicn_name_t *name = msgbuf_get_name(msgbuf); _remove_suffix(pkt_cache->prefix_to_suffixes, hicn_name_get_prefix(&entry->name), - hicn_name_get_suffix(&entry->name)); + hicn_name_get_suffix(&entry->name), pkt_cache->prefix_keys); // Do not update the LRU cache for evicted entries if (!is_evicted) cs_vft[pkt_cache->cs->type]->remove_entry(pkt_cache, entry); @@ -391,7 +398,7 @@ void pkt_cache_pit_remove_entry(pkt_cache_t *pkt_cache, const hicn_name_t *name = &entry->name; _remove_suffix(pkt_cache->prefix_to_suffixes, hicn_name_get_prefix(name), - hicn_name_get_suffix(name)); + hicn_name_get_suffix(name), pkt_cache->prefix_keys); pool_put(pkt_cache->entries, entry); diff --git a/hicn-light/src/hicn/core/packet_cache.h b/hicn-light/src/hicn/core/packet_cache.h index dcbc20c7d..1abdc57c2 100644 --- a/hicn-light/src/hicn/core/packet_cache.h +++ b/hicn-light/src/hicn/core/packet_cache.h @@ -41,6 +41,7 @@ #define HICNLIGHT_PACKET_CACHE_H #include <hicn/util/khash.h> +#include <hicn/util/slab.h> #include "content_store.h" #include "pit.h" #include "msgbuf_pool.h" @@ -112,6 +113,7 @@ typedef struct { cs_t *cs; pkt_cache_entry_t *entries; kh_pkt_cache_prefix_t *prefix_to_suffixes; + slab_t *prefix_keys; // Cached prefix info to avoid double lookups, // used for both single interest speculation and interest manifest @@ -455,19 +457,19 @@ unsigned __get_suffix(kh_pkt_cache_suffix_t *suffixes, hicn_name_suffix_t suffix); unsigned _get_suffix(kh_pkt_cache_prefix_t *prefixes, const hicn_name_prefix_t *prefix, - hicn_name_suffix_t suffix); + hicn_name_suffix_t suffix, slab_t *prefix_keys); void __add_suffix(kh_pkt_cache_suffix_t *suffixes, hicn_name_suffix_t suffix, unsigned val); void _add_suffix(kh_pkt_cache_prefix_t *prefixes, - const hicn_name_prefix_t *prefix, hicn_name_suffix_t suffix, - unsigned val); + const hicn_name_prefix_t *prefix, + const hicn_name_suffix_t suffix, unsigned val, slab_t *slab); void _remove_suffix(kh_pkt_cache_prefix_t *prefixes, - const hicn_name_prefix_t *prefix, - hicn_name_suffix_t suffix); + const hicn_name_prefix_t *prefix, hicn_name_suffix_t suffix, + slab_t *slab); void _prefix_map_free(kh_pkt_cache_prefix_t *prefix_to_suffixes); kh_pkt_cache_suffix_t *_get_suffixes(kh_pkt_cache_prefix_t *prefix_to_suffixes, const hicn_name_prefix_t *prefix, - bool create); + bool create, slab_t *prefix_keys); #endif /************** Content Store *****************************/ |