]> arthur.barton.de Git - netdata.git/blob - src/common.c
optimized pluginsd parsing for speed
[netdata.git] / src / common.c
1 // enable O_NOATIME
2 #define _GNU_SOURCE
3
4 #include <string.h>
5 #include <ctype.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <sys/mman.h>
12
13
14 #include "log.h"
15 #include "common.h"
16
17 /*
18 // http://stackoverflow.com/questions/7666509/hash-function-for-string
19 uint32_t simple_hash(const char *name)
20 {
21         const char *s = name;
22         uint32_t hash = 5381;
23         int i;
24
25         while((i = *s++)) hash = ((hash << 5) + hash) + i;
26
27         // fprintf(stderr, "HASH: %lu %s\n", hash, name);
28
29         return hash;
30 }
31 */
32
33
34 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
35 uint32_t simple_hash(const char *name) {
36         unsigned char *s = (unsigned char *)name;
37         uint32_t hval = 0x811c9dc5;
38
39         // FNV-1a algorithm
40         while (*s) {
41                 // multiply by the 32 bit FNV magic prime mod 2^32
42                 // gcc optimized
43                 hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
44
45                 // xor the bottom with the current octet
46                 hval ^= (uint32_t)*s++;
47         }
48
49         // fprintf(stderr, "HASH: %u = %s\n", hval, name);
50         return hval;
51 }
52
53 /*
54 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
55 // one at a time hash
56 uint32_t simple_hash(const char *name) {
57         unsigned char *s = (unsigned char *)name;
58         uint32_t h = 0;
59
60         while(*s) {
61                 h += *s++;
62                 h += (h << 10);
63                 h ^= (h >> 6);
64         }
65
66         h += (h << 3);
67         h ^= (h >> 11);
68         h += (h << 15);
69
70         // fprintf(stderr, "HASH: %u = %s\n", h, name);
71
72         return h;
73 }
74 */
75
76 void strreverse(char* begin, char* end)
77 {
78     char aux;
79     while (end > begin)
80         aux = *end, *end-- = *begin, *begin++ = aux;
81 }
82
83 char *mystrsep(char **ptr, char *s)
84 {
85         char *p = "";
86         while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
87         return(p);
88 }
89
90 char *trim(char *s)
91 {
92         // skip leading spaces
93         while(*s && isspace(*s)) s++;
94         if(!*s || *s == '#') return NULL;
95
96         // skip tailing spaces
97         int c = strlen(s) - 1;
98         while(c >= 0 && isspace(s[c])) {
99                 s[c] = '\0';
100                 c--;
101         }
102         if(c < 0) return NULL;
103         if(!*s) return NULL;
104         return s;
105 }
106
107 void *mymmap(const char *filename, unsigned long size, int flags)
108 {
109         int fd;
110         void *mem = NULL;
111
112         errno = 0;
113         fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
114         if(fd != -1) {
115                 if(lseek(fd, size, SEEK_SET) == (long)size) {
116                         if(write(fd, "", 1) == 1) {
117
118                                 if(ftruncate(fd, size))
119                                         error("Cannot truncate file '%s' to size %ld. Will use the larger file.", filename, size);
120
121                                 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
122                                 if(mem) {
123                                         if(madvise(mem, size, MADV_SEQUENTIAL|MADV_DONTFORK|MADV_WILLNEED) != 0)
124                                                 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
125                                 }
126                         }
127                         else error("Cannot write to file '%s' at position %ld.", filename, size);
128                 }
129                 else error("Cannot seek file '%s' to size %ld.", filename, size);
130
131                 close(fd);
132         }
133         else error("Cannot create/open file '%s'.", filename);
134
135         return mem;
136 }
137
138 int savememory(const char *filename, void *mem, unsigned long size)
139 {
140         char tmpfilename[FILENAME_MAX + 1];
141
142         snprintf(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
143
144         int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
145         if(fd < 0) {
146                 error("Cannot create/open file '%s'.", filename);
147                 return -1;
148         }
149
150         if(write(fd, mem, size) != (long)size) {
151                 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
152                 close(fd);
153                 return -1;
154         }
155
156         close(fd);
157
158         int ret = 0;
159         if(rename(tmpfilename, filename)) {
160                 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
161                 ret = -1;
162         }
163
164         return ret;
165 }
166
167 int fd_is_valid(int fd) {
168     return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
169 }
170