xref: /aosp_15_r20/external/jemalloc_new/src/android_je_stats.c (revision 1208bc7e437ced7eb82efac44ba17e3beba411da)
1*1208bc7eSAndroid Build Coastguard Worker /*
2*1208bc7eSAndroid Build Coastguard Worker  * Copyright (C) 2014 The Android Open Source Project
3*1208bc7eSAndroid Build Coastguard Worker  *
4*1208bc7eSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*1208bc7eSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*1208bc7eSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*1208bc7eSAndroid Build Coastguard Worker  *
8*1208bc7eSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*1208bc7eSAndroid Build Coastguard Worker  *
10*1208bc7eSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*1208bc7eSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*1208bc7eSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*1208bc7eSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*1208bc7eSAndroid Build Coastguard Worker  * limitations under the License.
15*1208bc7eSAndroid Build Coastguard Worker  */
16*1208bc7eSAndroid Build Coastguard Worker 
je_stats_arena(size_t arena_index,void (* callback)(size_t,size_t,size_t))17*1208bc7eSAndroid Build Coastguard Worker void je_stats_arena(size_t arena_index, void (*callback)(size_t, size_t, size_t)) {
18*1208bc7eSAndroid Build Coastguard Worker   malloc_mutex_lock(TSDN_NULL, &arenas_lock);
19*1208bc7eSAndroid Build Coastguard Worker   arena_t* arena = atomic_load_p(&arenas[arena_index], ATOMIC_ACQUIRE);
20*1208bc7eSAndroid Build Coastguard Worker   if (arena == NULL) {
21*1208bc7eSAndroid Build Coastguard Worker     malloc_mutex_unlock(TSDN_NULL, &arenas_lock);
22*1208bc7eSAndroid Build Coastguard Worker     return;
23*1208bc7eSAndroid Build Coastguard Worker   }
24*1208bc7eSAndroid Build Coastguard Worker 
25*1208bc7eSAndroid Build Coastguard Worker   for (unsigned j = 0; j < NBINS; j++) {
26*1208bc7eSAndroid Build Coastguard Worker     bin_t* bin = &arena->bins[j];
27*1208bc7eSAndroid Build Coastguard Worker 
28*1208bc7eSAndroid Build Coastguard Worker     /* NOTE: This includes allocations cached on every thread. */
29*1208bc7eSAndroid Build Coastguard Worker     malloc_mutex_lock(TSDN_NULL, &bin->lock);
30*1208bc7eSAndroid Build Coastguard Worker     callback(j, bin_infos[j].reg_size, bin->stats.curregs);
31*1208bc7eSAndroid Build Coastguard Worker     malloc_mutex_unlock(TSDN_NULL, &bin->lock);
32*1208bc7eSAndroid Build Coastguard Worker   }
33*1208bc7eSAndroid Build Coastguard Worker 
34*1208bc7eSAndroid Build Coastguard Worker   /* Accumulate the large allocation stats.
35*1208bc7eSAndroid Build Coastguard Worker    * Do not include stats.allocated_large, it is only updated by
36*1208bc7eSAndroid Build Coastguard Worker    * arena_stats_merge, and would include the data counted below.
37*1208bc7eSAndroid Build Coastguard Worker    */
38*1208bc7eSAndroid Build Coastguard Worker   for (unsigned j = NBINS; j < NSIZES; j++) {
39*1208bc7eSAndroid Build Coastguard Worker     /* Read ndalloc first so that we guarantee nmalloc >= ndalloc. */
40*1208bc7eSAndroid Build Coastguard Worker     uint64_t ndalloc = arena_stats_read_u64(TSDN_NULL, &arena->stats, &arena->stats.lstats[j - NBINS].ndalloc);
41*1208bc7eSAndroid Build Coastguard Worker     uint64_t nmalloc = arena_stats_read_u64(TSDN_NULL, &arena->stats, &arena->stats.lstats[j - NBINS].nmalloc);
42*1208bc7eSAndroid Build Coastguard Worker     callback(j, sz_index2size(j), (size_t)(nmalloc - ndalloc));
43*1208bc7eSAndroid Build Coastguard Worker   }
44*1208bc7eSAndroid Build Coastguard Worker   malloc_mutex_unlock(TSDN_NULL, &arenas_lock);
45*1208bc7eSAndroid Build Coastguard Worker }
46