118ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris/*
218ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris * Copyright (C) 2014 The Android Open Source Project
318ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris *
418ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris * Licensed under the Apache License, Version 2.0 (the "License");
518ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris * you may not use this file except in compliance with the License.
618ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris * You may obtain a copy of the License at
718ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris *
818ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris *      http://www.apache.org/licenses/LICENSE-2.0
918ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris *
1018ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris * Unless required by applicable law or agreed to in writing, software
1118ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris * distributed under the License is distributed on an "AS IS" BASIS,
1218ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1318ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris * See the License for the specific language governing permissions and
1418ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris * limitations under the License.
1518ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris */
1618ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris
1718ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris#include "jemalloc/internal/jemalloc_internal.h"
1818ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris
19608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferrisextern unsigned narenas_auto;
20608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferrisextern malloc_mutex_t arenas_lock;
21608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferrisextern arena_t **arenas;
22608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris
23608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris// This is an implementation that uses the same arena access pattern found
24608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris// in the arena_stats_merge function from src/arena.c.
2518ff84135248f3566b938c836a02fa6138a281f6Christopher Ferrisstruct mallinfo je_mallinfo() {
2618ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris  struct mallinfo mi;
2718ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris  memset(&mi, 0, sizeof(mi));
2818ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris
29608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris  malloc_mutex_lock(&arenas_lock);
30608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris  for (unsigned i = 0; i < narenas_auto; i++) {
31608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris    if (arenas[i] != NULL) {
32608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris      malloc_mutex_lock(&arenas[i]->lock);
33608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris      mi.hblkhd += arenas[i]->stats.mapped;
34608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris      mi.uordblks += arenas[i]->stats.allocated_large;
35608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris      mi.uordblks += arenas[i]->stats.allocated_huge;
36608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris      malloc_mutex_unlock(&arenas[i]->lock);
37608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris
38608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris      for (unsigned j = 0; j < NBINS; j++) {
39608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris        arena_bin_t* bin = &arenas[i]->bins[j];
40608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris
41608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris        malloc_mutex_lock(&bin->lock);
42608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris        mi.uordblks += bin->stats.allocated;
43608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris        malloc_mutex_unlock(&bin->lock);
44608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris      }
45608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris    }
4618ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris  }
47608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris  malloc_mutex_unlock(&arenas_lock);
48608a78fe3fa03130f60994151057ed1350d08b40Christopher Ferris  mi.fordblks = mi.hblkhd - mi.uordblks;
490f804259d71591e76988474a8c9eabdf1590c6e1Christopher Ferris  mi.usmblks = mi.hblkhd;
5018ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris  return mi;
5118ff84135248f3566b938c836a02fa6138a281f6Christopher Ferris}
52