Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <meminfo/sysmeminfo.h> |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <string> |
| 24 | |
| 25 | #include <android-base/file.h> |
| 26 | #include <android-base/logging.h> |
| 27 | #include <android-base/test_utils.h> |
| 28 | |
| 29 | #include <benchmark/benchmark.h> |
| 30 | |
| 31 | enum { |
| 32 | MEMINFO_TOTAL, |
| 33 | MEMINFO_FREE, |
| 34 | MEMINFO_BUFFERS, |
| 35 | MEMINFO_CACHED, |
| 36 | MEMINFO_SHMEM, |
| 37 | MEMINFO_SLAB, |
| 38 | MEMINFO_SLAB_RECLAIMABLE, |
| 39 | MEMINFO_SLAB_UNRECLAIMABLE, |
| 40 | MEMINFO_SWAP_TOTAL, |
| 41 | MEMINFO_SWAP_FREE, |
| 42 | MEMINFO_ZRAM_TOTAL, |
| 43 | MEMINFO_MAPPED, |
| 44 | MEMINFO_VMALLOC_USED, |
| 45 | MEMINFO_PAGE_TABLES, |
| 46 | MEMINFO_KERNEL_STACK, |
| 47 | MEMINFO_COUNT |
| 48 | }; |
| 49 | |
| 50 | void get_mem_info(uint64_t mem[], const char* file) { |
| 51 | char buffer[4096]; |
| 52 | unsigned int numFound = 0; |
| 53 | |
| 54 | int fd = open(file, O_RDONLY); |
| 55 | |
| 56 | if (fd < 0) { |
| 57 | printf("Unable to open %s: %s\n", file, strerror(errno)); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | const int len = read(fd, buffer, sizeof(buffer) - 1); |
| 62 | close(fd); |
| 63 | |
| 64 | if (len < 0) { |
| 65 | printf("Empty %s\n", file); |
| 66 | return; |
| 67 | } |
| 68 | buffer[len] = 0; |
| 69 | |
| 70 | static const char* const tags[] = { |
| 71 | "MemTotal:", "MemFree:", "Buffers:", "Cached:", "Shmem:", "Slab:", |
| 72 | "SReclaimable:", "SUnreclaim:", "SwapTotal:", "SwapFree:", "ZRam:", "Mapped:", |
| 73 | "VmallocUsed:", "PageTables:", "KernelStack:", NULL}; |
| 74 | |
| 75 | static const int tagsLen[] = {9, 8, 8, 7, 6, 5, 13, 11, 10, 9, 5, 7, 12, 11, 12, 0}; |
| 76 | |
| 77 | memset(mem, 0, sizeof(uint64_t) * 15); |
| 78 | char* p = buffer; |
| 79 | while (*p && (numFound < (sizeof(tagsLen) / sizeof(tagsLen[0])))) { |
| 80 | int i = 0; |
| 81 | while (tags[i]) { |
| 82 | //std::cout << "tag =" << tags[i] << " p = " << std::string(p, tagsLen[i]) << std::endl; |
| 83 | if (strncmp(p, tags[i], tagsLen[i]) == 0) { |
| 84 | p += tagsLen[i]; |
| 85 | while (*p == ' ') p++; |
| 86 | char* num = p; |
| 87 | while (*p >= '0' && *p <= '9') p++; |
| 88 | if (*p != 0) { |
| 89 | *p = 0; |
| 90 | p++; |
| 91 | } |
| 92 | mem[i] = atoll(num); |
| 93 | numFound++; |
| 94 | break; |
| 95 | } |
| 96 | i++; |
| 97 | } |
| 98 | while (*p && *p != '\n') { |
| 99 | p++; |
| 100 | } |
| 101 | if (*p) p++; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static void BM_ParseSysMemInfo(benchmark::State& state) { |
| 106 | std::string meminfo = R"meminfo(MemTotal: 3019740 kB |
| 107 | MemFree: 1809728 kB |
| 108 | MemAvailable: 2546560 kB |
| 109 | Buffers: 54736 kB |
| 110 | Cached: 776052 kB |
| 111 | SwapCached: 0 kB |
| 112 | Active: 445856 kB |
| 113 | Inactive: 459092 kB |
| 114 | Active(anon): 78492 kB |
| 115 | Inactive(anon): 2240 kB |
| 116 | Active(file): 367364 kB |
| 117 | Inactive(file): 456852 kB |
| 118 | Unevictable: 3096 kB |
| 119 | Mlocked: 3096 kB |
| 120 | SwapTotal: 0 kB |
| 121 | SwapFree: 0 kB |
| 122 | Dirty: 32 kB |
| 123 | Writeback: 0 kB |
| 124 | AnonPages: 74988 kB |
| 125 | Mapped: 62624 kB |
| 126 | Shmem: 4020 kB |
| 127 | Slab: 86464 kB |
| 128 | SReclaimable: 44432 kB |
| 129 | SUnreclaim: 42032 kB |
| 130 | KernelStack: 4880 kB |
| 131 | PageTables: 2900 kB |
| 132 | NFS_Unstable: 0 kB |
| 133 | Bounce: 0 kB |
| 134 | WritebackTmp: 0 kB |
| 135 | CommitLimit: 1509868 kB |
| 136 | Committed_AS: 80296 kB |
| 137 | VmallocTotal: 263061440 kB |
| 138 | VmallocUsed: 0 kB |
| 139 | VmallocChunk: 0 kB |
| 140 | AnonHugePages: 6144 kB |
| 141 | ShmemHugePages: 0 kB |
| 142 | ShmemPmdMapped: 0 kB |
| 143 | CmaTotal: 131072 kB |
| 144 | CmaFree: 130380 kB |
| 145 | HugePages_Total: 0 |
| 146 | HugePages_Free: 0 |
| 147 | HugePages_Rsvd: 0 |
| 148 | HugePages_Surp: 0 |
| 149 | Hugepagesize: 2048 kB)meminfo"; |
| 150 | |
| 151 | TemporaryFile tf; |
| 152 | ::android::base::WriteStringToFd(meminfo, tf.fd); |
| 153 | |
| 154 | uint64_t mem[MEMINFO_COUNT]; |
| 155 | for (auto _ : state) { |
| 156 | get_mem_info(mem, tf.path); |
| 157 | } |
| 158 | } |
| 159 | BENCHMARK(BM_ParseSysMemInfo); |
| 160 | |
| 161 | static void BM_ReadMemInfo(benchmark::State& state) { |
| 162 | std::string meminfo = R"meminfo(MemTotal: 3019740 kB |
| 163 | MemFree: 1809728 kB |
| 164 | MemAvailable: 2546560 kB |
| 165 | Buffers: 54736 kB |
| 166 | Cached: 776052 kB |
| 167 | SwapCached: 0 kB |
| 168 | Active: 445856 kB |
| 169 | Inactive: 459092 kB |
| 170 | Active(anon): 78492 kB |
| 171 | Inactive(anon): 2240 kB |
| 172 | Active(file): 367364 kB |
| 173 | Inactive(file): 456852 kB |
| 174 | Unevictable: 3096 kB |
| 175 | Mlocked: 3096 kB |
| 176 | SwapTotal: 0 kB |
| 177 | SwapFree: 0 kB |
| 178 | Dirty: 32 kB |
| 179 | Writeback: 0 kB |
| 180 | AnonPages: 74988 kB |
| 181 | Mapped: 62624 kB |
| 182 | Shmem: 4020 kB |
| 183 | Slab: 86464 kB |
| 184 | SReclaimable: 44432 kB |
| 185 | SUnreclaim: 42032 kB |
| 186 | KernelStack: 4880 kB |
| 187 | PageTables: 2900 kB |
| 188 | NFS_Unstable: 0 kB |
| 189 | Bounce: 0 kB |
| 190 | WritebackTmp: 0 kB |
| 191 | CommitLimit: 1509868 kB |
| 192 | Committed_AS: 80296 kB |
| 193 | VmallocTotal: 263061440 kB |
| 194 | VmallocUsed: 0 kB |
| 195 | VmallocChunk: 0 kB |
| 196 | AnonHugePages: 6144 kB |
| 197 | ShmemHugePages: 0 kB |
| 198 | ShmemPmdMapped: 0 kB |
| 199 | CmaTotal: 131072 kB |
| 200 | CmaFree: 130380 kB |
| 201 | HugePages_Total: 0 |
| 202 | HugePages_Free: 0 |
| 203 | HugePages_Rsvd: 0 |
| 204 | HugePages_Surp: 0 |
| 205 | Hugepagesize: 2048 kB)meminfo"; |
| 206 | |
| 207 | TemporaryFile tf; |
| 208 | android::base::WriteStringToFd(meminfo, tf.fd); |
| 209 | |
| 210 | std::string file = std::string(tf.path); |
| 211 | ::android::meminfo::SysMemInfo mi; |
| 212 | for (auto _ : state) { |
| 213 | mi.ReadMemInfo(file); |
| 214 | } |
| 215 | } |
| 216 | BENCHMARK(BM_ReadMemInfo); |
| 217 | |
| 218 | BENCHMARK_MAIN(); |