Home | History | Annotate | Download | only in benchmarks
      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 
     33 static volatile size_t sink;
     34 
     35 static constexpr int NTHREADS = 5;
     36 
     37 static std::atomic<int> thread_count;
     38 
     39 static void* thread_func(void* arg) {
     40   *reinterpret_cast<void**>(arg) = malloc(17);
     41   thread_count++;
     42   return nullptr;
     43 }
     44 
     45 static void BM_mallinfo(benchmark::State& state) {
     46   pthread_t t[NTHREADS];
     47   void* object[NTHREADS];
     48   // Create threads to populate thread-local arenas, if any.
     49   thread_count = 0;
     50   for (int i = 0; i < 5; i++) {
     51     int res = pthread_create(t + i, NULL, thread_func, object + i);
     52     if (res != 0) {
     53       errx(1, "ERROR: pthread_create failed");
     54     }
     55   }
     56   while (thread_count != NTHREADS) {
     57     sched_yield();
     58   }
     59   for (auto _ : state) {
     60     sink = mallinfo().uordblks;
     61   }
     62   for (int i = 0; i < 5; i++) {
     63     int res = pthread_join(t[i], NULL);
     64     if (res != 0) {
     65       errx(1, "ERROR: pthread_join failed");
     66     }
     67     free(object[i]);
     68   }
     69 }
     70 
     71 BIONIC_BENCHMARK(BM_mallinfo);
     72 
     73 static constexpr int BUF_SIZE = 100;
     74 
     75 static void BM_read_statm(benchmark::State& state) {
     76   char buf[BUF_SIZE];
     77   int statm_fd = open("/proc/self/statm", O_RDONLY);
     78   if (statm_fd == -1) {
     79     errx(1, "ERROR: Open failed");
     80   }
     81   for (auto _ : state) {
     82     off_t result = lseek(statm_fd, 0 /* offset */, SEEK_SET);
     83     if (result == (off_t) -1) {
     84       errx(1, "ERROR: lseek to beginning failed");
     85     }
     86     ssize_t nread = read(statm_fd, buf, BUF_SIZE);
     87     if (nread < 13 || nread > 50) {
     88       errx(1, "ERROR: Implausible read result; result = %zd", nread);
     89     }
     90     // We should really add the parsing overhead for the result; but that should be < 100 nsecs
     91     // if we tweak it enough.
     92   }
     93   if (close(statm_fd) == -1) {
     94     errx(1, "ERROR: Close failed");
     95   }
     96 }
     97 
     98 BIONIC_BENCHMARK(BM_read_statm);
     99