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