2 * Copyright (c) 2005 Jeffrey M. Hsu. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The DragonFly Project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific, prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $DragonFly: src/sys/kern/kern_objcache.c,v 1.23 2008/10/26 04:29:19 sephe Exp $
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/callout.h>
39 #include <sys/globaldata.h>
40 #include <sys/malloc.h>
41 #include <sys/queue.h>
42 #include <sys/objcache.h>
43 #include <sys/spinlock.h>
44 #include <sys/thread.h>
45 #include <sys/thread2.h>
46 #include <sys/spinlock2.h>
48 static MALLOC_DEFINE(M_OBJCACHE, "objcache", "Object Cache");
49 static MALLOC_DEFINE(M_OBJMAG, "objcache magazine", "Object Cache Magazine");
51 #define INITIAL_MAG_CAPACITY 64
56 SLIST_ENTRY(magazine) nextmagazine;
60 SLIST_HEAD(magazinelist, magazine);
62 #define MAGAZINE_HDRSIZE __offsetof(struct magazine, objects[0])
63 #define MAGAZINE_CAPACITY_MAX 128
64 #define MAGAZINE_CAPACITY_MIN 4
67 * per-cluster cache of magazines
69 * All fields in this structure are protected by the spinlock.
71 struct magazinedepot {
73 * The per-cpu object caches only exchanges completely full or
74 * completely empty magazines with the depot layer, so only have
75 * to cache these two types of magazines.
77 struct magazinelist fullmagazines;
78 struct magazinelist emptymagazines;
81 /* protect this structure */
84 /* magazines not yet allocated towards limit */
85 int unallocated_objects;
87 /* infrequently used fields */
88 int waiting; /* waiting for another cpu to
89 * return a full magazine to
91 int contested; /* depot contention count */
95 * per-cpu object cache
96 * All fields in this structure are protected by crit_enter().
98 struct percpu_objcache {
99 struct magazine *loaded_magazine; /* active magazine */
100 struct magazine *previous_magazine; /* backup magazine */
103 int gets_cumulative; /* total calls to get */
104 int gets_null; /* objcache_get returned NULL */
105 int puts_cumulative; /* total calls to put */
106 int puts_othercluster; /* returned to other cluster */
108 /* infrequently used fields */
109 int waiting; /* waiting for a thread on this cpu to
110 * return an obj to the per-cpu cache */
113 /* only until we have NUMA cluster topology information XXX */
114 #define MAXCLUSTERS 1
115 #define myclusterid 0
116 #define CLUSTER_OF(obj) 0
119 * Two-level object cache consisting of NUMA cluster-level depots of
120 * fully loaded or completely empty magazines and cpu-level caches of
121 * individual objects.
126 /* object constructor and destructor from blank storage */
127 objcache_ctor_fn *ctor;
128 objcache_dtor_fn *dtor;
131 /* interface to underlying allocator */
132 objcache_alloc_fn *alloc;
133 objcache_free_fn *free;
134 void *allocator_args;
136 LIST_ENTRY(objcache) oc_next;
137 int exhausted; /* oops */
139 /* NUMA-cluster level caches */
140 struct magazinedepot depot[MAXCLUSTERS];
142 struct percpu_objcache cache_percpu[]; /* per-cpu caches */
145 static struct spinlock objcachelist_spin;
146 static LIST_HEAD(objcachelist, objcache) allobjcaches;
147 static int magazine_capmin;
148 static int magazine_capmax;
150 static struct magazine *
151 mag_alloc(int capacity)
153 struct magazine *mag;
156 size = __offsetof(struct magazine, objects[capacity]);
157 KASSERT(size > 0 && (size & __VM_CACHELINE_MASK) == 0,
158 ("magazine size is not multiple cache line size"));
160 mag = kmalloc_cachealign(size, M_OBJMAG, M_INTWAIT | M_ZERO);
161 mag->capacity = capacity;
167 mag_capacity_align(int mag_capacity)
171 mag_size = __VM_CACHELINE_ALIGN(
172 __offsetof(struct magazine, objects[mag_capacity]));
173 mag_capacity = (mag_size - MAGAZINE_HDRSIZE) / sizeof(void *);
179 * Utility routine for objects that don't require any de-construction.
183 null_dtor(void *obj, void *privdata)
189 null_ctor(void *obj, void *privdata, int ocflags)
195 * Create an object cache.
198 objcache_create(const char *name, int cluster_limit, int nom_cache,
199 objcache_ctor_fn *ctor, objcache_dtor_fn *dtor, void *privdata,
200 objcache_alloc_fn *alloc, objcache_free_fn *free,
201 void *allocator_args)
204 struct magazinedepot *depot;
211 * Allocate object cache structure
213 oc = kmalloc_cachealign(
214 __offsetof(struct objcache, cache_percpu[ncpus]),
215 M_OBJCACHE, M_WAITOK | M_ZERO);
216 oc->name = kstrdup(name, M_TEMP);
217 oc->ctor = ctor ? ctor : null_ctor;
218 oc->dtor = dtor ? dtor : null_dtor;
219 oc->privdata = privdata;
222 oc->allocator_args = allocator_args;
225 * Initialize depot list(s).
227 depot = &oc->depot[0];
229 spin_init(&depot->spin);
230 SLIST_INIT(&depot->fullmagazines);
231 SLIST_INIT(&depot->emptymagazines);
234 * Figure out the nominal number of free objects to cache and
235 * the magazine capacity. By default we want to cache up to
236 * half the cluster_limit. If there is no cluster_limit then
237 * we want to cache up to 128 objects.
240 nom_cache = cluster_limit / 2;
241 if (cluster_limit && nom_cache > cluster_limit)
242 nom_cache = cluster_limit;
244 nom_cache = INITIAL_MAG_CAPACITY * 2;
247 * Magazine capacity for 2 active magazines per cpu plus 2
248 * magazines in the depot.
250 mag_capacity = mag_capacity_align(nom_cache / (ncpus + 1) / 2 + 1);
251 if (mag_capacity > magazine_capmax)
252 mag_capacity = magazine_capmax;
253 else if (mag_capacity < magazine_capmin)
254 mag_capacity = magazine_capmin;
255 depot->magcapacity = mag_capacity;
258 * The cluster_limit must be sufficient to have two magazines per
259 * cpu plus at least two magazines in the depot. However, because
260 * partial magazines can stay on the cpus what we really need here
261 * is to specify the number of extra magazines we allocate for the
264 if (cluster_limit == 0) {
265 depot->unallocated_objects = -1;
267 depot->unallocated_objects = ncpus * mag_capacity * 2 +
272 * Initialize per-cpu caches
274 for (cpuid = 0; cpuid < ncpus; cpuid++) {
275 struct percpu_objcache *cache_percpu = &oc->cache_percpu[cpuid];
277 cache_percpu->loaded_magazine = mag_alloc(mag_capacity);
278 cache_percpu->previous_magazine = mag_alloc(mag_capacity);
282 * Compute how many empty magazines to place in the depot. This
283 * determines the retained cache size and is based on nom_cache.
285 * The actual cache size is larger because there are two magazines
286 * for each cpu as well but those can be in any fill state so we
287 * just can't count them.
289 * There is a minimum of two magazines in the depot.
291 nmagdepot = nom_cache / mag_capacity + 1;
296 * Put empty magazines in depot
298 for (i = 0; i < nmagdepot; i++) {
299 struct magazine *mag = mag_alloc(mag_capacity);
300 SLIST_INSERT_HEAD(&depot->emptymagazines, mag, nextmagazine);
303 spin_lock(&objcachelist_spin);
304 LIST_INSERT_HEAD(&allobjcaches, oc, oc_next);
305 spin_unlock(&objcachelist_spin);
311 objcache_create_simple(malloc_type_t mtype, size_t objsize)
313 struct objcache_malloc_args *margs;
316 margs = kmalloc(sizeof(*margs), M_OBJCACHE, M_WAITOK|M_ZERO);
317 margs->objsize = objsize;
318 margs->mtype = mtype;
319 oc = objcache_create(mtype->ks_shortdesc, 0, 0,
321 objcache_malloc_alloc, objcache_malloc_free,
327 objcache_create_mbacked(malloc_type_t mtype, size_t objsize,
328 int cluster_limit, int nom_cache,
329 objcache_ctor_fn *ctor, objcache_dtor_fn *dtor,
332 struct objcache_malloc_args *margs;
335 margs = kmalloc(sizeof(*margs), M_OBJCACHE, M_WAITOK|M_ZERO);
336 margs->objsize = objsize;
337 margs->mtype = mtype;
338 oc = objcache_create(mtype->ks_shortdesc,
339 cluster_limit, nom_cache,
340 ctor, dtor, privdata,
341 objcache_malloc_alloc, objcache_malloc_free,
347 #define MAGAZINE_EMPTY(mag) (mag->rounds == 0)
348 #define MAGAZINE_NOTEMPTY(mag) (mag->rounds != 0)
349 #define MAGAZINE_FULL(mag) (mag->rounds == mag->capacity)
351 #define swap(x, y) ({ struct magazine *t = x; x = y; y = t; })
354 * Get an object from the object cache.
356 * WARNING! ocflags are only used when we have to go to the underlying
357 * allocator, so we cannot depend on flags such as M_ZERO.
360 objcache_get(struct objcache *oc, int ocflags)
362 struct percpu_objcache *cpucache = &oc->cache_percpu[mycpuid];
363 struct magazine *loadedmag;
364 struct magazine *emptymag;
366 struct magazinedepot *depot;
368 KKASSERT((ocflags & M_ZERO) == 0);
370 ++cpucache->gets_cumulative;
374 * Loaded magazine has an object. This is the hot path.
375 * It is lock-free and uses a critical section to block
376 * out interrupt handlers on the same processor.
378 loadedmag = cpucache->loaded_magazine;
379 if (MAGAZINE_NOTEMPTY(loadedmag)) {
380 obj = loadedmag->objects[--loadedmag->rounds];
385 /* Previous magazine has an object. */
386 if (MAGAZINE_NOTEMPTY(cpucache->previous_magazine)) {
387 swap(cpucache->loaded_magazine, cpucache->previous_magazine);
388 loadedmag = cpucache->loaded_magazine;
389 obj = loadedmag->objects[--loadedmag->rounds];
395 * Both magazines empty. Get a full magazine from the depot and
396 * move one of the empty ones to the depot.
398 * Obtain the depot spinlock.
400 * NOTE: Beyond this point, M_* flags are handled via oc->alloc()
402 depot = &oc->depot[myclusterid];
403 spin_lock(&depot->spin);
406 * Recheck the cpucache after obtaining the depot spinlock. This
407 * shouldn't be necessary now but don't take any chances.
409 if (MAGAZINE_NOTEMPTY(cpucache->loaded_magazine) ||
410 MAGAZINE_NOTEMPTY(cpucache->previous_magazine)
412 spin_unlock(&depot->spin);
416 /* Check if depot has a full magazine. */
417 if (!SLIST_EMPTY(&depot->fullmagazines)) {
418 emptymag = cpucache->previous_magazine;
419 cpucache->previous_magazine = cpucache->loaded_magazine;
420 cpucache->loaded_magazine = SLIST_FIRST(&depot->fullmagazines);
421 SLIST_REMOVE_HEAD(&depot->fullmagazines, nextmagazine);
424 * Return emptymag to the depot.
426 KKASSERT(MAGAZINE_EMPTY(emptymag));
427 SLIST_INSERT_HEAD(&depot->emptymagazines,
428 emptymag, nextmagazine);
429 spin_unlock(&depot->spin);
434 * The depot does not have any non-empty magazines. If we have
435 * not hit our object limit we can allocate a new object using
436 * the back-end allocator.
438 * note: unallocated_objects can be initialized to -1, which has
439 * the effect of removing any allocation limits.
441 if (depot->unallocated_objects) {
442 --depot->unallocated_objects;
443 spin_unlock(&depot->spin);
446 obj = oc->alloc(oc->allocator_args, ocflags);
448 if (oc->ctor(obj, oc->privdata, ocflags))
450 oc->free(obj, oc->allocator_args);
454 spin_lock(&depot->spin);
455 ++depot->unallocated_objects;
456 spin_unlock(&depot->spin);
462 * makes debugging easier when gets_cumulative does
463 * not include gets_null.
465 ++cpucache->gets_null;
466 --cpucache->gets_cumulative;
471 if (oc->exhausted == 0) {
472 kprintf("Warning, objcache(%s): Exhausted!\n", oc->name);
477 * Otherwise block if allowed to.
479 if ((ocflags & (M_WAITOK|M_NULLOK)) == M_WAITOK) {
482 ssleep(depot, &depot->spin, 0, "objcache_get", 0);
485 spin_unlock(&depot->spin);
492 ++cpucache->gets_null;
493 --cpucache->gets_cumulative;
495 spin_unlock(&depot->spin);
500 * Wrapper for malloc allocation routines.
503 objcache_malloc_alloc(void *allocator_args, int ocflags)
505 struct objcache_malloc_args *alloc_args = allocator_args;
507 return (kmalloc(alloc_args->objsize, alloc_args->mtype,
508 ocflags & OC_MFLAGS));
512 objcache_malloc_free(void *obj, void *allocator_args)
514 struct objcache_malloc_args *alloc_args = allocator_args;
516 kfree(obj, alloc_args->mtype);
520 * Wrapper for allocation policies that pre-allocate at initialization time
521 * and don't do run-time allocation.
524 objcache_nop_alloc(void *allocator_args, int ocflags)
530 objcache_nop_free(void *obj, void *allocator_args)
535 * Return an object to the object cache.
538 objcache_put(struct objcache *oc, void *obj)
540 struct percpu_objcache *cpucache = &oc->cache_percpu[mycpuid];
541 struct magazine *loadedmag;
542 struct magazinedepot *depot;
545 ++cpucache->puts_cumulative;
547 if (CLUSTER_OF(obj) != myclusterid) {
549 /* use lazy IPI to send object to owning cluster XXX todo */
550 ++cpucache->puts_othercluster;
558 * Free slot available in loaded magazine. This is the hot path.
559 * It is lock-free and uses a critical section to block out interrupt
560 * handlers on the same processor.
562 loadedmag = cpucache->loaded_magazine;
563 if (!MAGAZINE_FULL(loadedmag)) {
564 loadedmag->objects[loadedmag->rounds++] = obj;
565 if (cpucache->waiting)
566 wakeup_mycpu(&oc->depot[myclusterid]);
572 * Current magazine full, but previous magazine has room. XXX
574 if (!MAGAZINE_FULL(cpucache->previous_magazine)) {
575 swap(cpucache->loaded_magazine, cpucache->previous_magazine);
576 loadedmag = cpucache->loaded_magazine;
577 loadedmag->objects[loadedmag->rounds++] = obj;
578 if (cpucache->waiting)
579 wakeup_mycpu(&oc->depot[myclusterid]);
585 * Both magazines full. Get an empty magazine from the depot and
586 * move a full loaded magazine to the depot. Even though the
587 * magazine may wind up with space available after we block on
588 * the spinlock, we still cycle it through to avoid the non-optimal
591 * Obtain the depot spinlock.
593 depot = &oc->depot[myclusterid];
594 spin_lock(&depot->spin);
597 * If an empty magazine is available in the depot, cycle it
600 if (!SLIST_EMPTY(&depot->emptymagazines)) {
601 loadedmag = cpucache->previous_magazine;
602 cpucache->previous_magazine = cpucache->loaded_magazine;
603 cpucache->loaded_magazine = SLIST_FIRST(&depot->emptymagazines);
604 SLIST_REMOVE_HEAD(&depot->emptymagazines, nextmagazine);
607 * Return loadedmag to the depot. Due to blocking it may
608 * not be entirely full and could even be empty.
610 if (MAGAZINE_EMPTY(loadedmag)) {
611 SLIST_INSERT_HEAD(&depot->emptymagazines,
612 loadedmag, nextmagazine);
613 spin_unlock(&depot->spin);
615 SLIST_INSERT_HEAD(&depot->fullmagazines,
616 loadedmag, nextmagazine);
617 spin_unlock(&depot->spin);
625 * An empty mag is not available. This is a corner case which can
626 * occur due to cpus holding partially full magazines. Do not try
627 * to allocate a mag, just free the object.
629 ++depot->unallocated_objects;
630 spin_unlock(&depot->spin);
634 oc->dtor(obj, oc->privdata);
635 oc->free(obj, oc->allocator_args);
639 * The object is being put back into the cache, but the caller has
640 * indicated that the object is not in any shape to be reused and should
641 * be dtor'd immediately.
644 objcache_dtor(struct objcache *oc, void *obj)
646 struct magazinedepot *depot;
648 depot = &oc->depot[myclusterid];
649 spin_lock(&depot->spin);
650 ++depot->unallocated_objects;
651 spin_unlock(&depot->spin);
654 oc->dtor(obj, oc->privdata);
655 oc->free(obj, oc->allocator_args);
659 * Deallocate all objects in a magazine and free the magazine if requested.
660 * When freeit is TRUE the magazine must already be disassociated from the
663 * Must be called with a critical section held when called with a per-cpu
664 * magazine. The magazine may be indirectly modified during the loop.
666 * If the magazine moves during a dtor the operation is aborted. This is
667 * only allowed when freeit is FALSE.
669 * The number of objects freed is returned.
672 mag_purge(struct objcache *oc, struct magazine **magp, int freeit)
674 struct magazine *mag = *magp;
679 while (mag->rounds) {
680 obj = mag->objects[--mag->rounds];
681 oc->dtor(obj, oc->privdata); /* MAY BLOCK */
682 oc->free(obj, oc->allocator_args); /* MAY BLOCK */
686 * Cycle for interrupts.
688 if ((count & 15) == 0) {
694 * mag may have become invalid either due to dtor/free
695 * blocking or interrupt cycling, do not derefernce it
699 kprintf("mag_purge: mag ripped out\n");
704 KKASSERT(*magp == mag);
706 kfree(mag, M_OBJMAG);
712 * Disassociate zero or more magazines from a magazine list associated with
713 * the depot, update the depot, and move the magazines to a temporary
716 * The caller must check the depot for waiters and wake it up, typically
717 * after disposing of the magazines this function loads onto the temporary
721 maglist_disassociate(struct magazinedepot *depot, struct magazinelist *maglist,
722 struct magazinelist *tmplist, boolean_t purgeall)
724 struct magazine *mag;
726 while ((mag = SLIST_FIRST(maglist)) != NULL) {
727 SLIST_REMOVE_HEAD(maglist, nextmagazine);
728 SLIST_INSERT_HEAD(tmplist, mag, nextmagazine);
729 depot->unallocated_objects += mag->rounds;
734 * Deallocate all magazines and their contents from the passed temporary
735 * list. The magazines have already been accounted for by their depots.
737 * The total number of rounds freed is returned. This number is typically
738 * only used to determine whether a wakeup on the depot is needed or not.
741 maglist_purge(struct objcache *oc, struct magazinelist *maglist)
743 struct magazine *mag;
747 * can't use SLIST_FOREACH because blocking releases the depot
751 while ((mag = SLIST_FIRST(maglist)) != NULL) {
752 SLIST_REMOVE_HEAD(maglist, nextmagazine);
753 count += mag_purge(oc, &mag, TRUE);
760 * De-allocates all magazines on the full and empty magazine lists.
762 * Because this routine is called with a spinlock held, the magazines
763 * can only be disassociated and moved to a temporary list, not freed.
765 * The caller is responsible for freeing the magazines.
768 depot_disassociate(struct magazinedepot *depot, struct magazinelist *tmplist)
770 maglist_disassociate(depot, &depot->fullmagazines, tmplist, TRUE);
771 maglist_disassociate(depot, &depot->emptymagazines, tmplist, TRUE);
776 objcache_reclaim(struct objcache *oc)
778 struct percpu_objcache *cache_percpu = &oc->cache_percpu[myclusterid];
779 struct magazinedepot *depot = &oc->depot[myclusterid];
780 struct magazinelist tmplist;
783 SLIST_INIT(&tmplist);
785 count = mag_purge(oc, &cache_percpu->loaded_magazine, FALSE);
786 count += mag_purge(oc, &cache_percpu->previous_magazine, FALSE);
789 spin_lock(&depot->spin);
790 depot->unallocated_objects += count;
791 depot_disassociate(depot, &tmplist);
792 spin_unlock(&depot->spin);
793 count += maglist_purge(oc, &tmplist);
794 if (count && depot->waiting)
800 * Try to free up some memory. Return as soon as some free memory is found.
801 * For each object cache on the reclaim list, first try the current per-cpu
802 * cache, then the full magazine depot.
805 objcache_reclaimlist(struct objcache *oclist[], int nlist, int ocflags)
808 struct percpu_objcache *cpucache;
809 struct magazinedepot *depot;
810 struct magazinelist tmplist;
813 kprintf("objcache_reclaimlist\n");
815 SLIST_INIT(&tmplist);
817 for (i = 0; i < nlist; i++) {
819 cpucache = &oc->cache_percpu[mycpuid];
820 depot = &oc->depot[myclusterid];
823 count = mag_purge(oc, &cpucache->loaded_magazine, FALSE);
825 count += mag_purge(oc, &cpucache->previous_magazine, FALSE);
828 spin_lock(&depot->spin);
829 depot->unallocated_objects += count;
830 spin_unlock(&depot->spin);
835 spin_lock(&depot->spin);
836 maglist_disassociate(depot, &depot->fullmagazines,
838 spin_unlock(&depot->spin);
839 count = maglist_purge(oc, &tmplist);
850 * Destroy an object cache. Must have no existing references.
853 objcache_destroy(struct objcache *oc)
855 struct percpu_objcache *cache_percpu;
856 struct magazinedepot *depot;
857 int clusterid, cpuid;
858 struct magazinelist tmplist;
860 spin_lock(&objcachelist_spin);
861 LIST_REMOVE(oc, oc_next);
862 spin_unlock(&objcachelist_spin);
864 SLIST_INIT(&tmplist);
865 for (clusterid = 0; clusterid < MAXCLUSTERS; clusterid++) {
866 depot = &oc->depot[clusterid];
867 spin_lock(&depot->spin);
868 depot_disassociate(depot, &tmplist);
869 spin_unlock(&depot->spin);
871 maglist_purge(oc, &tmplist);
873 for (cpuid = 0; cpuid < ncpus; cpuid++) {
874 cache_percpu = &oc->cache_percpu[cpuid];
877 mag_purge(oc, &cache_percpu->loaded_magazine, TRUE);
878 mag_purge(oc, &cache_percpu->previous_magazine, TRUE);
880 cache_percpu->loaded_magazine = NULL;
881 cache_percpu->previous_magazine = NULL;
882 /* don't bother adjusting depot->unallocated_objects */
885 kfree(oc->name, M_TEMP);
886 kfree(oc, M_OBJCACHE);
891 * Populate the per-cluster depot with elements from a linear block
892 * of memory. Must be called for individually for each cluster.
893 * Populated depots should not be destroyed.
896 objcache_populate_linear(struct objcache *oc, void *base, int nelts, int size)
899 char *end = (char *)base + (nelts * size);
900 struct magazinedepot *depot = &oc->depot[myclusterid];
901 struct magazine *emptymag = mag_alloc(depot->magcapcity);
904 emptymag->objects[emptymag->rounds++] = p;
905 if (MAGAZINE_FULL(emptymag)) {
906 spin_lock_wr(&depot->spin);
907 SLIST_INSERT_HEAD(&depot->fullmagazines, emptymag,
909 depot->unallocated_objects += emptymag->rounds;
910 spin_unlock_wr(&depot->spin);
913 emptymag = mag_alloc(depot->magcapacity);
917 if (MAGAZINE_EMPTY(emptymag)) {
919 mag_purge(oc, &emptymag, TRUE);
922 spin_lock_wr(&depot->spin);
923 SLIST_INSERT_HEAD(&depot->fullmagazines, emptymag,
925 depot->unallocated_objects += emptymag->rounds;
926 spin_unlock_wr(&depot->spin);
929 emptymag = mag_alloc(depot->magcapacity);
936 * Check depot contention once a minute.
937 * 2 contested locks per second allowed.
939 static int objcache_rebalance_period;
940 static const int objcache_contention_rate = 120;
941 static struct callout objcache_callout;
943 #define MAXMAGSIZE 512
946 * Check depot contention and increase magazine size if necessary.
949 objcache_timer(void *dummy)
952 struct magazinedepot *depot;
953 struct magazinelist tmplist;
955 XXX we need to detect when an objcache is destroyed out from under
958 SLIST_INIT(&tmplist);
960 spin_lock_wr(&objcachelist_spin);
961 LIST_FOREACH(oc, &allobjcaches, oc_next) {
962 depot = &oc->depot[myclusterid];
963 if (depot->magcapacity < MAXMAGSIZE) {
964 if (depot->contested > objcache_contention_rate) {
965 spin_lock_wr(&depot->spin);
966 depot_disassociate(depot, &tmplist);
967 depot->magcapacity *= 2;
968 spin_unlock_wr(&depot->spin);
969 kprintf("objcache_timer: increasing cache %s"
970 " magsize to %d, contested %d times\n",
971 oc->name, depot->magcapacity,
974 depot->contested = 0;
976 spin_unlock_wr(&objcachelist_spin);
977 if (maglist_purge(oc, &tmplist) > 0 && depot->waiting)
979 spin_lock_wr(&objcachelist_spin);
981 spin_unlock_wr(&objcachelist_spin);
983 callout_reset(&objcache_callout, objcache_rebalance_period,
984 objcache_timer, NULL);
992 spin_init(&objcachelist_spin);
994 magazine_capmin = mag_capacity_align(MAGAZINE_CAPACITY_MIN);
995 magazine_capmax = mag_capacity_align(MAGAZINE_CAPACITY_MAX);
997 kprintf("objcache: magazine cap [%d, %d]\n",
998 magazine_capmin, magazine_capmax);
1002 callout_init_mp(&objcache_callout);
1003 objcache_rebalance_period = 60 * hz;
1004 callout_reset(&objcache_callout, objcache_rebalance_period,
1005 objcache_timer, NULL);
1008 SYSINIT(objcache, SI_BOOT2_OBJCACHE, SI_ORDER_FIRST, objcache_init, 0);