• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <stdint.h>
20 #include <string.h>
21 
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/sync.h>
24 
25 #include "test/core/util/memory_counters.h"
26 
27 static struct grpc_memory_counters g_memory_counters;
28 static gpr_allocation_functions g_old_allocs;
29 
30 static void* guard_malloc(size_t size);
31 static void* guard_realloc(void* vptr, size_t size);
32 static void guard_free(void* vptr);
33 
34 #ifdef GPR_LOW_LEVEL_COUNTERS
35 /* hide these from the microbenchmark atomic stats */
36 #define NO_BARRIER_FETCH_ADD(x, sz) \
37   __atomic_fetch_add((x), (sz), __ATOMIC_RELAXED)
38 #define NO_BARRIER_LOAD(x) __atomic_load_n((x), __ATOMIC_RELAXED)
39 #else
40 #define NO_BARRIER_FETCH_ADD(x, sz) gpr_atm_no_barrier_fetch_add(x, sz)
41 #define NO_BARRIER_LOAD(x) gpr_atm_no_barrier_load(x)
42 #endif
43 
guard_malloc(size_t size)44 static void* guard_malloc(size_t size) {
45   size_t* ptr;
46   if (!size) return nullptr;
47   NO_BARRIER_FETCH_ADD(&g_memory_counters.total_size_absolute, (gpr_atm)size);
48   NO_BARRIER_FETCH_ADD(&g_memory_counters.total_size_relative, (gpr_atm)size);
49   NO_BARRIER_FETCH_ADD(&g_memory_counters.total_allocs_absolute, (gpr_atm)1);
50   NO_BARRIER_FETCH_ADD(&g_memory_counters.total_allocs_relative, (gpr_atm)1);
51   ptr = static_cast<size_t*>(g_old_allocs.malloc_fn(size + sizeof(size)));
52   *ptr++ = size;
53   return ptr;
54 }
55 
guard_realloc(void * vptr,size_t size)56 static void* guard_realloc(void* vptr, size_t size) {
57   size_t* ptr = static_cast<size_t*>(vptr);
58   if (vptr == nullptr) {
59     return guard_malloc(size);
60   }
61   if (size == 0) {
62     guard_free(vptr);
63     return nullptr;
64   }
65   --ptr;
66   NO_BARRIER_FETCH_ADD(&g_memory_counters.total_size_absolute, (gpr_atm)size);
67   NO_BARRIER_FETCH_ADD(&g_memory_counters.total_size_relative, -(gpr_atm)*ptr);
68   NO_BARRIER_FETCH_ADD(&g_memory_counters.total_size_relative, (gpr_atm)size);
69   NO_BARRIER_FETCH_ADD(&g_memory_counters.total_allocs_absolute, (gpr_atm)1);
70   ptr = static_cast<size_t*>(g_old_allocs.realloc_fn(ptr, size + sizeof(size)));
71   *ptr++ = size;
72   return ptr;
73 }
74 
guard_free(void * vptr)75 static void guard_free(void* vptr) {
76   size_t* ptr = static_cast<size_t*>(vptr);
77   if (!vptr) return;
78   --ptr;
79   NO_BARRIER_FETCH_ADD(&g_memory_counters.total_size_relative, -(gpr_atm)*ptr);
80   NO_BARRIER_FETCH_ADD(&g_memory_counters.total_allocs_relative, -(gpr_atm)1);
81   g_old_allocs.free_fn(ptr);
82 }
83 
84 struct gpr_allocation_functions g_guard_allocs = {guard_malloc, nullptr,
85                                                   guard_realloc, guard_free};
86 
grpc_memory_counters_init()87 void grpc_memory_counters_init() {
88   memset(&g_memory_counters, 0, sizeof(g_memory_counters));
89   g_old_allocs = gpr_get_allocation_functions();
90   gpr_set_allocation_functions(g_guard_allocs);
91 }
92 
grpc_memory_counters_destroy()93 void grpc_memory_counters_destroy() {
94   gpr_set_allocation_functions(g_old_allocs);
95 }
96 
grpc_memory_counters_snapshot()97 struct grpc_memory_counters grpc_memory_counters_snapshot() {
98   struct grpc_memory_counters counters;
99   counters.total_size_relative =
100       NO_BARRIER_LOAD(&g_memory_counters.total_size_relative);
101   counters.total_size_absolute =
102       NO_BARRIER_LOAD(&g_memory_counters.total_size_absolute);
103   counters.total_allocs_relative =
104       NO_BARRIER_LOAD(&g_memory_counters.total_allocs_relative);
105   counters.total_allocs_absolute =
106       NO_BARRIER_LOAD(&g_memory_counters.total_allocs_absolute);
107   return counters;
108 }
109