blob: c27065f26053f5d931048d18da2679a270730514 [file] [log] [blame]
Nicolas "Pixel" Noble85a46dd2016-04-12 01:50:51 +02001/*
2 *
3 * Copyright 2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <stdint.h>
35#include <string.h>
36
37#include <grpc/support/alloc.h>
38#include <grpc/support/sync.h>
39
40#include "test/core/util/memory_counters.h"
41
Nicolas "Pixel" Noble85a46dd2016-04-12 01:50:51 +020042static struct grpc_memory_counters g_memory_counters;
43static gpr_allocation_functions g_old_allocs;
44
Nicolas "Pixel" Noble7c37a682016-04-12 19:08:00 +020045static void *guard_malloc(size_t size);
46static void *guard_realloc(void *vptr, size_t size);
47static void guard_free(void *vptr);
48
Craig Tiller75a41b42017-03-10 20:44:25 -080049#ifdef GPR_LOW_LEVEL_COUNTERS
50/* hide these from the microbenchmark atomic stats */
51#define NO_BARRIER_FETCH_ADD(x, sz) \
52 __atomic_fetch_add((x), (sz), __ATOMIC_RELAXED)
53#define NO_BARRIER_LOAD(x) __atomic_load_n((x), __ATOMIC_RELAXED)
54#else
55#define NO_BARRIER_FETCH_ADD(x, sz) gpr_atm_no_barrier_fetch_add(x, sz)
56#define NO_BARRIER_LOAD(x) gpr_atm_no_barrier_load(x)
57#endif
58
Nicolas "Pixel" Noble85a46dd2016-04-12 01:50:51 +020059static void *guard_malloc(size_t size) {
60 size_t *ptr;
Nicolas "Pixel" Noble7c37a682016-04-12 19:08:00 +020061 if (!size) return NULL;
Craig Tiller75a41b42017-03-10 20:44:25 -080062 NO_BARRIER_FETCH_ADD(&g_memory_counters.total_size_absolute, (gpr_atm)size);
63 NO_BARRIER_FETCH_ADD(&g_memory_counters.total_size_relative, (gpr_atm)size);
64 NO_BARRIER_FETCH_ADD(&g_memory_counters.total_allocs_absolute, (gpr_atm)1);
65 NO_BARRIER_FETCH_ADD(&g_memory_counters.total_allocs_relative, (gpr_atm)1);
Nicolas "Pixel" Noble85a46dd2016-04-12 01:50:51 +020066 ptr = g_old_allocs.malloc_fn(size + sizeof(size));
67 *ptr++ = size;
68 return ptr;
69}
70
71static void *guard_realloc(void *vptr, size_t size) {
72 size_t *ptr = vptr;
Nicolas "Pixel" Noble7c37a682016-04-12 19:08:00 +020073 if (vptr == NULL) {
74 return guard_malloc(size);
75 }
76 if (size == 0) {
77 guard_free(vptr);
78 return NULL;
79 }
Nicolas "Pixel" Noble85a46dd2016-04-12 01:50:51 +020080 --ptr;
Craig Tiller75a41b42017-03-10 20:44:25 -080081 NO_BARRIER_FETCH_ADD(&g_memory_counters.total_size_absolute, (gpr_atm)size);
82 NO_BARRIER_FETCH_ADD(&g_memory_counters.total_size_relative, -(gpr_atm)*ptr);
83 NO_BARRIER_FETCH_ADD(&g_memory_counters.total_size_relative, (gpr_atm)size);
84 NO_BARRIER_FETCH_ADD(&g_memory_counters.total_allocs_absolute, (gpr_atm)1);
Nicolas "Pixel" Noble85a46dd2016-04-12 01:50:51 +020085 ptr = g_old_allocs.realloc_fn(ptr, size + sizeof(size));
86 *ptr++ = size;
87 return ptr;
88}
89
90static void guard_free(void *vptr) {
91 size_t *ptr = vptr;
Nicolas "Pixel" Noble7c37a682016-04-12 19:08:00 +020092 if (!vptr) return;
Nicolas "Pixel" Noble85a46dd2016-04-12 01:50:51 +020093 --ptr;
Craig Tiller75a41b42017-03-10 20:44:25 -080094 NO_BARRIER_FETCH_ADD(&g_memory_counters.total_size_relative, -(gpr_atm)*ptr);
95 NO_BARRIER_FETCH_ADD(&g_memory_counters.total_allocs_relative, -(gpr_atm)1);
Nicolas "Pixel" Noble85a46dd2016-04-12 01:50:51 +020096 g_old_allocs.free_fn(ptr);
97}
98
Craig Tiller6f417882017-02-16 14:09:39 -080099struct gpr_allocation_functions g_guard_allocs = {guard_malloc, NULL,
100 guard_realloc, guard_free};
Nicolas "Pixel" Noble85a46dd2016-04-12 01:50:51 +0200101
102void grpc_memory_counters_init() {
103 memset(&g_memory_counters, 0, sizeof(g_memory_counters));
Nicolas "Pixel" Noble85a46dd2016-04-12 01:50:51 +0200104 g_old_allocs = gpr_get_allocation_functions();
105 gpr_set_allocation_functions(g_guard_allocs);
106}
107
108void grpc_memory_counters_destroy() {
109 gpr_set_allocation_functions(g_old_allocs);
Nicolas "Pixel" Noble85a46dd2016-04-12 01:50:51 +0200110}
111
112struct grpc_memory_counters grpc_memory_counters_snapshot() {
113 struct grpc_memory_counters counters;
Craig Tiller8f1b3152017-02-08 15:06:20 -0800114 counters.total_size_relative =
Craig Tiller75a41b42017-03-10 20:44:25 -0800115 NO_BARRIER_LOAD(&g_memory_counters.total_size_relative);
Craig Tiller8f1b3152017-02-08 15:06:20 -0800116 counters.total_size_absolute =
Craig Tiller75a41b42017-03-10 20:44:25 -0800117 NO_BARRIER_LOAD(&g_memory_counters.total_size_absolute);
Craig Tiller8f1b3152017-02-08 15:06:20 -0800118 counters.total_allocs_relative =
Craig Tiller75a41b42017-03-10 20:44:25 -0800119 NO_BARRIER_LOAD(&g_memory_counters.total_allocs_relative);
Craig Tiller8f1b3152017-02-08 15:06:20 -0800120 counters.total_allocs_absolute =
Craig Tiller75a41b42017-03-10 20:44:25 -0800121 NO_BARRIER_LOAD(&g_memory_counters.total_allocs_absolute);
Nicolas "Pixel" Noble85a46dd2016-04-12 01:50:51 +0200122 return counters;
123}