1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552 |
- // SPDX-License-Identifier: GPL-3.0-or-later
- #include "libnetdata.h"
- #ifdef __APPLE__
- #define INHERIT_NONE 0
- #endif /* __APPLE__ */
- #if defined(__FreeBSD__) || defined(__APPLE__)
- # define O_NOATIME 0
- # define MADV_DONTFORK INHERIT_NONE
- #endif /* __FreeBSD__ || __APPLE__*/
- struct rlimit rlimit_nofile = { .rlim_cur = 1024, .rlim_max = 1024 };
- #ifdef MADV_MERGEABLE
- int enable_ksm = 1;
- #else
- int enable_ksm = 0;
- #endif
- volatile sig_atomic_t netdata_exit = 0;
- const char *program_version = VERSION;
- // ----------------------------------------------------------------------------
- // memory allocation functions that handle failures
- // although netdata does not use memory allocations too often (netdata tries to
- // maintain its memory footprint stable during runtime, i.e. all buffers are
- // allocated during initialization and are adapted to current use throughout
- // its lifetime), these can be used to override the default system allocation
- // routines.
- #ifdef NETDATA_LOG_ALLOCATIONS
- #warning NETDATA_LOG_ALLOCATIONS ENABLED - set log_thread_memory_allocations=1 on any thread to log all its allocations - or use log_allocations() to log them on demand
- static __thread struct memory_statistics {
- volatile ssize_t malloc_calls_made;
- volatile ssize_t calloc_calls_made;
- volatile ssize_t realloc_calls_made;
- volatile ssize_t strdup_calls_made;
- volatile ssize_t free_calls_made;
- volatile ssize_t memory_calls_made;
- volatile ssize_t allocated_memory;
- volatile ssize_t mmapped_memory;
- } memory_statistics = { 0, 0, 0, 0, 0, 0, 0, 0 };
- __thread size_t log_thread_memory_allocations = 0;
- inline void log_allocations_int(const char *file, const char *function, const unsigned long line) {
- static __thread struct memory_statistics old = { 0, 0, 0, 0, 0, 0, 0, 0 };
- fprintf(stderr, "%s MEMORY ALLOCATIONS: (%04lu@%s:%s): Allocated %zd KiB (%+zd B), mmapped %zd KiB (%+zd B): : malloc %zd (%+zd), calloc %zd (%+zd), realloc %zd (%+zd), strdup %zd (%+zd), free %zd (%+zd)\n",
- netdata_thread_tag(),
- line, file, function,
- (memory_statistics.allocated_memory + 512) / 1024, memory_statistics.allocated_memory - old.allocated_memory,
- (memory_statistics.mmapped_memory + 512) / 1024, memory_statistics.mmapped_memory - old.mmapped_memory,
- memory_statistics.malloc_calls_made, memory_statistics.malloc_calls_made - old.malloc_calls_made,
- memory_statistics.calloc_calls_made, memory_statistics.calloc_calls_made - old.calloc_calls_made,
- memory_statistics.realloc_calls_made, memory_statistics.realloc_calls_made - old.realloc_calls_made,
- memory_statistics.strdup_calls_made, memory_statistics.strdup_calls_made - old.strdup_calls_made,
- memory_statistics.free_calls_made, memory_statistics.free_calls_made - old.free_calls_made
- );
- memcpy(&old, &memory_statistics, sizeof(struct memory_statistics));
- }
- static inline void mmap_accounting(size_t size) {
- if(log_thread_memory_allocations) {
- memory_statistics.memory_calls_made++;
- memory_statistics.mmapped_memory += size;
- }
- }
- void *mallocz_int(const char *file, const char *function, const unsigned long line, size_t size) {
- memory_statistics.memory_calls_made++;
- memory_statistics.malloc_calls_made++;
- memory_statistics.allocated_memory += size;
- if(log_thread_memory_allocations)
- log_allocations_int(file, function, line);
- size_t *n = (size_t *)malloc(sizeof(size_t) + size);
- if (unlikely(!n)) fatal("mallocz() cannot allocate %zu bytes of memory.", size);
- *n = size;
- return (void *)&n[1];
- }
- void *callocz_int(const char *file, const char *function, const unsigned long line, size_t nmemb, size_t size) {
- size = nmemb * size;
- memory_statistics.memory_calls_made++;
- memory_statistics.calloc_calls_made++;
- memory_statistics.allocated_memory += size;
- if(log_thread_memory_allocations)
- log_allocations_int(file, function, line);
- size_t *n = (size_t *)calloc(1, sizeof(size_t) + size);
- if (unlikely(!n)) fatal("callocz() cannot allocate %zu bytes of memory.", size);
- *n = size;
- return (void *)&n[1];
- }
- void *reallocz_int(const char *file, const char *function, const unsigned long line, void *ptr, size_t size) {
- if(!ptr) return mallocz_int(file, function, line, size);
- size_t *n = (size_t *)ptr;
- n--;
- size_t old_size = *n;
- n = realloc(n, sizeof(size_t) + size);
- if (unlikely(!n)) fatal("reallocz() cannot allocate %zu bytes of memory (from %zu bytes).", size, old_size);
- memory_statistics.memory_calls_made++;
- memory_statistics.realloc_calls_made++;
- memory_statistics.allocated_memory += (size - old_size);
- if(log_thread_memory_allocations)
- log_allocations_int(file, function, line);
- *n = size;
- return (void *)&n[1];
- }
- char *strdupz_int(const char *file, const char *function, const unsigned long line, const char *s) {
- size_t size = strlen(s) + 1;
- memory_statistics.memory_calls_made++;
- memory_statistics.strdup_calls_made++;
- memory_statistics.allocated_memory += size;
- if(log_thread_memory_allocations)
- log_allocations_int(file, function, line);
- size_t *n = (size_t *)malloc(sizeof(size_t) + size);
- if (unlikely(!n)) fatal("strdupz() cannot allocate %zu bytes of memory.", size);
- *n = size;
- char *t = (char *)&n[1];
- strcpy(t, s);
- return t;
- }
- void freez_int(const char *file, const char *function, const unsigned long line, void *ptr) {
- if(unlikely(!ptr)) return;
- size_t *n = (size_t *)ptr;
- n--;
- size_t size = *n;
- memory_statistics.memory_calls_made++;
- memory_statistics.free_calls_made++;
- memory_statistics.allocated_memory -= size;
- if(log_thread_memory_allocations)
- log_allocations_int(file, function, line);
- free(n);
- }
- #else
- char *strdupz(const char *s) {
- char *t = strdup(s);
- if (unlikely(!t)) fatal("Cannot strdup() string '%s'", s);
- return t;
- }
- // If ptr is NULL, no operation is performed.
- void freez(void *ptr) {
- free(ptr);
- }
- void *mallocz(size_t size) {
- void *p = malloc(size);
- if (unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", size);
- return p;
- }
- void *callocz(size_t nmemb, size_t size) {
- void *p = calloc(nmemb, size);
- if (unlikely(!p)) fatal("Cannot allocate %zu bytes of memory.", nmemb * size);
- return p;
- }
- void *reallocz(void *ptr, size_t size) {
- void *p = realloc(ptr, size);
- if (unlikely(!p)) fatal("Cannot re-allocate memory to %zu bytes.", size);
- return p;
- }
- #endif
- // --------------------------------------------------------------------------------------------------------------------
- void json_escape_string(char *dst, const char *src, size_t size) {
- const char *t;
- char *d = dst, *e = &dst[size - 1];
- for(t = src; *t && d < e ;t++) {
- if(unlikely(*t == '\\' || *t == '"')) {
- if(unlikely(d + 1 >= e)) break;
- *d++ = '\\';
- }
- *d++ = *t;
- }
- *d = '\0';
- }
- void json_fix_string(char *s) {
- unsigned char c;
- while((c = (unsigned char)*s)) {
- if(unlikely(c == '\\'))
- *s++ = '/';
- else if(unlikely(c == '"'))
- *s++ = '\'';
- else if(unlikely(isspace(c) || iscntrl(c)))
- *s++ = ' ';
- else if(unlikely(!isprint(c) || c > 127))
- *s++ = '_';
- else
- s++;
- }
- }
- unsigned char netdata_map_chart_names[256] = {
- [0] = '\0', //
- [1] = '_', //
- [2] = '_', //
- [3] = '_', //
- [4] = '_', //
- [5] = '_', //
- [6] = '_', //
- [7] = '_', //
- [8] = '_', //
- [9] = '_', //
- [10] = '_', //
- [11] = '_', //
- [12] = '_', //
- [13] = '_', //
- [14] = '_', //
- [15] = '_', //
- [16] = '_', //
- [17] = '_', //
- [18] = '_', //
- [19] = '_', //
- [20] = '_', //
- [21] = '_', //
- [22] = '_', //
- [23] = '_', //
- [24] = '_', //
- [25] = '_', //
- [26] = '_', //
- [27] = '_', //
- [28] = '_', //
- [29] = '_', //
- [30] = '_', //
- [31] = '_', //
- [32] = '_', //
- [33] = '_', // !
- [34] = '_', // "
- [35] = '_', // #
- [36] = '_', // $
- [37] = '_', // %
- [38] = '_', // &
- [39] = '_', // '
- [40] = '_', // (
- [41] = '_', // )
- [42] = '_', // *
- [43] = '_', // +
- [44] = '.', // ,
- [45] = '-', // -
- [46] = '.', // .
- [47] = '/', // /
- [48] = '0', // 0
- [49] = '1', // 1
- [50] = '2', // 2
- [51] = '3', // 3
- [52] = '4', // 4
- [53] = '5', // 5
- [54] = '6', // 6
- [55] = '7', // 7
- [56] = '8', // 8
- [57] = '9', // 9
- [58] = '_', // :
- [59] = '_', // ;
- [60] = '_', // <
- [61] = '_', // =
- [62] = '_', // >
- [63] = '_', // ?
- [64] = '_', // @
- [65] = 'a', // A
- [66] = 'b', // B
- [67] = 'c', // C
- [68] = 'd', // D
- [69] = 'e', // E
- [70] = 'f', // F
- [71] = 'g', // G
- [72] = 'h', // H
- [73] = 'i', // I
- [74] = 'j', // J
- [75] = 'k', // K
- [76] = 'l', // L
- [77] = 'm', // M
- [78] = 'n', // N
- [79] = 'o', // O
- [80] = 'p', // P
- [81] = 'q', // Q
- [82] = 'r', // R
- [83] = 's', // S
- [84] = 't', // T
- [85] = 'u', // U
- [86] = 'v', // V
- [87] = 'w', // W
- [88] = 'x', // X
- [89] = 'y', // Y
- [90] = 'z', // Z
- [91] = '_', // [
- [92] = '/', // backslash
- [93] = '_', // ]
- [94] = '_', // ^
- [95] = '_', // _
- [96] = '_', // `
- [97] = 'a', // a
- [98] = 'b', // b
- [99] = 'c', // c
- [100] = 'd', // d
- [101] = 'e', // e
- [102] = 'f', // f
- [103] = 'g', // g
- [104] = 'h', // h
- [105] = 'i', // i
- [106] = 'j', // j
- [107] = 'k', // k
- [108] = 'l', // l
- [109] = 'm', // m
- [110] = 'n', // n
- [111] = 'o', // o
- [112] = 'p', // p
- [113] = 'q', // q
- [114] = 'r', // r
- [115] = 's', // s
- [116] = 't', // t
- [117] = 'u', // u
- [118] = 'v', // v
- [119] = 'w', // w
- [120] = 'x', // x
- [121] = 'y', // y
- [122] = 'z', // z
- [123] = '_', // {
- [124] = '_', // |
- [125] = '_', // }
- [126] = '_', // ~
- [127] = '_', //
- [128] = '_', //
- [129] = '_', //
- [130] = '_', //
- [131] = '_', //
- [132] = '_', //
- [133] = '_', //
- [134] = '_', //
- [135] = '_', //
- [136] = '_', //
- [137] = '_', //
- [138] = '_', //
- [139] = '_', //
- [140] = '_', //
- [141] = '_', //
- [142] = '_', //
- [143] = '_', //
- [144] = '_', //
- [145] = '_', //
- [146] = '_', //
- [147] = '_', //
- [148] = '_', //
- [149] = '_', //
- [150] = '_', //
- [151] = '_', //
- [152] = '_', //
- [153] = '_', //
- [154] = '_', //
- [155] = '_', //
- [156] = '_', //
- [157] = '_', //
- [158] = '_', //
- [159] = '_', //
- [160] = '_', //
- [161] = '_', //
- [162] = '_', //
- [163] = '_', //
- [164] = '_', //
- [165] = '_', //
- [166] = '_', //
- [167] = '_', //
- [168] = '_', //
- [169] = '_', //
- [170] = '_', //
- [171] = '_', //
- [172] = '_', //
- [173] = '_', //
- [174] = '_', //
- [175] = '_', //
- [176] = '_', //
- [177] = '_', //
- [178] = '_', //
- [179] = '_', //
- [180] = '_', //
- [181] = '_', //
- [182] = '_', //
- [183] = '_', //
- [184] = '_', //
- [185] = '_', //
- [186] = '_', //
- [187] = '_', //
- [188] = '_', //
- [189] = '_', //
- [190] = '_', //
- [191] = '_', //
- [192] = '_', //
- [193] = '_', //
- [194] = '_', //
- [195] = '_', //
- [196] = '_', //
- [197] = '_', //
- [198] = '_', //
- [199] = '_', //
- [200] = '_', //
- [201] = '_', //
- [202] = '_', //
- [203] = '_', //
- [204] = '_', //
- [205] = '_', //
- [206] = '_', //
- [207] = '_', //
- [208] = '_', //
- [209] = '_', //
- [210] = '_', //
- [211] = '_', //
- [212] = '_', //
- [213] = '_', //
- [214] = '_', //
- [215] = '_', //
- [216] = '_', //
- [217] = '_', //
- [218] = '_', //
- [219] = '_', //
- [220] = '_', //
- [221] = '_', //
- [222] = '_', //
- [223] = '_', //
- [224] = '_', //
- [225] = '_', //
- [226] = '_', //
- [227] = '_', //
- [228] = '_', //
- [229] = '_', //
- [230] = '_', //
- [231] = '_', //
- [232] = '_', //
- [233] = '_', //
- [234] = '_', //
- [235] = '_', //
- [236] = '_', //
- [237] = '_', //
- [238] = '_', //
- [239] = '_', //
- [240] = '_', //
- [241] = '_', //
- [242] = '_', //
- [243] = '_', //
- [244] = '_', //
- [245] = '_', //
- [246] = '_', //
- [247] = '_', //
- [248] = '_', //
- [249] = '_', //
- [250] = '_', //
- [251] = '_', //
- [252] = '_', //
- [253] = '_', //
- [254] = '_', //
- [255] = '_' //
- };
- // make sure the supplied string
- // is good for a netdata chart/dimension ID/NAME
- void netdata_fix_chart_name(char *s) {
- while ((*s = netdata_map_chart_names[(unsigned char) *s])) s++;
- }
- unsigned char netdata_map_chart_ids[256] = {
- [0] = '\0', //
- [1] = '_', //
- [2] = '_', //
- [3] = '_', //
- [4] = '_', //
- [5] = '_', //
- [6] = '_', //
- [7] = '_', //
- [8] = '_', //
- [9] = '_', //
- [10] = '_', //
- [11] = '_', //
- [12] = '_', //
- [13] = '_', //
- [14] = '_', //
- [15] = '_', //
- [16] = '_', //
- [17] = '_', //
- [18] = '_', //
- [19] = '_', //
- [20] = '_', //
- [21] = '_', //
- [22] = '_', //
- [23] = '_', //
- [24] = '_', //
- [25] = '_', //
- [26] = '_', //
- [27] = '_', //
- [28] = '_', //
- [29] = '_', //
- [30] = '_', //
- [31] = '_', //
- [32] = '_', //
- [33] = '_', // !
- [34] = '_', // "
- [35] = '_', // #
- [36] = '_', // $
- [37] = '_', // %
- [38] = '_', // &
- [39] = '_', // '
- [40] = '_', // (
- [41] = '_', // )
- [42] = '_', // *
- [43] = '_', // +
- [44] = '.', // ,
- [45] = '-', // -
- [46] = '.', // .
- [47] = '_', // /
- [48] = '0', // 0
- [49] = '1', // 1
- [50] = '2', // 2
- [51] = '3', // 3
- [52] = '4', // 4
- [53] = '5', // 5
- [54] = '6', // 6
- [55] = '7', // 7
- [56] = '8', // 8
- [57] = '9', // 9
- [58] = '_', // :
- [59] = '_', // ;
- [60] = '_', // <
- [61] = '_', // =
- [62] = '_', // >
- [63] = '_', // ?
- [64] = '_', // @
- [65] = 'a', // A
- [66] = 'b', // B
- [67] = 'c', // C
- [68] = 'd', // D
- [69] = 'e', // E
- [70] = 'f', // F
- [71] = 'g', // G
- [72] = 'h', // H
- [73] = 'i', // I
- [74] = 'j', // J
- [75] = 'k', // K
- [76] = 'l', // L
- [77] = 'm', // M
- [78] = 'n', // N
- [79] = 'o', // O
- [80] = 'p', // P
- [81] = 'q', // Q
- [82] = 'r', // R
- [83] = 's', // S
- [84] = 't', // T
- [85] = 'u', // U
- [86] = 'v', // V
- [87] = 'w', // W
- [88] = 'x', // X
- [89] = 'y', // Y
- [90] = 'z', // Z
- [91] = '_', // [
- [92] = '_', // backslash
- [93] = '_', // ]
- [94] = '_', // ^
- [95] = '_', // _
- [96] = '_', // `
- [97] = 'a', // a
- [98] = 'b', // b
- [99] = 'c', // c
- [100] = 'd', // d
- [101] = 'e', // e
- [102] = 'f', // f
- [103] = 'g', // g
- [104] = 'h', // h
- [105] = 'i', // i
- [106] = 'j', // j
- [107] = 'k', // k
- [108] = 'l', // l
- [109] = 'm', // m
- [110] = 'n', // n
- [111] = 'o', // o
- [112] = 'p', // p
- [113] = 'q', // q
- [114] = 'r', // r
- [115] = 's', // s
- [116] = 't', // t
- [117] = 'u', // u
- [118] = 'v', // v
- [119] = 'w', // w
- [120] = 'x', // x
- [121] = 'y', // y
- [122] = 'z', // z
- [123] = '_', // {
- [124] = '_', // |
- [125] = '_', // }
- [126] = '_', // ~
- [127] = '_', //
- [128] = '_', //
- [129] = '_', //
- [130] = '_', //
- [131] = '_', //
- [132] = '_', //
- [133] = '_', //
- [134] = '_', //
- [135] = '_', //
- [136] = '_', //
- [137] = '_', //
- [138] = '_', //
- [139] = '_', //
- [140] = '_', //
- [141] = '_', //
- [142] = '_', //
- [143] = '_', //
- [144] = '_', //
- [145] = '_', //
- [146] = '_', //
- [147] = '_', //
- [148] = '_', //
- [149] = '_', //
- [150] = '_', //
- [151] = '_', //
- [152] = '_', //
- [153] = '_', //
- [154] = '_', //
- [155] = '_', //
- [156] = '_', //
- [157] = '_', //
- [158] = '_', //
- [159] = '_', //
- [160] = '_', //
- [161] = '_', //
- [162] = '_', //
- [163] = '_', //
- [164] = '_', //
- [165] = '_', //
- [166] = '_', //
- [167] = '_', //
- [168] = '_', //
- [169] = '_', //
- [170] = '_', //
- [171] = '_', //
- [172] = '_', //
- [173] = '_', //
- [174] = '_', //
- [175] = '_', //
- [176] = '_', //
- [177] = '_', //
- [178] = '_', //
- [179] = '_', //
- [180] = '_', //
- [181] = '_', //
- [182] = '_', //
- [183] = '_', //
- [184] = '_', //
- [185] = '_', //
- [186] = '_', //
- [187] = '_', //
- [188] = '_', //
- [189] = '_', //
- [190] = '_', //
- [191] = '_', //
- [192] = '_', //
- [193] = '_', //
- [194] = '_', //
- [195] = '_', //
- [196] = '_', //
- [197] = '_', //
- [198] = '_', //
- [199] = '_', //
- [200] = '_', //
- [201] = '_', //
- [202] = '_', //
- [203] = '_', //
- [204] = '_', //
- [205] = '_', //
- [206] = '_', //
- [207] = '_', //
- [208] = '_', //
- [209] = '_', //
- [210] = '_', //
- [211] = '_', //
- [212] = '_', //
- [213] = '_', //
- [214] = '_', //
- [215] = '_', //
- [216] = '_', //
- [217] = '_', //
- [218] = '_', //
- [219] = '_', //
- [220] = '_', //
- [221] = '_', //
- [222] = '_', //
- [223] = '_', //
- [224] = '_', //
- [225] = '_', //
- [226] = '_', //
- [227] = '_', //
- [228] = '_', //
- [229] = '_', //
- [230] = '_', //
- [231] = '_', //
- [232] = '_', //
- [233] = '_', //
- [234] = '_', //
- [235] = '_', //
- [236] = '_', //
- [237] = '_', //
- [238] = '_', //
- [239] = '_', //
- [240] = '_', //
- [241] = '_', //
- [242] = '_', //
- [243] = '_', //
- [244] = '_', //
- [245] = '_', //
- [246] = '_', //
- [247] = '_', //
- [248] = '_', //
- [249] = '_', //
- [250] = '_', //
- [251] = '_', //
- [252] = '_', //
- [253] = '_', //
- [254] = '_', //
- [255] = '_' //
- };
- // make sure the supplied string
- // is good for a netdata chart/dimension ID/NAME
- void netdata_fix_chart_id(char *s) {
- while ((*s = netdata_map_chart_ids[(unsigned char) *s])) s++;
- }
- /*
- // http://stackoverflow.com/questions/7666509/hash-function-for-string
- uint32_t simple_hash(const char *name)
- {
- const char *s = name;
- uint32_t hash = 5381;
- int i;
- while((i = *s++)) hash = ((hash << 5) + hash) + i;
- // fprintf(stderr, "HASH: %lu %s\n", hash, name);
- return hash;
- }
- */
- /*
- // http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
- uint32_t simple_hash(const char *name) {
- unsigned char *s = (unsigned char *) name;
- uint32_t hval = 0x811c9dc5;
- // FNV-1a algorithm
- while (*s) {
- // multiply by the 32 bit FNV magic prime mod 2^32
- // NOTE: No need to optimize with left shifts.
- // GCC will use imul instruction anyway.
- // Tested with 'gcc -O3 -S'
- //hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
- hval *= 16777619;
- // xor the bottom with the current octet
- hval ^= (uint32_t) *s++;
- }
- // fprintf(stderr, "HASH: %u = %s\n", hval, name);
- return hval;
- }
- uint32_t simple_uhash(const char *name) {
- unsigned char *s = (unsigned char *) name;
- uint32_t hval = 0x811c9dc5, c;
- // FNV-1a algorithm
- while ((c = *s++)) {
- if (unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
- hval *= 16777619;
- hval ^= c;
- }
- return hval;
- }
- */
- /*
- // http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
- // one at a time hash
- uint32_t simple_hash(const char *name) {
- unsigned char *s = (unsigned char *)name;
- uint32_t h = 0;
- while(*s) {
- h += *s++;
- h += (h << 10);
- h ^= (h >> 6);
- }
- h += (h << 3);
- h ^= (h >> 11);
- h += (h << 15);
- // fprintf(stderr, "HASH: %u = %s\n", h, name);
- return h;
- }
- */
- void strreverse(char *begin, char *end) {
- while (end > begin) {
- // clearer code.
- char aux = *end;
- *end-- = *begin;
- *begin++ = aux;
- }
- }
- char *strsep_on_1char(char **ptr, char c) {
- if(unlikely(!ptr || !*ptr))
- return NULL;
- // remember the position we started
- char *s = *ptr;
- // skip separators in front
- while(*s == c) s++;
- char *ret = s;
- // find the next separator
- while(*s++) {
- if(unlikely(*s == c)) {
- *s++ = '\0';
- *ptr = s;
- return ret;
- }
- }
- *ptr = NULL;
- return ret;
- }
- char *mystrsep(char **ptr, char *s) {
- char *p = "";
- while (p && !p[0] && *ptr) p = strsep(ptr, s);
- return (p);
- }
- char *trim(char *s) {
- // skip leading spaces
- while (*s && isspace(*s)) s++;
- if (!*s) return NULL;
- // skip tailing spaces
- // this way is way faster. Writes only one NUL char.
- ssize_t l = strlen(s);
- if (--l >= 0) {
- char *p = s + l;
- while (p > s && isspace(*p)) p--;
- *++p = '\0';
- }
- if (!*s) return NULL;
- return s;
- }
- inline char *trim_all(char *buffer) {
- char *d = buffer, *s = buffer;
- // skip spaces
- while(isspace(*s)) s++;
- while(*s) {
- // copy the non-space part
- while(*s && !isspace(*s)) *d++ = *s++;
- // add a space if we have to
- if(*s && isspace(*s)) {
- *d++ = ' ';
- s++;
- }
- // skip spaces
- while(isspace(*s)) s++;
- }
- *d = '\0';
- if(d > buffer) {
- d--;
- if(isspace(*d)) *d = '\0';
- }
- if(!buffer[0]) return NULL;
- return buffer;
- }
- static int memory_file_open(const char *filename, size_t size) {
- // info("memory_file_open('%s', %zu", filename, size);
- int fd = open(filename, O_RDWR | O_CREAT | O_NOATIME, 0664);
- if (fd != -1) {
- if (lseek(fd, size, SEEK_SET) == (off_t) size) {
- if (write(fd, "", 1) == 1) {
- if (ftruncate(fd, size))
- error("Cannot truncate file '%s' to size %zu. Will use the larger file.", filename, size);
- }
- else error("Cannot write to file '%s' at position %zu.", filename, size);
- }
- else error("Cannot seek file '%s' to size %zu.", filename, size);
- }
- else error("Cannot create/open file '%s'.", filename);
- return fd;
- }
- static inline int madvise_sequential(void *mem, size_t len) {
- static int logger = 1;
- int ret = madvise(mem, len, MADV_SEQUENTIAL);
- if (ret != 0 && logger-- > 0) error("madvise(MADV_SEQUENTIAL) failed.");
- return ret;
- }
- static inline int madvise_dontfork(void *mem, size_t len) {
- static int logger = 1;
- int ret = madvise(mem, len, MADV_DONTFORK);
- if (ret != 0 && logger-- > 0) error("madvise(MADV_DONTFORK) failed.");
- return ret;
- }
- static inline int madvise_willneed(void *mem, size_t len) {
- static int logger = 1;
- int ret = madvise(mem, len, MADV_WILLNEED);
- if (ret != 0 && logger-- > 0) error("madvise(MADV_WILLNEED) failed.");
- return ret;
- }
- #if __linux__
- static inline int madvise_dontdump(void *mem, size_t len) {
- static int logger = 1;
- int ret = madvise(mem, len, MADV_DONTDUMP);
- if (ret != 0 && logger-- > 0) error("madvise(MADV_DONTDUMP) failed.");
- return ret;
- }
- #else
- static inline int madvise_dontdump(void *mem, size_t len) {
- UNUSED(mem);
- UNUSED(len);
- return 0;
- }
- #endif
- static inline int madvise_mergeable(void *mem, size_t len) {
- #ifdef MADV_MERGEABLE
- static int logger = 1;
- int ret = madvise(mem, len, MADV_MERGEABLE);
- if (ret != 0 && logger-- > 0) error("madvise(MADV_MERGEABLE) failed.");
- return ret;
- #else
- UNUSED(mem);
- UNUSED(len);
-
- return 0;
- #endif
- }
- void *netdata_mmap(const char *filename, size_t size, int flags, int ksm) {
- // info("netdata_mmap('%s', %zu", filename, size);
- // MAP_SHARED is used in memory mode map
- // MAP_PRIVATE is used in memory mode ram and save
- if(unlikely(!(flags & MAP_SHARED) && !(flags & MAP_PRIVATE)))
- fatal("Neither MAP_SHARED or MAP_PRIVATE were given to netdata_mmap()");
- if(unlikely((flags & MAP_SHARED) && (flags & MAP_PRIVATE)))
- fatal("Both MAP_SHARED and MAP_PRIVATE were given to netdata_mmap()");
- if(unlikely((flags & MAP_SHARED) && (!filename || !*filename)))
- fatal("MAP_SHARED requested, without a filename to netdata_mmap()");
- // don't enable ksm is the global setting is disabled
- if(unlikely(!enable_ksm)) ksm = 0;
- // KSM only merges anonymous (private) pages, never pagecache (file) pages
- // but MAP_PRIVATE without MAP_ANONYMOUS it fails too, so we need it always
- if((flags & MAP_PRIVATE)) flags |= MAP_ANONYMOUS;
- int fd = -1;
- void *mem = MAP_FAILED;
- if(filename && *filename) {
- // open/create the file to be used
- fd = memory_file_open(filename, size);
- if(fd == -1) goto cleanup;
- }
- int fd_for_mmap = fd;
- if(fd != -1 && (flags & MAP_PRIVATE)) {
- // this is MAP_PRIVATE allocation
- // no need for mmap() to use our fd
- // we will copy the file into the memory allocated
- fd_for_mmap = -1;
- }
- mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, fd_for_mmap, 0);
- if (mem != MAP_FAILED) {
- #ifdef NETDATA_LOG_ALLOCATIONS
- mmap_accounting(size);
- #endif
- // if we have a file open, but we didn't give it to mmap(),
- // we have to read the file into the memory block we allocated
- if(fd != -1 && fd_for_mmap == -1) {
- if (lseek(fd, 0, SEEK_SET) == 0) {
- if (read(fd, mem, size) != (ssize_t) size)
- info("Cannot read from file '%s'", filename);
- }
- else info("Cannot seek to beginning of file '%s'.", filename);
- }
- madvise_sequential(mem, size);
- madvise_dontfork(mem, size);
- madvise_dontdump(mem, size);
- if(flags & MAP_SHARED) madvise_willneed(mem, size);
- if(ksm) madvise_mergeable(mem, size);
- }
- cleanup:
- if(fd != -1) close(fd);
- if(mem == MAP_FAILED) return NULL;
- errno = 0;
- return mem;
- }
- int memory_file_save(const char *filename, void *mem, size_t size) {
- char tmpfilename[FILENAME_MAX + 1];
- snprintfz(tmpfilename, FILENAME_MAX, "%s.%ld.tmp", filename, (long) getpid());
- int fd = open(tmpfilename, O_RDWR | O_CREAT | O_NOATIME, 0664);
- if (fd < 0) {
- error("Cannot create/open file '%s'.", filename);
- return -1;
- }
- if (write(fd, mem, size) != (ssize_t) size) {
- error("Cannot write to file '%s' %ld bytes.", filename, (long) size);
- close(fd);
- return -1;
- }
- close(fd);
- if (rename(tmpfilename, filename)) {
- error("Cannot rename '%s' to '%s'", tmpfilename, filename);
- return -1;
- }
- return 0;
- }
- int fd_is_valid(int fd) {
- return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
- }
- char *fgets_trim_len(char *buf, size_t buf_size, FILE *fp, size_t *len) {
- char *s = fgets(buf, (int)buf_size, fp);
- if (!s) return NULL;
- char *t = s;
- if (*t != '\0') {
- // find the string end
- while (*++t != '\0');
- // trim trailing spaces/newlines/tabs
- while (--t > s && *t == '\n')
- *t = '\0';
- }
- if (len)
- *len = t - s + 1;
- return s;
- }
- int vsnprintfz(char *dst, size_t n, const char *fmt, va_list args) {
- if(unlikely(!n)) return 0;
- int size = vsnprintf(dst, n, fmt, args);
- dst[n - 1] = '\0';
- if (unlikely((size_t) size > n)) size = (int)n;
- return size;
- }
- int snprintfz(char *dst, size_t n, const char *fmt, ...) {
- va_list args;
- va_start(args, fmt);
- int ret = vsnprintfz(dst, n, fmt, args);
- va_end(args);
- return ret;
- }
- /*
- // poor man cycle counting
- static unsigned long tsc;
- void begin_tsc(void) {
- unsigned long a, d;
- asm volatile ("cpuid\nrdtsc" : "=a" (a), "=d" (d) : "0" (0) : "ebx", "ecx");
- tsc = ((unsigned long)d << 32) | (unsigned long)a;
- }
- unsigned long end_tsc(void) {
- unsigned long a, d;
- asm volatile ("rdtscp" : "=a" (a), "=d" (d) : : "ecx");
- return (((unsigned long)d << 32) | (unsigned long)a) - tsc;
- }
- */
- int recursively_delete_dir(const char *path, const char *reason) {
- DIR *dir = opendir(path);
- if(!dir) {
- error("Cannot read %s directory to be deleted '%s'", reason?reason:"", path);
- return -1;
- }
- int ret = 0;
- struct dirent *de = NULL;
- while((de = readdir(dir))) {
- if(de->d_type == DT_DIR
- && (
- (de->d_name[0] == '.' && de->d_name[1] == '\0')
- || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
- ))
- continue;
- char fullpath[FILENAME_MAX + 1];
- snprintfz(fullpath, FILENAME_MAX, "%s/%s", path, de->d_name);
- if(de->d_type == DT_DIR) {
- int r = recursively_delete_dir(fullpath, reason);
- if(r > 0) ret += r;
- continue;
- }
- info("Deleting %s file '%s'", reason?reason:"", fullpath);
- if(unlikely(unlink(fullpath) == -1))
- error("Cannot delete %s file '%s'", reason?reason:"", fullpath);
- else
- ret++;
- }
- info("Deleting empty directory '%s'", path);
- if(unlikely(rmdir(path) == -1))
- error("Cannot delete empty directory '%s'", path);
- else
- ret++;
- closedir(dir);
- return ret;
- }
- static int is_virtual_filesystem(const char *path, char **reason) {
- #if defined(__APPLE__) || defined(__FreeBSD__)
- (void)path;
- (void)reason;
- #else
- struct statfs stat;
- // stat.f_fsid.__val[0] is a file system id
- // stat.f_fsid.__val[1] is the inode
- // so their combination uniquely identifies the file/dir
- if (statfs(path, &stat) == -1) {
- if(reason) *reason = "failed to statfs()";
- return -1;
- }
- if(stat.f_fsid.__val[0] != 0 || stat.f_fsid.__val[1] != 0) {
- errno = EINVAL;
- if(reason) *reason = "is not a virtual file system";
- return -1;
- }
- #endif
- return 0;
- }
- int verify_netdata_host_prefix() {
- if(!netdata_configured_host_prefix)
- netdata_configured_host_prefix = "";
- if(!*netdata_configured_host_prefix)
- return 0;
- char buffer[FILENAME_MAX + 1];
- char *path = netdata_configured_host_prefix;
- char *reason = "unknown reason";
- errno = 0;
- struct stat sb;
- if (stat(path, &sb) == -1) {
- reason = "failed to stat()";
- goto failed;
- }
- if((sb.st_mode & S_IFMT) != S_IFDIR) {
- errno = EINVAL;
- reason = "is not a directory";
- goto failed;
- }
- path = buffer;
- snprintfz(path, FILENAME_MAX, "%s/proc", netdata_configured_host_prefix);
- if(is_virtual_filesystem(path, &reason) == -1)
- goto failed;
- snprintfz(path, FILENAME_MAX, "%s/sys", netdata_configured_host_prefix);
- if(is_virtual_filesystem(path, &reason) == -1)
- goto failed;
- if(netdata_configured_host_prefix && *netdata_configured_host_prefix)
- info("Using host prefix directory '%s'", netdata_configured_host_prefix);
- return 0;
- failed:
- error("Ignoring host prefix '%s': path '%s' %s", netdata_configured_host_prefix, path, reason);
- netdata_configured_host_prefix = "";
- return -1;
- }
- char *strdupz_path_subpath(const char *path, const char *subpath) {
- if(unlikely(!path || !*path)) path = ".";
- if(unlikely(!subpath)) subpath = "";
- // skip trailing slashes in path
- size_t len = strlen(path);
- while(len > 0 && path[len - 1] == '/') len--;
- // skip leading slashes in subpath
- while(subpath[0] == '/') subpath++;
- // if the last character in path is / and (there is a subpath or path is now empty)
- // keep the trailing slash in path and remove the additional slash
- char *slash = "/";
- if(path[len] == '/' && (*subpath || len == 0)) {
- slash = "";
- len++;
- }
- else if(!*subpath) {
- // there is no subpath
- // no need for trailing slash
- slash = "";
- }
- char buffer[FILENAME_MAX + 1];
- snprintfz(buffer, FILENAME_MAX, "%.*s%s%s", (int)len, path, slash, subpath);
- return strdupz(buffer);
- }
- int path_is_dir(const char *path, const char *subpath) {
- char *s = strdupz_path_subpath(path, subpath);
- size_t max_links = 100;
- int is_dir = 0;
- struct stat statbuf;
- while(max_links-- && stat(s, &statbuf) == 0) {
- if((statbuf.st_mode & S_IFMT) == S_IFDIR) {
- is_dir = 1;
- break;
- }
- else if((statbuf.st_mode & S_IFMT) == S_IFLNK) {
- char buffer[FILENAME_MAX + 1];
- ssize_t l = readlink(s, buffer, FILENAME_MAX);
- if(l > 0) {
- buffer[l] = '\0';
- freez(s);
- s = strdupz(buffer);
- continue;
- }
- else {
- is_dir = 0;
- break;
- }
- }
- else {
- is_dir = 0;
- break;
- }
- }
- freez(s);
- return is_dir;
- }
- int path_is_file(const char *path, const char *subpath) {
- char *s = strdupz_path_subpath(path, subpath);
- size_t max_links = 100;
- int is_file = 0;
- struct stat statbuf;
- while(max_links-- && stat(s, &statbuf) == 0) {
- if((statbuf.st_mode & S_IFMT) == S_IFREG) {
- is_file = 1;
- break;
- }
- else if((statbuf.st_mode & S_IFMT) == S_IFLNK) {
- char buffer[FILENAME_MAX + 1];
- ssize_t l = readlink(s, buffer, FILENAME_MAX);
- if(l > 0) {
- buffer[l] = '\0';
- freez(s);
- s = strdupz(buffer);
- continue;
- }
- else {
- is_file = 0;
- break;
- }
- }
- else {
- is_file = 0;
- break;
- }
- }
- freez(s);
- return is_file;
- }
- void recursive_config_double_dir_load(const char *user_path, const char *stock_path, const char *subpath, int (*callback)(const char *filename, void *data), void *data, size_t depth) {
- if(depth > 3) {
- error("CONFIG: Max directory depth reached while reading user path '%s', stock path '%s', subpath '%s'", user_path, stock_path, subpath);
- return;
- }
- char *udir = strdupz_path_subpath(user_path, subpath);
- char *sdir = strdupz_path_subpath(stock_path, subpath);
- debug(D_HEALTH, "CONFIG traversing user-config directory '%s', stock config directory '%s'", udir, sdir);
- DIR *dir = opendir(udir);
- if (!dir) {
- error("CONFIG cannot open user-config directory '%s'.", udir);
- }
- else {
- struct dirent *de = NULL;
- while((de = readdir(dir))) {
- if(de->d_type == DT_DIR || de->d_type == DT_LNK) {
- if( !de->d_name[0] ||
- (de->d_name[0] == '.' && de->d_name[1] == '\0') ||
- (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
- ) {
- debug(D_HEALTH, "CONFIG ignoring user-config directory '%s/%s'", udir, de->d_name);
- continue;
- }
- if(path_is_dir(udir, de->d_name)) {
- recursive_config_double_dir_load(udir, sdir, de->d_name, callback, data, depth + 1);
- continue;
- }
- }
- if(de->d_type == DT_UNKNOWN || de->d_type == DT_REG || de->d_type == DT_LNK) {
- size_t len = strlen(de->d_name);
- if(path_is_file(udir, de->d_name) &&
- len > 5 && !strcmp(&de->d_name[len - 5], ".conf")) {
- char *filename = strdupz_path_subpath(udir, de->d_name);
- debug(D_HEALTH, "CONFIG calling callback for user file '%s'", filename);
- callback(filename, data);
- freez(filename);
- continue;
- }
- }
- debug(D_HEALTH, "CONFIG ignoring user-config file '%s/%s' of type %d", udir, de->d_name, (int)de->d_type);
- }
- closedir(dir);
- }
- debug(D_HEALTH, "CONFIG traversing stock config directory '%s', user config directory '%s'", sdir, udir);
- dir = opendir(sdir);
- if (!dir) {
- error("CONFIG cannot open stock config directory '%s'.", sdir);
- }
- else {
- if (strcmp(udir, sdir)) {
- struct dirent *de = NULL;
- while((de = readdir(dir))) {
- if(de->d_type == DT_DIR || de->d_type == DT_LNK) {
- if( !de->d_name[0] ||
- (de->d_name[0] == '.' && de->d_name[1] == '\0') ||
- (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
- ) {
- debug(D_HEALTH, "CONFIG ignoring stock config directory '%s/%s'", sdir, de->d_name);
- continue;
- }
- if(path_is_dir(sdir, de->d_name)) {
- // we recurse in stock subdirectory, only when there is no corresponding
- // user subdirectory - to avoid reading the files twice
- if(!path_is_dir(udir, de->d_name))
- recursive_config_double_dir_load(udir, sdir, de->d_name, callback, data, depth + 1);
- continue;
- }
- }
- if(de->d_type == DT_UNKNOWN || de->d_type == DT_REG || de->d_type == DT_LNK) {
- size_t len = strlen(de->d_name);
- if(path_is_file(sdir, de->d_name) && !path_is_file(udir, de->d_name) &&
- len > 5 && !strcmp(&de->d_name[len - 5], ".conf")) {
- char *filename = strdupz_path_subpath(sdir, de->d_name);
- debug(D_HEALTH, "CONFIG calling callback for stock file '%s'", filename);
- callback(filename, data);
- freez(filename);
- continue;
- }
- }
- debug(D_HEALTH, "CONFIG ignoring stock-config file '%s/%s' of type %d", udir, de->d_name, (int)de->d_type);
- }
- }
- closedir(dir);
- }
- debug(D_HEALTH, "CONFIG done traversing user-config directory '%s', stock config directory '%s'", udir, sdir);
- freez(udir);
- freez(sdir);
- }
- // Returns the number of bytes read from the file if file_size is not NULL.
- // The actual buffer has an extra byte set to zero (not included in the count).
- char *read_by_filename(char *filename, long *file_size)
- {
- FILE *f = fopen(filename, "r");
- if (!f)
- return NULL;
- if (fseek(f, 0, SEEK_END) < 0) {
- fclose(f);
- return NULL;
- }
- long size = ftell(f);
- if (size <= 0 || fseek(f, 0, SEEK_END) < 0) {
- fclose(f);
- return NULL;
- }
- char *contents = callocz(size + 1, 1);
- if (!contents) {
- fclose(f);
- return NULL;
- }
- if (fseek(f, 0, SEEK_SET) < 0) {
- fclose(f);
- freez(contents);
- return NULL;
- }
- size_t res = fread(contents, 1, size, f);
- if ( res != (size_t)size) {
- freez(contents);
- fclose(f);
- return NULL;
- }
- fclose(f);
- if (file_size)
- *file_size = size;
- return contents;
- }
- char *find_and_replace(const char *src, const char *find, const char *replace, const char *where)
- {
- size_t size = strlen(src) + 1;
- size_t find_len = strlen(find);
- size_t repl_len = strlen(replace);
- char *value, *dst;
- if (likely(where))
- size += (repl_len - find_len);
- value = mallocz(size);
- dst = value;
- if (likely(where)) {
- size_t count = where - src;
- memmove(dst, src, count);
- src += count;
- dst += count;
- memmove(dst, replace, repl_len);
- src += find_len;
- dst += repl_len;
- }
- strcpy(dst, src);
- return value;
- }
- bool bitmap256_get_bit(BITMAP256 *ptr, uint8_t idx) {
- if (unlikely(!ptr))
- return false;
- return (ptr->data[idx / 64] & (1ULL << (idx % 64)));
- }
- void bitmap256_set_bit(BITMAP256 *ptr, uint8_t idx, bool value) {
- if (unlikely(!ptr))
- return;
- if (likely(value))
- ptr->data[idx / 64] |= (1ULL << (idx % 64));
- else
- ptr->data[idx / 64] &= ~(1ULL << (idx % 64));
- }
|