* A Crit Bit tree based hash
*/
+#define PHK 1
+
#include "config.h"
#include <pthread.h>
static struct lock hcb_mtx;
+static VTAILQ_HEAD(,objhead) laylow = VTAILQ_HEAD_INITIALIZER(laylow);
+
/**********************************************************************
* Table for finding out how many bits two bytes have in common,
* counting from the MSB towards the LSB.
{
unsigned u, r;
- for (u = 0; u < oh1->digest_len && oh1->digest[u] == oh2->digest[u]; u++)
+ for (u = 0; u < DIGEST_LEN && oh1->digest[u] == oh2->digest[u]; u++)
;
r = hcb_bits(oh1->digest[u], oh2->digest[u]);
y->ptr = u;
while(hcb_is_y(*p)) {
y = hcb_l_y(*p);
- if (y->ptr > oh->digest_len)
+ if (y->ptr > DIGEST_LEN)
s = 0;
else
s = (oh->digest[y->ptr] & y->bitmask) != 0;
/* We found a node, does it match ? */
oh2 = hcb_l_node(*p);
- if (oh2->digest_len == oh->digest_len &&
- !memcmp(oh2->digest, oh->digest, oh->digest_len))
+ if (!memcmp(oh2->digest, oh->digest, DIGEST_LEN))
return (oh2);
if (!has_lock)
y = hcb_l_y(*p);
if (y->critbit > y2->critbit)
break;
- if (y->ptr > oh->digest_len)
+ if (y->ptr > DIGEST_LEN)
s = 0;
else
s = (oh->digest[y->ptr] & y->bitmask) != 0;
y = NULL;
while(hcb_is_y(*p)) {
y = hcb_l_y(*p);
- if (y->ptr > oh->digest_len)
+ if (y->ptr > DIGEST_LEN)
s = 0;
else
s = (oh->digest[y->ptr] & y->bitmask) != 0;
if (hcb_is_node(p)) {
oh = hcb_l_node(p);
fprintf(fd, "%*.*sN %u %u r%d <%02x%02x%02x...> <%s>\n",
- indent, indent, "", oh->digest_len, indent / 2,
+ indent, indent, "", DIGEST_LEN, indent / 2,
oh->refcnt,
oh->digest[0], oh->digest[1], oh->digest[2],
oh->hash);
}
+/**********************************************************************/
+
+#define COOL_DURATION 15 /* seconds */
+
+static void *
+hcb_cleaner(void *priv)
+{
+ struct objhead *oh, *oh2;
+ struct hcb_y *y;
+
+ THR_SetName("hcb_cleaner");
+ (void)priv;
+ while (1) {
+ sleep(1);
+ Lck_Lock(&hcb_mtx);
+ VTAILQ_FOREACH_SAFE(oh, &laylow, coollist, oh2) {
+ if (oh->hash != NULL) {
+ free(oh->hash);
+ oh->hash = NULL;
+ }
+ y = (void *)&oh->u;
+ if (y->leaf[0] || y->leaf[1])
+ continue;
+ if (++oh->refcnt > COOL_DURATION) {
+ VTAILQ_REMOVE(&laylow, oh, coollist);
+ if (PHK)
+ fprintf(stderr, "OH %p is cold enough\n", oh);
+ free(oh);
+ VSL_stats->n_objecthead--;
+ }
+ }
+ Lck_Unlock(&hcb_mtx);
+ }
+}
+
/**********************************************************************/
static void
hcb_start(void)
{
struct objhead *oh = NULL;
+ pthread_t tp;
(void)oh;
+ AZ(pthread_create(&tp, NULL, hcb_cleaner, NULL));
assert(params->hash_sha256);
assert(sizeof(struct hcb_y) <= sizeof(oh->u));
memset(&hcb_root, 0, sizeof hcb_root);
hcb_deref(struct objhead *oh)
{
int r;
- struct hcb_y *y;
r = 1;
CHECK_OBJ_NOTNULL(oh, OBJHEAD_MAGIC);
if (--oh->refcnt == 0) {
Lck_Lock(&hcb_mtx);
hcb_delete(&hcb_root, oh);
- y = (void*)&oh->u;
- if (y->leaf[0] == 0 && y->leaf[1] == 0)
- r = 0;
- else {
- /* XXX: on waiting list */
- }
+ VTAILQ_INSERT_TAIL(&laylow, oh, coollist);
Lck_Unlock(&hcb_mtx);
}
Lck_Unlock(&oh->mtx);
- if (0) {
+ if (PHK) {
fprintf(stderr, "%s %d %d <%s>\n", __func__, __LINE__, r, oh->hash);
dump(&hcb_root, stderr);
}
oh = hcb_insert(&hcb_root, noh, 1);
if (oh == noh) {
VSL_stats->hcb_insert++;
- if (0) {
+ if (PHK) {
fprintf(stderr, "%s %d\n", __func__, __LINE__);
dump(&hcb_root, stderr);
}
noh->hash = NULL;
noh->hashlen = 0;
VSL_stats->hcb_lock++;
- if (0) {
+ if (PHK) {
fprintf(stderr, "%s %d\n", __func__, __LINE__);
dump(&hcb_root, stderr);
}