|
| 1 | +// Copyright 2024-Present Couchbase, Inc. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License included |
| 4 | +// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified |
| 5 | +// in that file, in accordance with the Business Source License, use of this |
| 6 | +// software will be governed by the Apache License, Version 2.0, included in |
| 7 | +// the file licenses/APL2.txt. |
| 8 | + |
| 9 | +#if defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__) |
| 10 | + #include <cstdio> |
| 11 | + #include <cstdlib> |
| 12 | + #include <cstring> |
| 13 | + #include "c_heap_mem_usage.h" |
| 14 | + #include <malloc.h> |
| 15 | + |
| 16 | +size_t get_attribute_value(const char *str, const char *substr) { |
| 17 | + const char *pos = strstr(str, substr); |
| 18 | + if (pos == NULL) { |
| 19 | + return 0; |
| 20 | + } |
| 21 | + pos += strlen(substr); |
| 22 | + |
| 23 | + char* next_pos = NULL; |
| 24 | + size_t value = strtoull(pos, &next_pos, 10); |
| 25 | + if (next_pos == NULL || next_pos == pos) { |
| 26 | + return 0; |
| 27 | + } |
| 28 | + |
| 29 | + return value; |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +size_t get_attribute_value(const char *str, const char *first_substr, const char* second_substr) { |
| 34 | + const char *pos = strstr(str, first_substr); |
| 35 | + if (pos == NULL) { |
| 36 | + return 0; |
| 37 | + } |
| 38 | + pos += strlen(first_substr); |
| 39 | + pos = strstr(pos, second_substr); |
| 40 | + if (pos == NULL) { |
| 41 | + return 0; |
| 42 | + } |
| 43 | + pos += strlen(second_substr); |
| 44 | + |
| 45 | + char* next_pos = NULL; |
| 46 | + size_t value = strtoull(pos, &next_pos, 10); |
| 47 | + if (next_pos == NULL || next_pos == pos) { |
| 48 | + return 0; |
| 49 | + } |
| 50 | + |
| 51 | + return value; |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +size_t get_total_heap_bytes(){ |
| 56 | + // ref: https://gist.github.com/tadeu/95013963c64da4cd74a2c6f4fa4fd553 |
| 57 | + // There are three functions in Linux libc API to retrieve heap |
| 58 | + // information: `malloc_stats`, `mallinfo` and `malloc_info`. |
| 59 | + // The first two are still broken for 64-bit systems and will |
| 60 | + // report wrong values if there are more than 4 Gb of allocated |
| 61 | + // memory. The latter works for more than 4 Gb, but it outputs |
| 62 | + // a XML to a file, so `open_memstream` is used to avoid writing |
| 63 | + // to the disk, and a very simple "parsing" is done here using |
| 64 | + // C-string search. |
| 65 | + // |
| 66 | + // More info: |
| 67 | + // https://stackoverflow.com/questions/40878169/64-bit-capable-alternative-to-mallinfo |
| 68 | + // https://stackoverflow.com/questions/3903807/how-does-malloc-info-work |
| 69 | + // https://stackoverflow.com/questions/34292457/gnu-malloc-info-get-really-allocated-memory |
| 70 | + |
| 71 | + char* buf = NULL; |
| 72 | + size_t buf_size = 0; |
| 73 | + |
| 74 | + FILE* f = open_memstream(&buf, &buf_size); |
| 75 | + // this doesn't include the golang or the other process's stats |
| 76 | + // as per local testing |
| 77 | + malloc_info(0, f); |
| 78 | + fclose(f); |
| 79 | + |
| 80 | + // We are only interested in totals, so we skip everything until the |
| 81 | + // closing of the <heap>...</heap> block. |
| 82 | + const char* pos = strstr(buf, "</heap>"); |
| 83 | + |
| 84 | + // rest and fast are blocks that have been freed, we should subtract them |
| 85 | + size_t rest = get_attribute_value(pos, "<total type=\"rest\" count=\"", "\" size=\""); |
| 86 | + size_t fast = get_attribute_value(pos, "<total type=\"fast\" count=\"", "\" size=\""); |
| 87 | + |
| 88 | + // mmap and current are totals (mmap is used for very large blocks) |
| 89 | + size_t mmap = get_attribute_value(pos, "<total type=\"mmap\" count=\"", "\" size=\""); |
| 90 | + size_t current = get_attribute_value(pos, "<system type=\"current\" size=\""); |
| 91 | + |
| 92 | + size_t free_mem = rest + fast; |
| 93 | + size_t total_mem = mmap + current; |
| 94 | + |
| 95 | + size_t allocated_mem = total_mem > free_mem ? total_mem - free_mem : 0; |
| 96 | + |
| 97 | + free(buf); |
| 98 | + buf = NULL; |
| 99 | + return allocated_mem; |
| 100 | +} |
| 101 | +#endif |
0 commit comments