@cryptotaxi247 / netdata-1 / commits / 65452ac7e

properly call madvise() with memory mode = ram; properly free memory with memory mode = ram

Costa Tsaousis (ktsaou) committed Jun 7, 2017 at 21:27 UTC 65452ac7e72945795f7807a614dcae47ed7ff06f
3 files changed +26 -15
src/common.c
+14 -9
@@ -944,6 +944,8 @@ inline char *trim_all(char *buffer) {
944 }
945
946 static int memory_file_open(const char *filename, size_t size) {
947 + // info("memory_file_open('%s', %zu", filename, size);
948 +
949 int fd = open(filename, O_RDWR | O_CREAT | O_NOATIME, 0664);
950 if (fd != -1) {
951 if (lseek(fd, size, SEEK_SET) == (off_t) size) {
@@ -962,6 +964,7 @@ static int memory_file_open(const char *filename, size_t size) {
964
965 // mmap_shared is used for memory mode = map
966 static void *memory_file_mmap(const char *filename, size_t size, int flags) {
967 + // info("memory_file_mmap('%s', %zu", filename, size);
968 static int log_madvise = 1;
969
970 int fd = -1;
@@ -992,6 +995,7 @@ static void *memory_file_mmap(const char *filename, size_t size, int flags) {
995
996 #ifdef MADV_MERGEABLE
997 static void *memory_file_mmap_ksm(const char *filename, size_t size, int flags) {
998 + // info("memory_file_mmap_ksm('%s', %zu", filename, size);
999 static int log_madvise_2 = 1, log_madvise_3 = 1;
1000
1001 int fd = -1;
@@ -1011,17 +1015,17 @@ static void *memory_file_mmap_ksm(const char *filename, size_t size, int flags)
1015 error("Cannot read from file '%s'", filename);
1016 }
1017 else error("Cannot seek to beginning of file '%s'.", filename);
1018 + }
1019
1015 - // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
1016 - if (madvise(mem, size, MADV_SEQUENTIAL | MADV_DONTFORK) != 0 && log_madvise_2) {
1017 - error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.", filename);
1018 - log_madvise_2--;
1019 - }
1020 + // don't use MADV_SEQUENTIAL|MADV_DONTFORK, they disable MADV_MERGEABLE
1021 + if (madvise(mem, size, MADV_SEQUENTIAL | MADV_DONTFORK) != 0 && log_madvise_2) {
1022 + error("Cannot advise the kernel about the memory usage (MADV_SEQUENTIAL|MADV_DONTFORK) of file '%s'.", filename);
1023 + log_madvise_2--;
1024 + }
1025
1021 - if (madvise(mem, size, MADV_MERGEABLE) != 0 && log_madvise_3) {
1022 - error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.", filename);
1023 - log_madvise_3--;
1024 - }
1026 + if (madvise(mem, size, MADV_MERGEABLE) != 0 && log_madvise_3) {
1027 + error("Cannot advise the kernel about the memory usage (MADV_MERGEABLE) of file '%s'.", filename);
1028 + log_madvise_3--;
1029 }
1030 }
1031
@@ -1032,6 +1036,7 @@ static void *memory_file_mmap_ksm(const char *filename, size_t size, int flags)
1036 }
1037 #else
1038 static void *memory_file_mmap_ksm(const char *filename, size_t size, int flags) {
1039 + // info("memory_file_mmap_ksm FALLBACK ('%s', %zu", filename, size);
1040
1041 if(filename)
1042 return memory_file_mmap(filename, size, flags);
src/rrddim.c
+1 -1
@@ -276,6 +276,7 @@ void rrddim_free(RRDSET *st, RRDDIM *rd)
276 switch(rd->rrd_memory_mode) {
277 case RRD_MEMORY_MODE_SAVE:
278 case RRD_MEMORY_MODE_MAP:
279 + case RRD_MEMORY_MODE_RAM:
280 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
281 freez((void *)rd->id);
282 freez(rd->cache_filename);
@@ -283,7 +284,6 @@ void rrddim_free(RRDSET *st, RRDDIM *rd)
284 break;
285
286 case RRD_MEMORY_MODE_NONE:
286 - case RRD_MEMORY_MODE_RAM:
287 debug(D_RRD_CALLS, "Removing dimension '%s'.", rd->name);
288 freez((void *)rd->id);
289 freez(rd->cache_filename);
src/rrdset.c
+11 -5
@@ -279,12 +279,18 @@ void rrdset_free(RRDSET *st) {
279 // free directly allocated members
280 freez(st->config_section);
281
282 - if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE || st->rrd_memory_mode == RRD_MEMORY_MODE_MAP) {
283 - debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
284 - munmap(st, st->memsize);
282 + switch(st->rrd_memory_mode) {
283 + case RRD_MEMORY_MODE_SAVE:
284 + case RRD_MEMORY_MODE_MAP:
285 + case RRD_MEMORY_MODE_RAM:
286 + debug(D_RRD_CALLS, "Unmapping stats '%s'.", st->name);
287 + munmap(st, st->memsize);
288 + break;
289 +
290 + case RRD_MEMORY_MODE_NONE:
291 + freez(st);
292 + break;
293 }
286 - else
287 - freez(st);
294 }
295
296 void rrdset_save(RRDSET *st) {