Nicolas "Pixel" Noble | 85a46dd | 2016-04-12 01:50:51 +0200 | [diff] [blame] | 1 | /* |
| 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" Noble | 85a46dd | 2016-04-12 01:50:51 +0200 | [diff] [blame] | 42 | static struct grpc_memory_counters g_memory_counters; |
| 43 | static gpr_allocation_functions g_old_allocs; |
| 44 | |
Nicolas "Pixel" Noble | 7c37a68 | 2016-04-12 19:08:00 +0200 | [diff] [blame] | 45 | static void *guard_malloc(size_t size); |
| 46 | static void *guard_realloc(void *vptr, size_t size); |
| 47 | static void guard_free(void *vptr); |
| 48 | |
Craig Tiller | 75a41b4 | 2017-03-10 20:44:25 -0800 | [diff] [blame] | 49 | #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" Noble | 85a46dd | 2016-04-12 01:50:51 +0200 | [diff] [blame] | 59 | static void *guard_malloc(size_t size) { |
| 60 | size_t *ptr; |
Nicolas "Pixel" Noble | 7c37a68 | 2016-04-12 19:08:00 +0200 | [diff] [blame] | 61 | if (!size) return NULL; |
Craig Tiller | 75a41b4 | 2017-03-10 20:44:25 -0800 | [diff] [blame] | 62 | 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" Noble | 85a46dd | 2016-04-12 01:50:51 +0200 | [diff] [blame] | 66 | ptr = g_old_allocs.malloc_fn(size + sizeof(size)); |
| 67 | *ptr++ = size; |
| 68 | return ptr; |
| 69 | } |
| 70 | |
| 71 | static void *guard_realloc(void *vptr, size_t size) { |
| 72 | size_t *ptr = vptr; |
Nicolas "Pixel" Noble | 7c37a68 | 2016-04-12 19:08:00 +0200 | [diff] [blame] | 73 | if (vptr == NULL) { |
| 74 | return guard_malloc(size); |
| 75 | } |
| 76 | if (size == 0) { |
| 77 | guard_free(vptr); |
| 78 | return NULL; |
| 79 | } |
Nicolas "Pixel" Noble | 85a46dd | 2016-04-12 01:50:51 +0200 | [diff] [blame] | 80 | --ptr; |
Craig Tiller | 75a41b4 | 2017-03-10 20:44:25 -0800 | [diff] [blame] | 81 | 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" Noble | 85a46dd | 2016-04-12 01:50:51 +0200 | [diff] [blame] | 85 | ptr = g_old_allocs.realloc_fn(ptr, size + sizeof(size)); |
| 86 | *ptr++ = size; |
| 87 | return ptr; |
| 88 | } |
| 89 | |
| 90 | static void guard_free(void *vptr) { |
| 91 | size_t *ptr = vptr; |
Nicolas "Pixel" Noble | 7c37a68 | 2016-04-12 19:08:00 +0200 | [diff] [blame] | 92 | if (!vptr) return; |
Nicolas "Pixel" Noble | 85a46dd | 2016-04-12 01:50:51 +0200 | [diff] [blame] | 93 | --ptr; |
Craig Tiller | 75a41b4 | 2017-03-10 20:44:25 -0800 | [diff] [blame] | 94 | 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" Noble | 85a46dd | 2016-04-12 01:50:51 +0200 | [diff] [blame] | 96 | g_old_allocs.free_fn(ptr); |
| 97 | } |
| 98 | |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 99 | struct gpr_allocation_functions g_guard_allocs = {guard_malloc, NULL, |
| 100 | guard_realloc, guard_free}; |
Nicolas "Pixel" Noble | 85a46dd | 2016-04-12 01:50:51 +0200 | [diff] [blame] | 101 | |
| 102 | void grpc_memory_counters_init() { |
| 103 | memset(&g_memory_counters, 0, sizeof(g_memory_counters)); |
Nicolas "Pixel" Noble | 85a46dd | 2016-04-12 01:50:51 +0200 | [diff] [blame] | 104 | g_old_allocs = gpr_get_allocation_functions(); |
| 105 | gpr_set_allocation_functions(g_guard_allocs); |
| 106 | } |
| 107 | |
| 108 | void grpc_memory_counters_destroy() { |
| 109 | gpr_set_allocation_functions(g_old_allocs); |
Nicolas "Pixel" Noble | 85a46dd | 2016-04-12 01:50:51 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | struct grpc_memory_counters grpc_memory_counters_snapshot() { |
| 113 | struct grpc_memory_counters counters; |
Craig Tiller | 8f1b315 | 2017-02-08 15:06:20 -0800 | [diff] [blame] | 114 | counters.total_size_relative = |
Craig Tiller | 75a41b4 | 2017-03-10 20:44:25 -0800 | [diff] [blame] | 115 | NO_BARRIER_LOAD(&g_memory_counters.total_size_relative); |
Craig Tiller | 8f1b315 | 2017-02-08 15:06:20 -0800 | [diff] [blame] | 116 | counters.total_size_absolute = |
Craig Tiller | 75a41b4 | 2017-03-10 20:44:25 -0800 | [diff] [blame] | 117 | NO_BARRIER_LOAD(&g_memory_counters.total_size_absolute); |
Craig Tiller | 8f1b315 | 2017-02-08 15:06:20 -0800 | [diff] [blame] | 118 | counters.total_allocs_relative = |
Craig Tiller | 75a41b4 | 2017-03-10 20:44:25 -0800 | [diff] [blame] | 119 | NO_BARRIER_LOAD(&g_memory_counters.total_allocs_relative); |
Craig Tiller | 8f1b315 | 2017-02-08 15:06:20 -0800 | [diff] [blame] | 120 | counters.total_allocs_absolute = |
Craig Tiller | 75a41b4 | 2017-03-10 20:44:25 -0800 | [diff] [blame] | 121 | NO_BARRIER_LOAD(&g_memory_counters.total_allocs_absolute); |
Nicolas "Pixel" Noble | 85a46dd | 2016-04-12 01:50:51 +0200 | [diff] [blame] | 122 | return counters; |
| 123 | } |