summaryrefslogtreecommitdiff
path: root/libtnttk/cache.c
blob: e6e50d90d9887fd8216eaaeef50fb9258744b86c (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
 * Copyright (C) 2006,2007 Richard Braun <syn@sceen.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <errno.h>
#include <stdio.h>
#include <assert.h>
#include <stdint.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/mman.h>

#include <tnttk/list.h>
#include <tnttk/cache.h>

static long page_size;
static long page_mask;

/*
 * Page alignment.
 */
static inline size_t
round_page(size_t offset)
{
  return (offset + (page_size - 1)) & ~page_mask;
}

/*
 * Word alignment.
 */
static inline size_t
round_word(size_t offset)
{
  return (offset + (sizeof(size_t) - 1)) & ~(sizeof(size_t) - 1);
}

/*
 * Color size and mask.
 */
#define SLAB_COLOR_SIZE	sizeof(size_t)
#define SLAB_COLOR_MASK	(SLAB_COLOR_SIZE - 1)

/*
 * Color alignment.
 */
static inline size_t
slab_color_align(size_t size)
{
  return (size + (SLAB_COLOR_SIZE - 1)) & ~SLAB_COLOR_MASK;
}

/*
 * Bufctl, a descriptor of a buffer in a slab.
 *
 * If the buffer is allocated, slab points to the owning slab,
 * otherwise next points to the next buffer in the free list.
 */
union bufctl
{
  struct slab *slab;
  union bufctl *next;
};

typedef union bufctl * bufctl_t;
#define BUFCTL_NULL ((bufctl_t)0)

/*
 * Slab, a block of cached objects.
 */
struct slab
{
  list_link_t link;
  struct cache *cache;
  unsigned int ref_count;
  bufctl_t free_buffers;
  void *address;
};

typedef struct slab * slab_t;
#define SLAB_NULL ((slab_t)0)

/*
 * Cache of objects, using the slab as the base unit of allocation.
 */
struct cache
{
  list_link_t cache_link;
  list_link_t reap_cache_link;
  list_head_t slabs;
  pthread_mutex_t mutex;
  slab_t alloc_slab;
  unsigned int flags;
  const char *name;
  size_t object_size;
  size_t buffer_size;
  size_t slab_size;
  size_t slab_real_size;
  unsigned int objects_per_slab;
  unsigned int free_object_count;
  unsigned int color_offset;
  unsigned int color_max_offset;
  cache_constructor_t constructor;
  cache_destructor_t destructor;
};

/*
 * List of all caches.
 */
static list_head_t cache_list;

/*
 * Lock for cache list.
 */
static pthread_mutex_t cache_list_mutex;

/*
 * List of caches from which pages can be reclaimed by the VM system.
 */
static list_head_t reap_cache_list;

/*
 * Lock for reap cache list.
 */
static pthread_mutex_t reap_cache_list_mutex;

/*
 * Cache of cache objects.
 */
static struct cache cache_cache_store;
static cache_t cache_cache;

/*
 * Lock a cache.
 */
static inline void
cache_lock(cache_t cache)
{
  pthread_mutex_lock(&cache->mutex);
}

/*
 * Unlock a cache.
 */
static inline void
cache_unlock(cache_t cache)
{
  pthread_mutex_unlock(&cache->mutex);
}

/*
 * Check that cache allocation properties are consistent, i.e. that
 * cache->free_objects_count is 0 if and only if cache->alloc_slab indicates
 * there is no slab to allocate from.
 *
 * Return 1 if the cache allocation properties are consistent, 0 otherwise.
 *
 * cache must be locked.
 */
static inline int
cache_alloc_consistent(cache_t cache)
{
  return ((cache->free_object_count == 0) == list_end(cache->alloc_slab));
}

/*
 * Align, compute and set the sizes of objects and buffers in the given cache.
 *
 * cache must be locked.
 */
static inline void
cache_set_object_size(cache_t cache, size_t object_size)
{
  cache->object_size = round_word(object_size);
  cache->buffer_size = cache->object_size + sizeof(union bufctl);
  cache->buffer_size = slab_color_align(cache->buffer_size);
}

/*
 * Compute and set the slab size of a cache. cache_set_object_size()
 * must be called on this cache prior to calling this function.
 *
 * cache must be locked.
 */
static inline void
cache_compute_slab_size(cache_t cache)
{
  size_t slab_size;

  /*
   * A slab must be able to contain at least one buffer and its own descriptor.
   */
  slab_size = round_page(cache->buffer_size + sizeof(struct slab));
  cache->slab_real_size = slab_size;
  cache->slab_size = slab_size - sizeof(struct slab);
  cache->objects_per_slab = cache->slab_size / cache->buffer_size;
}

/*
 * Compute and set the coloring properties of a cache.
 * cache_compute_slab_size() must be called on this cache prior to calling
 * this function.
 *
 * cache must be locked.
 */
static inline void
cache_compute_coloring(cache_t cache)
{
  size_t used_size;

  used_size = cache->buffer_size * cache->objects_per_slab;
  cache->color_max_offset = cache->slab_size - used_size;
  cache->color_offset = 0;
}

/*
 * No op constructor and destructor.
 */
static void
null_constructor(void *object)
{
}

static void
null_destructor(void *object)
{
}

/*
 * Allocate a slab into the given cache.
 *
 * cache must be locked.
 *
 * Return 0 if successful, !0 otherwise.
 */
static int
cache_grow(cache_t cache)
{
  cache_constructor_t constructor;
  bufctl_t bufctl;
  void *slab_data;
  slab_t slab;
  size_t i;

  assert(cache_alloc_consistent(cache));
  slab_data = mmap(NULL, cache->slab_real_size,
                   PROT_READ | PROT_WRITE | PROT_EXEC,
                   MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);

  if (slab_data == NULL)
    return ENOMEM;

  slab = slab_data + cache->slab_size;

  /*
   * Initialize the slab.
   */
  slab->cache = cache;
  slab->ref_count = 0;
  slab->address = slab_data;

  /*
   * cache->alloc_slab is a pointer to a slab which has at least one
   * free object.
   */
  if (list_end(cache->alloc_slab))
    cache->alloc_slab = slab;

  /*
   * Handle cache coloring.
   */
  slab_data += cache->color_offset;
  cache->color_offset += SLAB_COLOR_SIZE;

  if (cache->color_offset >= cache->color_max_offset)
    cache->color_offset = 0;

  /*
   * Set constructor.
   */
  if (cache->constructor == CACHE_CONSTRUCTOR_NULL)
    constructor = null_constructor;

  else
    constructor = cache->constructor;

  /*
   * Initialize bufctls and objects.
   */
  bufctl = slab_data + cache->object_size;
  slab->free_buffers = bufctl;

  for (i = 1; i < cache->objects_per_slab; i++)
    {
      constructor(slab_data);
      slab_data += cache->buffer_size;
      bufctl->next = slab_data + cache->object_size;
      bufctl = bufctl->next;
    }

  constructor(slab_data);
  bufctl->next = BUFCTL_NULL;
  list_insert_tail(&cache->slabs, slab, link);
  cache->free_object_count += cache->objects_per_slab;
  assert(cache_alloc_consistent(cache));
  return 0;
}

/*
 * Remove fully free slabs from the cache, destroy all objects within the
 * slab and release the underlying pages.
 *
 * cache must be locked.
 */
static void
cache_reap(cache_t cache)
{
  cache_destructor_t destructor;
  slab_t slab, prev_slab;
  bufctl_t bufctl;
  void *object;
  int error;

  assert(cache_alloc_consistent(cache));

  /*
   * Set destructor.
   */
  if (cache->destructor == CACHE_DESTRUCTOR_NULL)
    destructor = null_destructor;

  else
    destructor = cache->destructor;

  /*
   * Process the list in reverse order since fully free slabs are at the end.
   */
  for (slab = list_last(&cache->slabs, slab_t);
       !list_end(slab) && (slab->ref_count == 0);
       slab = prev_slab)
    {
      assert(list_end(list_next(slab, link)));
      bufctl = slab->free_buffers;

      while (bufctl != BUFCTL_NULL)
        {
          object = ((void *)bufctl) - cache->object_size;
          destructor(object);
          bufctl = bufctl->next;
        }

      prev_slab = list_prev(slab, link);

      /*
       * Can't use this slab anymore. Use next, which is the list end. There
       * can't be any free slabs left, as cache->alloc_slab always points to
       * the first free (or partially free) slab, and all slabs after this one
       * have already been deleted.
       */
      if (cache->alloc_slab == slab)
        cache->alloc_slab = list_next(slab, link);

      list_remove(&cache->slabs, slab, link);
      cache->free_object_count -= cache->objects_per_slab;
      error = munmap(slab->address, cache->slab_real_size);
      assert(!error);
      assert(cache_alloc_consistent(cache));
    }

  pthread_mutex_lock(&reap_cache_list_mutex);
  list_remove(&reap_cache_list, cache, reap_cache_link);
  pthread_mutex_unlock(&reap_cache_list_mutex);
}

void *
cache_alloc(cache_t cache)
{
  bufctl_t bufctl;
  slab_t slab;
  void *object;

  cache_lock(cache);
  assert(cache_alloc_consistent(cache));

  if (list_end(cache->alloc_slab))
    {
      int error;

      error = cache_grow(cache);

      if (error)
        {
          cache_unlock(cache);
          return NULL;
        }
    }

  slab = cache->alloc_slab;
  bufctl = slab->free_buffers;
  object = ((void *)bufctl) - cache->object_size;
  slab->free_buffers = bufctl->next;
  bufctl->slab = slab;
  slab->ref_count++;
  cache->free_object_count--;

  /*
   * The current slab is full, handle cache->alloc_slab. We can directly take
   * the next slab since they are sorted (full slabs first, partially free
   * slabs next, fully free slabs at the end of the list). If there is no slab
   * left, cache->alloc_slab will be set to the list end.
   */
  if (slab->ref_count == cache->objects_per_slab)
    cache->alloc_slab = list_next(cache->alloc_slab, link);

  assert(cache_alloc_consistent(cache));
  cache_unlock(cache);
  return object;
}

void
cache_free(cache_t cache, void *address)
{
  bufctl_t bufctl;
  slab_t slab;

  cache_lock(cache);
  assert(cache_alloc_consistent(cache));
  bufctl = address + cache->object_size;
  slab = bufctl->slab;
  bufctl->next = slab->free_buffers;
  slab->free_buffers = bufctl;
  slab->ref_count--;
  cache->free_object_count++;

  /*
   * Special case optimized.
   */
  if ((cache->alloc_slab == SLAB_NULL)
      && (slab == list_last(&cache->slabs, slab_t)))
    cache->alloc_slab = slab;

  /*
   * This slab is now partially free. Keep the slab list sorted.
   */
  else if (slab->ref_count == (cache->objects_per_slab - 1))
    {
      list_remove(&cache->slabs, slab, link);

      if (cache->alloc_slab == SLAB_NULL)
        list_insert_tail(&cache->slabs, slab, link);

      else
        list_insert_before(&cache->slabs, cache->alloc_slab, slab, link);

      cache->alloc_slab = slab;
    }

  /*
   * This slab is now completely free, move it at the end of the list for
   * easy removal if memory is reclaimed.
   */
  else if (slab->ref_count == 0)
    {
      if (slab != list_last(&cache->slabs, slab_t))
        {
          if (slab == cache->alloc_slab)
            cache->alloc_slab = list_next(slab, link);

          list_remove(&cache->slabs, slab, link);
          list_insert_tail(&cache->slabs, slab, link);
        }

      if (list_link_null(cache, reap_cache_link))
        {
          pthread_mutex_lock(&reap_cache_list_mutex);
          list_insert_tail(&reap_cache_list, cache, reap_cache_link);
          pthread_mutex_unlock(&reap_cache_list_mutex);
        }
    }

  assert(cache_alloc_consistent(cache));
  cache_unlock(cache);
}

/*
 * Cache constructor.
 */
static void
cache_constructor(void *object)
{
  cache_t cache;

  cache = (cache_t)object;
  list_link_init(cache, reap_cache_link);
  list_init(&cache->slabs);
  pthread_mutex_init(&cache->mutex, NULL);
  cache->alloc_slab = list_last(&cache->slabs, slab_t);
  cache->free_object_count = 0;

  /*
   * XXX Currently unused.
   */
  cache->flags = 0;
}

/*
 * Cache destructor.
 */
static void
cache_destructor(void *object)
{
  cache_t cache;

  cache = (cache_t)object;
  assert(list_link_null(cache, reap_cache_link));
  assert(list_empty(&cache->slabs));
  pthread_mutex_destroy(&cache->mutex);
  assert(cache_alloc_consistent(cache));
  assert(cache->flags == 0);
}

cache_t
cache_create(const char *name, size_t object_size,
             cache_constructor_t constructor,
             cache_destructor_t destructor)
{
  cache_t cache;

  cache = cache_alloc(cache_cache);
  cache->name = name;
  cache_set_object_size(cache, object_size);
  cache_compute_slab_size(cache);
  cache_compute_coloring(cache);
  cache->constructor = constructor;
  cache->destructor = destructor;
  pthread_mutex_lock(&cache_list_mutex);
  list_insert_tail(&cache_list, cache, cache_link);
  pthread_mutex_unlock(&cache_list_mutex);
  return cache;
}

void
cache_destroy(cache_t cache)
{
  cache_lock(cache);
  assert(cache_alloc_consistent(cache));

  if (!list_end(cache->alloc_slab))
    cache_reap(cache);

  assert(list_empty(&cache->slabs));
  pthread_mutex_lock(&cache_list_mutex);
  list_remove(&cache_list, cache, cache_link);
  pthread_mutex_unlock(&cache_list_mutex);
  cache_unlock(cache);
  cache_free(cache_cache, cache);
}

void
cache_show_info(cache_t cache)
{
  if (cache == CACHE_NULL)
    {
      pthread_mutex_lock(&cache_list_mutex);

      list_for_each(&cache_list, cache, cache_link)
        cache_show_info(cache);

      pthread_mutex_unlock(&cache_list_mutex);
    }

  else
    {
      slab_t slab;
      size_t i;

      cache_lock(cache);
      printf("slab: cache info :\n");
      printf("slab: flags             : 0x0 (currently unused)\n");
      printf("slab: name              : %s\n", cache->name);
      printf("slab: object_size       : %u\n", cache->object_size);
      printf("slab: buffer size       : %u\n", cache->buffer_size);
      printf("slab: slab_size         : %u\n", cache->slab_size);
      printf("slab: slab_real_size    : %u\n", cache->slab_real_size);
      printf("slab: objects_per_slab  : %u\n", cache->objects_per_slab);
      printf("slab: free_object_count : %u\n", cache->free_object_count);
      printf("slab: color_offset      : %u\n", cache->color_offset);
      printf("slab: color_max_offset  : %u\n", cache->color_max_offset);

      i = 0;

      list_for_each(&cache->slabs, slab, link)
        {
          printf("slab:  slab %u : ref_count : %u %s\n", i, slab->ref_count,
                 (slab == cache->alloc_slab) ? "(is alloc slab)" : "");
          i++;
        }

      cache_unlock(cache);
    }
}

void
slab_reclaim(void)
{
  cache_t cache;

  pthread_mutex_lock(&reap_cache_list_mutex);

  while (!list_empty(&reap_cache_list))
    {
      cache = list_first(&reap_cache_list, cache_t);
      cache_lock(cache);
      cache_reap(cache);
      cache_unlock(cache);
    }

  pthread_mutex_unlock(&reap_cache_list_mutex);
}

void
slab_init(void)
{
  page_size = sysconf(_SC_PAGESIZE);
  page_mask = page_size - 1;

  /*
   * Manually create the cache of cache objects (we can't use
   * cache_create() here).
   */
  cache_cache = &cache_cache_store;
  cache_constructor(cache_cache);
  cache_cache->name = "cache";
  cache_set_object_size(cache_cache, sizeof(struct cache));
  cache_compute_slab_size(cache_cache);
  cache_compute_coloring(cache_cache);
  cache_cache->constructor = cache_constructor;
  cache_cache->destructor = cache_destructor;
  list_init(&cache_list);
  pthread_mutex_init(&cache_list_mutex, NULL);
  list_init(&reap_cache_list);
  pthread_mutex_init(&reap_cache_list_mutex, NULL);
  list_insert_tail(&cache_list, cache_cache, cache_link);
}