|
2 | 2 | #include "mem/frame.h" |
3 | 3 | #include "mem/memstat.h" |
4 | 4 | #include "string_builder.h" |
| 5 | +#include "term/klog.h" |
5 | 6 |
|
6 | | -#define MEMINFO_TODO 0ULL // Things to do in the future ;) |
| 7 | +static bool meminfo_append_kb(string_builder_t *builder, const char *key, uint64_t value_kb) { |
| 8 | + return string_builder_append(builder, "%s:%8llu kB\n", key, value_kb); |
| 9 | +} |
7 | 10 |
|
8 | | -char *meminfo_origin[] = {"MemTotal:\t\t%llu kB\n", "MemFree:\t\t%llu kB\n", |
9 | | - "MemAvailable:\t%llu kB\n", "Buffers:\t\t%llu kB\n", |
10 | | - "Cached:\t\t%llu kB\n", "SwapCached:\t\t%llu kB\n", |
11 | | - "Active:\t\t%llu kB\n", "Inactive:\t\t%llu kB\n", |
12 | | - "Active(anon):\t%llu kB\n", "Inactive(anon):\t%llu kB\n", |
13 | | - "Active(file):\t%llu kB\n", "Inactive(file):\t%llu kB\n", |
14 | | - "Unevictable:\t\t%llu kB\n", "Mlocked:\t\t%llu kB\n", |
15 | | - "SwapTotal:\t\t%llu kB\n", "SwapFree:\t\t%llu kB\n", |
16 | | - "Zswap:\t\t%llu kB\n", "Zswapped:\t\t%llu kB\n", |
17 | | - "Dirty:\t\t%llu kB\n", "Writeback:\t\t%llu kB\n", |
18 | | - "AnonPages: \t\t%llu kB\n", "Mapped:\t\t%llu kB\n", |
19 | | - "Shmem:\t\t%llu kB\n", "KReclaimable:\t%llu kB\n", |
20 | | - "Slab:\t\t%llu kB\n", "SReclaimable:\t\t%llu kB\n", |
21 | | - "SUnreclaim:\t\t%llu kB\n", "KernelStack:\t\t%llu kB\n", |
22 | | - "PageTables:\t\t%llu kB\n", "SecPageTables:\t\t%llu kB\n", |
23 | | - "NFS_Unstable:\t\t%llu kB\n", "Bounce:\t\t%llu kB\n", |
24 | | - "WritebackTmp:\t\t%llu kB\n", "CommitLimit:\t%llu kB\n", |
25 | | - "Committed_AS:\t%llu kB\n", "VmallocTotal:\t%llu kB\n", |
26 | | - "VmallocUsed:\t\t%llu kB\n", "VmallocChunk:\t\t%llu kB\n", |
27 | | - "Percpu:\t\t%llu kB\n", "HardwareCorrupted:\t%llu kB\n", |
28 | | - "AnonHugePages:\t%llu kB\n", "ShmemHugePages:\t\t%llu kB\n", |
29 | | - "ShmemPmdMapped:\t\t%llu kB\n", "FileHugePages:\t\t%llu kB\n", |
30 | | - "FilePmdMapped:\t\t%llu kB\n", "Unaccepted:\t\t%llu kB\n", |
31 | | - "HugePages_Total:\t\t%llu\n", "HugePages_Free:\t\t%llu\n", |
32 | | - "HugePages_Rsvd:\t\t%llu\n", "HugePages_Surp:\t\t%llu\n", |
33 | | - "Hugepagesize:\t%llu kB\n", "Hugetlb:\t\t%llu kB\n", |
34 | | - "DirectMap4k:\t%llu kB\n", "DirectMap2M:\t%llu kB\n", |
35 | | - "DirectMap1G:\t%llu kB\n"}; |
| 11 | +static bool meminfo_append_raw(string_builder_t *builder, const char *key, uint64_t value) { |
| 12 | + return string_builder_append(builder, "%s:%8llu\n", key, value); |
| 13 | +} |
36 | 14 |
|
37 | 15 | char *proc_gen_meminfo(size_t *context_len) { |
38 | 16 | string_builder_t *builder = create_string_builder(4096); |
39 | 17 | if (unlikely(builder == NULL)) return NULL; |
40 | 18 |
|
41 | | - bool status = false; |
42 | | - for (size_t i = 0; i < 55; i++) { |
43 | | - status &= string_builder_append(builder,meminfo_origin[i],MEMINFO_TODO); |
44 | | - } |
| 19 | + const uint64_t mem_total_kb = get_origin_frames() * 4; |
| 20 | + const uint64_t mem_free_kb = get_usable_frames() * 4; |
| 21 | + const uint64_t mem_available_kb = mem_free_kb; |
| 22 | + const uint64_t mem_used_kb = mem_total_kb - mem_free_kb; |
| 23 | + const uint64_t bad_kb = get_bad_memory() / 1024; |
45 | 24 |
|
46 | | -// int length = sprintf(result, meminfo_origin, |
47 | | -// get_memory_size() / 1024, // MemTotal |
48 | | -// (get_memory_size() - get_used_memory()) / 1024, // MemFree |
49 | | -// get_available_memory() / 1024, // MemAvailable |
50 | | -// MEMINFO_TODO, // Buffers |
51 | | -// MEMINFO_TODO, // Cached |
52 | | -// MEMINFO_TODO, // SwapCached |
53 | | -// MEMINFO_TODO, // Active |
54 | | -// MEMINFO_TODO, // Inactive |
55 | | -// MEMINFO_TODO, // Active(anon) |
56 | | -// MEMINFO_TODO, // Inactive(anon) |
57 | | -// MEMINFO_TODO, // Active(file) |
58 | | -// MEMINFO_TODO, // Inactive(file) |
59 | | -// MEMINFO_TODO, // Unevictable |
60 | | -// MEMINFO_TODO, // Mlocked |
61 | | -// MEMINFO_TODO, // SwapTotal |
62 | | -// MEMINFO_TODO, // SwapFree |
63 | | -// MEMINFO_TODO, // Zswap |
64 | | -// MEMINFO_TODO, // Zswapped |
65 | | -// MEMINFO_TODO, // Dirty |
66 | | -// MEMINFO_TODO, // Writeback |
67 | | -// MEMINFO_TODO, // AnonPages |
68 | | -// MEMINFO_TODO, // Mapped |
69 | | -// MEMINFO_TODO, // Shmem |
70 | | -// MEMINFO_TODO, // KReclaimable |
71 | | -// MEMINFO_TODO, // Slab |
72 | | -// MEMINFO_TODO, // SReclaimable |
73 | | -// MEMINFO_TODO, // SUnreclaim |
74 | | -// MAX_STACK_SIZE, // KernelStack |
75 | | -// MEMINFO_TODO, // PageTables |
76 | | -// MEMINFO_TODO, // SecPageTables |
77 | | -// MEMINFO_TODO, // NFS_Unstable |
78 | | -// MEMINFO_TODO, // Bounce |
79 | | -// MEMINFO_TODO, // WritebackTmp |
80 | | -// MEMINFO_TODO, // CommitLimit |
81 | | -// MEMINFO_TODO, // Committed_AS |
82 | | -// MEMINFO_TODO, // VmallocTotal |
83 | | -// MEMINFO_TODO, // VmallocUsed |
84 | | -// MEMINFO_TODO, // VmallocChunk |
85 | | -// MEMINFO_TODO, // Percpu |
86 | | -// MEMINFO_TODO, // HardwareCorrupted |
87 | | -// MEMINFO_TODO, // AnonHugePages |
88 | | -// MEMINFO_TODO, // ShmemHugePages |
89 | | -// MEMINFO_TODO, // ShmemPmdMapped |
90 | | -// MEMINFO_TODO, // FileHugePages |
91 | | -// MEMINFO_TODO, // FilePmdMapped |
92 | | -// MEMINFO_TODO, // Unaccepted |
93 | | -// MEMINFO_TODO, // HugePages_Total |
94 | | -// MEMINFO_TODO, // HugePages_Free |
95 | | -// MEMINFO_TODO, // HugePages_Rsvd |
96 | | -// MEMINFO_TODO, // HugePages_Surp |
97 | | -// MEMINFO_TODO, // Hugepagesize |
98 | | -// MEMINFO_TODO, // Hugetlb |
99 | | -// MEMINFO_TODO, // DirectMap4k |
100 | | -// MEMINFO_TODO, // DirectMap2M |
101 | | -// MEMINFO_TODO // DirectMap1G |
102 | | -// ); |
| 25 | + logkf("proc_meminfo: %llu %llu %llu\n\r", mem_total_kb, mem_free_kb, mem_used_kb); |
| 26 | + |
| 27 | + const uint64_t zero_kb = 0; |
| 28 | + const uint64_t swap_total_kb = 0; |
| 29 | + const uint64_t swap_free_kb = 0; |
| 30 | + const uint64_t commit_limit_kb = mem_total_kb + swap_total_kb; |
| 31 | + const uint64_t committed_as_kb = mem_used_kb; |
| 32 | + |
| 33 | + if (!meminfo_append_kb(builder, "MemTotal", mem_total_kb)) goto err; |
| 34 | + if (!meminfo_append_kb(builder, "MemFree", mem_free_kb)) goto err; |
| 35 | + if (!meminfo_append_kb(builder, "MemAvailable", mem_available_kb)) goto err; |
| 36 | + if (!meminfo_append_kb(builder, "Buffers", zero_kb)) goto err; |
| 37 | + if (!meminfo_append_kb(builder, "Cached", zero_kb)) goto err; |
| 38 | + if (!meminfo_append_kb(builder, "SwapCached", zero_kb)) goto err; |
| 39 | + if (!meminfo_append_kb(builder, "Active", zero_kb)) goto err; |
| 40 | + if (!meminfo_append_kb(builder, "Inactive", zero_kb)) goto err; |
| 41 | + if (!meminfo_append_kb(builder, "Active(anon)", zero_kb)) goto err; |
| 42 | + if (!meminfo_append_kb(builder, "Inactive(anon)", zero_kb)) goto err; |
| 43 | + if (!meminfo_append_kb(builder, "Active(file)", zero_kb)) goto err; |
| 44 | + if (!meminfo_append_kb(builder, "Inactive(file)", zero_kb)) goto err; |
| 45 | + if (!meminfo_append_kb(builder, "Unevictable", zero_kb)) goto err; |
| 46 | + if (!meminfo_append_kb(builder, "Mlocked", zero_kb)) goto err; |
| 47 | + if (!meminfo_append_kb(builder, "SwapTotal", swap_total_kb)) goto err; |
| 48 | + if (!meminfo_append_kb(builder, "SwapFree", swap_free_kb)) goto err; |
| 49 | + if (!meminfo_append_kb(builder, "Dirty", zero_kb)) goto err; |
| 50 | + if (!meminfo_append_kb(builder, "Writeback", zero_kb)) goto err; |
| 51 | + if (!meminfo_append_kb(builder, "AnonPages", zero_kb)) goto err; |
| 52 | + if (!meminfo_append_kb(builder, "Mapped", zero_kb)) goto err; |
| 53 | + if (!meminfo_append_kb(builder, "Shmem", zero_kb)) goto err; |
| 54 | + if (!meminfo_append_kb(builder, "KReclaimable", zero_kb)) goto err; |
| 55 | + if (!meminfo_append_kb(builder, "Slab", zero_kb)) goto err; |
| 56 | + if (!meminfo_append_kb(builder, "SReclaimable", zero_kb)) goto err; |
| 57 | + if (!meminfo_append_kb(builder, "SUnreclaim", zero_kb)) goto err; |
| 58 | + if (!meminfo_append_kb(builder, "KernelStack", STACK_SIZE)) goto err; |
| 59 | + if (!meminfo_append_kb(builder, "PageTables", zero_kb)) goto err; |
| 60 | + if (!meminfo_append_kb(builder, "NFS_Unstable", zero_kb)) goto err; |
| 61 | + if (!meminfo_append_kb(builder, "Bounce", zero_kb)) goto err; |
| 62 | + if (!meminfo_append_kb(builder, "WritebackTmp", zero_kb)) goto err; |
| 63 | + if (!meminfo_append_kb(builder, "CommitLimit", commit_limit_kb)) goto err; |
| 64 | + if (!meminfo_append_kb(builder, "Committed_AS", committed_as_kb)) goto err; |
| 65 | + if (!meminfo_append_kb(builder, "VmallocTotal", zero_kb)) goto err; |
| 66 | + if (!meminfo_append_kb(builder, "VmallocUsed", zero_kb)) goto err; |
| 67 | + if (!meminfo_append_kb(builder, "VmallocChunk", zero_kb)) goto err; |
| 68 | + if (!meminfo_append_kb(builder, "Percpu", zero_kb)) goto err; |
| 69 | + if (!meminfo_append_kb(builder, "HardwareCorrupted", bad_kb)) goto err; |
| 70 | + if (!meminfo_append_kb(builder, "AnonHugePages", zero_kb)) goto err; |
| 71 | + if (!meminfo_append_kb(builder, "ShmemHugePages", zero_kb)) goto err; |
| 72 | + if (!meminfo_append_kb(builder, "ShmemPmdMapped", zero_kb)) goto err; |
| 73 | + if (!meminfo_append_kb(builder, "FileHugePages", zero_kb)) goto err; |
| 74 | + if (!meminfo_append_kb(builder, "FilePmdMapped", zero_kb)) goto err; |
| 75 | + if (!meminfo_append_kb(builder, "Unaccepted", zero_kb)) goto err; |
| 76 | + if (!meminfo_append_raw(builder, "HugePages_Total", 0)) goto err; |
| 77 | + if (!meminfo_append_raw(builder, "HugePages_Free", 0)) goto err; |
| 78 | + if (!meminfo_append_raw(builder, "HugePages_Rsvd", 0)) goto err; |
| 79 | + if (!meminfo_append_raw(builder, "HugePages_Surp", 0)) goto err; |
| 80 | + if (!meminfo_append_kb(builder, "Hugepagesize", 0)) goto err; |
| 81 | + if (!meminfo_append_kb(builder, "Hugetlb", 0)) goto err; |
| 82 | + if (!meminfo_append_kb(builder, "DirectMap4k", 0)) goto err; |
| 83 | + if (!meminfo_append_kb(builder, "DirectMap2M", 0)) goto err; |
| 84 | + if (!meminfo_append_kb(builder, "DirectMap1G", 0)) goto err; |
103 | 85 |
|
104 | 86 | *context_len = builder->size; |
105 | 87 | char *data = builder->data; |
106 | 88 | free(builder); |
107 | 89 | return data; |
| 90 | +err: |
| 91 | + free(builder->data); |
| 92 | + free(builder); |
| 93 | + return NULL; |
108 | 94 | } |
109 | 95 |
|
110 | 96 | size_t proc_meminfo_stat(proc_handle_t *handle) { |
@@ -134,6 +120,6 @@ size_t proc_meminfo_read(proc_handle_t *handle, void *addr, size_t offset, size_ |
134 | 120 | memcpy(addr, content + offset, to_copy); |
135 | 121 | free(content); |
136 | 122 |
|
137 | | - ((char *)addr)[to_copy] = '\0'; |
| 123 | + if(to_copy < size) ((char *)addr)[to_copy] = '\0'; |
138 | 124 | return to_copy; |
139 | 125 | } |
0 commit comments