| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | #pragma once |
| 3 | |
| 4 | #include <mos/types.hpp> |
| 5 | |
| 6 | typedef enum |
| 7 | { |
| 8 | MEM_PAGETABLE, // page table pages |
| 9 | MEM_SLAB, // slab allocator |
| 10 | MEM_PAGECACHE, // page cache |
| 11 | MEM_KERNEL, // kernel memory (e.g. kernel stack) |
| 12 | MEM_USER, // user memory (e.g. user code, data, stack) |
| 13 | |
| 14 | _MEM_MAX_TYPES, |
| 15 | } mmstat_type_t; |
| 16 | |
| 17 | extern const char *mem_type_names[_MEM_MAX_TYPES]; |
| 18 | |
| 19 | /** |
| 20 | * @brief Increment the memory usage statistics. |
| 21 | * |
| 22 | * @param type The type of memory. |
| 23 | * @param size The size of memory. |
| 24 | */ |
| 25 | void mmstat_inc(mmstat_type_t type, size_t size); |
| 26 | #define mmstat_inc1(type) mmstat_inc(type, 1) |
| 27 | |
| 28 | /** |
| 29 | * @brief Decrement the memory usage statistics. |
| 30 | * |
| 31 | * @param type The type of memory. |
| 32 | * @param size The size of memory. |
| 33 | */ |
| 34 | void mmstat_dec(mmstat_type_t type, size_t size); |
| 35 | #define mmstat_dec1(type) mmstat_dec(type, 1) |
| 36 | |
| 37 | /** |
| 38 | * @brief Memory usage statistics for a specific vmap area. |
| 39 | * |
| 40 | * @details Different types of memory: |
| 41 | * The metrics in this struct only describe what is 'mapped' in the vmap area. |
| 42 | * Unmapped pages are not counted. |
| 43 | * |
| 44 | * On a page fault, the page is mapped in and the following happens: |
| 45 | * |
| 46 | * Private File-backed: |
| 47 | * Read pagecache++, cow++, |
| 48 | * Written regular++, |
| 49 | * if the page is already mapped, pagecache--, cow-- (page is not in the page cache) |
| 50 | * Forked cow += regular, regular = 0 (regular pages now becomes cow pages, pagecache ones stay pagecache (read-only)) |
| 51 | * Shared File-backed: |
| 52 | * Read pagecache++, regular++, |
| 53 | * Written if the page wasn't previously mapped, pagecache++, regular++ (a new pagecache page is now mapped) |
| 54 | * Private Anonymous: |
| 55 | * Read cow++, |
| 56 | * zero page is mapped |
| 57 | * Written regular++, cow--, |
| 58 | * Forked cow += regular, regular = 0 (regular pages now becomes cow pages) |
| 59 | * Shared Anonymous: |
| 60 | * NOT IMPLEMENTED (yet) |
| 61 | * |
| 62 | */ |
| 63 | typedef struct |
| 64 | { |
| 65 | size_t regular; ///< regular pages with no special flags being set or unset |
| 66 | size_t pagecache; ///< pages that are in the page cache (file-backed only) |
| 67 | size_t cow; ///< pages that are copy-on-write |
| 68 | } vmap_stat_t; |
| 69 | |
| 70 | #define vmap_stat_inc(vmap, type) (vmap)->stat.type += 1 |
| 71 | #define vmap_stat_dec(vmap, type) (vmap)->stat.type -= 1 |
| 72 | |