summaryrefslogtreecommitdiffstats
path: root/stats.c
blob: ea766d5fb096973f3d935a582c46a40ddf781b5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as
 *   published by the Free Software Foundation, Inc.,
 *   59 Temple Place Ste 330, Boston MA 02111-1307, USA, version 2.1,
 *   incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * stats.c
 *
 * Produce memory allocation statistics.
 */

#include <assert.h>
#include <stdlib.h>
#include <inttypes.h>
#include <limits.h>
#include <string.h>

#include "lpsm.h"
#include "internals.h"
#include "bitops.h"

#ifdef PRINT_DEBUG_INFO
/*
 * Debugging routine to print the number of allocations of each size
 */
void lpsm_print_statistics(void)
{
  int i, o, obit, ebit, xbit;
  int fcount, acount, total_slabs;
  unsigned long bt = 0, bf = 0, ba = 0;
  struct slab_info *si;

  /* Print buddy system stats */

  for ( i = 0, o = AH->arena_size_lg2 ;
	o >= BUDDY_ORDER_MIN ;
	i++, o-- ) {
    obit = 1 << i;
    ebit = obit << 1;
    fcount = acount = 0;
    for ( xbit = obit ; xbit < ebit ; xbit++ ) {
      if ( test_bit(AH->free_bitmap, xbit) )
	fcount++;
    }
    for ( xbit = obit ; xbit < ebit ; xbit++ ) {
      if ( test_bit(AH->alloc_bitmap, xbit) )
	acount++;
    }

    DPRINTF(("    rorder %2d order %2d free %8d used %8d\n",
	     i, AH->arena_size_lg2-i, fcount, acount));

    bt += (fcount+acount) * (1UL << o);
    bf += fcount * (1UL << o);
    ba += acount * (1UL << o);
  }
  DPRINTF(("    buddy: %lu bytes free, %lu bytes allocated, %lu total\n",
	   bf, ba, bt));

  bt = bf = ba = 0;

  for ( si = &AH->slab[0], i = 0 ;
	i < SLAB_INFO_COUNT ;
	i++, si++ ) {
    if ( !si->size )
      break;
    total_slabs = si->total_pages * si->count;
    DPRINTF(("    slab size %4d free %8d used %8d\n",
	     si->size, total_slabs-si->alloc_slabs, si->alloc_slabs));
    bt += total_slabs * si->size;
    bf += (total_slabs-si->alloc_slabs) * si->size;
    ba += si->alloc_slabs * si->size;
  }
  DPRINTF(("    slab:  %lu bytes free, %lu bytes allocated, %lu total\n",
	   bf, ba, bt));
}
#endif

/*
 * Get allocation statistics.  This returned a malloc()'d list of
 * {block size, free blocks, allocated blocks} followed by an
 * all-zero datum.
 */
struct lpsm_alloc_stats *lpsm_alloc_stats(void)
{
  struct lpsm_alloc_stats *as, *asp;
  struct slab_info *si;
  int nb, ns;
  int i, o;
  int xbit, obit, ebit;
  size_t fcount, acount, *last_buddy_alloc;

  /* Compute number of buddy and slab allocation types */
  nb = AH->arena_size_lg2-BUDDY_ORDER_MIN+1;
  for ( ns = 0 ; ns < SLAB_INFO_COUNT ; ns++ )
    if ( !AH->slab[ns].size )
      break;

  as = malloc((nb+ns+1) * sizeof(struct lpsm_alloc_stats));
  if ( !as )
    return NULL;

  for ( asp = as, i = 0, o = AH->arena_size_lg2 ;
	o >= BUDDY_ORDER_MIN ;
	asp++, i++, o-- ) {
    asp->size = (size_t)1 << o;
    obit = 1 << i;
    ebit = obit << 1;
    fcount = acount = 0;
    for ( xbit = obit ; xbit < ebit ; xbit++ ) {
      if ( test_bit(AH->free_bitmap, xbit) )
	fcount++;
    }
    asp->free = fcount;
    for ( xbit = obit ; xbit < ebit ; xbit++ ) {
      if ( test_bit(AH->alloc_bitmap, xbit) )
	acount++;
    }
    asp->alloc = acount;
  }

  /* The alloc counter for the last buddy size include all the slab blocks.
     We want to count them separately. */
  last_buddy_alloc = &asp[-1].alloc;

  for ( i = 0, si = &AH->slab[0] ;
	i < SLAB_INFO_COUNT && si->size ;
	i++, si++, asp++ ) {
    last_buddy_alloc -= si->total_pages;
    asp->size  = si->size;
    asp->alloc = si->alloc_slabs;
    asp->free  = (si->total_pages * si->count) - si->alloc_slabs;
  }
  /* Sentinel block at end */
  asp->size = asp->alloc = asp->free = 0;
  return as;
}