Simplify hash_resize.c
cover Simplify hash_resize.c
From 7b69a9b37ba9a73a50aad5cbb097235ddfe75870 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Mon, 14 Jan 2019 07:25:14 +0900
Subject: [PATCH 0/6] Simplify hash_resize.c
Hi Paul,
This patch set updates hash_resize.c, which you suggested for me to take over, and the related text.
I added a couple of Quick Quizzes as well, and in one of them, I included your version of hash_resize.c.
Patch #1 updates hash_resize.c in the way I suggested. Note that in this update, "#ifndef FCV_SNIPPET" blocks are used to hide code for debugging (hash value checks) in code snippets. This code can actually be compiled with "-DFCV_SNIPPET" to see the performance without hash value checks.
Patch #2 adds a couple of Quick Quizzes. Your version of hash_resize.c is added as hash_resize_s.c.
Patch #3 removes unnecessary folding in a code snippet.
Patch #4 adjusts a few sentence to the simpler approach.
Patch #5 adds another Quick Quiz.
Patch #6 adds an "#error" directive for the lack of rcu_barrier().
All of the updates in text would need your native eyes to be polished.
Thanks, Akira
PS:
I couldn't make out your concern of "if we ever want to iterate over the hash table". Can you elaborate it?
--
Akira Yokosawa (6):
datastruct/hash: Simplify hash_resize.c
datastruct/hash: Add a couple of Quick Quizzes regarding hash_resize.c datastruct/hash: Fix unnecessary folding in code snippet datastruct/hash: Adjust context to updated code in hash_resize.c datastruct/hash: Add Quick Quiz on READ_ONCE/WRITE_ONCE in
hash_resize.c
datastruct/hash: Display error msg if rcu_barrier() is not available
CodeSamples/datastruct/hash/Makefile | 5 +- CodeSamples/datastruct/hash/hash_resize.c | 62 +++-- CodeSamples/datastruct/hash/hash_resize_s.c | 365 ++++++++++++++++++++++++++++ CodeSamples/datastruct/hash/hashtorture.h | 11 +- datastruct/datastruct.tex | 146 +++++++---- 5 files changed, 513 insertions(+), 76 deletions(-) create mode 100644 CodeSamples/datastruct/hash/hash_resize_s.c
Signature · 1 line
2.7.4
PATCH 1/6 datastruct/hash: Simplify hash_resize.c
From 1ffe7edfb90cbb8efb2a33d2ae8c3e855e684acf Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Sun, 13 Jan 2019 19:47:23 +0900
Subject: [PATCH 1/6] datastruct/hash: Simplify hash_resize.c
By keeping updating the current (old) hash table until resizing completes, hashtab_lookup() only needs to see the current hash table. Instead, hashtab_add() and hashtab_del() need to update the bucket in the current hash table as well as that in the new hash table if hashtab_lock_mod() has locked the new bucket.
This simplifies the lookup side and no performance degradation is expected as long as no resizing is in progress.
Note that during resize operation, the cost of updates can be doubled in the worst case.
--- CodeSamples/datastruct/hash/hash_resize.c | 56 ++++++++++++++----------- CodeSamples/datastruct/hash/hashtorture.h | 6 ++- datastruct/datastruct.tex | 69 ++++++++++--------------------- 3 files changed, 59 insertions(+), 72 deletions(-)
CodeSamples/datastruct/hash/hash_resize.c +32 -24
@@ -30,7 +30,9 @@struct ht_elem {struct rcu_head rh;struct cds_list_head hte_next[2 ];//\lnlbl{ht_elem:next} -unsigned long hte_hash; +#ifndef FCV_SNIPPET +unsigned long hte_hash[2 ]; +#endif /* #ifndef FCV_SNIPPET */ };/* Hash-table bucket element. */
@@ -41,7 +43,9 @@ struct ht_bucket {struct ht_lock_state {//\lnlbl{hls:b} struct ht_bucket *hbp[2 ]; +#ifndef FCV_SNIPPET unsigned long hls_hash[2 ]; +#endif /* #ifndef FCV_SNIPPET */ int hls_idx[2 ]; };//\lnlbl{hls:e}
@@ -181,8 +185,10 @@ hashtab_lock_mod(struct hashtab *htp_master, void *key, htbp =ht_get_bucket (htp, key, &b, &h);//\lnlbl{l:refbucket} spin_lock (&htbp->htb_lock);//\lnlbl{l:acq_bucket} lsp->hbp[0 ] = htbp;//\lnlbl{l:lsp0b} - lsp->hls_idx[0 ] = htp->ht_idx; - lsp->hls_hash[0 ] = h;//\lnlbl{l:lsp0e} + lsp->hls_idx[0 ] = htp->ht_idx;//\lnlbl{l:lsp0e} +#ifndef FCV_SNIPPET + lsp->hls_hash[0 ] = h; +#endif /* #ifndef FCV_SNIPPET */ if (b >READ_ONCE (htp->ht_resize_cur)) {//\lnlbl{l:ifresized} lsp->hbp[1 ] =NULL ;//\lnlbl{l:lsp1_1} return ;//\lnlbl{l:fastret1}
@@ -190,12 +196,11 @@ hashtab_lock_mod(struct hashtab *htp_master, void *key, htp =rcu_dereference (htp->ht_new);//\lnlbl{l:new_hashtbl} htbp =ht_get_bucket (htp, key, &b, &h);//\lnlbl{l:get_newbkt} spin_lock (&htbp->htb_lock);//\lnlbl{l:acq_newbkt} - lsp->hbp[1 ] = lsp->hbp[0 ];//\lnlbl{l:lsp1b} - lsp->hls_idx[1 ] = lsp->hls_idx[0 ]; - lsp->hls_hash[1 ] = lsp->hls_hash[0 ]; - lsp->hbp[0 ] = htbp; - lsp->hls_idx[0 ] = htp->ht_idx; - lsp->hls_hash[0 ] = h;//\lnlbl{l:lsp1e} + lsp->hbp[1 ] = htbp;//\lnlbl{l:lsp1b} + lsp->hls_idx[1 ] = htp->ht_idx;//\lnlbl{l:lsp1e} +#ifndef FCV_SNIPPET + lsp->hls_hash[1 ] = h; +#endif /* #ifndef FCV_SNIPPET */ }//\lnlbl{l:e} static void //\lnlbl{ul:b}
@@ -230,12 +235,7 @@ hashtab_lookup(struct hashtab *htp_master, void *key) htp =rcu_dereference (htp_master->ht_cur);//\lnlbl{lkp:get_curtbl} htep =ht_search_bucket (htp, key);//\lnlbl{lkp:get_curbkt} -if (htep)//\lnlbl{lkp:entchk} -return htep;//\lnlbl{lkp:ret_match} - htp =rcu_dereference (htp->ht_new);//\lnlbl{lkp:get_nxttbl} -if (!htp)//\lnlbl{lkp:htpchk} -return NULL ;//\lnlbl{lkp:noresize} -return ht_search_bucket (htp, key);//\lnlbl{lkp:ret_nxtbkt} +return htep;//\lnlbl{lkp:ret} }//\lnlbl{lkp:e} /*
@@ -248,9 +248,16 @@ void hashtab_add(struct ht_elem *htep, //\lnlbl{add:b}struct ht_bucket *htbp = lsp->hbp[0 ];//\lnlbl{add:htbp} int i = lsp->hls_idx[0 ];//\lnlbl{add:i} - htep->hte_hash = lsp->hls_hash[0 ];//\lnlbl{add:hash} - htep->hte_next[!i].prev =NULL ;//\lnlbl{add:initp} +#ifndef FCV_SNIPPET + htep->hte_hash[0 ] = lsp->hls_hash[0 ]; +#endif /* #ifndef FCV_SNIPPET */ cds_list_add_rcu (&htep->hte_next[i], &htbp->htb_head);//\lnlbl{add:add} +if ((htbp = lsp->hbp[1 ])) {//\lnlbl{add:ifnew} +#ifndef FCV_SNIPPET + htep->hte_hash[1 ] = lsp->hls_hash[1 ]; +#endif /* #ifndef FCV_SNIPPET */ +cds_list_add_rcu (&htep->hte_next[!i], &htbp->htb_head);//\lnlbl{add:addnew} + } }//\lnlbl{add:e} /*
@@ -262,14 +269,9 @@ void hashtab_del(struct ht_elem *htep, //\lnlbl{del:b} {int i = lsp->hls_idx[0 ];//\lnlbl{del:i} -if (htep->hte_next[i].prev) {//\lnlbl{del:if} -cds_list_del_rcu (&htep->hte_next[i]);//\lnlbl{del:del} - htep->hte_next[i].prev =NULL ;//\lnlbl{del:init} - } -if (lsp->hbp[1 ] && htep->hte_next[!i].prev) {//\lnlbl{del:ifnew} +cds_list_del_rcu (&htep->hte_next[i]);//\lnlbl{del:del} +if (lsp->hbp[1 ])//\lnlbl{del:ifnew} cds_list_del_rcu (&htep->hte_next[!i]);//\lnlbl{del:delnew} - htep->hte_next[!i].prev =NULL ;//\lnlbl{del:initnew} - } }//\lnlbl{del:e} //\end{snippet}
@@ -350,5 +352,11 @@ void defer_del_rcu(struct rcu_head *rhp)#define quiescent_state () rcu_quiescent_state() +#ifndef FCV_SNIPPET +#define check_hash () (htep->hte_hash[0] != hash && htep->hte_hash[1] != hash) +#else +#define check_hash () (0) +#endif /* #ifndef FCV_SNIPPET */ +#include "hashtorture.h" #endif /* #ifdef TEST_HASH */
CodeSamples/datastruct/hash/hashtorture.h +5 -1
@@ -62,6 +62,10 @@ void (*defer_del_done)(struct ht_elem *htep) = NULL;#define rcu_barrier () do ; while (0)#endif /* #ifndef quiescent_state */ +#ifndef check_hash +#define check_hash () (htep->hte_hash != hash) +#endif /* #ifndef check_hash */ +/* * Test variables. */
@@ -988,7 +992,7 @@ int zoo_lookup(char *key) htep =hashtab_lookup (perftest_htp, hash, key); zhep = container_of(htep, struct zoo_he, zhe_e);BUG_ON (htep && - (htep->hte_hash != hash || + (check_hash () ||strncmp (zhep->name, (char *)key,ZOO_NAMELEN ) !=0 ));hashtab_unlock_lookup (perftest_htp, hash);hashtab_lookup_done (htep);
datastruct/datastruct.tex +22 -47
117 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [PATCH 1/6] datastruct/hash: Simplify hash_resize.c
FCV_SNIPPET" blocks are used to hide
code for debugging
Quoted 30 lines · Akira Yokosawa
From
1ffe7edfb90cbb8efb2a33d2ae8c3e855e684acfMon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Sun, 13 Jan 2019 19:47:23 +0900
Subject: [PATCH 1/6] datastruct/hash: Simplifyhash_resize.cBy keeping updating the current (old) hash table until resizing completes,
hashtab_lookup()only needs to see the current hash table. Instead,hashtab_add()andhashtab_del()need to update the bucket in the current hash table as well as that in the new hash table ifhashtab_lock_mod()has locked the new bucket.This simplifies the lookup side and no performance degradation is expected as long as no resizing is in progress.
Note that during resize operation, the cost of updates can be doubled in the worst case.
Signed-off-by Akira Yokosawa <[email protected]>--- CodeSamples/datastruct/hash/hash_resize.c | 56 ++++++++++++++----------- CodeSamples/datastruct/hash/hashtorture.h | 6 ++- datastruct/datastruct.tex | 69 ++++++++++--------------------- 3 files changed, 59 insertions(+), 72 deletions(-)CodeSamples/datastruct/hash/hash_resize.c +0
-0
CodeSamples/datastruct/hash/hash_resize.c line 35
Quoted 7 lines
CodeSamples/datastruct/hash/hash_resize.c +3
-1@@ -30,7 +30,9 @@struct ht_elem {struct rcu_head rh;struct cds_list_head hte_next[2 ];//\lnlbl{ht_elem:next} -unsigned long hte_hash; +#ifndef FCV_SNIPPET +unsigned long hte_hash[2 ]; +#endif /* #ifndef FCV_SNIPPET */
Hi Akira,
I understand that the micro FCV_SNIPPET is used to hide code for checking hash value in this patch set. I just curious about the exact meaning of the term FCV_SNIPPET itself. Specifically, what does the acronym FCV stand for? I encountered it multiple times while checking the code of perfbook, but didn't find any documentation.
Thanks,
--Junchang
Quoted 261 lines
CodeSamples/datastruct/hash/hash_resize.c +29
-23};/* Hash-table bucket element. */ @@ -41,7 +43,9 @@ struct ht_bucket {struct ht_lock_state {//\lnlbl{hls:b} struct ht_bucket *hbp[2 ]; +#ifndef FCV_SNIPPET unsigned long hls_hash[2 ]; +#endif /* #ifndef FCV_SNIPPET */ int hls_idx[2 ]; };//\lnlbl{hls:e} @@ -181,8 +185,10 @@ hashtab_lock_mod(struct hashtab *htp_master, void *key, htbp =ht_get_bucket (htp, key, &b, &h);//\lnlbl{l:refbucket} spin_lock (&htbp->htb_lock);//\lnlbl{l:acq_bucket} lsp->hbp[0 ] = htbp;//\lnlbl{l:lsp0b} - lsp->hls_idx[0 ] = htp->ht_idx; - lsp->hls_hash[0 ] = h;//\lnlbl{l:lsp0e} + lsp->hls_idx[0 ] = htp->ht_idx;//\lnlbl{l:lsp0e} +#ifndef FCV_SNIPPET + lsp->hls_hash[0 ] = h; +#endif /* #ifndef FCV_SNIPPET */ if (b >READ_ONCE (htp->ht_resize_cur)) {//\lnlbl{l:ifresized} lsp->hbp[1 ] =NULL ;//\lnlbl{l:lsp1_1} return ;//\lnlbl{l:fastret1} @@ -190,12 +196,11 @@ hashtab_lock_mod(struct hashtab *htp_master, void *key, htp =rcu_dereference (htp->ht_new);//\lnlbl{l:new_hashtbl} htbp =ht_get_bucket (htp, key, &b, &h);//\lnlbl{l:get_newbkt} spin_lock (&htbp->htb_lock);//\lnlbl{l:acq_newbkt} - lsp->hbp[1 ] = lsp->hbp[0 ];//\lnlbl{l:lsp1b} - lsp->hls_idx[1 ] = lsp->hls_idx[0 ]; - lsp->hls_hash[1 ] = lsp->hls_hash[0 ]; - lsp->hbp[0 ] = htbp; - lsp->hls_idx[0 ] = htp->ht_idx; - lsp->hls_hash[0 ] = h;//\lnlbl{l:lsp1e} + lsp->hbp[1 ] = htbp;//\lnlbl{l:lsp1b} + lsp->hls_idx[1 ] = htp->ht_idx;//\lnlbl{l:lsp1e} +#ifndef FCV_SNIPPET + lsp->hls_hash[1 ] = h; +#endif /* #ifndef FCV_SNIPPET */ }//\lnlbl{l:e} static void //\lnlbl{ul:b}@@ -230,12 +235,7 @@ hashtab_lookup(struct hashtab *htp_master, void *key) htp =rcu_dereference (htp_master->ht_cur);//\lnlbl{lkp:get_curtbl} htep =ht_search_bucket (htp, key);//\lnlbl{lkp:get_curbkt} -if (htep)//\lnlbl{lkp:entchk} -return htep;//\lnlbl{lkp:ret_match} - htp =rcu_dereference (htp->ht_new);//\lnlbl{lkp:get_nxttbl} -if (!htp)//\lnlbl{lkp:htpchk} -return NULL ;//\lnlbl{lkp:noresize} -return ht_search_bucket (htp, key);//\lnlbl{lkp:ret_nxtbkt} +return htep;//\lnlbl{lkp:ret} }//\lnlbl{lkp:e} /* @@ -248,9 +248,16 @@ void hashtab_add(struct ht_elem *htep, //\lnlbl{add:b}struct ht_bucket *htbp = lsp->hbp[0 ];//\lnlbl{add:htbp} int i = lsp->hls_idx[0 ];//\lnlbl{add:i} - htep->hte_hash = lsp->hls_hash[0 ];//\lnlbl{add:hash} - htep->hte_next[!i].prev =NULL ;//\lnlbl{add:initp} +#ifndef FCV_SNIPPET + htep->hte_hash[0 ] = lsp->hls_hash[0 ]; +#endif /* #ifndef FCV_SNIPPET */ cds_list_add_rcu (&htep->hte_next[i], &htbp->htb_head);//\lnlbl{add:add} +if ((htbp = lsp->hbp[1 ])) {//\lnlbl{add:ifnew} +#ifndef FCV_SNIPPET + htep->hte_hash[1 ] = lsp->hls_hash[1 ]; +#endif /* #ifndef FCV_SNIPPET */ +cds_list_add_rcu (&htep->hte_next[!i], &htbp->htb_head);//\lnlbl{add:addnew} + } }//\lnlbl{add:e} /* @@ -262,14 +269,9 @@ void hashtab_del(struct ht_elem *htep, //\lnlbl{del:b} {int i = lsp->hls_idx[0 ];//\lnlbl{del:i} -if (htep->hte_next[i].prev) {//\lnlbl{del:if} -cds_list_del_rcu (&htep->hte_next[i]);//\lnlbl{del:del} - htep->hte_next[i].prev =NULL ;//\lnlbl{del:init} - } -if (lsp->hbp[1 ] && htep->hte_next[!i].prev) {//\lnlbl{del:ifnew} +cds_list_del_rcu (&htep->hte_next[i]);//\lnlbl{del:del} +if (lsp->hbp[1 ])//\lnlbl{del:ifnew} cds_list_del_rcu (&htep->hte_next[!i]);//\lnlbl{del:delnew} - htep->hte_next[!i].prev =NULL ;//\lnlbl{del:initnew} - } }//\lnlbl{del:e} //\end{snippet} @@ -350,5 +352,11 @@ void defer_del_rcu(struct rcu_head *rhp)#define quiescent_state () rcu_quiescent_state() +#ifndef FCV_SNIPPET +#define check_hash () (htep->hte_hash[0] != hash && htep->hte_hash[1] != hash) +#else +#define check_hash () (0) +#endif /* #ifndef FCV_SNIPPET */ +#include "hashtorture.h" #endif /* #ifdef TEST_HASH */ CodeSamples/datastruct/hash/hashtorture.h +5
-1@@ -62,6 +62,10 @@ void (*defer_del_done)(struct ht_elem *htep) = NULL;#define rcu_barrier () do ; while (0)#endif /* #ifndef quiescent_state */ +#ifndef check_hash +#define check_hash () (htep->hte_hash != hash) +#endif /* #ifndef check_hash */ +/* * Test variables. */ @@ -988,7 +992,7 @@ int zoo_lookup(char *key) htep =hashtab_lookup (perftest_htp, hash, key); zhep = container_of(htep, struct zoo_he, zhe_e);BUG_ON (htep && - (htep->hte_hash != hash || + (check_hash () ||strncmp (zhep->name, (char *)key,ZOO_NAMELEN ) !=0 ));hashtab_unlock_lookup (perftest_htp, hash);hashtab_lookup_done (htep);datastruct/datastruct.tex +22
-48@@ -887,7 +887,8 @@ which is the subject of the next section. Resizing is accomplished by the classic approach of inserting a level of indirection, in this case, the \co{ht} structure shown on lines~\lnref{ht:b}-\lnref{ht:e} of -Listing~\ref{lst:datastruct:Resizable Hash-Table Data Structures}. +Listing~\ref{lst:datastruct:Resizable Hash-Table Data Structures} +(\path{hash_resize.c}). The \co{hashtab} structure shown on lines~\lnref{hashtab:b}-\lnref{hashtab:e} contains only a pointer to the current \co{ht} structure along with a spinlock that@@ -1030,8 +1031,8 @@ corresponding to the key. Line~\lnref{acq_bucket} acquires that bucket's lock, which will prevent any concurrent resizing operation from distributing that bucket, though of course it will have no effect if that bucket has already been distributed. -Lines~\lnref{lsp0b}-\lnref{lsp0e} store the bucket pointer, -pointer-set index, and hash value into their respective fields in the +Lines~\lnref{lsp0b}-\lnref{lsp0e} store the bucket pointer and +pointer-set index into their respective fields in the \co{ht_lock_state} structure, which communicates the information to \co{hashtab_add()}, \co{hashtab_del()}, and \co{hashtab_unlock_mod()}. Line~\lnref{ifresized} then checks to see if a concurrent resize@@ -1045,8 +1046,8 @@ Otherwise, a concurrent resize operation has already distributed this bucket, so line~\lnref{new_hashtbl} proceeds to the new hash table, line~\lnref{get_newbkt} selects the bucket corresponding to the key, and line~\lnref{acq_newbkt} acquires the bucket's lock. -Lines~\lnref{lsp1b}-\lnref{lsp1e} store the bucket pointer, -pointer-set index, and hash value into their respective fields in the +Lines~\lnref{lsp1b}-\lnref{lsp1e} store the bucket pointer and +pointer-set index into their respective fields in the \co{ht_lock_state} structure, which communicates the information to \co{hashtab_add()}, \co{hashtab_del()}, and \co{hashtab_unlock_mod()}. Note that in this case, two locks are held, one in the old \co{ht_bucket}@@ -1108,28 +1109,20 @@ hash lookups. Line~\lnref{get_curtbl} fetches the current hash table and line~\lnref{get_curbkt} searches the bucket corresponding to the specified key. -If line~\lnref{entchk} determines that the search was successful, -line~\lnref{ret_match} returns a pointer to the element that was located. -Otherwise, line~\lnref{get_nxttbl} picks up a pointer to the next version, -and if line~\lnref{htpchk} determines that there is no resize in progress, -line~\lnref{noresize} returns \co{NULL}. -When there is a resize in progress, execution reaches -line~\lnref{ret_nxtbkt}, which returns the result of searching the -bucket corresponding to \co{key} in the new version. +Line~\lnref{ret} returns a pointer to the element that was located +or \co{NULL} when the search failed. \end{lineref} \begin{lineref}[ln:datastruct:hash_resize:access:add] The \co{hashtab_add()} function on lines~\lnref{b}-\lnref{e} of the listing adds new data elements to the hash table. -Line~\lnref{new} determines whether a resize was in progress and the -corresponding old bucket has already been distributed (value of one) -or not (value of zero). -Line~\lnref{htbp} picks up the \co{ht_bucket} structure into which the +Line~\lnref{htbp} picks up the current \co{ht_bucket} structure into which the new element is to be added, and line~\lnref{i} picks up the index of the pointer pair. -Line~\lnref{hash} stores the hash value in the to-be-added element -for debug purposes, -Finally, line~\lnref{add} adds the new element to the proper hash bucket. +Line~\lnref{add} adds the new element to the current hash bucket. +Then line~\lnref{ifnew} determines if the bucket has been distributed to +a new hash table, and if so, +line~\lnref{addnew} adds the new element to the bucket in the new hash table. The caller is required to handle concurrency, for example, by invoking \co{hashtab_lock_mod()} before the call to \co{hashtab_add()} and invoking \co{hashtab_unlock_mod()} afterwards.@@ -1140,34 +1133,16 @@ The \co{hashtab_del()} function on lines~\lnref{b}-\lnref{e} of the listing removes an existing element from the hash table. Line~\lnref{i} picks up the index of the pointer pair -and line~\lnref{del} removes the specified element. +and line~\lnref{del} removes the specified element from the current table. +Then line~\lnref{ifnew} determines if the bucket has been distributed to +a new hash table, and if so, +line~\lnref{delnew} removes the sepcified element from the bucket in +the new table. As with \co{hashtab_add()}, the caller is responsible for concurrency control and this concurrency control suffices for synchronizing with a concurrent resize operation. \end{lineref} -\QuickQuiz{} - The \co{hashtab_del()} function in - Listing~\ref{lst:datastruct:Resizable Hash-Table Access Functions} - might not remove the element from the old hash table. - Doesn't this mean that readers might access this newly removed - element after it has been freed? -\QuickQuizAnswer{ - No. - The \co{hashtab_del()} function omits removing the element - from the old hash table only if the resize operation has - already progressed beyond the bucket containing the just-deleted - element. - But this means that new \co{hashtab_lookup()} operations will - use the new hash table when looking up that element. - Therefore, only old \co{hashtab_lookup()} operations that started - before the \co{hashtab_del()} might encounter the newly - removed element. - This means that \co{hashtab_del()} need only wait for an - RCU grace period to avoid inconveniencing - \co{hashtab_lookup()} operations. -} \QuickQuizEnd - \begin{listing*}[tb] \input{CodeSamples/datastruct/hash/[email protected]} \caption{Resizable Hash-Table Resizing}@@ -1211,10 +1186,10 @@ and line~\lnref{acq_oldcur} acquires that bucket's spinlock. Listing~\ref{lst:datastruct:Resizable Hash-Table Resizing}, what guarantees that the update to \co{->ht_new} on line~\lnref{set_newtbl} will be seen as happening before the update to \co{->ht_resize_cur} - on line~\lnref{update_resize} from the perspective of \co{hashtab_lookup()}, - \co{hashtab_add()}, and \co{hashtab_del()}? - In other words, what prevents \co{hashtab_lookup()}, - \co{hashtab_add()}, and \co{hashtab_del()}, from dereferencing + on line~\lnref{update_resize} from the perspective of + \co{hashtab_add()} and \co{hashtab_del()}? + In other words, what prevents \co{hashtab_add()} + and \co{hashtab_del()} from dereferencing a NULL pointer loaded from \co{->ht_new}? \end{lineref} \QuickQuizAnswer{ --2.7.4
Re: [PATCH 1/6] datastruct/hash: Simplify hash_resize.c
Hi Junchang,
Quoted 44 lines · Junchang Wang
FCV_SNIPPET" blocks are used to hide
code for debuggingQuoted 30 lines · Akira Yokosawa
From
1ffe7edfb90cbb8efb2a33d2ae8c3e855e684acfMon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Sun, 13 Jan 2019 19:47:23 +0900
Subject: [PATCH 1/6] datastruct/hash: Simplifyhash_resize.cBy keeping updating the current (old) hash table until resizing completes,
hashtab_lookup()only needs to see the current hash table. Instead,hashtab_add()andhashtab_del()need to update the bucket in the current hash table as well as that in the new hash table ifhashtab_lock_mod()has locked the new bucket.This simplifies the lookup side and no performance degradation is expected as long as no resizing is in progress.
Note that during resize operation, the cost of updates can be doubled in the worst case.
Signed-off-by Akira Yokosawa <[email protected]>--- CodeSamples/datastruct/hash/hash_resize.c | 56 ++++++++++++++----------- CodeSamples/datastruct/hash/hashtorture.h | 6 ++- datastruct/datastruct.tex | 69 ++++++++++--------------------- 3 files changed, 59 insertions(+), 72 deletions(-)CodeSamples/datastruct/hash/hash_resize.c +0
-0CodeSamples/datastruct/hash/hash_resize.c line 35
Quoted 7 lines
CodeSamples/datastruct/hash/hash_resize.c +3
-1@@ -30,7 +30,9 @@struct ht_elem {struct rcu_head rh;struct cds_list_head hte_next[2 ];//\lnlbl{ht_elem:next} -unsigned long hte_hash; +#ifndef FCV_SNIPPET +unsigned long hte_hash[2 ]; +#endif /* #ifndef FCV_SNIPPET */ Hi Akira,
Quoted 5 lines
I understand that the micro
FCV_SNIPPETis used to hide code for checking hash value in this patch set. I just curious about the exact meaning of the termFCV_SNIPPETitself. Specifically, what does the acronym FCV stand for? I encountered it multiple times while checking the code of perfbook, but didn't find any documentation.
Brief documentation is given in the header of utilities/fcvextract.pl. "FCV" came from a LaTeX package name "fancyvrb".
The macro FCV_SNIPPET is not supposed to be actually defined. In this particular case, defining it can eliminate the code to check hash values.
Does this short answer make sense to you?
To add explanation of FCV_SNIPPET and other new features of fcvextract.pl in the style guide is on my to-do list.
Thanks, Akira
Quoted 5 lines
Thanks,
--Junchang
[...]
Re: [PATCH 1/6] datastruct/hash: Simplify hash_resize.c
Quoted 54 lines · Akira Yokosawa
Hi Junchang,
Quoted 44 lines · Junchang Wang
FCV_SNIPPET" blocks are used to hide
code for debuggingQuoted 30 lines · Akira Yokosawa
From
1ffe7edfb90cbb8efb2a33d2ae8c3e855e684acfMon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Sun, 13 Jan 2019 19:47:23 +0900
Subject: [PATCH 1/6] datastruct/hash: Simplifyhash_resize.cBy keeping updating the current (old) hash table until resizing completes,
hashtab_lookup()only needs to see the current hash table. Instead,hashtab_add()andhashtab_del()need to update the bucket in the current hash table as well as that in the new hash table ifhashtab_lock_mod()has locked the new bucket.This simplifies the lookup side and no performance degradation is expected as long as no resizing is in progress.
Note that during resize operation, the cost of updates can be doubled in the worst case.
Signed-off-by Akira Yokosawa <[email protected]>--- CodeSamples/datastruct/hash/hash_resize.c | 56 ++++++++++++++----------- CodeSamples/datastruct/hash/hashtorture.h | 6 ++- datastruct/datastruct.tex | 69 ++++++++++--------------------- 3 files changed, 59 insertions(+), 72 deletions(-)CodeSamples/datastruct/hash/hash_resize.c +0
-0CodeSamples/datastruct/hash/hash_resize.c line 35
Quoted 7 lines
CodeSamples/datastruct/hash/hash_resize.c +3
-1@@ -30,7 +30,9 @@struct ht_elem {struct rcu_head rh;struct cds_list_head hte_next[2 ];//\lnlbl{ht_elem:next} -unsigned long hte_hash; +#ifndef FCV_SNIPPET +unsigned long hte_hash[2 ]; +#endif /* #ifndef FCV_SNIPPET */ Hi Akira,
Quoted 5 lines
I understand that the micro
FCV_SNIPPETis used to hide code for checking hash value in this patch set. I just curious about the exact meaning of the termFCV_SNIPPETitself. Specifically, what does the acronym FCV stand for? I encountered it multiple times while checking the code of perfbook, but didn't find any documentation.
Quoted 8 lines
Brief documentation is given in the header of
utilities/fcvextract.pl. "FCV" came from a LaTeX package name "fancyvrb".
The macroFCV_SNIPPETis not supposed to be actually defined. In this particular case, defining it can eliminate the code to check hash values.Does this short answer make sense to you?
Hi Akira,
Yes, it's pretty clear; now I know where the term FCV comes from. :-) Thanks a lot.
--Junchang
Quoted 11 lines
To add explanation of
FCV_SNIPPETand other new features of fcvextract.pl in the style guide is on my to-do list.Thanks, Akira
Quoted 5 lines
Thanks,
--Junchang[...]
PATCH 2/6 datastruct/hash: Add a couple of Quick Quizzes regarding hash_resize.c
In reply to Akira YokosawaFrom f937854c677547147ac16b492c57c1961ff372c7 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Sun, 13 Jan 2019 19:56:22 +0900
Subject: [PATCH 2/6] datastruct/hash: Add a couple of Quick Quizzes regarding hash_resize.c
Also copy hash_resize.c as of commit 180bd693751b ("datastruct/hash:
Always place to-be-added-to bucket in [0]") to hash_size_s.c and include listings of its access functions and the update-side locking function in an answer to one of the added quick quizzes.
--- CodeSamples/datastruct/hash/Makefile | 5 +- CodeSamples/datastruct/hash/hash_resize.c | 3 +- CodeSamples/datastruct/hash/hash_resize_s.c | 365 ++++++++++++++++++++++++++++ datastruct/datastruct.tex | 60 +++++ 4 files changed, 431 insertions(+), 2 deletions(-) create mode 100644 CodeSamples/datastruct/hash/hash_resize_s.c
CodeSamples/datastruct/hash/Makefile +4 -1
@@ -16,7 +16,7 @@ include ../../Makefile.arch -PROGS = hash_bkt hash_bkt_hazptr hash_bkt_rcu hash_global hash_resize +PROGS = hash_bkt hash_bkt_hazptr hash_bkt_rcu hash_global hash_resize hash_resize_s top := ../.. include $(top)/depends.mk
@@ -49,5 +49,8 @@ hash_global: hash_global.c ../../api.h hashtorture.h hash_resize: hash_resize.c ../../api.h hashtorture.h cc $(GCC_ARGS) -DTEST_HASH -o hash_resize hash_resize.c -lpthread -lurcu -lurcu-signal +hash_resize_s: hash_resize_s.c ../../api.h hashtorture.h + cc $(GCC_ARGS) -DTEST_HASH -o hash_resize_s hash_resize_s.c -lpthread -lurcu -lurcu-signal + clean: rm -f $(PROGS)
CodeSamples/datastruct/hash/hash_resize.c +2 -1
@@ -16,7 +16,8 @@* along with this program; if not, you can access it online at * http://www.gnu.org/licenses/gpl-2.0.html. * -* Copyright (c) 2013 Paul E. McKenney, IBM Corporation. +* Copyright (c) 2013-2019 Paul E. McKenney, IBM Corporation. +* Copyright (c) 2019 Akira Yokosawa */ #define _GNU_SOURCE
CodeSamples/datastruct/hash/hash_resize_s.c new file +365 -0
365 lines not on this page · raw message
datastruct/datastruct.tex +60 -0
72 lines not on this page · raw message
Signature · 1 line
2.7.4
PATCH 3/6 datastruct/hash: Fix unnecessary folding in code snippet
In reply to Akira YokosawaFrom c6c7f21935282a955a421ca63787b6e8fc70a0dd Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Sun, 13 Jan 2019 19:59:13 +0900
Subject: [PATCH 3/6] datastruct/hash: Fix unnecessary folding in code snippet
--- CodeSamples/datastruct/hash/hash_resize.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
CodeSamples/datastruct/hash/hash_resize.c +1 -2
@@ -56,8 +56,7 @@ struct ht { //\lnlbl{ht:b}long ht_resize_cur;//\lnlbl{ht:resize_cur} struct ht *ht_new;//\lnlbl{ht:new} int ht_idx;//\lnlbl{ht:idx} -int (*ht_cmp)(struct ht_elem *htep,//\lnlbl{ht:cmp} -void *key); +int (*ht_cmp)(struct ht_elem *htep,void *key);//\lnlbl{ht:cmp} unsigned long (*ht_gethash)(void *key);void *(*ht_getkey)(struct ht_elem *htep);//\lnlbl{ht:getkey} struct ht_bucket ht_bkt[0 ];//\lnlbl{ht:bkt}
Signature · 1 line
2.7.4
PATCH 4/6 datastruct/hash: Adjust context to updated code in hash_resize.c
In reply to Akira YokosawaFrom fd50eec2691a61abd52b038c750e173ab1160b02 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Sun, 13 Jan 2019 20:00:20 +0900
Subject: [PATCH 4/6] datastruct/hash: Adjust context to updated code in hash_resize.c
--- datastruct/datastruct.tex | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
datastruct/datastruct.tex +4 -5
@@ -952,7 +952,7 @@ In contrast, when resizing, it is also necessary to determine which of the old and new sets of buckets to select from. If the bucket that would be selected from the old table has already been distributed into the new table, then the bucket should be selected -from the new table. +from the new table as well as from the old table. Conversely, if the bucket that would be selected from the old table has not yet been distributed, then the bucket should be selected from the old table.
@@ -1073,15 +1073,14 @@ section. \QuickQuiz{} Suppose that one thread is inserting an element into the - new hash table during a resize operation. + hash table during a resize operation. What prevents this insertion from being lost due to a subsequent resize operation completing before the insertion does? \QuickQuizAnswer{ The second resize operation will not be able to move beyond the bucket into which the insertion is taking place due to - the insertion holding the lock on one of the hash buckets in - the new hash table (the second hash table of three in this - example). + the insertion holding the lock(s) on one or both of the hash + buckets in the hash tables. Furthermore, the insertion operation takes place within an RCU read-side critical section. As we will see when we examine the \co{hashtab_resize()}
Signature · 1 line
2.7.4
PATCH 5/6 datastruct/hash: Add Quick Quiz on READ_ONCE/WRITE_ONCE in hash_resize.c
In reply to Akira YokosawaFrom f56134cbf4de7748ed7f361f7630b1e7a22c2822 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Sun, 13 Jan 2019 20:04:01 +0900
Subject: [PATCH 5/6] datastruct/hash: Add Quick Quiz on READ_ONCE/WRITE_ONCE in hash_resize.c
The answer is borrowed from the change log of Paul's commit 1aac0c703482 ("datastruct/hash: Remove extraneous barrier from hashtab_resize()")
--- datastruct/datastruct.tex | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-)
datastruct/datastruct.tex +25 -7
@@ -1115,15 +1115,15 @@ or \co{NULL} when the search failed. \QuickQuiz{} The \co{hashtab_lookup()} function in Listing~\ref{lst:datastruct:Resizable Hash-Table Access Functions} - searches only the current hash bucket. - Doesn't this mean that readers might miss a newly added - element? + does not take account of concurrent resize operations. + Doesn't this mean that readers might miss an element + added while resizing is progressing? \QuickQuizAnswer{ No. - As will be described soon, - The \co{hashtab_add()} and \co{hashtab_del()} functions - keep updating the current hash table while resizing is - progressing. + As we will see soon, + the \co{hashtab_add()} and \co{hashtab_del()} functions + keep the current hash table up-to-date until a resize operation + completes. } \QuickQuizEnd \begin{lineref}[ln:datastruct:hash_resize:access:add]
@@ -1292,6 +1292,24 @@ line~\lnref{free} frees the old hash table, and finally line~\lnref{ret_success} returns success. \end{lineref} +\QuickQuiz{} + \begin{lineref}[ln:datastruct:hash_resize:resize] + Why is there a \co{WRITE_ONCE()} on line~\lnref{update_resize} + in Listing~\ref{lst:datastruct:Resizable Hash-Table Resizing}? + \end{lineref} +\QuickQuizAnswer{ + \begin{lineref}[ln:datastruct:hash_resize:lock_unlock_mod] + Together with the \co{READ_ONCE()} + on line~\lnref{l:ifresized} in \co{hashtab_lock_mod()} + of Listing~\ref{lst:datastruct:Resizable Hash-Table Update-Side + Concurrency Control}, + it tells the compiler that the non-initialization accesses + to \co{->ht_resize_cur} must remain because reads + from \co{->ht_resize_cur} really can race with writes, + just not in a way to change the ``if'' conditions. + \end{lineref} +} \QuickQuizEnd + \subsection{Resizable Hash Table Discussion} \label{sec:datastruct:Resizable Hash Table Discussion}
Signature · 1 line
2.7.4
PATCH 6/6 datastruct/hash: Display error msg if rcu_barrier() is not available
In reply to Akira YokosawaFrom 7b69a9b37ba9a73a50aad5cbb097235ddfe75870 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Mon, 14 Jan 2019 00:12:41 +0900
Subject: [PATCH 6/6] datastruct/hash: Display error msg if rcu_barrier() is not available
--- CodeSamples/datastruct/hash/hashtorture.h | 5 +++++ 1 file changed, 5 insertions(+)
CodeSamples/datastruct/hash/hashtorture.h +5 -0
@@ -60,6 +60,11 @@ void (*defer_del_done)(struct ht_elem *htep) = NULL;#define quiescent_state () do ; while (0)#define synchronize_rcu () do ; while (0)#define rcu_barrier () do ; while (0) +#else /* #ifndef quiescent_state */ +# ifndef rcu_barrier +# error You need a modern version of liburcu which has "rcu_barrier()". +# define rcu_barrier () do ; while (0) +# endif /* #ifndef rcu_barrier */ #endif /* #ifndef quiescent_state */ #ifndef check_hash
Signature · 1 line
2.7.4
Quoted 24 lines · Akira Yokosawa
Quoted 1 line
From
7b69a9b37ba9a73a50aad5cbb097235ddfe75870Mon Sep 17 00:00:00 2001From: Akira Yokosawa <[email protected]>
Date: Mon, 14 Jan 2019 07:25:14 +0900
Subject: [PATCH 0/6] Simplifyhash_resize.cHi Paul,
This patch set updates
hash_resize.c, which you suggested for me to take over, and the related text.I added a couple of Quick Quizzes as well, and in one of them, I included your version of
hash_resize.c.Patch #1 updates
hash_resize.cin the way I suggested. Note that in this update, "#ifndefFCV_SNIPPET" blocks are used to hide code for debugging (hash value checks) in code snippets. This code can actually be compiled with "-DFCV_SNIPPET" to see the performance without hash value checks.Patch #2 adds a couple of Quick Quizzes. Your version of
hash_resize.cis added ashash_resize_s.c.Patch #3 removes unnecessary folding in a code snippet.
Quoted 7 lines
Patch #4 adjusts a few sentence to the simpler approach.
Patch #5 adds another Quick Quiz.
Patch #6 adds an "#error" directive for the lack of
rcu_barrier().All of the updates in text would need your native eyes to be polished.
Nice! Queued and pushed, thank you! I will review and send any needed update by end of tomorrow, Pacific Time.
Just FYI, this also pushed out a couple of commits starting my rework of Section 9.5.1.
Quoted 6 lines
Thanks, Akira
PS:
I couldn't make out your concern of "if we ever want to iterate over the hash table". Can you elaborate it?
Consider a hash_iterate() function that takes a pointer to a function. Then hash_iterate() would invoke this function on each element currently in the hash table, for a relatively relaxed definition of "currently". With your hash table, a naive implementation might iterate over some data items twice due to their being in both the old and the new tables.
But this is not hard to fix, for example, by creating some sort of list of elements and the eliminating duplicates, then invoking the specified function on each of them. Of course, this entire process would need to be carried out within an RCU read-side critical section.
This assumes that iteration over the entire hash table is rare, which I would certainly expect it to be, given how expensive it is.
Plus, we currently don't support iteration, so it isn't currently a problem anyway. ;-)
Thanx, Paul
Quoted 21 lines
--
Akira Yokosawa (6):
datastruct/hash: Simplifyhash_resize.c
datastruct/hash: Add a couple of Quick Quizzes regardinghash_resize.cdatastruct/hash: Fix unnecessary folding in code snippet datastruct/hash: Adjust context to updated code inhash_resize.cdatastruct/hash: Add Quick Quiz on READ_ONCE/WRITE_ONCE in
hash_resize.c
datastruct/hash: Display error msg ifrcu_barrier()is not availableCodeSamples/datastruct/hash/Makefile | 5 +- CodeSamples/datastruct/hash/hash_resize.c | 62 +++-- CodeSamples/datastruct/hash/hash_resize_s.c | 365 ++++++++++++++++++++++++++++ CodeSamples/datastruct/hash/hashtorture.h | 11 +- datastruct/datastruct.tex | 146 +++++++---- 5 files changed, 513 insertions(+), 76 deletions(-) create mode 100644 CodeSamples/datastruct/hash/hash_resize_s.cSignature · 1 line
2.7.4
Quoted 27 lines · Paul E. McKenney
Quoted 26 lines · Akira Yokosawa
Quoted 1 line
From
7b69a9b37ba9a73a50aad5cbb097235ddfe75870Mon Sep 17 00:00:00 2001From: Akira Yokosawa <[email protected]>
Date: Mon, 14 Jan 2019 07:25:14 +0900
Subject: [PATCH 0/6] Simplifyhash_resize.cHi Paul,
This patch set updates
hash_resize.c, which you suggested for me to take over, and the related text.I added a couple of Quick Quizzes as well, and in one of them, I included your version of
hash_resize.c.Patch #1 updates
hash_resize.cin the way I suggested. Note that in this update, "#ifndefFCV_SNIPPET" blocks are used to hide code for debugging (hash value checks) in code snippets. This code can actually be compiled with "-DFCV_SNIPPET" to see the performance without hash value checks.Patch #2 adds a couple of Quick Quizzes. Your version of
hash_resize.cis added ashash_resize_s.c.Patch #3 removes unnecessary folding in a code snippet.
Patch #4 adjusts a few sentence to the simpler approach.
Quoted 8 lines
Quoted 5 lines
Patch #5 adds another Quick Quiz.
Patch #6 adds an "#error" directive for the lack of
rcu_barrier().All of the updates in text would need your native eyes to be polished.
Nice! Queued and pushed, thank you! I will review and send any needed update by end of tomorrow, Pacific Time.
Here is a summary of changes:
o Move the ht_lock_state structure definition to follow the ht
structure, matching the order of discussion in the text. Everything seems to build and run OK with this change. Also illustrates the advantages of line labels! ;-)
o I considered inlining ht_search_bucket() into its only caller
in hashtab_lookup(), but decided against it. More flexibility for change with it as is.
o I added ht_search_bucket() to the in-text list of things
permitting lookups and modifications to run concurrently with a resize operation.
o I tweaked the description of hashtab_lock_mod(), hashtab_unlock_mod(),
hashtab_add(), and hashtab_del().
o I tweaked the QQ asking about searches missing adds during resizes.
o English is strange, so "Less Changes" must become "Fewer Changes".
o I put the long labels on a single line out of paranoia.
I pushed these out on a working branch named paulmck.2019.01.14a. Please let me know whether I messed anything up, and once it looks good to you, I will pull these two commits into the master branch.
Thanx, Paul
Quoted 52 lines · Paul E. McKenney
Quoted 27 lines · Paul E. McKenney
Quoted 26 lines · Akira Yokosawa
Quoted 1 line
From
7b69a9b37ba9a73a50aad5cbb097235ddfe75870Mon Sep 17 00:00:00 2001From: Akira Yokosawa <[email protected]>
Date: Mon, 14 Jan 2019 07:25:14 +0900
Subject: [PATCH 0/6] Simplifyhash_resize.cHi Paul,
This patch set updates
hash_resize.c, which you suggested for me to take over, and the related text.I added a couple of Quick Quizzes as well, and in one of them, I included your version of
hash_resize.c.Patch #1 updates
hash_resize.cin the way I suggested. Note that in this update, "#ifndefFCV_SNIPPET" blocks are used to hide code for debugging (hash value checks) in code snippets. This code can actually be compiled with "-DFCV_SNIPPET" to see the performance without hash value checks.Patch #2 adds a couple of Quick Quizzes. Your version of
hash_resize.cis added ashash_resize_s.c.Patch #3 removes unnecessary folding in a code snippet.
Patch #4 adjusts a few sentence to the simpler approach.
Quoted 8 lines
Quoted 5 lines
Patch #5 adds another Quick Quiz.
Patch #6 adds an "#error" directive for the lack of
rcu_barrier().All of the updates in text would need your native eyes to be polished.
Nice! Queued and pushed, thank you! I will review and send any needed update by end of tomorrow, Pacific Time.
Here is a summary of changes:
o Move the
ht_lock_statestructure definition to follow the ht
structure, matching the order of discussion in the text. Everything seems to build and run OK with this change. Also illustrates the advantages of line labels! ;-)o I considered inlining
ht_search_bucket()into its only caller
inhashtab_lookup(), but decided against it. More flexibility for change with it as is.o I added
ht_search_bucket()to the in-text list of things
permitting lookups and modifications to run concurrently with a resize operation.
Quoted 6 lines
o I tweaked the description of
hashtab_lock_mod(),hashtab_unlock_mod(),
hashtab_add(), andhashtab_del().o I tweaked the QQ asking about searches missing adds during resizes.
o English is strange, so "Less Changes" must become "Fewer Changes".
Sigh, I thought I knew this rule... Thanks for the fix.
Quoted 6 lines
o I put the long labels on a single line out of paranoia.
I pushed these out on a working branch named paulmck.2019.01.14a. Please let me know whether I messed anything up, and once it looks good to you, I will pull these two commits into the master branch.
There is an irrelevant clause added in an answer to a QQ. See below for my suggestion. It doesn't sound fluent enough, though.
Thanks, Akira
Quoted 3 lines
Thanx, Paul
----8<----
From f2fe1c0f2f1bd65b78588c25b24c0eb8359d79d6 Mon Sep 17 00:00:00 2001
From: Akira Yokosawa <[email protected]>
Date: Tue, 15 Jan 2019 23:52:27 +0900
Subject: [PATCH] datastruct/hash: Fix irrelevant clause in answer to quick quiz
hashtab_lookup() is not affected by the reordering of ->hbp[] and ->hls_idx[]. Instead, mention the increase of cost at the earlier explanation of hashtab_lookup().
--- datastruct/datastruct.tex | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
datastruct/datastruct.tex +4 -4
@@ -1189,13 +1189,13 @@ a concurrent resize operation. either the old bucket if it is not resized yet, or to the new bucket if it has been resized, and \co{hashtab_del()} removes the specified element from any buckets into which it has been inserted. - \co{hashtab_lookup()} searches the new bucket if the search - of the old bucket fails. + The \co{hashtab_lookup()} function searches the new bucket + if the search of the old bucket fails + at the expense of extra checks. The alternative \co{hashtab_lock_mod()} returns the locking state of the new bucket in \co{->hbp[0]} and \co{->hls_idx[0]} if resize operation is in progress. - This reordering simplifies \co{hashtab_add()}, but at the expense - of an extra check in \co{hashtab_lookup()}. + This reordering simplifies \co{hashtab_add()}. Further analysis of the code is left as an exercise for the reader. } \QuickQuizEnd
Signature · 1 line
2.7.4
Quoted 63 lines · Akira Yokosawa
Quoted 52 lines · Paul E. McKenney
Quoted 27 lines · Paul E. McKenney
Quoted 26 lines · Akira Yokosawa
Quoted 1 line
From
7b69a9b37ba9a73a50aad5cbb097235ddfe75870Mon Sep 17 00:00:00 2001From: Akira Yokosawa <[email protected]>
Date: Mon, 14 Jan 2019 07:25:14 +0900
Subject: [PATCH 0/6] Simplifyhash_resize.cHi Paul,
This patch set updates
hash_resize.c, which you suggested for me to take over, and the related text.I added a couple of Quick Quizzes as well, and in one of them, I included your version of
hash_resize.c.Patch #1 updates
hash_resize.cin the way I suggested. Note that in this update, "#ifndefFCV_SNIPPET" blocks are used to hide code for debugging (hash value checks) in code snippets. This code can actually be compiled with "-DFCV_SNIPPET" to see the performance without hash value checks.Patch #2 adds a couple of Quick Quizzes. Your version of
hash_resize.cis added ashash_resize_s.c.Patch #3 removes unnecessary folding in a code snippet.
Patch #4 adjusts a few sentence to the simpler approach.
Quoted 8 lines
Quoted 5 lines
Patch #5 adds another Quick Quiz.
Patch #6 adds an "#error" directive for the lack of
rcu_barrier().All of the updates in text would need your native eyes to be polished.
Nice! Queued and pushed, thank you! I will review and send any needed update by end of tomorrow, Pacific Time.
Here is a summary of changes:
o Move the
ht_lock_statestructure definition to follow the ht
structure, matching the order of discussion in the text. Everything seems to build and run OK with this change. Also illustrates the advantages of line labels! ;-)o I considered inlining
ht_search_bucket()into its only caller
inhashtab_lookup(), but decided against it. More flexibility for change with it as is.o I added
ht_search_bucket()to the in-text list of things
permitting lookups and modifications to run concurrently with a resize operation.Quoted 6 lines
o I tweaked the description of
hashtab_lock_mod(),hashtab_unlock_mod(),
hashtab_add(), andhashtab_del().o I tweaked the QQ asking about searches missing adds during resizes.
o English is strange, so "Less Changes" must become "Fewer Changes".
Sigh, I thought I knew this rule... Thanks for the fix.
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
o I put the long labels on a single line out of paranoia.
I pushed these out on a working branch named paulmck.2019.01.14a. Please let me know whether I messed anything up, and once it looks good to you, I will pull these two commits into the master branch.
There is an irrelevant clause added in an answer to a QQ. See below for my suggestion. It doesn't sound fluent enough, though.
Good catch!
How does the following amended patch look?
Thanx, Paul
------------------------------------------------------------------------
commit 731c855ab11b9601a40324c6ef6b5ced6d7833e9
Author: Akira Yokosawa <[email protected]> Date: Tue Jan 15 23:52:27 2019 +0900
datastruct/hash: Fix irrelevant clause in answer to quick quiz
hashtab_lookup() is not affected by the reordering of ->hbp[] and ->hls_idx[]. Instead, mention the increase of cost at the earlier explanation of hashtab_lookup().
datastruct/datastruct.tex +7 -5
@@ -1189,13 +1189,15 @@ a concurrent resize operation. either the old bucket if it is not resized yet, or to the new bucket if it has been resized, and \co{hashtab_del()} removes the specified element from any buckets into which it has been inserted. - \co{hashtab_lookup()} searches the new bucket if the search - of the old bucket fails. + The \co{hashtab_lookup()} function searches the new bucket + if the search of the old bucket fails, which has the disadvantage + of adding overhead to the lookup fastpath. The alternative \co{hashtab_lock_mod()} returns the locking state of the new bucket in \co{->hbp[0]} and \co{->hls_idx[0]} - if resize operation is in progress. - This reordering simplifies \co{hashtab_add()}, but at the expense - of an extra check in \co{hashtab_lookup()}. + if resize operation is in progress, instead of the perhaps + more natural choice of \co{->hbp[1]} and \co{->hls_idx[1]}. + However, this less-natural choice has the advantage of simplifying + \co{hashtab_add()}. Further analysis of the code is left as an exercise for the reader. } \QuickQuizEnd
Quoted 70 lines · Paul E. McKenney
Quoted 69 lines · Akira Yokosawa
Quoted 52 lines · Paul E. McKenney
Quoted 27 lines · Paul E. McKenney
Quoted 26 lines · Akira Yokosawa
Quoted 1 line
From
7b69a9b37ba9a73a50aad5cbb097235ddfe75870Mon Sep 17 00:00:00 2001From: Akira Yokosawa <[email protected]>
Date: Mon, 14 Jan 2019 07:25:14 +0900
Subject: [PATCH 0/6] Simplifyhash_resize.cHi Paul,
This patch set updates
hash_resize.c, which you suggested for me to take over, and the related text.I added a couple of Quick Quizzes as well, and in one of them, I included your version of
hash_resize.c.Patch #1 updates
hash_resize.cin the way I suggested. Note that in this update, "#ifndefFCV_SNIPPET" blocks are used to hide code for debugging (hash value checks) in code snippets. This code can actually be compiled with "-DFCV_SNIPPET" to see the performance without hash value checks.Patch #2 adds a couple of Quick Quizzes. Your version of
hash_resize.cis added ashash_resize_s.c.Patch #3 removes unnecessary folding in a code snippet.
Patch #4 adjusts a few sentence to the simpler approach.
Quoted 8 lines
Quoted 5 lines
Patch #5 adds another Quick Quiz.
Patch #6 adds an "#error" directive for the lack of
rcu_barrier().All of the updates in text would need your native eyes to be polished.
Nice! Queued and pushed, thank you! I will review and send any needed update by end of tomorrow, Pacific Time.
Here is a summary of changes:
o Move the
ht_lock_statestructure definition to follow the ht
structure, matching the order of discussion in the text. Everything seems to build and run OK with this change. Also illustrates the advantages of line labels! ;-)o I considered inlining
ht_search_bucket()into its only caller
inhashtab_lookup(), but decided against it. More flexibility for change with it as is.o I added
ht_search_bucket()to the in-text list of things
permitting lookups and modifications to run concurrently with a resize operation.Quoted 6 lines
o I tweaked the description of
hashtab_lock_mod(),hashtab_unlock_mod(),
hashtab_add(), andhashtab_del().o I tweaked the QQ asking about searches missing adds during resizes.
o English is strange, so "Less Changes" must become "Fewer Changes".
Sigh, I thought I knew this rule... Thanks for the fix.
Quoted 6 lines
o I put the long labels on a single line out of paranoia.
I pushed these out on a working branch named paulmck.2019.01.14a. Please let me know whether I messed anything up, and once it looks good to you, I will pull these two commits into the master branch.
Quoted 6 lines
Quoted 2 lines
There is an irrelevant clause added in an answer to a QQ. See below for my suggestion. It doesn't sound fluent enough, though.
Good catch!
How does the following amended patch look?
It looks perfect!
Thanks, Akira
Quoted 45 lines
Thanx, Paul
------------------------------------------------------------------------
commit
731c855ab11b9601a40324c6ef6b5ced6d7833e9Author: Akira Yokosawa <[email protected]> Date: Tue Jan 15 23:52:27 2019 +0900datastruct/hash: Fix irrelevant clause in answer to quick quiz
hashtab_lookup()is not affected by the reordering of ->hbp[] and ->hls_idx[]. Instead, mention the increase of cost at the earlier explanation ofhashtab_lookup().Signed-off-by Akira Yokosawa <[email protected]>[ paulmck: Wordsmithing. ]Signed-off-by Paul E. McKenney <[email protected]>datastruct/datastruct.tex +7
-5@@ -1189,13 +1189,15 @@ a concurrent resize operation. either the old bucket if it is not resized yet, or to the new bucket if it has been resized, and \co{hashtab_del()} removes the specified element from any buckets into which it has been inserted. - \co{hashtab_lookup()} searches the new bucket if the search - of the old bucket fails. + The \co{hashtab_lookup()} function searches the new bucket + if the search of the old bucket fails, which has the disadvantage + of adding overhead to the lookup fastpath. The alternative \co{hashtab_lock_mod()} returns the locking state of the new bucket in \co{->hbp[0]} and \co{->hls_idx[0]} - if resize operation is in progress. - This reordering simplifies \co{hashtab_add()}, but at the expense - of an extra check in \co{hashtab_lookup()}. + if resize operation is in progress, instead of the perhaps + more natural choice of \co{->hbp[1]} and \co{->hls_idx[1]}. + However, this less-natural choice has the advantage of simplifying + \co{hashtab_add()}. Further analysis of the code is left as an exercise for the reader. } \QuickQuizEnd
Quoted 71 lines · Akira Yokosawa
Quoted 70 lines · Paul E. McKenney
Quoted 69 lines · Akira Yokosawa
Quoted 52 lines · Paul E. McKenney
Quoted 27 lines · Paul E. McKenney
Quoted 26 lines · Akira Yokosawa
Quoted 1 line
From
7b69a9b37ba9a73a50aad5cbb097235ddfe75870Mon Sep 17 00:00:00 2001From: Akira Yokosawa <[email protected]>
Date: Mon, 14 Jan 2019 07:25:14 +0900
Subject: [PATCH 0/6] Simplifyhash_resize.cHi Paul,
This patch set updates
hash_resize.c, which you suggested for me to take over, and the related text.I added a couple of Quick Quizzes as well, and in one of them, I included your version of
hash_resize.c.Patch #1 updates
hash_resize.cin the way I suggested. Note that in this update, "#ifndefFCV_SNIPPET" blocks are used to hide code for debugging (hash value checks) in code snippets. This code can actually be compiled with "-DFCV_SNIPPET" to see the performance without hash value checks.Patch #2 adds a couple of Quick Quizzes. Your version of
hash_resize.cis added ashash_resize_s.c.Patch #3 removes unnecessary folding in a code snippet.
Patch #4 adjusts a few sentence to the simpler approach.
Quoted 8 lines
Quoted 5 lines
Patch #5 adds another Quick Quiz.
Patch #6 adds an "#error" directive for the lack of
rcu_barrier().All of the updates in text would need your native eyes to be polished.
Nice! Queued and pushed, thank you! I will review and send any needed update by end of tomorrow, Pacific Time.
Here is a summary of changes:
o Move the
ht_lock_statestructure definition to follow the ht
structure, matching the order of discussion in the text. Everything seems to build and run OK with this change. Also illustrates the advantages of line labels! ;-)o I considered inlining
ht_search_bucket()into its only caller
inhashtab_lookup(), but decided against it. More flexibility for change with it as is.o I added
ht_search_bucket()to the in-text list of things
permitting lookups and modifications to run concurrently with a resize operation.Quoted 6 lines
o I tweaked the description of
hashtab_lock_mod(),hashtab_unlock_mod(),
hashtab_add(), andhashtab_del().o I tweaked the QQ asking about searches missing adds during resizes.
o English is strange, so "Less Changes" must become "Fewer Changes".
Sigh, I thought I knew this rule... Thanks for the fix.
Quoted 6 lines
o I put the long labels on a single line out of paranoia.
I pushed these out on a working branch named paulmck.2019.01.14a. Please let me know whether I messed anything up, and once it looks good to you, I will pull these two commits into the master branch.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
There is an irrelevant clause added in an answer to a QQ. See below for my suggestion. It doesn't sound fluent enough, though.
Good catch!
How does the following amended patch look?
It looks perfect!
Very good, thank you! I have merged it and pushed the result.
Thanx, Paul
Quoted 48 lines
Thanks, Akira
Quoted 45 lines
Thanx, Paul
------------------------------------------------------------------------
commit
731c855ab11b9601a40324c6ef6b5ced6d7833e9Author: Akira Yokosawa <[email protected]> Date: Tue Jan 15 23:52:27 2019 +0900datastruct/hash: Fix irrelevant clause in answer to quick quiz
hashtab_lookup()is not affected by the reordering of ->hbp[] and ->hls_idx[]. Instead, mention the increase of cost at the earlier explanation ofhashtab_lookup().Signed-off-by Akira Yokosawa <[email protected]>[ paulmck: Wordsmithing. ]Signed-off-by Paul E. McKenney <[email protected]>datastruct/datastruct.tex +7
-5@@ -1189,13 +1189,15 @@ a concurrent resize operation. either the old bucket if it is not resized yet, or to the new bucket if it has been resized, and \co{hashtab_del()} removes the specified element from any buckets into which it has been inserted. - \co{hashtab_lookup()} searches the new bucket if the search - of the old bucket fails. + The \co{hashtab_lookup()} function searches the new bucket + if the search of the old bucket fails, which has the disadvantage + of adding overhead to the lookup fastpath. The alternative \co{hashtab_lock_mod()} returns the locking state of the new bucket in \co{->hbp[0]} and \co{->hls_idx[0]} - if resize operation is in progress. - This reordering simplifies \co{hashtab_add()}, but at the expense - of an extra check in \co{hashtab_lookup()}. + if resize operation is in progress, instead of the perhaps + more natural choice of \co{->hbp[1]} and \co{->hls_idx[1]}. + However, this less-natural choice has the advantage of simplifying + \co{hashtab_add()}. Further analysis of the code is left as an exercise for the reader. } \QuickQuizEnd
Quoted 43 lines · Paul E. McKenney
Quoted 24 lines · Akira Yokosawa
Quoted 1 line
From
7b69a9b37ba9a73a50aad5cbb097235ddfe75870Mon Sep 17 00:00:00 2001From: Akira Yokosawa <[email protected]>
Date: Mon, 14 Jan 2019 07:25:14 +0900
Subject: [PATCH 0/6] Simplifyhash_resize.cHi Paul,
This patch set updates
hash_resize.c, which you suggested for me to take over, and the related text.I added a couple of Quick Quizzes as well, and in one of them, I included your version of
hash_resize.c.Patch #1 updates
hash_resize.cin the way I suggested. Note that in this update, "#ifndefFCV_SNIPPET" blocks are used to hide code for debugging (hash value checks) in code snippets. This code can actually be compiled with "-DFCV_SNIPPET" to see the performance without hash value checks.Patch #2 adds a couple of Quick Quizzes. Your version of
hash_resize.cis added ashash_resize_s.c.Patch #3 removes unnecessary folding in a code snippet.
Quoted 7 lines
Patch #4 adjusts a few sentence to the simpler approach.
Patch #5 adds another Quick Quiz.
Patch #6 adds an "#error" directive for the lack of
rcu_barrier().All of the updates in text would need your native eyes to be polished.
Nice! Queued and pushed, thank you! I will review and send any needed update by end of tomorrow, Pacific Time.
Just FYI, this also pushed out a couple of commits starting my rework of Section 9.5.1.
Quoted 4 lines
Thanks, Akira
PS:
Quoted 8 lines
Quoted 2 lines
I couldn't make out your concern of "if we ever want to iterate over the hash table". Can you elaborate it?
Consider a
hash_iterate()function that takes a pointer to a function. Thenhash_iterate()would invoke this function on each element currently in the hash table, for a relatively relaxed definition of "currently". With your hash table, a naive implementation might iterate over some data items twice due to their being in both the old and the new tables.
If we stick to the old table, which is kept updated during resizing, there shouldn't be any duplication of items.
What am I missing?
Thanks, Akira
Quoted 36 lines
But this is not hard to fix, for example, by creating some sort of list of elements and the eliminating duplicates, then invoking the specified function on each of them. Of course, this entire process would need to be carried out within an RCU read-side critical section.
This assumes that iteration over the entire hash table is rare, which I would certainly expect it to be, given how expensive it is.
Plus, we currently don't support iteration, so it isn't currently a problem anyway. ;-)
Thanx, Paul
Quoted 21 lines
--
Akira Yokosawa (6):
datastruct/hash: Simplifyhash_resize.c
datastruct/hash: Add a couple of Quick Quizzes regardinghash_resize.cdatastruct/hash: Fix unnecessary folding in code snippet datastruct/hash: Adjust context to updated code inhash_resize.cdatastruct/hash: Add Quick Quiz on READ_ONCE/WRITE_ONCE in
hash_resize.c
datastruct/hash: Display error msg ifrcu_barrier()is not availableCodeSamples/datastruct/hash/Makefile | 5 +- CodeSamples/datastruct/hash/hash_resize.c | 62 +++-- CodeSamples/datastruct/hash/hash_resize_s.c | 365 ++++++++++++++++++++++++++++ CodeSamples/datastruct/hash/hashtorture.h | 11 +- datastruct/datastruct.tex | 146 +++++++---- 5 files changed, 513 insertions(+), 76 deletions(-) create mode 100644 CodeSamples/datastruct/hash/hash_resize_s.cSignature · 1 line
2.7.4
Quoted 53 lines · Akira Yokosawa
Quoted 43 lines · Paul E. McKenney
Quoted 24 lines · Akira Yokosawa
Quoted 1 line
From
7b69a9b37ba9a73a50aad5cbb097235ddfe75870Mon Sep 17 00:00:00 2001From: Akira Yokosawa <[email protected]>
Date: Mon, 14 Jan 2019 07:25:14 +0900
Subject: [PATCH 0/6] Simplifyhash_resize.cHi Paul,
This patch set updates
hash_resize.c, which you suggested for me to take over, and the related text.I added a couple of Quick Quizzes as well, and in one of them, I included your version of
hash_resize.c.Patch #1 updates
hash_resize.cin the way I suggested. Note that in this update, "#ifndefFCV_SNIPPET" blocks are used to hide code for debugging (hash value checks) in code snippets. This code can actually be compiled with "-DFCV_SNIPPET" to see the performance without hash value checks.Patch #2 adds a couple of Quick Quizzes. Your version of
hash_resize.cis added ashash_resize_s.c.Patch #3 removes unnecessary folding in a code snippet.
Quoted 7 lines
Patch #4 adjusts a few sentence to the simpler approach.
Patch #5 adds another Quick Quiz.
Patch #6 adds an "#error" directive for the lack of
rcu_barrier().All of the updates in text would need your native eyes to be polished.
Nice! Queued and pushed, thank you! I will review and send any needed update by end of tomorrow, Pacific Time.
Just FYI, this also pushed out a couple of commits starting my rework of Section 9.5.1.
Quoted 4 lines
Thanks, Akira
PS:
Quoted 8 lines
Quoted 2 lines
I couldn't make out your concern of "if we ever want to iterate over the hash table". Can you elaborate it?
Consider a
hash_iterate()function that takes a pointer to a function. Thenhash_iterate()would invoke this function on each element currently in the hash table, for a relatively relaxed definition of "currently". With your hash table, a naive implementation might iterate over some data items twice due to their being in both the old and the new tables.
Quoted 4 lines
If we stick to the old table, which is kept updated during resizing, there shouldn't be any duplication of items.
What am I missing?
Fair point. Please accept my apologies for my confusion. I think that the right person is now maintaining hash_resize.c. ;-)
Thanx, Paul
Quoted 39 lines
Thanks, Akira
Quoted 36 lines
But this is not hard to fix, for example, by creating some sort of list of elements and the eliminating duplicates, then invoking the specified function on each of them. Of course, this entire process would need to be carried out within an RCU read-side critical section.
This assumes that iteration over the entire hash table is rare, which I would certainly expect it to be, given how expensive it is.
Plus, we currently don't support iteration, so it isn't currently a problem anyway. ;-)
Thanx, Paul
Quoted 21 lines
--
Akira Yokosawa (6):
datastruct/hash: Simplifyhash_resize.c
datastruct/hash: Add a couple of Quick Quizzes regardinghash_resize.cdatastruct/hash: Fix unnecessary folding in code snippet datastruct/hash: Adjust context to updated code inhash_resize.cdatastruct/hash: Add Quick Quiz on READ_ONCE/WRITE_ONCE in
hash_resize.c
datastruct/hash: Display error msg ifrcu_barrier()is not availableCodeSamples/datastruct/hash/Makefile | 5 +- CodeSamples/datastruct/hash/hash_resize.c | 62 +++-- CodeSamples/datastruct/hash/hash_resize_s.c | 365 ++++++++++++++++++++++++++++ CodeSamples/datastruct/hash/hashtorture.h | 11 +- datastruct/datastruct.tex | 146 +++++++---- 5 files changed, 513 insertions(+), 76 deletions(-) create mode 100644 CodeSamples/datastruct/hash/hash_resize_s.cSignature · 1 line
2.7.4