From 9aa406ee734cc162cfbc44e4c4d5e6b0993c1ac7 Mon Sep 17 00:00:00 2001 From: jdybnis Date: Sun, 23 Nov 2008 23:14:56 +0000 Subject: [PATCH] clean up skiplist debug print change skiplist and list name prefixes --- struct/list.c | 44 ++++++++++++++--------------- struct/skiplist.c | 71 ++++++++++++++++++++++++++--------------------- 2 files changed, 62 insertions(+), 53 deletions(-) diff --git a/struct/list.c b/struct/list.c index 40a9735..d9e4612 100644 --- a/struct/list.c +++ b/struct/list.c @@ -31,7 +31,7 @@ node_t *node_alloc (uint64_t key, uint64_t value) { return item; } -list_t *list_alloc (void) { +list_t *ll_alloc (void) { list_t *list = (list_t *)nbd_malloc(sizeof(list_t)); list->head = node_alloc(0, 0); list->last = node_alloc((uint64_t)-1, 0); @@ -98,8 +98,8 @@ static node_t *find_pred (node_t **pred_ptr, list_t *list, uint64_t key, int hel } // Fast find. Do not help unlink partially removed nodes and do not return the found item's predecessor. -uint64_t list_lookup (list_t *list, uint64_t key) { - TRACE("l3", "list_lookup: searching for key %p in list %p", key, list); +uint64_t ll_lookup (list_t *list, uint64_t key) { + TRACE("l3", "ll_lookup: searching for key %p in list %p", key, list); node_t *item = find_pred(NULL, list, key, FALSE); // If we found an matching the return its value. @@ -107,8 +107,8 @@ uint64_t list_lookup (list_t *list, uint64_t key) { } // Insert the , if it doesn't already exist in the -uint64_t list_add (list_t *list, uint64_t key, uint64_t value) { - TRACE("l3", "list_add: inserting key %p value %p", key, value); +uint64_t ll_add (list_t *list, uint64_t key, uint64_t value) { + TRACE("l3", "ll_add: inserting key %p value %p", key, value); node_t *pred; node_t *item = NULL; do { @@ -116,52 +116,52 @@ uint64_t list_add (list_t *list, uint64_t key, uint64_t value) { // If a node matching already exists in the list, return its value. if (next->key == key) { - TRACE("l3", "list_add: there is already an item %p (value %p) with the same key", next, next->value); + TRACE("l3", "ll_add: there is already an item %p (value %p) with the same key", next, next->value); if (EXPECT_FALSE(item != NULL)) { nbd_free(item); } return next->value; } - TRACE("l3", "list_add: attempting to insert item between %p and %p", pred, next); + TRACE("l3", "ll_add: attempting to insert item between %p and %p", pred, next); if (EXPECT_TRUE(item == NULL)) { item = node_alloc(key, value); } item->next = next; node_t *other = SYNC_CAS(&pred->next, next, item); if (other == next) { - TRACE("l3", "list_add: successfully inserted item %p", item, 0); + TRACE("l3", "ll_add: successfully inserted item %p", item, 0); return DOES_NOT_EXIST; // success } - TRACE("l3", "list_add: failed to change pred's link: expected %p found %p", next, other); + TRACE("l3", "ll_add: failed to change pred's link: expected %p found %p", next, other); } while (1); } -uint64_t list_remove (list_t *list, uint64_t key) { - TRACE("l3", "list_remove: removing item with key %p from list %p", key, list); +uint64_t ll_remove (list_t *list, uint64_t key) { + TRACE("l3", "ll_remove: removing item with key %p from list %p", key, list); node_t *pred; node_t *item = find_pred(&pred, list, key, TRUE); if (item->key != key) { - TRACE("l3", "list_remove: remove failed, an item with a matching key does not exist in the list", 0, 0); + TRACE("l3", "ll_remove: remove failed, an item with a matching key does not exist in the list", 0, 0); return DOES_NOT_EXIST; } // Mark removed. This must be atomic. If multiple threads try to remove the same item // only one of them should succeed. if (EXPECT_FALSE(IS_TAGGED(item->next))) { - TRACE("l3", "list_remove: %p is already marked for removal by another thread", item, 0); + TRACE("l3", "ll_remove: %p is already marked for removal by another thread", item, 0); return DOES_NOT_EXIST; } node_t *next = SYNC_FETCH_AND_OR(&item->next, TAG); if (EXPECT_FALSE(IS_TAGGED(next))) { - TRACE("l3", "list_remove: lost race -- %p is already marked for removal by another thread", item, 0); + TRACE("l3", "ll_remove: lost race -- %p is already marked for removal by another thread", item, 0); return DOES_NOT_EXIST; } uint64_t value = item->value; // Unlink from the list. - TRACE("l3", "list_remove: link item's pred %p to it's successor %p", pred, next); + TRACE("l3", "ll_remove: link item's pred %p to it's successor %p", pred, next); node_t *other; if ((other = SYNC_CAS(&pred->next, item, next)) != item) { - TRACE("l3", "list_remove: unlink failed; pred's link changed from %p to %p", item, other); + TRACE("l3", "ll_remove: unlink failed; pred's link changed from %p to %p", item, other); // By marking the item earlier, we logically removed it. It is safe to leave the item. // Another thread will finish physically removing it from the list. return value; @@ -172,7 +172,7 @@ uint64_t list_remove (list_t *list, uint64_t key) { return value; } -void list_print (list_t *list) { +void ll_print (list_t *list) { node_t *item; item = list->head; while (item) { @@ -194,7 +194,7 @@ void list_print (list_t *list) { static volatile int wait_; static long num_threads_; -static list_t *list_; +static list_t *ll_; void *worker (void *arg) { int id = (int)(size_t)arg; @@ -209,9 +209,9 @@ void *worker (void *arg) { int n = rand_r(&rand_seed); int key = (n & 0xF) + 1; if (n & (1 << 8)) { - list_add(list_, key, 1); + ll_add(ll_, key, 1); } else { - list_remove(list_, key); + ll_remove(ll_, key); } rcu_update(); @@ -251,7 +251,7 @@ int main (int argc, char **argv) { } } - list_ = list_alloc(); + ll_ = ll_alloc(); struct timeval tv1, tv2; gettimeofday(&tv1, NULL); @@ -270,7 +270,7 @@ int main (int argc, char **argv) { gettimeofday(&tv2, NULL); int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000; printf("Th:%ld Time:%dms\n", num_threads_, ms); - list_print(list_); + ll_print(ll_); lwt_dump("lwt.out"); return 0; diff --git a/struct/skiplist.c b/struct/skiplist.c index 2d81fdd..9a26720 100644 --- a/struct/skiplist.c +++ b/struct/skiplist.c @@ -9,6 +9,8 @@ * See also Kir Fraser's dissertation "Practical Lock Freedom" * www.cl.cam.ac.uk/techreports/UCAM-CL-TR-579.pdf * + * This code depends on certain stores and loads being ordered. Be careful on non-x86 platforms + * with weaker memory-models. This code probably won't work without adding some memory barriers. */ #include #include @@ -17,7 +19,7 @@ #include "struct.h" #include "mem.h" -#define MAX_LEVEL 3 +#define MAX_LEVEL 31 typedef struct node { uint64_t key; @@ -52,7 +54,7 @@ node_t *node_alloc (int top_level, uint64_t key, uint64_t value) { return item; } -skiplist_t *skiplist_alloc (void) { +skiplist_t *sl_alloc (void) { skiplist_t *skiplist = (skiplist_t *)nbd_malloc(sizeof(skiplist_t)); skiplist->head = node_alloc(MAX_LEVEL, 0, 0); skiplist->last = node_alloc(MAX_LEVEL, (uint64_t)-1, 0); @@ -131,8 +133,8 @@ static node_t *find_preds (node_t **preds, skiplist_t *skiplist, uint64_t key, i } // Fast find that does not help unlink partially removed nodes and does not return the node's predecessors. -uint64_t skiplist_lookup (skiplist_t *skiplist, uint64_t key) { - TRACE("s3", "skiplist_lookup: searching for key %p in skiplist %p", key, skiplist); +uint64_t sl_lookup (skiplist_t *skiplist, uint64_t key) { + TRACE("s3", "sl_lookup: searching for key %p in skiplist %p", key, skiplist); node_t *item = find_preds(NULL, skiplist, key, FALSE); // If we found an matching the return its value. @@ -140,8 +142,8 @@ uint64_t skiplist_lookup (skiplist_t *skiplist, uint64_t key) { } // Insert the if it doesn't already exist in the -uint64_t skiplist_add_i (int n, skiplist_t *skiplist, uint64_t key, uint64_t value) { - TRACE("s3", "skiplist_add: inserting key %p value %p", key, value); +uint64_t sl_add_r (int r, skiplist_t *skiplist, uint64_t key, uint64_t value) { + TRACE("s3", "sl_add: inserting key %p value %p", key, value); node_t *preds[MAX_LEVEL+1]; node_t *item = NULL; do { @@ -149,24 +151,24 @@ uint64_t skiplist_add_i (int n, skiplist_t *skiplist, uint64_t key, uint64_t val // If a node matching already exists in the skiplist, return its value. if (next->key == key) { - TRACE("s3", "skiplist_add: there is already an item %p (value %p) with the same key", next, next->value); + TRACE("s3", "sl_add: there is already an item %p (value %p) with the same key", next, next->value); if (EXPECT_FALSE(item != NULL)) { nbd_free(item); } return next->value; } // First insert into the bottom level. - if (EXPECT_TRUE(item == NULL)) { item = node_alloc(random_level(n), key, value); } - TRACE("s3", "skiplist_add: attempting to insert item between %p and %p", preds[0], next); + if (EXPECT_TRUE(item == NULL)) { item = node_alloc(random_level(r), key, value); } + TRACE("s3", "sl_add: attempting to insert item between %p and %p", preds[0], next); item->next[0] = next; for (int level = 1; level <= item->top_level; ++level) { item->next[level] = preds[level]->next[level]; } node_t *other = SYNC_CAS(&preds[0]->next[0], next, item); if (other == next) { - TRACE("s3", "skiplist_add: successfully inserted item %p at level 0", item, 0); + TRACE("s3", "sl_add: successfully inserted item %p at level 0", item, 0); break; // success } - TRACE("s3", "skiplist_add: failed to change pred's link: expected %p found %p", next, other); + TRACE("s3", "sl_add: failed to change pred's link: expected %p found %p", next, other); } while (1); @@ -192,25 +194,25 @@ uint64_t skiplist_add_i (int n, skiplist_t *skiplist, uint64_t key, uint64_t val break; } while (1); - TRACE("s3", "skiplist_add: attempting to insert item between %p and %p", pred, next); + TRACE("s3", "sl_add: attempting to insert item between %p and %p", pred, next); node_t *other = SYNC_CAS(&pred->next[level], next, item); if (other == next) { - TRACE("s3", "skiplist_add: successfully inserted item %p at level %llu", item, level); + TRACE("s3", "sl_add: successfully inserted item %p at level %llu", item, level); break; // success } - TRACE("s3", "skiplist_add: failed to change pred's link: expected %p found %p", next, other); + TRACE("s3", "sl_add: failed to change pred's link: expected %p found %p", next, other); } while (1); } return value; } -uint64_t skiplist_remove (skiplist_t *skiplist, uint64_t key) { - TRACE("s3", "skiplist_remove: removing item with key %p from skiplist %p", key, skiplist); +uint64_t sl_remove (skiplist_t *skiplist, uint64_t key) { + TRACE("s3", "sl_remove: removing item with key %p from skiplist %p", key, skiplist); node_t *preds[MAX_LEVEL+1]; node_t *item = find_preds(preds, skiplist, key, TRUE); if (item->key != key) { - TRACE("s3", "skiplist_remove: remove failed, an item with a matching key does not exist in the skiplist", 0, 0); + TRACE("s3", "sl_remove: remove failed, an item with a matching key does not exist in the skiplist", 0, 0); return DOES_NOT_EXIST; } @@ -219,14 +221,14 @@ uint64_t skiplist_remove (skiplist_t *skiplist, uint64_t key) { // them succeeds. for (int level = item->top_level; level >= 0; --level) { if (EXPECT_FALSE(IS_TAGGED(item->next[level]))) { - TRACE("s3", "skiplist_remove: %p is already marked for removal by another thread", item, 0); + TRACE("s3", "sl_remove: %p is already marked for removal by another thread", item, 0); if (level == 0) return DOES_NOT_EXIST; continue; } node_t *next = SYNC_FETCH_AND_OR(&item->next[level], TAG); if (EXPECT_FALSE(IS_TAGGED(next))) { - TRACE("s3", "skiplist_remove: lost race -- %p is already marked for removal by another thread", item, 0); + TRACE("s3", "sl_remove: lost race -- %p is already marked for removal by another thread", item, 0); if (level == 0) return DOES_NOT_EXIST; continue; @@ -240,10 +242,10 @@ uint64_t skiplist_remove (skiplist_t *skiplist, uint64_t key) { while (level >= 0) { node_t *pred = preds[level]; node_t *next = item->next[level]; - TRACE("s3", "skiplist_remove: link item's pred %p to it's successor %p", pred, STRIP_TAG(next)); + TRACE("s3", "sl_remove: link item's pred %p to it's successor %p", pred, STRIP_TAG(next)); node_t *other = NULL; if ((other = SYNC_CAS(&pred->next[level], item, STRIP_TAG(next))) != item) { - TRACE("s3", "skiplist_remove: unlink failed; pred's link changed from %p to %p", item, other); + TRACE("s3", "sl_remove: unlink failed; pred's link changed from %p to %p", item, other); // By marking the item earlier, we logically removed it. It is safe to leave the item partially // unlinked. Another thread will finish physically removing it from the skiplist. return value; @@ -256,10 +258,12 @@ uint64_t skiplist_remove (skiplist_t *skiplist, uint64_t key) { return value; } -void skiplist_print (skiplist_t *skiplist) { +void sl_print (skiplist_t *skiplist) { for (int level = MAX_LEVEL; level >= 0; --level) { - printf("(%d) ", level); node_t *item = skiplist->head; + if (item->next[level] == skiplist->last) + continue; + printf("(%d) ", level); while (item) { node_t *next = item->next[level]; printf("%s%p:0x%llx ", IS_TAGGED(next) ? "*" : "", item, item->key); @@ -269,14 +273,19 @@ void skiplist_print (skiplist_t *skiplist) { fflush(stdout); } + printf("\n"); node_t *item = skiplist->head; while (item) { assert(item->top_level <= MAX_LEVEL); int is_marked = IS_TAGGED(item->next[0]); printf("%s%p:0x%llx (%d", is_marked ? "*" : "", item, item->key, item->top_level); - for (int i = 1; i <= item->top_level; ++i) { - node_t *next = (node_t *)STRIP_TAG(item->next[i]); - printf(" %p:0x%llx", item->next[i], next ? next->key : 0); + for (int level = 1; level <= item->top_level; ++level) { + node_t *next = (node_t *)STRIP_TAG(item->next[level]); + printf(" %p:0x%llx", item->next[level], next ? next->key : 0); + if (item == skiplist->head && item->next[level] == skiplist->last) + break; + if (item == skiplist->last && item->next[level] == NULL) + break; } printf(")\n"); fflush(stdout); @@ -295,7 +304,7 @@ void skiplist_print (skiplist_t *skiplist) { static volatile int wait_; static long num_threads_; -static skiplist_t *skiplist_; +static skiplist_t *sl_; void *worker (void *arg) { int id = (int)(size_t)arg; @@ -310,9 +319,9 @@ void *worker (void *arg) { int n = rand_r(&rand_seed); int key = (n & 0xF) + 1; if (n & (1 << 8)) { - skiplist_add_i(n, skiplist_, key, 1); + sl_add_r(n, sl_, key, 1); } else { - skiplist_remove(skiplist_, key); + sl_remove(sl_, key); } rcu_update(); @@ -352,7 +361,7 @@ int main (int argc, char **argv) { } } - skiplist_ = skiplist_alloc(); + sl_ = sl_alloc(); struct timeval tv1, tv2; gettimeofday(&tv1, NULL); @@ -371,7 +380,7 @@ int main (int argc, char **argv) { gettimeofday(&tv2, NULL); int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000; printf("Th:%ld Time:%dms\n", num_threads_, ms); - skiplist_print(skiplist_); + sl_print(sl_); lwt_dump("lwt.out"); return 0; -- 2.40.0