Skip to content

Commit 222476c

Browse files
committed
hash table CHANGE new debug category for hash table changes
Fixes #963
1 parent 8fe2bc6 commit 222476c

File tree

5 files changed

+87
-3
lines changed

5 files changed

+87
-3
lines changed

src/hash_table.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,73 @@ lyht_find_next(struct hash_table *ht, void *val_p, uint32_t hash, void **match_p
606606
return 1;
607607
}
608608

609+
/* prints little-endian numbers, will also work on big-endian just the values will look weird */
610+
static char *
611+
lyht_dbgprint_val2str(void *val_p, int32_t hits, uint16_t rec_size)
612+
{
613+
char *val;
614+
int32_t i, j, val_size;
615+
616+
val_size = rec_size - (sizeof(struct ht_rec) - 1);
617+
618+
val = malloc(val_size * 2 + 1);
619+
for (i = 0, j = val_size - 1; i < val_size; ++i, --j) {
620+
if (hits > 0) {
621+
sprintf(val + i * 2, "%02x", *(((uint8_t *)val_p) + j));
622+
} else {
623+
sprintf(val + i * 2, " ");
624+
}
625+
}
626+
627+
return val;
628+
}
629+
630+
static void
631+
lyht_dbgprint_ht(struct hash_table *ht, const char *info)
632+
{
633+
struct ht_rec *rec;
634+
uint32_t i, i_len;
635+
char *val;
636+
637+
if (LY_LLDBG > ly_log_level) {
638+
return;
639+
}
640+
641+
LOGDBG(LY_LDGHASH, "");
642+
LOGDBG(LY_LDGHASH, "hash table %s (used %u, size %u):", info, ht->used, ht->size);
643+
644+
val = malloc(11);
645+
sprintf(val, "%u", ht->size);
646+
i_len = strlen(val);
647+
free(val);
648+
649+
for (i = 0; i < ht->size; ++i) {
650+
rec = lyht_get_rec(ht->recs, ht->rec_size, i);
651+
val = lyht_dbgprint_val2str(&rec->val, rec->hits, ht->rec_size);
652+
if (rec->hits > 0) {
653+
LOGDBG(LY_LDGHASH, "[%*u] val %s hash %10u %% %*u hits %2d",
654+
(int)i_len, i, val, rec->hash, (int)i_len, rec->hash & (ht->size - 1), rec->hits);
655+
} else {
656+
LOGDBG(LY_LDGHASH, "[%*u] val %s hash %10s %% %*s hits %2d",
657+
(int)i_len, i, val, "", (int)i_len, "", rec->hits);
658+
}
659+
free(val);
660+
}
661+
LOGDBG(LY_LDGHASH, "");
662+
}
663+
664+
static void
665+
lyht_dbgprint_value(void *val_p, uint32_t hash, uint16_t rec_size, const char *operation)
666+
{
667+
if (LY_LLDBG > ly_log_level) {
668+
return;
669+
}
670+
671+
char *val = lyht_dbgprint_val2str(val_p, 1, rec_size);
672+
LOGDBG(LY_LDGHASH, "%s value %s with hash %u", operation, val, hash);
673+
free(val);
674+
}
675+
609676
int
610677
lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash,
611678
values_equal_cb resize_val_equal, void **match_p)
@@ -615,6 +682,9 @@ lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash,
615682
int r, ret;
616683
values_equal_cb old_val_equal;
617684

685+
lyht_dbgprint_ht(ht, "before");
686+
lyht_dbgprint_value(val_p, hash, ht->rec_size, "inserting");
687+
618688
if (!lyht_find_first(ht, hash, &rec)) {
619689
/* we found matching shortened hash */
620690
if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
@@ -688,6 +758,8 @@ lyht_insert_with_resize_cb(struct hash_table *ht, void *val_p, uint32_t hash,
688758
}
689759
}
690760
}
761+
762+
lyht_dbgprint_ht(ht, "after");
691763
return ret;
692764
}
693765

@@ -704,8 +776,12 @@ lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash)
704776
int32_t i;
705777
int first_matched = 0, r, ret;
706778

779+
lyht_dbgprint_ht(ht, "before");
780+
lyht_dbgprint_value(val_p, hash, ht->rec_size, "removing");
781+
707782
if (lyht_find_first(ht, hash, &rec)) {
708783
/* hash not found */
784+
LOGDBG(LY_LDGHASH, "remove failed");
709785
return 1;
710786
}
711787
if ((rec->hash == hash) && ht->val_equal(val_p, &rec->val, 1, ht->cb_data)) {
@@ -743,6 +819,7 @@ lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash)
743819
} else {
744820
/* value not found even in collisions */
745821
assert(!first_matched);
822+
LOGDBG(LY_LDGHASH, "remove failed");
746823
return 1;
747824
}
748825

@@ -757,5 +834,6 @@ lyht_remove(struct hash_table *ht, void *val_p, uint32_t hash)
757834
}
758835
}
759836

837+
lyht_dbgprint_ht(ht, "after");
760838
return ret;
761839
}

src/hash_table.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ typedef int (*values_equal_cb)(void *val1_p, void *val2_p, int mod, void *cb_dat
6060
*/
6161
struct ht_rec {
6262
uint32_t hash; /* hash of the value */
63-
int32_t hits; /* collision/overflow value count - 1 (a filled entry has 1 hit,
64-
* special value -1 means a deleted record) */
63+
int32_t hits; /* (collision/overflow value count - 1) (a filled entry has 1 hit),
64+
* special value (-1) means a deleted record) */
6565
unsigned char val[1]; /* arbitrary-size value */
6666
} _PACKED;
6767

@@ -87,7 +87,7 @@ struct hash_table {
8787
struct dict_rec {
8888
char *value;
8989
uint32_t refcount;
90-
};
90+
} _PACKED;
9191

9292
/**
9393
* dictionary to store repeating strings

src/libyang.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,7 @@ int ly_log_options(int opts);
19161916
#define LY_LDGXPATH 0x08 /**< XPath parsing end evaluation. */
19171917
#define LY_LDGDIFF 0x10 /**< Diff processing and creation. */
19181918
#define LY_LDGAPI 0x20 /**< API tracing. */
1919+
#define LY_LDGHASH 0x40 /**< Hash table modifications. */
19191920

19201921
/**
19211922
* @}

src/log.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ ly_log_dbg(int group, const char *format, ...)
266266
case LY_LDGAPI:
267267
str_group = "API";
268268
break;
269+
case LY_LDGHASH:
270+
str_group = "HASH";
271+
break;
269272
default:
270273
LOGINT(NULL);
271274
return;

tests/internal/test_hash_table.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,5 +364,7 @@ int main(void)
364364
cmocka_unit_test_setup_teardown(test_invalid_move2, setup_f, teardown_f),
365365
};
366366

367+
/*ly_verb(LY_LLDBG);
368+
ly_verb_dbg(LY_LDGHASH);*/
367369
return cmocka_run_group_tests(tests, NULL, NULL);
368370
}

0 commit comments

Comments
 (0)