4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
24 #include <sys/spa_impl.h>
25 #include <sys/counter.h>
26 #include <sys/zio_compress.h>
27 #include <sys/zio_checksum.h>
28 #include <sys/zfs_context.h>
30 #include <sys/zfs_refcount.h>
32 #include <sys/vdev_trim.h>
33 #include <sys/vdev_impl.h>
34 #include <sys/dsl_pool.h>
35 #include <sys/zio_checksum.h>
36 #include <sys/multilist.h>
39 #include <sys/fm/fs/zfs.h>
40 #include <sys/eventhandler.h>
41 #include <sys/callb.h>
42 #include <sys/kstat.h>
44 #include <zfs_fletcher.h>
45 #include <sys/arc_impl.h>
47 #include <sys/aggsum.h>
48 #include <sys/vnode.h>
50 #include <machine/vmparam.h>
52 #include <sys/vmmeter.h>
54 static struct sx arc_vnlru_lock;
55 static struct vnode *arc_vnlru_marker;
57 extern struct vfsops zfs_vfsops;
59 uint_t zfs_arc_free_target = 0;
62 arc_free_target_init(void *unused __unused)
64 zfs_arc_free_target = vm_cnt.v_free_target;
66 SYSINIT(arc_free_target_init, SI_SUB_KTHREAD_PAGE, SI_ORDER_ANY,
67 arc_free_target_init, NULL);
70 * We don't have a tunable for arc_free_target due to the dependency on
71 * pagedaemon initialisation.
74 sysctl_vfs_zfs_arc_free_target(SYSCTL_HANDLER_ARGS)
79 val = zfs_arc_free_target;
80 err = sysctl_handle_int(oidp, &val, 0, req);
81 if (err != 0 || req->newptr == NULL)
86 if (val > vm_cnt.v_page_count)
89 zfs_arc_free_target = val;
93 SYSCTL_DECL(_vfs_zfs);
95 SYSCTL_PROC(_vfs_zfs, OID_AUTO, arc_free_target,
96 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof (uint_t),
97 sysctl_vfs_zfs_arc_free_target, "IU",
98 "Desired number of free pages below which ARC triggers reclaim");
102 arc_available_memory(void)
104 int64_t lowest = INT64_MAX;
108 * Cooperate with pagedaemon when it's time for it to scan
109 * and reclaim some pages.
111 n = PAGESIZE * ((int64_t)freemem - zfs_arc_free_target);
115 #if defined(__i386) || !defined(UMA_MD_SMALL_ALLOC)
117 * If we're on an i386 platform, it's possible that we'll exhaust the
118 * kernel heap space before we ever run out of available physical
119 * memory. Most checks of the size of the heap_area compare against
120 * tune.t_minarmem, which is the minimum available real memory that we
121 * can have in the system. However, this is generally fixed at 25 pages
122 * which is so low that it's useless. In this comparison, we seek to
123 * calculate the total heap-size, and reclaim if more than 3/4ths of the
124 * heap is allocated. (Or, in the calculation, if less than 1/4th is
127 n = uma_avail() - (long)(uma_limit() / 4);
133 DTRACE_PROBE1(arc__available_memory, int64_t, lowest);
138 * Return a default max arc size based on the amount of physical memory.
141 arc_default_max(uint64_t min, uint64_t allmem)
145 if (allmem >= 1 << 30)
146 size = allmem - (1 << 30);
149 return (MAX(allmem * 5 / 8, size));
153 * Helper function for arc_prune_async() it is responsible for safely
154 * handling the execution of a registered arc_prune_func_t.
157 arc_prune_task(void *arg)
159 int64_t nr_scan = *(int64_t *)arg;
161 arc_reduce_target_size(ptob(nr_scan));
163 sx_xlock(&arc_vnlru_lock);
164 vnlru_free_vfsops(nr_scan, &zfs_vfsops, arc_vnlru_marker);
165 sx_xunlock(&arc_vnlru_lock);
169 * Notify registered consumers they must drop holds on a portion of the ARC
170 * buffered they reference. This provides a mechanism to ensure the ARC can
171 * honor the arc_meta_limit and reclaim otherwise pinned ARC buffers. This
172 * is analogous to dnlc_reduce_cache() but more generic.
174 * This operation is performed asynchronously so it may be safely called
175 * in the context of the arc_reclaim_thread(). A reference is taken here
176 * for each registered arc_prune_t and the arc_prune_task() is responsible
177 * for releasing it once the registered arc_prune_func_t has completed.
180 arc_prune_async(int64_t adjust)
185 if ((adjustptr = malloc(sizeof (int64_t), M_TEMP, M_NOWAIT)) == NULL)
189 taskq_dispatch(arc_prune_taskq, arc_prune_task, adjustptr, TQ_SLEEP);
190 ARCSTAT_BUMP(arcstat_prune);
196 return (ptob(physmem));
200 arc_memory_throttle(spa_t *spa, uint64_t reserve, uint64_t txg)
206 arc_free_memory(void)
208 return (ptob(freemem));
211 static eventhandler_tag arc_event_lowmem = NULL;
214 arc_lowmem(void *arg __unused, int howto __unused)
216 int64_t free_memory, to_free;
218 arc_no_grow = B_TRUE;
220 arc_growtime = gethrtime() + SEC2NSEC(arc_grow_retry);
221 free_memory = arc_available_memory();
222 to_free = (arc_c >> arc_shrink_shift) - MIN(free_memory, 0);
223 DTRACE_PROBE2(arc__needfree, int64_t, free_memory, int64_t, to_free);
224 arc_reduce_target_size(to_free);
227 * It is unsafe to block here in arbitrary threads, because we can come
228 * here from ARC itself and may hold ARC locks and thus risk a deadlock
229 * with ARC reclaim thread.
231 if (curproc == pageproc)
232 arc_wait_for_eviction(to_free);
234 arc_wait_for_eviction(0);
238 arc_lowmem_init(void)
240 arc_event_lowmem = EVENTHANDLER_REGISTER(vm_lowmem, arc_lowmem, NULL,
241 EVENTHANDLER_PRI_FIRST);
242 arc_vnlru_marker = vnlru_alloc_marker();
243 sx_init(&arc_vnlru_lock, "arc vnlru lock");
247 arc_lowmem_fini(void)
249 if (arc_event_lowmem != NULL)
250 EVENTHANDLER_DEREGISTER(vm_lowmem, arc_event_lowmem);
251 if (arc_vnlru_marker != NULL) {
252 vnlru_free_marker(arc_vnlru_marker);
253 sx_destroy(&arc_vnlru_lock);
258 arc_register_hotplug(void)
263 arc_unregister_hotplug(void)