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