replaced simple_hash with the FNV-1a 32bit algorithm
Costa Tsaousis (ktsaou) committed
Apr 3, 2015 at 00:14 UTC
593c38de208f03e840c9632f64ff9d7e06313500
8 files changed
+45
-21
src/common.c
+24
-2
@@ -14,11 +14,12 @@
14
#include "log.h"
15
#include "common.h"
16
17
+/*
18
// http://stackoverflow.com/questions/7666509/hash-function-for-string
18
-unsigned long simple_hash(const char *name)
19
+uint32_t simple_hash(const char *name)
20
{
21
const char *s = name;
21
- unsigned long hash = 5381;
22
+ uint32_t hash = 5381;
23
int i;
24
25
while((i = *s++)) hash = ((hash << 5) + hash) + i;
@@ -27,6 +28,27 @@ unsigned long simple_hash(const char *name)
28
29
return hash;
30
}
31
+*/
32
+
33
+// http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
34
+uint32_t simple_hash(const char *name) {
35
+ unsigned char *s = (unsigned char *)name;
36
+ uint32_t hval = 0x811c9dc5;
37
+
38
+ // FNV-1a algorithm
39
+ while (*s) {
40
+ // multiply by the 32 bit FNV magic prime mod 2^32
41
+ // gcc optimized
42
+ hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
43
+
44
+ // xor the bottom with the current octet
45
+ hval ^= (uint32_t)*s++;
46
+ }
47
+
48
+ // fprintf(stderr, "HASH: %u = %s\n", hval, name);
49
+ return hval;
50
+}
51
+
52
53
void strreverse(char* begin, char* end)
54
{
src/common.h
+3
-1
@@ -1,10 +1,12 @@
1
+#include <inttypes.h>
2
+
3
#ifndef NETDATA_COMMON_H
4
#define NETDATA_COMMON_H 1
5
6
#define abs(x) ((x < 0)? -x : x)
7
#define usecdiff(now, last) (((((now)->tv_sec * 1000000ULL) + (now)->tv_usec) - (((last)->tv_sec * 1000000ULL) + (last)->tv_usec)))
8
7
-extern unsigned long simple_hash(const char *name);
9
+extern uint32_t simple_hash(const char *name);
10
extern void strreverse(char* begin, char* end);
11
extern char *mystrsep(char **ptr, char *s);
12
extern char *qstrsep(char **ptr);
src/config.c
+4
-4
@@ -23,7 +23,7 @@ pthread_rwlock_t config_rwlock = PTHREAD_RWLOCK_INITIALIZER;
23
struct config_value {
24
avl avl; // the index - this has to be first!
25
26
- unsigned long hash; // a simple hash to speed up searching
26
+ uint32_t hash; // a simple hash to speed up searching
27
// we first compare hashes, and only if the hashes are equal we do string comparisons
28
29
char *name;
@@ -37,7 +37,7 @@ struct config_value {
37
struct config {
38
avl avl;
39
40
- unsigned long hash; // a simple hash to speed up searching
40
+ uint32_t hash; // a simple hash to speed up searching
41
// we first compare hashes, and only if the hashes are equal we do string comparisons
42
43
char *name;
@@ -63,7 +63,7 @@ static int config_value_compare(void* a, void* b) {
63
#define config_value_index_add(co, cv) avl_insert(&((co)->values_index), (avl *)(cv))
64
#define config_value_index_del(co, cv) avl_remove(&((co)->values_index), (avl *)(cv))
65
66
-static struct config_value *config_value_index_find(struct config *co, const char *name, unsigned long hash) {
66
+static struct config_value *config_value_index_find(struct config *co, const char *name, uint32_t hash) {
67
struct config_value *result = NULL, tmp;
68
tmp.hash = (hash)?hash:simple_hash(name);
69
tmp.name = (char *)name;
@@ -91,7 +91,7 @@ avl_tree config_root_index = {
91
#define config_index_add(cfg) avl_insert(&config_root_index, (avl *)(cfg))
92
#define config_index_del(cfg) avl_remove(&config_root_index, (avl *)(cfg))
93
94
-static struct config *config_index_find(const char *name, unsigned long hash) {
94
+static struct config *config_index_find(const char *name, uint32_t hash) {
95
struct config *result = NULL, tmp;
96
tmp.hash = (hash)?hash:simple_hash(name);
97
tmp.name = (char *)name;
src/dictionary.c
+1
-1
@@ -23,7 +23,7 @@ static int name_value_compare(void* a, void* b) {
23
#define name_value_index_add(dict, cv) avl_insert(&((dict)->values_index), (avl *)(cv))
24
#define name_value_index_del(dict, cv) avl_remove(&((dict)->values_index), (avl *)(cv))
25
26
-static NAME_VALUE *dictionary_name_value_index_find(DICTIONARY *dict, const char *name, unsigned long hash) {
26
+static NAME_VALUE *dictionary_name_value_index_find(DICTIONARY *dict, const char *name, uint32_t hash) {
27
NAME_VALUE *result = NULL, tmp;
28
tmp.hash = (hash)?hash:simple_hash(name);
29
tmp.name = (char *)name;
src/dictionary.h
+1
-1
@@ -6,7 +6,7 @@
6
typedef struct name_value {
7
avl avl; // the index - this has to be first!
8
9
- unsigned long hash; // a simple hash to speed up searching
9
+ uint32_t hash; // a simple hash to speed up searching
10
// we first compare hashes, and only if the hashes are equal we do string comparisons
11
12
char *name;
src/plugins.d/apps_plugin.c
+6
-6
@@ -727,7 +727,7 @@ int read_proc_pid_io(struct pid_stat *p) {
727
728
snprintf(filename, FILENAME_MAX, "/proc/%d/io", p->pid);
729
730
- ff = procfile_reopen(ff, filename, ":");
730
+ ff = procfile_reopen(ff, filename, "");
731
if(!ff) return 1;
732
733
ff = procfile_readall(ff);
@@ -801,8 +801,8 @@ int walk_down(pid_t pid, int level) {
801
802
struct file_descriptor {
803
avl avl;
804
- unsigned long magic;
805
- unsigned long hash;
804
+ uint32_t magic;
805
+ uint32_t hash;
806
const char *name;
807
int type;
808
long count;
@@ -829,7 +829,7 @@ avl_tree all_files_index = {
829
file_descriptor_compare
830
};
831
832
-static struct file_descriptor *file_descriptor_find(const char *name, unsigned long hash) {
832
+static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
833
struct file_descriptor *result = NULL, tmp;
834
tmp.hash = (hash)?hash:simple_hash(name);
835
tmp.name = name;
@@ -883,9 +883,9 @@ void file_descriptor_not_used(int id)
883
unsigned long file_descriptor_find_or_add(const char *name)
884
{
885
static int last_pos = 0;
886
- unsigned long hash = simple_hash(name);
886
+ uint32_t hash = simple_hash(name);
887
888
- if(debug) fprintf(stderr, "apps.plugin: adding or finding name '%s' with hash %lu\n", name, hash);
888
+ if(debug) fprintf(stderr, "apps.plugin: adding or finding name '%s' with hash %u\n", name, hash);
889
890
struct file_descriptor *fd = file_descriptor_find(name, hash);
891
if(fd) {
src/rrd.c
+3
-3
@@ -50,7 +50,7 @@ avl_tree rrdset_root_index = {
50
#define rrdset_index_add(st) avl_insert(&rrdset_root_index, (avl *)(st))
51
#define rrdset_index_del(st) avl_remove(&rrdset_root_index, (avl *)(st))
52
53
-static RRDSET *rrdset_index_find(const char *id, unsigned long hash) {
53
+static RRDSET *rrdset_index_find(const char *id, uint32_t hash) {
54
RRDSET *result = NULL, tmp;
55
strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
56
tmp.id[RRD_ID_LENGTH_MAX] = '\0';
@@ -90,7 +90,7 @@ int rrdset_index_add_name(RRDSET *st) {
90
91
#define rrdset_index_del_name(st) avl_remove(&rrdset_root_index_name, (avl *)(&st->avlname))
92
93
-static RRDSET *rrdset_index_find_name(const char *name, unsigned long hash) {
93
+static RRDSET *rrdset_index_find_name(const char *name, uint32_t hash) {
94
void *result = NULL;
95
RRDSET tmp;
96
tmp.name = name;
@@ -125,7 +125,7 @@ static int rrddim_compare(void* a, void* b) {
125
#define rrddim_index_add(st, rd) avl_insert(&((st)->dimensions_index), (avl *)(rd))
126
#define rrddim_index_del(st,rd ) avl_remove(&((st)->dimensions_index), (avl *)(rd))
127
128
-static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, unsigned long hash) {
128
+static RRDDIM *rrddim_index_find(RRDSET *st, const char *id, uint32_t hash) {
129
RRDDIM *result = NULL, tmp;
130
tmp.hash = (hash)?hash:simple_hash(id);
131
strncpy(tmp.id, id, RRD_ID_LENGTH_MAX);
src/rrd.h
+3
-3
@@ -101,7 +101,7 @@ struct rrddim {
101
// ------------------------------------------------------------------------
102
// members for temporary data we need for calculations
103
104
- unsigned long hash; // a simple hash of the id, to speed up searching / indexing
104
+ uint32_t hash; // a simple hash of the id, to speed up searching / indexing
105
// instead of strcmp() every item in the binary index
106
// we first compare the hashes
107
@@ -209,10 +209,10 @@ struct rrdset {
209
210
unsigned long long first_entry_t; // the timestamp (in microseconds) of the oldest entry in the db
211
212
- unsigned long hash; // a simple hash on the id, to speed up searching
212
+ uint32_t hash; // a simple hash on the id, to speed up searching
213
// we first compare hashes, and only if the hashes are equal we do string comparisons
214
215
- unsigned long hash_name; // a simple hash on the name
215
+ uint32_t hash_name; // a simple hash on the name
216
217
unsigned long long usec_since_last_update; // the time in microseconds since the last collection of data
218