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(__offsetof(struct objcache, cache_percpu[ncpus]),
192 M_OBJCACHE, M_WAITOK | M_ZERO);
193 oc->name = kstrdup(name, M_TEMP);
194 oc->ctor = ctor ? ctor : null_ctor;
195 oc->dtor = dtor ? dtor : null_dtor;
196 oc->privdata = privdata;
199 oc->allocator_args = allocator_args;
202 * Initialize depot list(s).
204 depot = &oc->depot[0];
206 spin_init(&depot->spin);
207 SLIST_INIT(&depot->fullmagazines);
208 SLIST_INIT(&depot->emptymagazines);
211 * Figure out the nominal number of free objects to cache and
212 * the magazine capacity. By default we want to cache up to
213 * half the cluster_limit. If there is no cluster_limit then
214 * we want to cache up to 128 objects.
217 nom_cache = cluster_limit / 2;
218 if (cluster_limit && nom_cache > cluster_limit)
219 nom_cache = cluster_limit;
221 nom_cache = INITIAL_MAG_CAPACITY * 2;
224 * Magazine capacity for 2 active magazines per cpu plus 2
225 * magazines in the depot. Minimum capacity is 4 objects.
227 mag_capacity = nom_cache / (ncpus + 1) / 2 + 1;
228 if (mag_capacity > 128)
230 if (mag_capacity < 4)
232 depot->magcapacity = mag_capacity;
235 * The cluster_limit must be sufficient to have two magazines per
236 * cpu plus at least two magazines in the depot. However, because
237 * partial magazines can stay on the cpus what we really need here
238 * is to specify the number of extra magazines we allocate for the
241 if (cluster_limit == 0) {
242 depot->unallocated_objects = -1;
244 depot->unallocated_objects = ncpus * mag_capacity * 2 +
249 * Initialize per-cpu caches
251 for (cpuid = 0; cpuid < ncpus; cpuid++) {
252 struct percpu_objcache *cache_percpu = &oc->cache_percpu[cpuid];
254 cache_percpu->loaded_magazine = mag_alloc(mag_capacity);
255 cache_percpu->previous_magazine = mag_alloc(mag_capacity);
259 * Compute how many empty magazines to place in the depot. This
260 * determines the retained cache size and is based on nom_cache.
262 * The actual cache size is larger because there are two magazines
263 * for each cpu as well but those can be in any fill state so we
264 * just can't count them.
266 * There is a minimum of two magazines in the depot.
268 nmagdepot = nom_cache / mag_capacity + 1;
272 kprintf("ndepotmags=%-3d x mag_cap=%-3d for %s\n",
273 nmagdepot, mag_capacity, name);
277 * Put empty magazines in depot
279 for (i = 0; i < nmagdepot; i++) {
280 struct magazine *mag = mag_alloc(mag_capacity);
281 SLIST_INSERT_HEAD(&depot->emptymagazines, mag, nextmagazine);
284 spin_lock(&objcachelist_spin);
285 LIST_INSERT_HEAD(&allobjcaches, oc, oc_next);
286 spin_unlock(&objcachelist_spin);
292 objcache_create_simple(malloc_type_t mtype, size_t objsize)
294 struct objcache_malloc_args *margs;
297 margs = kmalloc(sizeof(*margs), M_OBJCACHE, M_WAITOK|M_ZERO);
298 margs->objsize = objsize;
299 margs->mtype = mtype;
300 oc = objcache_create(mtype->ks_shortdesc, 0, 0,
302 objcache_malloc_alloc, objcache_malloc_free,
308 objcache_create_mbacked(malloc_type_t mtype, size_t objsize,
309 int cluster_limit, int nom_cache,
310 objcache_ctor_fn *ctor, objcache_dtor_fn *dtor,
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,
320 cluster_limit, nom_cache,
321 ctor, dtor, privdata,
322 objcache_malloc_alloc, objcache_malloc_free,
328 #define MAGAZINE_EMPTY(mag) (mag->rounds == 0)
329 #define MAGAZINE_NOTEMPTY(mag) (mag->rounds != 0)
330 #define MAGAZINE_FULL(mag) (mag->rounds == mag->capacity)
332 #define swap(x, y) ({ struct magazine *t = x; x = y; y = t; })
335 * Get an object from the object cache.
337 * WARNING! ocflags are only used when we have to go to the underlying
338 * allocator, so we cannot depend on flags such as M_ZERO.
341 objcache_get(struct objcache *oc, int ocflags)
343 struct percpu_objcache *cpucache = &oc->cache_percpu[mycpuid];
344 struct magazine *loadedmag;
345 struct magazine *emptymag;
347 struct magazinedepot *depot;
349 KKASSERT((ocflags & M_ZERO) == 0);
351 ++cpucache->gets_cumulative;
355 * Loaded magazine has an object. This is the hot path.
356 * It is lock-free and uses a critical section to block
357 * out interrupt handlers on the same processor.
359 loadedmag = cpucache->loaded_magazine;
360 if (MAGAZINE_NOTEMPTY(loadedmag)) {
361 obj = loadedmag->objects[--loadedmag->rounds];
366 /* Previous magazine has an object. */
367 if (MAGAZINE_NOTEMPTY(cpucache->previous_magazine)) {
368 swap(cpucache->loaded_magazine, cpucache->previous_magazine);
369 loadedmag = cpucache->loaded_magazine;
370 obj = loadedmag->objects[--loadedmag->rounds];
376 * Both magazines empty. Get a full magazine from the depot and
377 * move one of the empty ones to the depot.
379 * Obtain the depot spinlock.
381 * NOTE: Beyond this point, M_* flags are handled via oc->alloc()
383 depot = &oc->depot[myclusterid];
384 spin_lock(&depot->spin);
387 * Recheck the cpucache after obtaining the depot spinlock. This
388 * shouldn't be necessary now but don't take any chances.
390 if (MAGAZINE_NOTEMPTY(cpucache->loaded_magazine) ||
391 MAGAZINE_NOTEMPTY(cpucache->previous_magazine)
393 spin_unlock(&depot->spin);
397 /* Check if depot has a full magazine. */
398 if (!SLIST_EMPTY(&depot->fullmagazines)) {
399 emptymag = cpucache->previous_magazine;
400 cpucache->previous_magazine = cpucache->loaded_magazine;
401 cpucache->loaded_magazine = SLIST_FIRST(&depot->fullmagazines);
402 SLIST_REMOVE_HEAD(&depot->fullmagazines, nextmagazine);
405 * Return emptymag to the depot.
407 KKASSERT(MAGAZINE_EMPTY(emptymag));
408 SLIST_INSERT_HEAD(&depot->emptymagazines,
409 emptymag, nextmagazine);
410 spin_unlock(&depot->spin);
415 * The depot does not have any non-empty magazines. If we have
416 * not hit our object limit we can allocate a new object using
417 * the back-end allocator.
419 * note: unallocated_objects can be initialized to -1, which has
420 * the effect of removing any allocation limits.
422 if (depot->unallocated_objects) {
423 --depot->unallocated_objects;
424 spin_unlock(&depot->spin);
427 obj = oc->alloc(oc->allocator_args, ocflags);
429 if (oc->ctor(obj, oc->privdata, ocflags))
431 oc->free(obj, oc->allocator_args);
435 spin_lock(&depot->spin);
436 ++depot->unallocated_objects;
437 spin_unlock(&depot->spin);
443 * makes debugging easier when gets_cumulative does
444 * not include gets_null.
446 ++cpucache->gets_null;
447 --cpucache->gets_cumulative;
452 if (oc->exhausted == 0) {
453 kprintf("Warning, objcache(%s): Exhausted!\n", oc->name);
458 * Otherwise block if allowed to.
460 if ((ocflags & (M_WAITOK|M_NULLOK)) == M_WAITOK) {
463 ssleep(depot, &depot->spin, 0, "objcache_get", 0);
466 spin_unlock(&depot->spin);
473 ++cpucache->gets_null;
474 --cpucache->gets_cumulative;
476 spin_unlock(&depot->spin);
481 * Wrapper for malloc allocation routines.
484 objcache_malloc_alloc(void *allocator_args, int ocflags)
486 struct objcache_malloc_args *alloc_args = allocator_args;
488 return (kmalloc(alloc_args->objsize, alloc_args->mtype,
489 ocflags & OC_MFLAGS));
493 objcache_malloc_free(void *obj, void *allocator_args)
495 struct objcache_malloc_args *alloc_args = allocator_args;
497 kfree(obj, alloc_args->mtype);
501 * Wrapper for allocation policies that pre-allocate at initialization time
502 * and don't do run-time allocation.
505 objcache_nop_alloc(void *allocator_args, int ocflags)
511 objcache_nop_free(void *obj, void *allocator_args)
516 * Return an object to the object cache.
519 objcache_put(struct objcache *oc, void *obj)
521 struct percpu_objcache *cpucache = &oc->cache_percpu[mycpuid];
522 struct magazine *loadedmag;
523 struct magazinedepot *depot;
526 ++cpucache->puts_cumulative;
528 if (CLUSTER_OF(obj) != myclusterid) {
530 /* use lazy IPI to send object to owning cluster XXX todo */
531 ++cpucache->puts_othercluster;
539 * Free slot available in loaded magazine. This is the hot path.
540 * It is lock-free and uses a critical section to block out interrupt
541 * handlers on the same processor.
543 loadedmag = cpucache->loaded_magazine;
544 if (!MAGAZINE_FULL(loadedmag)) {
545 loadedmag->objects[loadedmag->rounds++] = obj;
546 if (cpucache->waiting)
547 wakeup_mycpu(&oc->depot[myclusterid]);
553 * Current magazine full, but previous magazine has room. XXX
555 if (!MAGAZINE_FULL(cpucache->previous_magazine)) {
556 swap(cpucache->loaded_magazine, cpucache->previous_magazine);
557 loadedmag = cpucache->loaded_magazine;
558 loadedmag->objects[loadedmag->rounds++] = obj;
559 if (cpucache->waiting)
560 wakeup_mycpu(&oc->depot[myclusterid]);
566 * Both magazines full. Get an empty magazine from the depot and
567 * move a full loaded magazine to the depot. Even though the
568 * magazine may wind up with space available after we block on
569 * the spinlock, we still cycle it through to avoid the non-optimal
572 * Obtain the depot spinlock.
574 depot = &oc->depot[myclusterid];
575 spin_lock(&depot->spin);
578 * If an empty magazine is available in the depot, cycle it
581 if (!SLIST_EMPTY(&depot->emptymagazines)) {
582 loadedmag = cpucache->previous_magazine;
583 cpucache->previous_magazine = cpucache->loaded_magazine;
584 cpucache->loaded_magazine = SLIST_FIRST(&depot->emptymagazines);
585 SLIST_REMOVE_HEAD(&depot->emptymagazines, nextmagazine);
588 * Return loadedmag to the depot. Due to blocking it may
589 * not be entirely full and could even be empty.
591 if (MAGAZINE_EMPTY(loadedmag)) {
592 SLIST_INSERT_HEAD(&depot->emptymagazines,
593 loadedmag, nextmagazine);
594 spin_unlock(&depot->spin);
596 SLIST_INSERT_HEAD(&depot->fullmagazines,
597 loadedmag, nextmagazine);
598 spin_unlock(&depot->spin);
606 * An empty mag is not available. This is a corner case which can
607 * occur due to cpus holding partially full magazines. Do not try
608 * to allocate a mag, just free the object.
610 ++depot->unallocated_objects;
611 spin_unlock(&depot->spin);
615 oc->dtor(obj, oc->privdata);
616 oc->free(obj, oc->allocator_args);
620 * The object is being put back into the cache, but the caller has
621 * indicated that the object is not in any shape to be reused and should
622 * be dtor'd immediately.
625 objcache_dtor(struct objcache *oc, void *obj)
627 struct magazinedepot *depot;
629 depot = &oc->depot[myclusterid];
630 spin_lock(&depot->spin);
631 ++depot->unallocated_objects;
632 spin_unlock(&depot->spin);
635 oc->dtor(obj, oc->privdata);
636 oc->free(obj, oc->allocator_args);
640 * Deallocate all objects in a magazine and free the magazine if requested.
641 * When freeit is TRUE the magazine must already be disassociated from the
644 * Must be called with a critical section held when called with a per-cpu
645 * magazine. The magazine may be indirectly modified during the loop.
647 * If the magazine moves during a dtor the operation is aborted. This is
648 * only allowed when freeit is FALSE.
650 * The number of objects freed is returned.
653 mag_purge(struct objcache *oc, struct magazine **magp, int freeit)
655 struct magazine *mag = *magp;
660 while (mag->rounds) {
661 obj = mag->objects[--mag->rounds];
662 oc->dtor(obj, oc->privdata); /* MAY BLOCK */
663 oc->free(obj, oc->allocator_args); /* MAY BLOCK */
667 * Cycle for interrupts.
669 if ((count & 15) == 0) {
675 * mag may have become invalid either due to dtor/free
676 * blocking or interrupt cycling, do not derefernce it
680 kprintf("mag_purge: mag ripped out\n");
685 KKASSERT(*magp == mag);
687 kfree(mag, M_OBJMAG);
693 * Disassociate zero or more magazines from a magazine list associated with
694 * the depot, update the depot, and move the magazines to a temporary
697 * The caller must check the depot for waiters and wake it up, typically
698 * after disposing of the magazines this function loads onto the temporary
702 maglist_disassociate(struct magazinedepot *depot, struct magazinelist *maglist,
703 struct magazinelist *tmplist, boolean_t purgeall)
705 struct magazine *mag;
707 while ((mag = SLIST_FIRST(maglist)) != NULL) {
708 SLIST_REMOVE_HEAD(maglist, nextmagazine);
709 SLIST_INSERT_HEAD(tmplist, mag, nextmagazine);
710 depot->unallocated_objects += mag->rounds;
715 * Deallocate all magazines and their contents from the passed temporary
716 * list. The magazines have already been accounted for by their depots.
718 * The total number of rounds freed is returned. This number is typically
719 * only used to determine whether a wakeup on the depot is needed or not.
722 maglist_purge(struct objcache *oc, struct magazinelist *maglist)
724 struct magazine *mag;
728 * can't use SLIST_FOREACH because blocking releases the depot
732 while ((mag = SLIST_FIRST(maglist)) != NULL) {
733 SLIST_REMOVE_HEAD(maglist, nextmagazine);
734 count += mag_purge(oc, &mag, TRUE);
741 * De-allocates all magazines on the full and empty magazine lists.
743 * Because this routine is called with a spinlock held, the magazines
744 * can only be disassociated and moved to a temporary list, not freed.
746 * The caller is responsible for freeing the magazines.
749 depot_disassociate(struct magazinedepot *depot, struct magazinelist *tmplist)
751 maglist_disassociate(depot, &depot->fullmagazines, tmplist, TRUE);
752 maglist_disassociate(depot, &depot->emptymagazines, tmplist, TRUE);
757 objcache_reclaim(struct objcache *oc)
759 struct percpu_objcache *cache_percpu = &oc->cache_percpu[myclusterid];
760 struct magazinedepot *depot = &oc->depot[myclusterid];
761 struct magazinelist tmplist;
764 SLIST_INIT(&tmplist);
766 count = mag_purge(oc, &cache_percpu->loaded_magazine, FALSE);
767 count += mag_purge(oc, &cache_percpu->previous_magazine, FALSE);
770 spin_lock(&depot->spin);
771 depot->unallocated_objects += count;
772 depot_disassociate(depot, &tmplist);
773 spin_unlock(&depot->spin);
774 count += maglist_purge(oc, &tmplist);
775 if (count && depot->waiting)
781 * Try to free up some memory. Return as soon as some free memory is found.
782 * For each object cache on the reclaim list, first try the current per-cpu
783 * cache, then the full magazine depot.
786 objcache_reclaimlist(struct objcache *oclist[], int nlist, int ocflags)
789 struct percpu_objcache *cpucache;
790 struct magazinedepot *depot;
791 struct magazinelist tmplist;
794 kprintf("objcache_reclaimlist\n");
796 SLIST_INIT(&tmplist);
798 for (i = 0; i < nlist; i++) {
800 cpucache = &oc->cache_percpu[mycpuid];
801 depot = &oc->depot[myclusterid];
804 count = mag_purge(oc, &cpucache->loaded_magazine, FALSE);
806 count += mag_purge(oc, &cpucache->previous_magazine, FALSE);
809 spin_lock(&depot->spin);
810 depot->unallocated_objects += count;
811 spin_unlock(&depot->spin);
816 spin_lock(&depot->spin);
817 maglist_disassociate(depot, &depot->fullmagazines,
819 spin_unlock(&depot->spin);
820 count = maglist_purge(oc, &tmplist);
831 * Destroy an object cache. Must have no existing references.
834 objcache_destroy(struct objcache *oc)
836 struct percpu_objcache *cache_percpu;
837 struct magazinedepot *depot;
838 int clusterid, cpuid;
839 struct magazinelist tmplist;
841 spin_lock(&objcachelist_spin);
842 LIST_REMOVE(oc, oc_next);
843 spin_unlock(&objcachelist_spin);
845 SLIST_INIT(&tmplist);
846 for (clusterid = 0; clusterid < MAXCLUSTERS; clusterid++) {
847 depot = &oc->depot[clusterid];
848 spin_lock(&depot->spin);
849 depot_disassociate(depot, &tmplist);
850 spin_unlock(&depot->spin);
852 maglist_purge(oc, &tmplist);
854 for (cpuid = 0; cpuid < ncpus; cpuid++) {
855 cache_percpu = &oc->cache_percpu[cpuid];
858 mag_purge(oc, &cache_percpu->loaded_magazine, TRUE);
859 mag_purge(oc, &cache_percpu->previous_magazine, TRUE);
861 cache_percpu->loaded_magazine = NULL;
862 cache_percpu->previous_magazine = NULL;
863 /* don't bother adjusting depot->unallocated_objects */
866 kfree(oc->name, M_TEMP);
867 kfree(oc, M_OBJCACHE);
872 * Populate the per-cluster depot with elements from a linear block
873 * of memory. Must be called for individually for each cluster.
874 * Populated depots should not be destroyed.
877 objcache_populate_linear(struct objcache *oc, void *base, int nelts, int size)
880 char *end = (char *)base + (nelts * size);
881 struct magazinedepot *depot = &oc->depot[myclusterid];
882 struct magazine *emptymag = mag_alloc(depot->magcapcity);
885 emptymag->objects[emptymag->rounds++] = p;
886 if (MAGAZINE_FULL(emptymag)) {
887 spin_lock_wr(&depot->spin);
888 SLIST_INSERT_HEAD(&depot->fullmagazines, emptymag,
890 depot->unallocated_objects += emptymag->rounds;
891 spin_unlock_wr(&depot->spin);
894 emptymag = mag_alloc(depot->magcapacity);
898 if (MAGAZINE_EMPTY(emptymag)) {
900 mag_purge(oc, &emptymag, TRUE);
903 spin_lock_wr(&depot->spin);
904 SLIST_INSERT_HEAD(&depot->fullmagazines, emptymag,
906 depot->unallocated_objects += emptymag->rounds;
907 spin_unlock_wr(&depot->spin);
910 emptymag = mag_alloc(depot->magcapacity);
917 * Check depot contention once a minute.
918 * 2 contested locks per second allowed.
920 static int objcache_rebalance_period;
921 static const int objcache_contention_rate = 120;
922 static struct callout objcache_callout;
924 #define MAXMAGSIZE 512
927 * Check depot contention and increase magazine size if necessary.
930 objcache_timer(void *dummy)
933 struct magazinedepot *depot;
934 struct magazinelist tmplist;
936 XXX we need to detect when an objcache is destroyed out from under
939 SLIST_INIT(&tmplist);
941 spin_lock_wr(&objcachelist_spin);
942 LIST_FOREACH(oc, &allobjcaches, oc_next) {
943 depot = &oc->depot[myclusterid];
944 if (depot->magcapacity < MAXMAGSIZE) {
945 if (depot->contested > objcache_contention_rate) {
946 spin_lock_wr(&depot->spin);
947 depot_disassociate(depot, &tmplist);
948 depot->magcapacity *= 2;
949 spin_unlock_wr(&depot->spin);
950 kprintf("objcache_timer: increasing cache %s"
951 " magsize to %d, contested %d times\n",
952 oc->name, depot->magcapacity,
955 depot->contested = 0;
957 spin_unlock_wr(&objcachelist_spin);
958 if (maglist_purge(oc, &tmplist) > 0 && depot->waiting)
960 spin_lock_wr(&objcachelist_spin);
962 spin_unlock_wr(&objcachelist_spin);
964 callout_reset(&objcache_callout, objcache_rebalance_period,
965 objcache_timer, NULL);
973 spin_init(&objcachelist_spin);
975 callout_init_mp(&objcache_callout);
976 objcache_rebalance_period = 60 * hz;
977 callout_reset(&objcache_callout, objcache_rebalance_period,
978 objcache_timer, NULL);
981 SYSINIT(objcache, SI_BOOT2_OBJCACHE, SI_ORDER_FIRST, objcache_init, 0);