Hans Boehm | 3e5754c | 2018-10-18 10:37:34 -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 | // This measures the time required to compute two different native heap size metrics that |
| 18 | // we might use to guide GC triggering. |
| 19 | |
| 20 | #include <atomic> |
| 21 | #include <err.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <malloc.h> |
| 24 | #include <pthread.h> |
| 25 | #include <sched.h> |
| 26 | #include <unistd.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
| 29 | |
| 30 | #include <benchmark/benchmark.h> |
| 31 | #include "util.h" |
| 32 | |
Hans Boehm | 3e5754c | 2018-10-18 10:37:34 -0700 | [diff] [blame] | 33 | static constexpr int NTHREADS = 5; |
| 34 | |
| 35 | static std::atomic<int> thread_count; |
| 36 | |
| 37 | static void* thread_func(void* arg) { |
| 38 | *reinterpret_cast<void**>(arg) = malloc(17); |
| 39 | thread_count++; |
| 40 | return nullptr; |
| 41 | } |
| 42 | |
| 43 | static void BM_mallinfo(benchmark::State& state) { |
| 44 | pthread_t t[NTHREADS]; |
| 45 | void* object[NTHREADS]; |
| 46 | // Create threads to populate thread-local arenas, if any. |
| 47 | thread_count = 0; |
| 48 | for (int i = 0; i < 5; i++) { |
| 49 | int res = pthread_create(t + i, NULL, thread_func, object + i); |
| 50 | if (res != 0) { |
| 51 | errx(1, "ERROR: pthread_create failed"); |
| 52 | } |
| 53 | } |
| 54 | while (thread_count != NTHREADS) { |
| 55 | sched_yield(); |
| 56 | } |
| 57 | for (auto _ : state) { |
Elliott Hughes | e8693e7 | 2020-10-22 13:43:59 -0700 | [diff] [blame] | 58 | benchmark::DoNotOptimize(mallinfo().uordblks); |
Hans Boehm | 3e5754c | 2018-10-18 10:37:34 -0700 | [diff] [blame] | 59 | } |
| 60 | for (int i = 0; i < 5; i++) { |
| 61 | int res = pthread_join(t[i], NULL); |
| 62 | if (res != 0) { |
| 63 | errx(1, "ERROR: pthread_join failed"); |
| 64 | } |
| 65 | free(object[i]); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | BIONIC_BENCHMARK(BM_mallinfo); |
| 70 | |
| 71 | static constexpr int BUF_SIZE = 100; |
| 72 | |
| 73 | static void BM_read_statm(benchmark::State& state) { |
| 74 | char buf[BUF_SIZE]; |
| 75 | int statm_fd = open("/proc/self/statm", O_RDONLY); |
| 76 | if (statm_fd == -1) { |
| 77 | errx(1, "ERROR: Open failed"); |
| 78 | } |
| 79 | for (auto _ : state) { |
| 80 | off_t result = lseek(statm_fd, 0 /* offset */, SEEK_SET); |
| 81 | if (result == (off_t) -1) { |
| 82 | errx(1, "ERROR: lseek to beginning failed"); |
| 83 | } |
| 84 | ssize_t nread = read(statm_fd, buf, BUF_SIZE); |
| 85 | if (nread < 13 || nread > 50) { |
| 86 | errx(1, "ERROR: Implausible read result; result = %zd", nread); |
| 87 | } |
| 88 | // We should really add the parsing overhead for the result; but that should be < 100 nsecs |
| 89 | // if we tweak it enough. |
| 90 | } |
| 91 | if (close(statm_fd) == -1) { |
| 92 | errx(1, "ERROR: Close failed"); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | BIONIC_BENCHMARK(BM_read_statm); |