4 #include <sys/syscall.h>
17 #include "appconfig.h"
19 char *global_host_prefix = "";
23 // http://stackoverflow.com/questions/7666509/hash-function-for-string
24 uint32_t simple_hash(const char *name)
30 while((i = *s++)) hash = ((hash << 5) + hash) + i;
32 // fprintf(stderr, "HASH: %lu %s\n", hash, name);
39 // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
40 uint32_t simple_hash(const char *name) {
41 unsigned char *s = (unsigned char *)name;
42 uint32_t hval = 0x811c9dc5;
46 // multiply by the 32 bit FNV magic prime mod 2^32
48 hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
50 // xor the bottom with the current octet
51 hval ^= (uint32_t)*s++;
54 // fprintf(stderr, "HASH: %u = %s\n", hval, name);
59 // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
61 uint32_t simple_hash(const char *name) {
62 unsigned char *s = (unsigned char *)name;
75 // fprintf(stderr, "HASH: %u = %s\n", h, name);
81 void strreverse(char* begin, char* end)
85 aux = *end, *end-- = *begin, *begin++ = aux;
88 char *mystrsep(char **ptr, char *s)
91 while ( p && !p[0] && *ptr ) p = strsep(ptr, s);
97 // skip leading spaces
98 while(*s && isspace(*s)) s++;
99 if(!*s || *s == '#') return NULL;
101 // skip tailing spaces
102 long c = (long) strlen(s) - 1;
103 while(c >= 0 && isspace(s[c])) {
107 if(c < 0) return NULL;
112 void *mymmap(const char *filename, size_t size, int flags, int ksm)
118 fd = open(filename, O_RDWR|O_CREAT|O_NOATIME, 0664);
120 if(lseek(fd, size, SEEK_SET) == (long)size) {
121 if(write(fd, "", 1) == 1) {
122 if(ftruncate(fd, size))
123 error("Cannot truncate file '%s' to size %ld. Will use the larger file.", filename, size);
125 #ifdef MADV_MERGEABLE
126 if(flags & MAP_SHARED || !enable_ksm || !ksm) {
128 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags, fd, 0);
130 int advise = MADV_SEQUENTIAL|MADV_DONTFORK;
131 if(flags & MAP_SHARED) advise |= MADV_WILLNEED;
133 if(madvise(mem, size, advise) != 0)
134 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
136 #ifdef MADV_MERGEABLE
139 mem = mmap(NULL, size, PROT_READ|PROT_WRITE, flags|MAP_ANONYMOUS, -1, 0);
141 if(lseek(fd, 0, SEEK_SET) == 0) {
142 if(read(fd, mem, size) != (ssize_t)size)
143 error("Cannot read from file '%s'", filename);
146 error("Cannot seek to beginning of file '%s'.", filename);
148 // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
149 if(madvise(mem, size, MADV_MERGEABLE) != 0)
150 error("Cannot advise the kernel about the memory usage of file '%s'.", filename);
153 error("Cannot allocate PRIVATE ANONYMOUS memory for KSM for file '%s'.", filename);
157 else error("Cannot write to file '%s' at position %ld.", filename, size);
159 else error("Cannot seek file '%s' to size %ld.", filename, size);
163 else error("Cannot create/open file '%s'.", filename);
168 int savememory(const char *filename, void *mem, unsigned long size)
170 char tmpfilename[FILENAME_MAX + 1];
172 snprintf(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long)getpid());
174 int fd = open(tmpfilename, O_RDWR|O_CREAT|O_NOATIME, 0664);
176 error("Cannot create/open file '%s'.", filename);
180 if(write(fd, mem, size) != (long)size) {
181 error("Cannot write to file '%s' %ld bytes.", filename, (long)size);
189 if(rename(tmpfilename, filename)) {
190 error("Cannot rename '%s' to '%s'", tmpfilename, filename);
197 int fd_is_valid(int fd) {
198 return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
202 ***************************************************************************
203 * Get number of clock ticks per second.
204 ***************************************************************************
212 if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
216 hz = (unsigned int) ticks;
221 return syscall(SYS_gettid);