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