aboutsummaryrefslogtreecommitdiff
path: root/benchmarks/get_heap_size_benchmark.cpp
blob: c3680dc449daff51f88533a3480cbfd87032f5d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// This measures the time required to compute two different native heap size metrics that
// we might use to guide GC triggering.

#include <atomic>
#include <err.h>
#include <fcntl.h>
#include <malloc.h>
#include <pthread.h>
#include <sched.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <benchmark/benchmark.h>
#include "util.h"

static volatile size_t sink;

static constexpr int NTHREADS = 5;

static std::atomic<int> thread_count;

static void* thread_func(void* arg) {
  *reinterpret_cast<void**>(arg) = malloc(17);
  thread_count++;
  return nullptr;
}

static void BM_mallinfo(benchmark::State& state) {
  pthread_t t[NTHREADS];
  void* object[NTHREADS];
  // Create threads to populate thread-local arenas, if any.
  thread_count = 0;
  for (int i = 0; i < 5; i++) {
    int res = pthread_create(t + i, NULL, thread_func, object + i);
    if (res != 0) {
      errx(1, "ERROR: pthread_create failed");
    }
  }
  while (thread_count != NTHREADS) {
    sched_yield();
  }
  for (auto _ : state) {
    sink = mallinfo().uordblks;
  }
  for (int i = 0; i < 5; i++) {
    int res = pthread_join(t[i], NULL);
    if (res != 0) {
      errx(1, "ERROR: pthread_join failed");
    }
    free(object[i]);
  }
}

BIONIC_BENCHMARK(BM_mallinfo);

static constexpr int BUF_SIZE = 100;

static void BM_read_statm(benchmark::State& state) {
  char buf[BUF_SIZE];
  int statm_fd = open("/proc/self/statm", O_RDONLY);
  if (statm_fd == -1) {
    errx(1, "ERROR: Open failed");
  }
  for (auto _ : state) {
    off_t result = lseek(statm_fd, 0 /* offset */, SEEK_SET);
    if (result == (off_t) -1) {
      errx(1, "ERROR: lseek to beginning failed");
    }
    ssize_t nread = read(statm_fd, buf, BUF_SIZE);
    if (nread < 13 || nread > 50) {
      errx(1, "ERROR: Implausible read result; result = %zd", nread);
    }
    // We should really add the parsing overhead for the result; but that should be < 100 nsecs
    // if we tweak it enough.
  }
  if (close(statm_fd) == -1) {
    errx(1, "ERROR: Close failed");
  }
}

BIONIC_BENCHMARK(BM_read_statm);