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);
63 * per-cluster cache of magazines
65 * All fields in this structure are protected by the spinlock.
67 struct magazinedepot {
69 * The per-cpu object caches only exchanges completely full or
70 * completely empty magazines with the depot layer, so only have
71 * to cache these two types of magazines.
73 struct magazinelist fullmagazines;
74 struct magazinelist emptymagazines;
77 /* protect this structure */
80 /* magazines not yet allocated towards limit */
81 int unallocated_objects;
83 /* infrequently used fields */
84 int waiting; /* waiting for another cpu to
85 * return a full magazine to
87 int contested; /* depot contention count */
91 * per-cpu object cache
92 * All fields in this structure are protected by crit_enter().
94 struct percpu_objcache {
95 struct magazine *loaded_magazine; /* active magazine */
96 struct magazine *previous_magazine; /* backup magazine */
99 int gets_cumulative; /* total calls to get */
100 int gets_null; /* objcache_get returned NULL */
101 int puts_cumulative; /* total calls to put */
102 int puts_othercluster; /* returned to other cluster */
104 /* infrequently used fields */
105 int waiting; /* waiting for a thread on this cpu to
106 * return an obj to the per-cpu cache */
109 /* only until we have NUMA cluster topology information XXX */
110 #define MAXCLUSTERS 1
111 #define myclusterid 0
112 #define CLUSTER_OF(obj) 0
115 * Two-level object cache consisting of NUMA cluster-level depots of
116 * fully loaded or completely empty magazines and cpu-level caches of
117 * individual objects.
122 /* object constructor and destructor from blank storage */
123 objcache_ctor_fn *ctor;
124 objcache_dtor_fn *dtor;
127 /* interface to underlying allocator */
128 objcache_alloc_fn *alloc;
129 objcache_free_fn *free;
130 void *allocator_args;
132 LIST_ENTRY(objcache) oc_next;
133 int exhausted; /* oops */
135 /* NUMA-cluster level caches */
136 struct magazinedepot depot[MAXCLUSTERS];
138 struct percpu_objcache cache_percpu[]; /* per-cpu caches */
141 static struct spinlock objcachelist_spin;
142 static LIST_HEAD(objcachelist, objcache) allobjcaches;
144 static struct magazine *
145 mag_alloc(int capacity)
147 struct magazine *mag;
149 mag = kmalloc(__offsetof(struct magazine, objects[capacity]),
150 M_OBJMAG, M_INTWAIT | M_ZERO);
151 mag->capacity = capacity;
157 * Utility routine for objects that don't require any de-construction.
161 null_dtor(void *obj, void *privdata)
167 null_ctor(void *obj, void *privdata, int ocflags)
173 * Create an object cache.
176 objcache_create(const char *name, int cluster_limit, int nom_cache,
177 objcache_ctor_fn *ctor, objcache_dtor_fn *dtor, void *privdata,
178 objcache_alloc_fn *alloc, objcache_free_fn *free,
179 void *allocator_args)
182 struct magazinedepot *depot;
189 * Allocate object cache structure
191 oc = kmalloc_cachealign(
192 __offsetof(struct objcache, cache_percpu[ncpus]),
193 M_OBJCACHE, M_WAITOK | M_ZERO);
194 oc->name = kstrdup(name, M_TEMP);
195 oc->ctor = ctor ? ctor : null_ctor;
196 oc->dtor = dtor ? dtor : null_dtor;
197 oc->privdata = privdata;
200 oc->allocator_args = allocator_args;
203 * Initialize depot list(s).
205 depot = &oc->depot[0];
207 spin_init(&depot->spin);
208 SLIST_INIT(&depot->fullmagazines);
209 SLIST_INIT(&depot->emptymagazines);
212 * Figure out the nominal number of free objects to cache and
213 * the magazine capacity. By default we want to cache up to
214 * half the cluster_limit. If there is no cluster_limit then
215 * we want to cache up to 128 objects.
218 nom_cache = cluster_limit / 2;
219 if (cluster_limit && nom_cache > cluster_limit)
220 nom_cache = cluster_limit;
222 nom_cache = INITIAL_MAG_CAPACITY * 2;
225 * Magazine capacity for 2 active magazines per cpu plus 2
226 * magazines in the depot. Minimum capacity is 4 objects.
228 mag_capacity = nom_cache / (ncpus + 1) / 2 + 1;
229 if (mag_capacity > 128)
231 if (mag_capacity < 4)
233 depot->magcapacity = mag_capacity;
236 * The cluster_limit must be sufficient to have two magazines per
237 * cpu plus at least two magazines in the depot. However, because
238 * partial magazines can stay on the cpus what we really need here
239 * is to specify the number of extra magazines we allocate for the
242 if (cluster_limit == 0) {
243 depot->unallocated_objects = -1;
245 depot->unallocated_objects = ncpus * mag_capacity * 2 +
250 * Initialize per-cpu caches
252 for (cpuid = 0; cpuid < ncpus; cpuid++) {
253 struct percpu_objcache *cache_percpu = &oc->cache_percpu[cpuid];
255 cache_percpu->loaded_magazine = mag_alloc(mag_capacity);
256 cache_percpu->previous_magazine = mag_alloc(mag_capacity);
260 * Compute how many empty magazines to place in the depot. This
261 * determines the retained cache size and is based on nom_cache.
263 * The actual cache size is larger because there are two magazines
264 * for each cpu as well but those can be in any fill state so we
265 * just can't count them.
267 * There is a minimum of two magazines in the depot.
269 nmagdepot = nom_cache / mag_capacity + 1;
273 kprintf("ndepotmags=%-3d x mag_cap=%-3d for %s\n",
274 nmagdepot, mag_capacity, name);
278 * Put empty magazines in depot
280 for (i = 0; i < nmagdepot; i++) {
281 struct magazine *mag = mag_alloc(mag_capacity);
282 SLIST_INSERT_HEAD(&depot->emptymagazines, mag, nextmagazine);
285 spin_lock(&objcachelist_spin);
286 LIST_INSERT_HEAD(&allobjcaches, oc, oc_next);
287 spin_unlock(&objcachelist_spin);
293 objcache_create_simple(malloc_type_t mtype, size_t objsize)
295 struct objcache_malloc_args *margs;
298 margs = kmalloc(sizeof(*margs), M_OBJCACHE, M_WAITOK|M_ZERO);
299 margs->objsize = objsize;
300 margs->mtype = mtype;
301 oc = objcache_create(mtype->ks_shortdesc, 0, 0,
303 objcache_malloc_alloc, objcache_malloc_free,
309 objcache_create_mbacked(malloc_type_t mtype, size_t objsize,
310 int cluster_limit, int nom_cache,
311 objcache_ctor_fn *ctor, objcache_dtor_fn *dtor,
314 struct objcache_malloc_args *margs;
317 margs = kmalloc(sizeof(*margs), M_OBJCACHE, M_WAITOK|M_ZERO);
318 margs->objsize = objsize;
319 margs->mtype = mtype;
320 oc = objcache_create(mtype->ks_shortdesc,
321 cluster_limit, nom_cache,
322 ctor, dtor, privdata,
323 objcache_malloc_alloc, objcache_malloc_free,
329 #define MAGAZINE_EMPTY(mag) (mag->rounds == 0)
330 #define MAGAZINE_NOTEMPTY(mag) (mag->rounds != 0)
331 #define MAGAZINE_FULL(mag) (mag->rounds == mag->capacity)
333 #define swap(x, y) ({ struct magazine *t = x; x = y; y = t; })
336 * Get an object from the object cache.
338 * WARNING! ocflags are only used when we have to go to the underlying
339 * allocator, so we cannot depend on flags such as M_ZERO.
342 objcache_get(struct objcache *oc, int ocflags)
344 struct percpu_objcache *cpucache = &oc->cache_percpu[mycpuid];
345 struct magazine *loadedmag;
346 struct magazine *emptymag;
348 struct magazinedepot *depot;
350 KKASSERT((ocflags & M_ZERO) == 0);
352 ++cpucache->gets_cumulative;
356 * Loaded magazine has an object. This is the hot path.
357 * It is lock-free and uses a critical section to block
358 * out interrupt handlers on the same processor.
360 loadedmag = cpucache->loaded_magazine;
361 if (MAGAZINE_NOTEMPTY(loadedmag)) {
362 obj = loadedmag->objects[--loadedmag->rounds];
367 /* Previous magazine has an object. */
368 if (MAGAZINE_NOTEMPTY(cpucache->previous_magazine)) {
369 swap(cpucache->loaded_magazine, cpucache->previous_magazine);
370 loadedmag = cpucache->loaded_magazine;
371 obj = loadedmag->objects[--loadedmag->rounds];
377 * Both magazines empty. Get a full magazine from the depot and
378 * move one of the empty ones to the depot.
380 * Obtain the depot spinlock.
382 * NOTE: Beyond this point, M_* flags are handled via oc->alloc()
384 depot = &oc->depot[myclusterid];
385 spin_lock(&depot->spin);
388 * Recheck the cpucache after obtaining the depot spinlock. This
389 * shouldn't be necessary now but don't take any chances.
391 if (MAGAZINE_NOTEMPTY(cpucache->loaded_magazine) ||
392 MAGAZINE_NOTEMPTY(cpucache->previous_magazine)
394 spin_unlock(&depot->spin);
398 /* Check if depot has a full magazine. */
399 if (!SLIST_EMPTY(&depot->fullmagazines)) {
400 emptymag = cpucache->previous_magazine;
401 cpucache->previous_magazine = cpucache->loaded_magazine;
402 cpucache->loaded_magazine = SLIST_FIRST(&depot->fullmagazines);
403 SLIST_REMOVE_HEAD(&depot->fullmagazines, nextmagazine);
406 * Return emptymag to the depot.
408 KKASSERT(MAGAZINE_EMPTY(emptymag));
409 SLIST_INSERT_HEAD(&depot->emptymagazines,
410 emptymag, nextmagazine);
411 spin_unlock(&depot->spin);
416 * The depot does not have any non-empty magazines. If we have
417 * not hit our object limit we can allocate a new object using
418 * the back-end allocator.
420 * note: unallocated_objects can be initialized to -1, which has
421 * the effect of removing any allocation limits.
423 if (depot->unallocated_objects) {
424 --depot->unallocated_objects;
425 spin_unlock(&depot->spin);
428 obj = oc->alloc(oc->allocator_args, ocflags);
430 if (oc->ctor(obj, oc->privdata, ocflags))
432 oc->free(obj, oc->allocator_args);
436 spin_lock(&depot->spin);
437 ++depot->unallocated_objects;
438 spin_unlock(&depot->spin);
444 * makes debugging easier when gets_cumulative does
445 * not include gets_null.
447 ++cpucache->gets_null;
448 --cpucache->gets_cumulative;
453 if (oc->exhausted == 0) {
454 kprintf("Warning, objcache(%s): Exhausted!\n", oc->name);
459 * Otherwise block if allowed to.
461 if ((ocflags & (M_WAITOK|M_NULLOK)) == M_WAITOK) {
464 ssleep(depot, &depot->spin, 0, "objcache_get", 0);
467 spin_unlock(&depot->spin);
474 ++cpucache->gets_null;
475 --cpucache->gets_cumulative;
477 spin_unlock(&depot->spin);
482 * Wrapper for malloc allocation routines.
485 objcache_malloc_alloc(void *allocator_args, int ocflags)
487 struct objcache_malloc_args *alloc_args = allocator_args;
489 return (kmalloc(alloc_args->objsize, alloc_args->mtype,
490 ocflags & OC_MFLAGS));
494 objcache_malloc_free(void *obj, void *allocator_args)
496 struct objcache_malloc_args *alloc_args = allocator_args;
498 kfree(obj, alloc_args->mtype);
502 * Wrapper for allocation policies that pre-allocate at initialization time
503 * and don't do run-time allocation.
506 objcache_nop_alloc(void *allocator_args, int ocflags)
512 objcache_nop_free(void *obj, void *allocator_args)
517 * Return an object to the object cache.
520 objcache_put(struct objcache *oc, void *obj)
522 struct percpu_objcache *cpucache = &oc->cache_percpu[mycpuid];
523 struct magazine *loadedmag;
524 struct magazinedepot *depot;
527 ++cpucache->puts_cumulative;
529 if (CLUSTER_OF(obj) != myclusterid) {
531 /* use lazy IPI to send object to owning cluster XXX todo */
532 ++cpucache->puts_othercluster;
540 * Free slot available in loaded magazine. This is the hot path.
541 * It is lock-free and uses a critical section to block out interrupt
542 * handlers on the same processor.
544 loadedmag = cpucache->loaded_magazine;
545 if (!MAGAZINE_FULL(loadedmag)) {
546 loadedmag->objects[loadedmag->rounds++] = obj;
547 if (cpucache->waiting)
548 wakeup_mycpu(&oc->depot[myclusterid]);
554 * Current magazine full, but previous magazine has room. XXX
556 if (!MAGAZINE_FULL(cpucache->previous_magazine)) {
557 swap(cpucache->loaded_magazine, cpucache->previous_magazine);
558 loadedmag = cpucache->loaded_magazine;
559 loadedmag->objects[loadedmag->rounds++] = obj;
560 if (cpucache->waiting)
561 wakeup_mycpu(&oc->depot[myclusterid]);
567 * Both magazines full. Get an empty magazine from the depot and
568 * move a full loaded magazine to the depot. Even though the
569 * magazine may wind up with space available after we block on
570 * the spinlock, we still cycle it through to avoid the non-optimal
573 * Obtain the depot spinlock.
575 depot = &oc->depot[myclusterid];
576 spin_lock(&depot->spin);
579 * If an empty magazine is available in the depot, cycle it
582 if (!SLIST_EMPTY(&depot->emptymagazines)) {
583 loadedmag = cpucache->previous_magazine;
584 cpucache->previous_magazine = cpucache->loaded_magazine;
585 cpucache->loaded_magazine = SLIST_FIRST(&depot->emptymagazines);
586 SLIST_REMOVE_HEAD(&depot->emptymagazines, nextmagazine);
589 * Return loadedmag to the depot. Due to blocking it may
590 * not be entirely full and could even be empty.
592 if (MAGAZINE_EMPTY(loadedmag)) {
593 SLIST_INSERT_HEAD(&depot->emptymagazines,
594 loadedmag, nextmagazine);
595 spin_unlock(&depot->spin);
597 SLIST_INSERT_HEAD(&depot->fullmagazines,
598 loadedmag, nextmagazine);
599 spin_unlock(&depot->spin);
607 * An empty mag is not available. This is a corner case which can
608 * occur due to cpus holding partially full magazines. Do not try
609 * to allocate a mag, just free the object.
611 ++depot->unallocated_objects;
612 spin_unlock(&depot->spin);
616 oc->dtor(obj, oc->privdata);
617 oc->free(obj, oc->allocator_args);
621 * The object is being put back into the cache, but the caller has
622 * indicated that the object is not in any shape to be reused and should
623 * be dtor'd immediately.
626 objcache_dtor(struct objcache *oc, void *obj)
628 struct magazinedepot *depot;
630 depot = &oc->depot[myclusterid];
631 spin_lock(&depot->spin);
632 ++depot->unallocated_objects;
633 spin_unlock(&depot->spin);
636 oc->dtor(obj, oc->privdata);
637 oc->free(obj, oc->allocator_args);
641 * Deallocate all objects in a magazine and free the magazine if requested.
642 * When freeit is TRUE the magazine must already be disassociated from the
645 * Must be called with a critical section held when called with a per-cpu
646 * magazine. The magazine may be indirectly modified during the loop.
648 * If the magazine moves during a dtor the operation is aborted. This is
649 * only allowed when freeit is FALSE.
651 * The number of objects freed is returned.
654 mag_purge(struct objcache *oc, struct magazine **magp, int freeit)
656 struct magazine *mag = *magp;
661 while (mag->rounds) {
662 obj = mag->objects[--mag->rounds];
663 oc->dtor(obj, oc->privdata); /* MAY BLOCK */
664 oc->free(obj, oc->allocator_args); /* MAY BLOCK */
668 * Cycle for interrupts.
670 if ((count & 15) == 0) {
676 * mag may have become invalid either due to dtor/free
677 * blocking or interrupt cycling, do not derefernce it
681 kprintf("mag_purge: mag ripped out\n");
686 KKASSERT(*magp == mag);
688 kfree(mag, M_OBJMAG);
694 * Disassociate zero or more magazines from a magazine list associated with
695 * the depot, update the depot, and move the magazines to a temporary
698 * The caller must check the depot for waiters and wake it up, typically
699 * after disposing of the magazines this function loads onto the temporary
703 maglist_disassociate(struct magazinedepot *depot, struct magazinelist *maglist,
704 struct magazinelist *tmplist, boolean_t purgeall)
706 struct magazine *mag;
708 while ((mag = SLIST_FIRST(maglist)) != NULL) {
709 SLIST_REMOVE_HEAD(maglist, nextmagazine);
710 SLIST_INSERT_HEAD(tmplist, mag, nextmagazine);
711 depot->unallocated_objects += mag->rounds;
716 * Deallocate all magazines and their contents from the passed temporary
717 * list. The magazines have already been accounted for by their depots.
719 * The total number of rounds freed is returned. This number is typically
720 * only used to determine whether a wakeup on the depot is needed or not.
723 maglist_purge(struct objcache *oc, struct magazinelist *maglist)
725 struct magazine *mag;
729 * can't use SLIST_FOREACH because blocking releases the depot
733 while ((mag = SLIST_FIRST(maglist)) != NULL) {
734 SLIST_REMOVE_HEAD(maglist, nextmagazine);
735 count += mag_purge(oc, &mag, TRUE);
742 * De-allocates all magazines on the full and empty magazine lists.
744 * Because this routine is called with a spinlock held, the magazines
745 * can only be disassociated and moved to a temporary list, not freed.
747 * The caller is responsible for freeing the magazines.
750 depot_disassociate(struct magazinedepot *depot, struct magazinelist *tmplist)
752 maglist_disassociate(depot, &depot->fullmagazines, tmplist, TRUE);
753 maglist_disassociate(depot, &depot->emptymagazines, tmplist, TRUE);
758 objcache_reclaim(struct objcache *oc)
760 struct percpu_objcache *cache_percpu = &oc->cache_percpu[myclusterid];
761 struct magazinedepot *depot = &oc->depot[myclusterid];
762 struct magazinelist tmplist;
765 SLIST_INIT(&tmplist);
767 count = mag_purge(oc, &cache_percpu->loaded_magazine, FALSE);
768 count += mag_purge(oc, &cache_percpu->previous_magazine, FALSE);
771 spin_lock(&depot->spin);
772 depot->unallocated_objects += count;
773 depot_disassociate(depot, &tmplist);
774 spin_unlock(&depot->spin);
775 count += maglist_purge(oc, &tmplist);
776 if (count && depot->waiting)
782 * Try to free up some memory. Return as soon as some free memory is found.
783 * For each object cache on the reclaim list, first try the current per-cpu
784 * cache, then the full magazine depot.
787 objcache_reclaimlist(struct objcache *oclist[], int nlist, int ocflags)
790 struct percpu_objcache *cpucache;
791 struct magazinedepot *depot;
792 struct magazinelist tmplist;
795 kprintf("objcache_reclaimlist\n");
797 SLIST_INIT(&tmplist);
799 for (i = 0; i < nlist; i++) {
801 cpucache = &oc->cache_percpu[mycpuid];
802 depot = &oc->depot[myclusterid];
805 count = mag_purge(oc, &cpucache->loaded_magazine, FALSE);
807 count += mag_purge(oc, &cpucache->previous_magazine, FALSE);
810 spin_lock(&depot->spin);
811 depot->unallocated_objects += count;
812 spin_unlock(&depot->spin);
817 spin_lock(&depot->spin);
818 maglist_disassociate(depot, &depot->fullmagazines,
820 spin_unlock(&depot->spin);
821 count = maglist_purge(oc, &tmplist);
832 * Destroy an object cache. Must have no existing references.
835 objcache_destroy(struct objcache *oc)
837 struct percpu_objcache *cache_percpu;
838 struct magazinedepot *depot;
839 int clusterid, cpuid;
840 struct magazinelist tmplist;
842 spin_lock(&objcachelist_spin);
843 LIST_REMOVE(oc, oc_next);
844 spin_unlock(&objcachelist_spin);
846 SLIST_INIT(&tmplist);
847 for (clusterid = 0; clusterid < MAXCLUSTERS; clusterid++) {
848 depot = &oc->depot[clusterid];
849 spin_lock(&depot->spin);
850 depot_disassociate(depot, &tmplist);
851 spin_unlock(&depot->spin);
853 maglist_purge(oc, &tmplist);
855 for (cpuid = 0; cpuid < ncpus; cpuid++) {
856 cache_percpu = &oc->cache_percpu[cpuid];
859 mag_purge(oc, &cache_percpu->loaded_magazine, TRUE);
860 mag_purge(oc, &cache_percpu->previous_magazine, TRUE);
862 cache_percpu->loaded_magazine = NULL;
863 cache_percpu->previous_magazine = NULL;
864 /* don't bother adjusting depot->unallocated_objects */
867 kfree(oc->name, M_TEMP);
868 kfree(oc, M_OBJCACHE);
873 * Populate the per-cluster depot with elements from a linear block
874 * of memory. Must be called for individually for each cluster.
875 * Populated depots should not be destroyed.
878 objcache_populate_linear(struct objcache *oc, void *base, int nelts, int size)
881 char *end = (char *)base + (nelts * size);
882 struct magazinedepot *depot = &oc->depot[myclusterid];
883 struct magazine *emptymag = mag_alloc(depot->magcapcity);
886 emptymag->objects[emptymag->rounds++] = p;
887 if (MAGAZINE_FULL(emptymag)) {
888 spin_lock_wr(&depot->spin);
889 SLIST_INSERT_HEAD(&depot->fullmagazines, emptymag,
891 depot->unallocated_objects += emptymag->rounds;
892 spin_unlock_wr(&depot->spin);
895 emptymag = mag_alloc(depot->magcapacity);
899 if (MAGAZINE_EMPTY(emptymag)) {
901 mag_purge(oc, &emptymag, TRUE);
904 spin_lock_wr(&depot->spin);
905 SLIST_INSERT_HEAD(&depot->fullmagazines, emptymag,
907 depot->unallocated_objects += emptymag->rounds;
908 spin_unlock_wr(&depot->spin);
911 emptymag = mag_alloc(depot->magcapacity);
918 * Check depot contention once a minute.
919 * 2 contested locks per second allowed.
921 static int objcache_rebalance_period;
922 static const int objcache_contention_rate = 120;
923 static struct callout objcache_callout;
925 #define MAXMAGSIZE 512
928 * Check depot contention and increase magazine size if necessary.
931 objcache_timer(void *dummy)
934 struct magazinedepot *depot;
935 struct magazinelist tmplist;
937 XXX we need to detect when an objcache is destroyed out from under
940 SLIST_INIT(&tmplist);
942 spin_lock_wr(&objcachelist_spin);
943 LIST_FOREACH(oc, &allobjcaches, oc_next) {
944 depot = &oc->depot[myclusterid];
945 if (depot->magcapacity < MAXMAGSIZE) {
946 if (depot->contested > objcache_contention_rate) {
947 spin_lock_wr(&depot->spin);
948 depot_disassociate(depot, &tmplist);
949 depot->magcapacity *= 2;
950 spin_unlock_wr(&depot->spin);
951 kprintf("objcache_timer: increasing cache %s"
952 " magsize to %d, contested %d times\n",
953 oc->name, depot->magcapacity,
956 depot->contested = 0;
958 spin_unlock_wr(&objcachelist_spin);
959 if (maglist_purge(oc, &tmplist) > 0 && depot->waiting)
961 spin_lock_wr(&objcachelist_spin);
963 spin_unlock_wr(&objcachelist_spin);
965 callout_reset(&objcache_callout, objcache_rebalance_period,
966 objcache_timer, NULL);
974 spin_init(&objcachelist_spin);
976 callout_init_mp(&objcache_callout);
977 objcache_rebalance_period = 60 * hz;
978 callout_reset(&objcache_callout, objcache_rebalance_period,
979 objcache_timer, NULL);
982 SYSINIT(objcache, SI_BOOT2_OBJCACHE, SI_ORDER_FIRST, objcache_init, 0);