Subversion Repositories HelenOS-historic

Rev

Rev 768 | Rev 771 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 768 Rev 769
Line 24... Line 24...
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
-
 
29
/*
-
 
30
 * The SLAB allocator is closely modelled after Opensolaris SLAB allocator
-
 
31
 * http://www.usenix.org/events/usenix01/full_papers/bonwick/bonwick_html/
-
 
32
 *
-
 
33
 * with the following exceptions:
-
 
34
 *   - empty SLABS are deallocated immediately
-
 
35
 *     (in Linux they are kept in linked list, in Solaris ???)
-
 
36
 *   - empty magazines are deallocated when not needed
-
 
37
 *     (in Solaris they are held in linked list in slab cache)
-
 
38
 *
-
 
39
 *   Following features are not currently supported but would be easy to do:
-
 
40
 *   - cache coloring
-
 
41
 *   - dynamic magazine growing (different magazine sizes are already
-
 
42
 *     supported, but we would need to adjust allocating strategy)
-
 
43
 *
-
 
44
 * The SLAB allocator supports per-CPU caches ('magazines') to facilitate
-
 
45
 * good SMP scaling.
-
 
46
 *
-
 
47
 * When a new object is being allocated, it is first checked, if it is
-
 
48
 * available in CPU-bound magazine. If it is not found there, it is
-
 
49
 * allocated from CPU-shared SLAB - if partial full is found, it is used,
-
 
50
 * otherwise a new one is allocated.
-
 
51
 *
-
 
52
 * When an object is being deallocated, it is put to CPU-bound magazine.
-
 
53
 * If there is no such magazine, new one is allocated (if it fails,
-
 
54
 * the object is deallocated into SLAB). If the magazine is full, it is
-
 
55
 * put into cpu-shared list of magazines and new one is allocated.
-
 
56
 *
-
 
57
 * The CPU-bound magazine is actually a pair of magazine to avoid
-
 
58
 * thrashing when somebody is allocating/deallocating 1 item at the magazine
-
 
59
 * size boundary. LIFO order is enforced, which should avoid fragmentation
-
 
60
 * as much as possible.
-
 
61
 *  
-
 
62
 * Every cache contains list of full slabs and list of partialy full slabs.
-
 
63
 * Empty SLABS are immediately freed (thrashing will be avoided because
-
 
64
 * of magazines).
-
 
65
 *
-
 
66
 * The SLAB information structure is kept inside the data area, if possible.
-
 
67
 * The cache can be marked that it should not use magazines. This is used
-
 
68
 * only for SLAB related caches to avoid deadlocks and infinite recursion
-
 
69
 * (the SLAB allocator uses itself for allocating all it's control structures).
-
 
70
 *
-
 
71
 * The SLAB allocator allocates lot of space and does not free it. When
-
 
72
 * frame allocator fails to allocate the frame, it calls slab_reclaim().
-
 
73
 * It tries 'light reclaim' first, then brutal reclaim. The light reclaim
-
 
74
 * releases slabs from cpu-shared magazine-list, until at least 1 slab
-
 
75
 * is deallocated in each cache (this algorithm should probably change).
-
 
76
 * The brutal reclaim removes all cached objects, even from CPU-bound
-
 
77
 * magazines.
-
 
78
 *
-
 
79
 *
-
 
80
 */
-
 
81
 
-
 
82
 
29
#include <synch/spinlock.h>
83
#include <synch/spinlock.h>
30
#include <mm/slab.h>
84
#include <mm/slab.h>
31
#include <list.h>
85
#include <list.h>
32
#include <memstr.h>
86
#include <memstr.h>
33
#include <align.h>
87
#include <align.h>
Line 38... Line 92...
38
#include <arch.h>
92
#include <arch.h>
39
#include <panic.h>
93
#include <panic.h>
40
#include <debug.h>
94
#include <debug.h>
41
 
95
 
42
SPINLOCK_INITIALIZE(slab_cache_lock);
96
SPINLOCK_INITIALIZE(slab_cache_lock);
43
LIST_INITIALIZE(slab_cache_list);
97
static LIST_INITIALIZE(slab_cache_list);
44
 
-
 
45
slab_cache_t mag_cache;
-
 
46
 
98
 
-
 
99
/** Magazine cache */
-
 
100
static slab_cache_t mag_cache;
-
 
101
/** Cache for cache descriptors */
-
 
102
static slab_cache_t slab_cache_cache;
-
 
103
 
-
 
104
/** Cache for external slab descriptors
-
 
105
 * This time we want per-cpu cache, so do not make it static
-
 
106
 * - using SLAB for internal SLAB structures will not deadlock,
-
 
107
 *   as all slab structures are 'small' - control structures of
-
 
108
 *   their caches do not require further allocation
-
 
109
 */
-
 
110
static slab_cache_t *slab_extern_cache;
47
 
111
 
-
 
112
/** Slab descriptor */
48
typedef struct {
113
typedef struct {
49
    slab_cache_t *cache; /**< Pointer to parent cache */
114
    slab_cache_t *cache; /**< Pointer to parent cache */
50
    link_t link;       /* List of full/partial slabs */
115
    link_t link;       /* List of full/partial slabs */
51
    void *start;       /**< Start address of first available item */
116
    void *start;       /**< Start address of first available item */
52
    count_t available; /**< Count of available items in this slab */
117
    count_t available; /**< Count of available items in this slab */
Line 57... Line 122...
57
/* SLAB allocation functions          */
122
/* SLAB allocation functions          */
58
 
123
 
59
/**
124
/**
60
 * Allocate frames for slab space and initialize
125
 * Allocate frames for slab space and initialize
61
 *
126
 *
62
 * TODO: Change slab_t allocation to slab_alloc(????), malloc with flags!!
-
 
63
 */
127
 */
64
static slab_t * slab_space_alloc(slab_cache_t *cache, int flags)
128
static slab_t * slab_space_alloc(slab_cache_t *cache, int flags)
65
{
129
{
66
    void *data;
130
    void *data;
67
    slab_t *slab;
131
    slab_t *slab;
Line 74... Line 138...
74
    data = (void *)frame_alloc(FRAME_KA | flags, cache->order, &status, &zone);
138
    data = (void *)frame_alloc(FRAME_KA | flags, cache->order, &status, &zone);
75
    if (status != FRAME_OK) {
139
    if (status != FRAME_OK) {
76
        return NULL;
140
        return NULL;
77
    }
141
    }
78
    if (! (cache->flags & SLAB_CACHE_SLINSIDE)) {
142
    if (! (cache->flags & SLAB_CACHE_SLINSIDE)) {
79
        slab = malloc(sizeof(*slab)); // , flags);
143
        slab = slab_alloc(slab_extern_cache, flags);
80
        if (!slab) {
144
        if (!slab) {
81
            frame_free((__address)data);
145
            frame_free((__address)data);
82
            return NULL;
146
            return NULL;
83
        }
147
        }
84
    } else {
148
    } else {
Line 112... Line 176...
112
 */
176
 */
113
static count_t slab_space_free(slab_cache_t *cache, slab_t *slab)
177
static count_t slab_space_free(slab_cache_t *cache, slab_t *slab)
114
{
178
{
115
    frame_free((__address)slab->start);
179
    frame_free((__address)slab->start);
116
    if (! (cache->flags & SLAB_CACHE_SLINSIDE))
180
    if (! (cache->flags & SLAB_CACHE_SLINSIDE))
117
        free(slab);
181
        slab_free(slab_extern_cache, slab);
118
 
182
 
119
    atomic_dec(&cache->allocated_slabs);
183
    atomic_dec(&cache->allocated_slabs);
120
   
184
   
121
    return 1 << cache->order;
185
    return 1 << cache->order;
122
}
186
}
Line 241... Line 305...
241
 
305
 
242
    return frames;
306
    return frames;
243
}
307
}
244
 
308
 
245
/**
309
/**
-
 
310
 * Find full magazine, set it as current and return it
-
 
311
 *
-
 
312
 * Assume cpu_magazine lock is held
-
 
313
 */
-
 
314
static slab_magazine_t * get_full_current_mag(slab_cache_t *cache)
-
 
315
{
-
 
316
    slab_magazine_t *cmag, *lastmag, *newmag;
-
 
317
 
-
 
318
    cmag = cache->mag_cache[CPU->id].current;
-
 
319
    lastmag = cache->mag_cache[CPU->id].last;
-
 
320
    if (cmag) { /* First try local CPU magazines */
-
 
321
        if (cmag->busy)
-
 
322
            return cmag;
-
 
323
 
-
 
324
        if (lastmag && lastmag->busy) {
-
 
325
            cache->mag_cache[CPU->id].current = lastmag;
-
 
326
            cache->mag_cache[CPU->id].last = cmag;
-
 
327
            return lastmag;
-
 
328
        }
-
 
329
    }
-
 
330
    /* Local magazines are empty, import one from magazine list */
-
 
331
    spinlock_lock(&cache->lock);
-
 
332
    if (list_empty(&cache->magazines)) {
-
 
333
        spinlock_unlock(&cache->lock);
-
 
334
        return NULL;
-
 
335
    }
-
 
336
    newmag = list_get_instance(cache->magazines.next,
-
 
337
                   slab_magazine_t,
-
 
338
                   link);
-
 
339
    list_remove(&newmag->link);
-
 
340
    spinlock_unlock(&cache->lock);
-
 
341
 
-
 
342
    if (lastmag)
-
 
343
        slab_free(&mag_cache, lastmag);
-
 
344
    cache->mag_cache[CPU->id].last = cmag;
-
 
345
    cache->mag_cache[CPU->id].current = newmag;
-
 
346
    return newmag;
-
 
347
}
-
 
348
 
-
 
349
/**
246
 * Try to find object in CPU-cache magazines
350
 * Try to find object in CPU-cache magazines
247
 *
351
 *
248
 * @return Pointer to object or NULL if not available
352
 * @return Pointer to object or NULL if not available
249
 */
353
 */
250
static void * magazine_obj_get(slab_cache_t *cache)
354
static void * magazine_obj_get(slab_cache_t *cache)
Line 252... Line 356...
252
    slab_magazine_t *mag;
356
    slab_magazine_t *mag;
253
    void *obj;
357
    void *obj;
254
 
358
 
255
    spinlock_lock(&cache->mag_cache[CPU->id].lock);
359
    spinlock_lock(&cache->mag_cache[CPU->id].lock);
256
 
360
 
257
    mag = cache->mag_cache[CPU->id].current;
361
    mag = get_full_current_mag(cache);
258
    if (!mag)
362
    if (!mag) {
259
        goto out;
-
 
260
 
-
 
261
    if (!mag->busy) {
-
 
262
        /* If current is empty && last exists && not empty, exchange */
-
 
263
        if (cache->mag_cache[CPU->id].last \
363
        spinlock_unlock(&cache->mag_cache[CPU->id].lock);
264
            && cache->mag_cache[CPU->id].last->busy) {
-
 
265
            cache->mag_cache[CPU->id].current = cache->mag_cache[CPU->id].last;
-
 
266
            cache->mag_cache[CPU->id].last = mag;
-
 
267
            mag = cache->mag_cache[CPU->id].current;
-
 
268
            goto gotit;
-
 
269
        }
-
 
270
        /* If still not busy, exchange current with some from
-
 
271
         * other full magazines */
-
 
272
        spinlock_lock(&cache->lock);
-
 
273
        if (list_empty(&cache->magazines)) {
-
 
274
            spinlock_unlock(&cache->lock);
-
 
275
            goto out;
364
        return NULL;
276
        }
-
 
277
        /* Free current magazine and take one from list */
-
 
278
        slab_free(&mag_cache, mag);
-
 
279
 
-
 
280
        mag = list_get_instance(cache->magazines.next,
-
 
281
                    slab_magazine_t,
-
 
282
                    link);
-
 
283
        list_remove(&mag->link);
-
 
284
       
-
 
285
        spinlock_unlock(&cache->lock);
-
 
286
    }
365
    }
287
gotit:
-
 
288
    obj = mag->objs[--mag->busy];
366
    obj = mag->objs[--mag->busy];
289
    spinlock_unlock(&cache->mag_cache[CPU->id].lock);
367
    spinlock_unlock(&cache->mag_cache[CPU->id].lock);
290
    atomic_dec(&cache->cached_objs);
368
    atomic_dec(&cache->cached_objs);
291
   
369
   
292
    return obj;
370
    return obj;
293
out:   
-
 
294
    spinlock_unlock(&cache->mag_cache[CPU->id].lock);
-
 
295
    return NULL;
-
 
296
}
371
}
297
 
372
 
298
/**
373
/**
299
 * Assure that the current magazine is empty, return pointer to it, or NULL if
374
 * Assure that the current magazine is empty, return pointer to it, or NULL if
300
 * no empty magazine available and cannot be allocated
375
 * no empty magazine is available and cannot be allocated
301
 *
376
 *
302
 * We have 2 magazines bound to processor.
377
 * We have 2 magazines bound to processor.
303
 * First try the current.
378
 * First try the current.
304
 *  If full, try the last.
379
 *  If full, try the last.
305
 *   If full, put to magazines list.
380
 *   If full, put to magazines list.
Line 352... Line 427...
352
    slab_magazine_t *mag;
427
    slab_magazine_t *mag;
353
 
428
 
354
    spinlock_lock(&cache->mag_cache[CPU->id].lock);
429
    spinlock_lock(&cache->mag_cache[CPU->id].lock);
355
 
430
 
356
    mag = make_empty_current_mag(cache);
431
    mag = make_empty_current_mag(cache);
357
    if (!mag)
432
    if (!mag) {
-
 
433
        spinlock_unlock(&cache->mag_cache[CPU->id].lock);
358
        goto errout;
434
        return -1;
-
 
435
    }
359
   
436
   
360
    mag->objs[mag->busy++] = obj;
437
    mag->objs[mag->busy++] = obj;
361
 
438
 
362
    spinlock_unlock(&cache->mag_cache[CPU->id].lock);
439
    spinlock_unlock(&cache->mag_cache[CPU->id].lock);
363
    atomic_inc(&cache->cached_objs);
440
    atomic_inc(&cache->cached_objs);
364
    return 0;
441
    return 0;
365
errout:
-
 
366
    spinlock_unlock(&cache->mag_cache[CPU->id].lock);
-
 
367
    return -1;
-
 
368
}
442
}
369
 
443
 
370
 
444
 
371
/**************************************/
445
/**************************************/
372
/* SLAB CACHE functions */
446
/* SLAB CACHE functions */
Line 458... Line 532...
458
                 void (*destructor)(void *obj),
532
                 void (*destructor)(void *obj),
459
                 int flags)
533
                 int flags)
460
{
534
{
461
    slab_cache_t *cache;
535
    slab_cache_t *cache;
462
 
536
 
463
    cache = malloc(sizeof(*cache) + config.cpu_count*sizeof(cache->mag_cache[0]));
537
    cache = slab_alloc(&slab_cache_cache, 0);
464
    _slab_cache_create(cache, name, size, align, constructor, destructor,
538
    _slab_cache_create(cache, name, size, align, constructor, destructor,
465
               flags);
539
               flags);
466
    return cache;
540
    return cache;
467
}
541
}
468
 
542
 
Line 481... Line 555...
481
   
555
   
482
    if (cache->flags & SLAB_CACHE_NOMAGAZINE)
556
    if (cache->flags & SLAB_CACHE_NOMAGAZINE)
483
        return 0; /* Nothing to do */
557
        return 0; /* Nothing to do */
484
   
558
   
485
    /* First lock all cpu caches, then the complete cache lock */
559
    /* First lock all cpu caches, then the complete cache lock */
-
 
560
    if (flags & SLAB_RECLAIM_ALL) {
486
    for (i=0; i < config.cpu_count; i++)
561
        for (i=0; i < config.cpu_count; i++)
487
        spinlock_lock(&cache->mag_cache[i].lock);
562
            spinlock_lock(&cache->mag_cache[i].lock);
-
 
563
    }
488
    spinlock_lock(&cache->lock);
564
    spinlock_lock(&cache->lock);
489
   
565
   
490
    if (flags & SLAB_RECLAIM_ALL) {
566
    if (flags & SLAB_RECLAIM_ALL) {
491
        /* Aggressive memfree */
567
        /* Aggressive memfree */
492
        /* Destroy CPU magazines */
568
        /* Destroy CPU magazines */
Line 516... Line 592...
516
        if (!(flags & SLAB_RECLAIM_ALL) && frames)
592
        if (!(flags & SLAB_RECLAIM_ALL) && frames)
517
            break;
593
            break;
518
    }
594
    }
519
   
595
   
520
    spinlock_unlock(&cache->lock);
596
    spinlock_unlock(&cache->lock);
-
 
597
    if (flags & SLAB_RECLAIM_ALL) {
521
    for (i=0; i < config.cpu_count; i++)
598
        for (i=0; i < config.cpu_count; i++)
522
        spinlock_unlock(&cache->mag_cache[i].lock);
599
            spinlock_unlock(&cache->mag_cache[i].lock);
-
 
600
    }
523
   
601
   
524
    return frames;
602
    return frames;
525
}
603
}
526
 
604
 
527
/** Check that there are no slabs and remove cache from system  */
605
/** Check that there are no slabs and remove cache from system  */
Line 540... Line 618...
540
 
618
 
541
    spinlock_lock(&slab_cache_lock);
619
    spinlock_lock(&slab_cache_lock);
542
    list_remove(&cache->link);
620
    list_remove(&cache->link);
543
    spinlock_unlock(&slab_cache_lock);
621
    spinlock_unlock(&slab_cache_lock);
544
 
622
 
545
    free(cache);
623
    slab_free(&slab_cache_cache, cache);
546
}
624
}
547
 
625
 
548
/** Allocate new object from cache - if no flags given, always returns
626
/** Allocate new object from cache - if no flags given, always returns
549
    memory */
627
    memory */
550
void * slab_alloc(slab_cache_t *cache, int flags)
628
void * slab_alloc(slab_cache_t *cache, int flags)
Line 562... Line 640...
562
        spinlock_lock(&cache->lock);
640
        spinlock_lock(&cache->lock);
563
        result = slab_obj_create(cache, flags);
641
        result = slab_obj_create(cache, flags);
564
        spinlock_unlock(&cache->lock);
642
        spinlock_unlock(&cache->lock);
565
    }
643
    }
566
 
644
 
567
    if (result)
-
 
568
        atomic_inc(&cache->allocated_objs);
-
 
569
 
-
 
570
    interrupts_restore(ipl);
645
    interrupts_restore(ipl);
571
 
646
 
-
 
647
    if (result)
-
 
648
        atomic_inc(&cache->allocated_objs);
572
 
649
 
573
    return result;
650
    return result;
574
}
651
}
575
 
652
 
576
/** Return object to cache  */
653
/** Return object to cache  */
Line 585... Line 662...
585
       
662
       
586
        spinlock_lock(&cache->lock);
663
        spinlock_lock(&cache->lock);
587
        slab_obj_destroy(cache, obj, NULL);
664
        slab_obj_destroy(cache, obj, NULL);
588
        spinlock_unlock(&cache->lock);
665
        spinlock_unlock(&cache->lock);
589
    }
666
    }
590
    atomic_dec(&cache->allocated_objs);
-
 
591
    interrupts_restore(ipl);
667
    interrupts_restore(ipl);
-
 
668
    atomic_dec(&cache->allocated_objs);
592
}
669
}
593
 
670
 
594
/* Go through all caches and reclaim what is possible */
671
/* Go through all caches and reclaim what is possible */
595
count_t slab_reclaim(int flags)
672
count_t slab_reclaim(int flags)
596
{
673
{
Line 637... Line 714...
637
    _slab_cache_create(&mag_cache,
714
    _slab_cache_create(&mag_cache,
638
               "slab_magazine",
715
               "slab_magazine",
639
               sizeof(slab_magazine_t)+SLAB_MAG_SIZE*sizeof(void*),
716
               sizeof(slab_magazine_t)+SLAB_MAG_SIZE*sizeof(void*),
640
               sizeof(__address),
717
               sizeof(__address),
641
               NULL, NULL,
718
               NULL, NULL,
-
 
719
               SLAB_CACHE_NOMAGAZINE | SLAB_CACHE_SLINSIDE);
-
 
720
    /* Initialize slab_cache cache */
-
 
721
    _slab_cache_create(&slab_cache_cache,
-
 
722
               "slab_cache",
-
 
723
               sizeof(slab_cache_cache) + config.cpu_count*sizeof(slab_cache_cache.mag_cache[0]),
-
 
724
               sizeof(__address),
-
 
725
               NULL, NULL,
-
 
726
               SLAB_CACHE_NOMAGAZINE | SLAB_CACHE_SLINSIDE);
-
 
727
    /* Initialize external slab cache */
-
 
728
    slab_extern_cache = slab_cache_create("slab_extern",
-
 
729
                          sizeof(slab_t),
-
 
730
                          0, NULL, NULL,
642
               SLAB_CACHE_NOMAGAZINE);
731
                          SLAB_CACHE_SLINSIDE);
643
 
732
 
644
    /* Initialize structures for malloc */
733
    /* Initialize structures for malloc */
645
}
734
}