DPDK patches and discussions
 help / color / mirror / Atom feed
* [dpdk-dev] [PATCH] hash: added rte_hash_keys to extract all keys
@ 2014-08-12 21:47 Tomas Vestelind
  2014-08-28 19:23 ` Thomas Monjalon
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Tomas Vestelind @ 2014-08-12 21:47 UTC (permalink / raw)
  To: dev

I added a function which extracts all the configured keys in a hash map.
This is good to have when debugging and printing data store in hash
maps.

Signed-off-by: Tomas Vestelind <tomas.vestelind@gmail.com>
---
 lib/librte_hash/rte_hash.c |   26 ++++++++++++++++++++++++++
 lib/librte_hash/rte_hash.h |   15 +++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/lib/librte_hash/rte_hash.c b/lib/librte_hash/rte_hash.c
index d02b6b4..2108c4f 100644
--- a/lib/librte_hash/rte_hash.c
+++ b/lib/librte_hash/rte_hash.c
@@ -481,3 +481,29 @@ rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
 
 	return 0;
 }
+
+unsigned int
+rte_hash_keys(const struct rte_hash *h, void *keys)
+{
+    unsigned int found_keys = 0;
+    unsigned int bucket, entry;
+
+    /* Go through each bucket and all its entries */
+    for (bucket = 0; bucket < h->num_buckets; bucket++) {
+        const hash_sig_t *sig = get_sig_tbl_bucket(h, bucket);
+
+        for (entry = 0; entry < h->bucket_entries; entry++) {
+            /* If the signature is valid, find and save the corresponding key */
+            if (sig[entry] != NULL_SIGNATURE) {
+               uint8_t *key_bucket = get_key_tbl_bucket(h, bucket);
+               void *key = get_key_from_bucket(h, key_bucket, entry);
+               rte_memcpy(keys, key, h->key_len);
+
+               keys = (uint8_t* )keys + h->key_len;
+               found_keys++;
+            }
+        }
+    }
+
+    return found_keys;
+}
diff --git a/lib/librte_hash/rte_hash.h b/lib/librte_hash/rte_hash.h
index 2ecaf1a..e0fb28f 100644
--- a/lib/librte_hash/rte_hash.h
+++ b/lib/librte_hash/rte_hash.h
@@ -303,6 +303,21 @@ rte_hash_hash(const struct rte_hash *h, const void *key)
 int
 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
 		      uint32_t num_keys, int32_t *positions);
+
+/**
+ * Copy the hash table keys to the supplied list of keys.
+ * This operation is multi-thread safe.
+ *
+ * @param h
+ *   Hash table to look in.
+ * @param keys
+ *   A pointer to a list of where keys will be written.
+ *   Must be large enough to fit a potentially full hash map.
+ * @return
+ *   The number of found keys.
+ */
+unsigned int
+rte_hash_keys(const struct rte_hash *h, void *keys);
 #ifdef __cplusplus
 }
 #endif
-- 
1.7.10.4

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [dpdk-dev] [PATCH] hash: added rte_hash_keys to extract all keys
  2014-08-12 21:47 [dpdk-dev] [PATCH] hash: added rte_hash_keys to extract all keys Tomas Vestelind
@ 2014-08-28 19:23 ` Thomas Monjalon
  2014-08-28 22:30 ` Stephen Hemminger
  2014-08-29 10:34 ` Ananyev, Konstantin
  2 siblings, 0 replies; 6+ messages in thread
From: Thomas Monjalon @ 2014-08-28 19:23 UTC (permalink / raw)
  To: dev

2014-08-12 23:47, Tomas Vestelind:
> I added a function which extracts all the configured keys in a hash map.
> This is good to have when debugging and printing data store in hash
> maps.

Someone to review this patch, please?
(and the other one for rte_hash_clear)

-- 
Thomas

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [dpdk-dev] [PATCH] hash: added rte_hash_keys to extract all keys
  2014-08-12 21:47 [dpdk-dev] [PATCH] hash: added rte_hash_keys to extract all keys Tomas Vestelind
  2014-08-28 19:23 ` Thomas Monjalon
@ 2014-08-28 22:30 ` Stephen Hemminger
  2014-08-29 10:34 ` Ananyev, Konstantin
  2 siblings, 0 replies; 6+ messages in thread
From: Stephen Hemminger @ 2014-08-28 22:30 UTC (permalink / raw)
  To: Tomas Vestelind; +Cc: dev

On Tue, 12 Aug 2014 23:47:33 +0200
Tomas Vestelind <tomas.vestelind@gmail.com> wrote:

> I added a function which extracts all the configured keys in a hash map.
> This is good to have when debugging and printing data store in hash
> maps.
> 
> Signed-off-by: Tomas Vestelind <tomas.vestelind@gmail.com>
> ---
>  lib/librte_hash/rte_hash.c |   26 ++++++++++++++++++++++++++
>  lib/librte_hash/rte_hash.h |   15 +++++++++++++++
>  2 files changed, 41 insertions(+)
> 
> diff --git a/lib/librte_hash/rte_hash.c b/lib/librte_hash/rte_hash.c
> index d02b6b4..2108c4f 100644
> --- a/lib/librte_hash/rte_hash.c
> +++ b/lib/librte_hash/rte_hash.c
> @@ -481,3 +481,29 @@ rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
>  
>  	return 0;
>  }
> +
> +unsigned int
> +rte_hash_keys(const struct rte_hash *h, void *keys)
> +{
> +    unsigned int found_keys = 0;
> +    unsigned int bucket, entry;
> +
> +    /* Go through each bucket and all its entries */
> +    for (bucket = 0; bucket < h->num_buckets; bucket++) {
> +        const hash_sig_t *sig = get_sig_tbl_bucket(h, bucket);
> +
> +        for (entry = 0; entry < h->bucket_entries; entry++) {
> +            /* If the signature is valid, find and save the corresponding key */
> +            if (sig[entry] != NULL_SIGNATURE) {
> +               uint8_t *key_bucket = get_key_tbl_bucket(h, bucket);
> +               void *key = get_key_from_bucket(h, key_bucket, entry);
> +               rte_memcpy(keys, key, h->key_len);
> +
> +               keys = (uint8_t* )keys + h->key_len;
> +               found_keys++;
> +            }
> +        }
> +    }
> +
> +    return found_keys;
> +}
> diff --git a/lib/librte_hash/rte_hash.h b/lib/librte_hash/rte_hash.h
> index 2ecaf1a..e0fb28f 100644
> --- a/lib/librte_hash/rte_hash.h
> +++ b/lib/librte_hash/rte_hash.h
> @@ -303,6 +303,21 @@ rte_hash_hash(const struct rte_hash *h, const void *key)
>  int
>  rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
>  		      uint32_t num_keys, int32_t *positions);
> +
> +/**
> + * Copy the hash table keys to the supplied list of keys.
> + * This operation is multi-thread safe.
> + *
> + * @param h
> + *   Hash table to look in.
> + * @param keys
> + *   A pointer to a list of where keys will be written.
> + *   Must be large enough to fit a potentially full hash map.
> + * @return
> + *   The number of found keys.
> + */
> +unsigned int
> +rte_hash_keys(const struct rte_hash *h, void *keys);
>  #ifdef __cplusplus
>  }
>  #endif

Please indent with tabs not spaces.
Using rte_memcpy() is no better than just using memcpy.
Please put blank line after declartions.

Writing code with continue makes it easier?
       for (entry = 0; entry < h->bucket_entries; entry++) {
           /* If the signature is valid, find and save the corresponding key */
           if (sig[entry] == NULL_SIGNATURE
			continue;

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [dpdk-dev] [PATCH] hash: added rte_hash_keys to extract all keys
  2014-08-12 21:47 [dpdk-dev] [PATCH] hash: added rte_hash_keys to extract all keys Tomas Vestelind
  2014-08-28 19:23 ` Thomas Monjalon
  2014-08-28 22:30 ` Stephen Hemminger
@ 2014-08-29 10:34 ` Ananyev, Konstantin
  2014-08-29 16:08   ` Stephen Hemminger
  2 siblings, 1 reply; 6+ messages in thread
From: Ananyev, Konstantin @ 2014-08-29 10:34 UTC (permalink / raw)
  To: Tomas Vestelind, dev

Hi Tomas,

> From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Tomas Vestelind
> Sent: Tuesday, August 12, 2014 10:48 PM
> To: dev@dpdk.org
> Subject: [dpdk-dev] [PATCH] hash: added rte_hash_keys to extract all keys
> 
> I added a function which extracts all the configured keys in a hash map.
> This is good to have when debugging and printing data store in hash
> maps.
> 
> Signed-off-by: Tomas Vestelind <tomas.vestelind@gmail.com>
> ---
>  lib/librte_hash/rte_hash.c |   26 ++++++++++++++++++++++++++
>  lib/librte_hash/rte_hash.h |   15 +++++++++++++++
>  2 files changed, 41 insertions(+)
> 
> diff --git a/lib/librte_hash/rte_hash.c b/lib/librte_hash/rte_hash.c
> index d02b6b4..2108c4f 100644
> --- a/lib/librte_hash/rte_hash.c
> +++ b/lib/librte_hash/rte_hash.c
> @@ -481,3 +481,29 @@ rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
> 
>  	return 0;
>  }
> +
> +unsigned int
> +rte_hash_keys(const struct rte_hash *h, void *keys)
> +{
> +    unsigned int found_keys = 0;
> +    unsigned int bucket, entry;
> +
> +    /* Go through each bucket and all its entries */
> +    for (bucket = 0; bucket < h->num_buckets; bucket++) {
> +        const hash_sig_t *sig = get_sig_tbl_bucket(h, bucket);
> +
> +        for (entry = 0; entry < h->bucket_entries; entry++) {
> +            /* If the signature is valid, find and save the corresponding key */
> +            if (sig[entry] != NULL_SIGNATURE) {
> +               uint8_t *key_bucket = get_key_tbl_bucket(h, bucket);
> +               void *key = get_key_from_bucket(h, key_bucket, entry);
> +               rte_memcpy(keys, key, h->key_len);
> +
> +               keys = (uint8_t* )keys + h->key_len;
> +               found_keys++;
> +            }
> +        }
> +    }

I suppose we need to add one extra parameter:  keys_size (size of the buffer provided) and inside the loop check that we wouldn't go beyond it.
Also it would probably helpful to have a function that would return to the user size of buffer needed to sore all keys from the hash.
What you probably can do: implement a function that would iterate over all elements in the hash table and for each of them call a callback (taken as an argument).
Then all these functions and rte_hash_clear() could be implemented on top of it.
Something like:

typedef int (*rte_hash_elem_iter_t)(const struct rte_hash*hash, const void *key, void *user_arg);
void
rte_hash_elem_iterate(const struct rte_hash *h, rte_hash_key_elem_t iter, void *arg)
{
   ...
}

About rte_hash_clear(const struct rte_hash *h) - can't we just do memset(h->sig_tbl, NULL_SIGNATURE, sig_tbl_size);?
BTW, why it is pointer to the const strucuture if we modifying it?

> +
> +    return found_keys;
> +}
> diff --git a/lib/librte_hash/rte_hash.h b/lib/librte_hash/rte_hash.h
> index 2ecaf1a..e0fb28f 100644
> --- a/lib/librte_hash/rte_hash.h
> +++ b/lib/librte_hash/rte_hash.h
> @@ -303,6 +303,21 @@ rte_hash_hash(const struct rte_hash *h, const void *key)
>  int
>  rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
>  		      uint32_t num_keys, int32_t *positions);
> +
> +/**
> + * Copy the hash table keys to the supplied list of keys.
> + * This operation is multi-thread safe.
> + *
> + * @param h
> + *   Hash table to look in.
> + * @param keys
> + *   A pointer to a list of where keys will be written.
> + *   Must be large enough to fit a potentially full hash map.
> + * @return
> + *   The number of found keys.
> + */
> +unsigned int
> +rte_hash_keys(const struct rte_hash *h, void *keys);
>  #ifdef __cplusplus
>  }
>  #endif
> --
> 1.7.10.4

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [dpdk-dev] [PATCH] hash: added rte_hash_keys to extract all keys
  2014-08-29 10:34 ` Ananyev, Konstantin
@ 2014-08-29 16:08   ` Stephen Hemminger
  2014-09-02  8:29     ` Tomas Vestelind
  0 siblings, 1 reply; 6+ messages in thread
From: Stephen Hemminger @ 2014-08-29 16:08 UTC (permalink / raw)
  To: Ananyev, Konstantin; +Cc: dev

We implemented a more general hash iterator, thought the patch was already submitted.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [dpdk-dev] [PATCH] hash: added rte_hash_keys to extract all keys
  2014-08-29 16:08   ` Stephen Hemminger
@ 2014-09-02  8:29     ` Tomas Vestelind
  0 siblings, 0 replies; 6+ messages in thread
From: Tomas Vestelind @ 2014-09-02  8:29 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: dev

Hello!

If there's already an iterator then I guess this patch is superfluous.
I tried searching for it among the e-mails but I can find it, can
someone point me to it?

If you still think this has some values I will take the given feedback
and make corrections.

Best regards,
Tomas Vestelind

ps. Stephen sorry for the extra mail, I forgot to cc the correct
people in the first one.

On 8/29/14, Stephen Hemminger <stephen@networkplumber.org> wrote:
> We implemented a more general hash iterator, thought the patch was already
> submitted.
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-09-02  8:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-12 21:47 [dpdk-dev] [PATCH] hash: added rte_hash_keys to extract all keys Tomas Vestelind
2014-08-28 19:23 ` Thomas Monjalon
2014-08-28 22:30 ` Stephen Hemminger
2014-08-29 10:34 ` Ananyev, Konstantin
2014-08-29 16:08   ` Stephen Hemminger
2014-09-02  8:29     ` Tomas Vestelind

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).