Skip to content

Commit

Permalink
Slab: slots statistics.
Browse files Browse the repository at this point in the history
For each slot, the number of total and used entries, as well as
the number of allocation requests and failures, are tracked.
  • Loading branch information
mdocguard committed Dec 7, 2016
1 parent 366f131 commit 9ccf719
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
57 changes: 46 additions & 11 deletions src/core/ngx_slab.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ ngx_slab_init(ngx_slab_pool_t *pool)
}

p += n * sizeof(ngx_slab_page_t);
size -= n * sizeof(ngx_slab_page_t);

pool->stats = (ngx_slab_stat_t *) p;
ngx_memzero(pool->stats, n * sizeof(ngx_slab_stat_t));

p += n * sizeof(ngx_slab_stat_t);

size -= n * (sizeof(ngx_slab_page_t) + sizeof(ngx_slab_stat_t));

pages = (ngx_uint_t) (size / (ngx_pagesize + sizeof(ngx_slab_page_t)));

Expand Down Expand Up @@ -205,6 +211,8 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *pool, size_t size)
slot = 0;
}

pool->stats[slot].reqs++;

ngx_log_debug2(NGX_LOG_DEBUG_ALLOC, ngx_cycle->log, 0,
"slab alloc: %uz slot: %ui", size, slot);

Expand Down Expand Up @@ -232,11 +240,13 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *pool, size_t size)

i = (n * sizeof(uintptr_t) * 8 + i) << shift;

p = (uintptr_t) bitmap + i;

pool->stats[slot].used++;

if (bitmap[n] == NGX_SLAB_BUSY) {
for (n = n + 1; n < map; n++) {
if (bitmap[n] != NGX_SLAB_BUSY) {
p = (uintptr_t) bitmap + i;

goto done;
}
}
Expand All @@ -249,8 +259,6 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *pool, size_t size)
page->prev = NGX_SLAB_SMALL;
}

p = (uintptr_t) bitmap + i;

goto done;
}
}
Expand All @@ -276,6 +284,8 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *pool, size_t size)

p = ngx_slab_page_addr(pool, page) + (i << shift);

pool->stats[slot].used++;

goto done;
}

Expand Down Expand Up @@ -305,6 +315,8 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *pool, size_t size)

p = ngx_slab_page_addr(pool, page) + (i << shift);

pool->stats[slot].used++;

goto done;
}
}
Expand Down Expand Up @@ -339,8 +351,12 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *pool, size_t size)

slots[slot].next = page;

pool->stats[slot].total += (ngx_pagesize >> shift) - n;

p = ngx_slab_page_addr(pool, page) + (n << shift);

pool->stats[slot].used++;

goto done;

} else if (shift == ngx_slab_exact_shift) {
Expand All @@ -351,8 +367,12 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *pool, size_t size)

slots[slot].next = page;

pool->stats[slot].total += sizeof(uintptr_t) * 8;

p = ngx_slab_page_addr(pool, page);

pool->stats[slot].used++;

goto done;

} else { /* shift > ngx_slab_exact_shift */
Expand All @@ -363,14 +383,20 @@ ngx_slab_alloc_locked(ngx_slab_pool_t *pool, size_t size)

slots[slot].next = page;

pool->stats[slot].total += ngx_pagesize >> shift;

p = ngx_slab_page_addr(pool, page);

pool->stats[slot].used++;

goto done;
}
}

p = 0;

pool->stats[slot].fails++;

done:

ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, ngx_cycle->log, 0,
Expand Down Expand Up @@ -425,7 +451,7 @@ ngx_slab_free_locked(ngx_slab_pool_t *pool, void *p)
{
size_t size;
uintptr_t slab, m, *bitmap;
ngx_uint_t n, type, slot, shift, map;
ngx_uint_t i, n, type, slot, shift, map;
ngx_slab_page_t *slots, *page;

ngx_log_debug1(NGX_LOG_DEBUG_ALLOC, ngx_cycle->log, 0, "slab free: %p", p);
Expand Down Expand Up @@ -458,10 +484,10 @@ ngx_slab_free_locked(ngx_slab_pool_t *pool, void *p)
((uintptr_t) p & ~((uintptr_t) ngx_pagesize - 1));

if (bitmap[n] & m) {
slot = shift - pool->min_shift;

if (page->next == NULL) {
slots = ngx_slab_slots(pool);
slot = shift - pool->min_shift;

page->next = slots[slot].next;
slots[slot].next = page;
Expand All @@ -484,14 +510,16 @@ ngx_slab_free_locked(ngx_slab_pool_t *pool, void *p)

map = (ngx_pagesize >> shift) / (sizeof(uintptr_t) * 8);

for (n = 1; n < map; n++) {
if (bitmap[n]) {
for (i = 1; i < map; i++) {
if (bitmap[i]) {
goto done;
}
}

ngx_slab_free_pages(pool, page, 1);

pool->stats[slot].total -= (ngx_pagesize >> shift) - n;

goto done;
}

Expand All @@ -508,9 +536,10 @@ ngx_slab_free_locked(ngx_slab_pool_t *pool, void *p)
}

if (slab & m) {
slot = ngx_slab_exact_shift - pool->min_shift;

if (slab == NGX_SLAB_BUSY) {
slots = ngx_slab_slots(pool);
slot = ngx_slab_exact_shift - pool->min_shift;

page->next = slots[slot].next;
slots[slot].next = page;
Expand All @@ -527,6 +556,8 @@ ngx_slab_free_locked(ngx_slab_pool_t *pool, void *p)

ngx_slab_free_pages(pool, page, 1);

pool->stats[slot].total -= sizeof(uintptr_t) * 8;

goto done;
}

Expand All @@ -545,10 +576,10 @@ ngx_slab_free_locked(ngx_slab_pool_t *pool, void *p)
+ NGX_SLAB_MAP_SHIFT);

if (slab & m) {
slot = shift - pool->min_shift;

if (page->next == NULL) {
slots = ngx_slab_slots(pool);
slot = shift - pool->min_shift;

page->next = slots[slot].next;
slots[slot].next = page;
Expand All @@ -565,6 +596,8 @@ ngx_slab_free_locked(ngx_slab_pool_t *pool, void *p)

ngx_slab_free_pages(pool, page, 1);

pool->stats[slot].total -= ngx_pagesize >> shift;

goto done;
}

Expand Down Expand Up @@ -604,6 +637,8 @@ ngx_slab_free_locked(ngx_slab_pool_t *pool, void *p)

done:

pool->stats[slot].used--;

ngx_slab_junk(p, size);

return;
Expand Down
11 changes: 11 additions & 0 deletions src/core/ngx_slab.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ struct ngx_slab_page_s {
};


typedef struct {
ngx_uint_t total;
ngx_uint_t used;

ngx_uint_t reqs;
ngx_uint_t fails;
} ngx_slab_stat_t;


typedef struct {
ngx_shmtx_sh_t lock;

Expand All @@ -32,6 +41,8 @@ typedef struct {
ngx_slab_page_t *last;
ngx_slab_page_t free;

ngx_slab_stat_t *stats;

u_char *start;
u_char *end;

Expand Down

0 comments on commit 9ccf719

Please sign in to comment.