1

I recently started going through memcached source code and i came across this structure. Based on my understanding, there are approximately 64 slabs and and each slab represents a unique chunk size. If we took the first slab class ( size 80 , say ) then the pages which are belongs to this slab will have it's memory broken into 80 bytes.

typedef struct {
  unsigned int size;      // sizes of items
  unsigned int perslab;   // how many items per slab

  void *slots;            // list of item ptrs
  unsigned int sl_curr;   // total free items in list

  unsigned int slabs;     // how many slabs were allocated for this class

  void **slab_list;       // array of slab pointers
  unsigned int list_size; // size of prev array

  size_t requested;       // The number of requested bytes

} slabclass_t;

I do not understand this line,

unsigned int slabs;     // how many slabs were allocated for this class

What does he mean by how many slabs were allocated for a slab class? Every slab class must be unique right? why will there be multiple slabs within one slab class? Am i missing something?

Alain Merigot
  • 10,667
  • 3
  • 18
  • 31
arkham knight
  • 373
  • 7
  • 18

1 Answers1

1

An allocated slab of class slabclass_t is basically a chunk of memory that hosts perslab number of items of size size. If all the items in that slab are used, Memcached allocates another chunk of memory and adds it to the slab_list. These chunks of memory are also referred to as pages or slab_pages.

So if you start a new Memcached server and store one item for a slab class (say size=80), then for this slab class slabs=1. Once you store perslab+1 items in that class you will have slabs=2 and the slab_list will contain 2 items.

Basically, you have a slab_list and slabs its length whereas list_size is its capacity.

I derived most of this from slabs.c so correct me if I got something wrong.

  • Hi, thank you for the explanation. I'm still confused over what's the difference between pages and slabs? You mentioned that an allocated slab is basically a chunk of memory that hosts `perslab` number of items of size `size`. Does the "item" here refer to a chunk since I understood chunk as basic building blocks that store items in the cache? And when you refer to slab, is the same as slab_page or page? – Happytreat Jan 06 '21 at 03:18
  • 1
    Sorry for the confusion. When I wrote "chunk of memory" I just meant a piece/region/slice of memory. You are right, `chunks` are the blocks that store the items. So a `slab` has `perslab` chunks that each holds one item. – Sascha Trifunovic Jan 11 '21 at 09:11
  • 1
    Yes, as I mention above, `slabs` are also called `pages` or `slab_pages`. – Sascha Trifunovic Jan 11 '21 at 09:11