• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 /* This is an implementation that uses the same arena access pattern found
18  * in the arena_stats_merge function from src/arena.c.
19  */
je_mallinfo()20 struct mallinfo je_mallinfo() {
21   struct mallinfo mi;
22   memset(&mi, 0, sizeof(mi));
23 
24   malloc_mutex_lock(TSDN_NULL, &arenas_lock);
25   for (unsigned i = 0; i < narenas_auto; i++) {
26     if (arenas[i] != NULL) {
27       malloc_mutex_lock(TSDN_NULL, &arenas[i]->lock);
28       mi.hblkhd += arenas[i]->stats.mapped;
29       mi.uordblks += arenas[i]->stats.allocated_large;
30       mi.uordblks += arenas[i]->stats.allocated_huge;
31       malloc_mutex_unlock(TSDN_NULL, &arenas[i]->lock);
32 
33       for (unsigned j = 0; j < NBINS; j++) {
34         arena_bin_t* bin = &arenas[i]->bins[j];
35 
36         malloc_mutex_lock(TSDN_NULL, &bin->lock);
37         mi.uordblks += arena_bin_info[j].reg_size * bin->stats.curregs;
38         malloc_mutex_unlock(TSDN_NULL, &bin->lock);
39       }
40     }
41   }
42   malloc_mutex_unlock(TSDN_NULL, &arenas_lock);
43   mi.fordblks = mi.hblkhd - mi.uordblks;
44   mi.usmblks = mi.hblkhd;
45   return mi;
46 }
47 
__mallinfo_narenas()48 size_t __mallinfo_narenas() {
49   return narenas_auto;
50 }
51 
__mallinfo_nbins()52 size_t __mallinfo_nbins() {
53   return NBINS;
54 }
55 
__mallinfo_arena_info(size_t aidx)56 struct mallinfo __mallinfo_arena_info(size_t aidx) {
57   struct mallinfo mi;
58   memset(&mi, 0, sizeof(mi));
59 
60   malloc_mutex_lock(TSDN_NULL, &arenas_lock);
61   if (aidx < narenas_auto) {
62     if (arenas[aidx] != NULL) {
63       malloc_mutex_lock(TSDN_NULL, &arenas[aidx]->lock);
64       mi.hblkhd = arenas[aidx]->stats.mapped;
65       mi.ordblks = arenas[aidx]->stats.allocated_large;
66       mi.uordblks = arenas[aidx]->stats.allocated_huge;
67       malloc_mutex_unlock(TSDN_NULL, &arenas[aidx]->lock);
68 
69       for (unsigned j = 0; j < NBINS; j++) {
70         arena_bin_t* bin = &arenas[aidx]->bins[j];
71 
72         malloc_mutex_lock(TSDN_NULL, &bin->lock);
73         mi.fsmblks += arena_bin_info[j].reg_size * bin->stats.curregs;
74         malloc_mutex_unlock(TSDN_NULL, &bin->lock);
75       }
76     }
77   }
78   malloc_mutex_unlock(TSDN_NULL, &arenas_lock);
79   return mi;
80 }
81 
__mallinfo_bin_info(size_t aidx,size_t bidx)82 struct mallinfo __mallinfo_bin_info(size_t aidx, size_t bidx) {
83   struct mallinfo mi;
84   memset(&mi, 0, sizeof(mi));
85 
86   malloc_mutex_lock(TSDN_NULL, &arenas_lock);
87   if (aidx < narenas_auto && bidx < NBINS) {
88     if (arenas[aidx] != NULL) {
89       arena_bin_t* bin = &arenas[aidx]->bins[bidx];
90 
91       malloc_mutex_lock(TSDN_NULL, &bin->lock);
92       mi.ordblks = arena_bin_info[bidx].reg_size * bin->stats.curregs;
93       mi.uordblks = bin->stats.nmalloc;
94       mi.fordblks = bin->stats.ndalloc;
95       malloc_mutex_unlock(TSDN_NULL, &bin->lock);
96     }
97   }
98   malloc_mutex_unlock(TSDN_NULL, &arenas_lock);
99   return mi;
100 }
101