2 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sysproto.h>
32 #include <sys/kernel.h>
33 #include <sys/sysctl.h>
34 #include <sys/vnode.h>
35 #include <sys/malloc.h>
36 #include <sys/mount.h>
41 #include <nfs/xdr_subs.h>
42 #include <nfs/nfsproto.h>
43 #include <nfsserver/nfs.h>
44 #include <nfsserver/nfsm_subs.h>
45 #include <nfsserver/nfs_fha.h>
47 static MALLOC_DEFINE(M_NFS_FHA, "NFS FHA", "NFS FHA");
49 /* Sysctl defaults. */
50 #define DEF_BIN_SHIFT 18 /* 256k */
51 #define DEF_MAX_NFSDS_PER_FH 8
52 #define DEF_MAX_REQS_PER_NFSD 4
56 u_int32_t max_nfsds_per_fh;
57 u_int32_t max_reqs_per_nfsd;
60 struct sysctl_ctx_list fha_clist;
62 SYSCTL_DECL(_vfs_nfsrv);
63 SYSCTL_DECL(_vfs_nfsrv_fha);
65 /* Static sysctl node for the fha from the top-level vfs_nfsrv node. */
66 SYSCTL_NODE(_vfs_nfsrv, OID_AUTO, fha, CTLFLAG_RD, 0, "fha node");
68 /* This is the global structure that represents the state of the fha system. */
69 static struct fha_global {
70 struct fha_hash_entry_list *hashtable;
75 * These are the entries in the filehandle hash. They talk about a specific
76 * file, requests against which are being handled by one or more nfsds. We
77 * keep a chain of nfsds against the file. We only have more than one if reads
78 * are ongoing, and then only if the reads affect disparate regions of the
81 * In general, we want to assign a new request to an existing nfsd if it is
82 * going to contend with work happening already on that nfsd, or if the
83 * operation is a read and the nfsd is already handling a proximate read. We
84 * do this to avoid jumping around in the read stream unnecessarily, and to
85 * avoid contention between threads over single files.
87 struct fha_hash_entry {
88 LIST_ENTRY(fha_hash_entry) link;
93 struct svcthread_list threads;
95 LIST_HEAD(fha_hash_entry_list, fha_hash_entry);
97 /* A structure used for passing around data internally. */
104 static int fhe_stats_sysctl(SYSCTL_HANDLER_ARGS);
107 nfs_fha_init(void *foo)
111 * A small hash table to map filehandles to fha_hash_entry
114 g_fha.hashtable = hashinit(256, M_NFS_FHA, &g_fha.hashmask);
117 * Initialize the sysctl context list for the fha module.
119 sysctl_ctx_init(&fha_clist);
121 fha_ctls.bin_shift = DEF_BIN_SHIFT;
122 fha_ctls.max_nfsds_per_fh = DEF_MAX_NFSDS_PER_FH;
123 fha_ctls.max_reqs_per_nfsd = DEF_MAX_REQS_PER_NFSD;
125 SYSCTL_ADD_UINT(&fha_clist, SYSCTL_STATIC_CHILDREN(_vfs_nfsrv_fha),
126 OID_AUTO, "bin_shift", CTLFLAG_RW,
127 &fha_ctls.bin_shift, 0, "For FHA reads, no two requests will "
128 "contend if they're 2^(bin_shift) bytes apart");
130 SYSCTL_ADD_UINT(&fha_clist, SYSCTL_STATIC_CHILDREN(_vfs_nfsrv_fha),
131 OID_AUTO, "max_nfsds_per_fh", CTLFLAG_RW,
132 &fha_ctls.max_nfsds_per_fh, 0, "Maximum nfsd threads that "
133 "should be working on requests for the same file handle");
135 SYSCTL_ADD_UINT(&fha_clist, SYSCTL_STATIC_CHILDREN(_vfs_nfsrv_fha),
136 OID_AUTO, "max_reqs_per_nfsd", CTLFLAG_RW,
137 &fha_ctls.max_reqs_per_nfsd, 0, "Maximum requests that "
138 "single nfsd thread should be working on at any time");
140 SYSCTL_ADD_OID(&fha_clist, SYSCTL_STATIC_CHILDREN(_vfs_nfsrv_fha),
141 OID_AUTO, "fhe_stats", CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
142 fhe_stats_sysctl, "A", "");
146 nfs_fha_uninit(void *foo)
149 hashdestroy(g_fha.hashtable, M_NFS_FHA, g_fha.hashmask);
152 SYSINIT(nfs_fha, SI_SUB_ROOT_CONF, SI_ORDER_ANY, nfs_fha_init, NULL);
153 SYSUNINIT(nfs_fha, SI_SUB_ROOT_CONF, SI_ORDER_ANY, nfs_fha_uninit, NULL);
156 * This just specifies that offsets should obey affinity when within
157 * the same 1Mbyte (1<<20) chunk for the file (reads only for now).
160 fha_extract_info(struct svc_req *req, struct fha_info *i)
165 static u_int64_t random_fh = 0;
167 int v3 = (req->rq_vers == 3);
172 * We start off with a random fh. If we get a reasonable
173 * procnum, we set the fh. If there's a concept of offset
174 * that we're interested in, we set that.
178 i->locktype = LK_EXCLUSIVE;
181 * Extract the procnum and convert to v3 form if necessary,
182 * taking care to deal with out-of-range procnums. Caller will
183 * ensure that rq_vers is either 2 or 3.
185 procnum = req->rq_proc;
187 if (procnum > NFSV2PROC_STATFS)
189 procnum = nfsrv_nfsv3_procid[procnum];
193 * We do affinity for most. However, we divide a realm of affinity
194 * by file offset so as to allow for concurrent random access. We
195 * only do this for reads today, but this may change when IFS supports
196 * efficient concurrent writes.
198 if (procnum == NFSPROC_FSSTAT ||
199 procnum == NFSPROC_FSINFO ||
200 procnum == NFSPROC_PATHCONF ||
201 procnum == NFSPROC_NOOP ||
202 procnum == NFSPROC_NULL)
205 error = nfs_realign(&req->rq_args, M_DONTWAIT);
209 dpos = mtod(md, caddr_t);
211 /* Grab the filehandle. */
212 error = nfsm_srvmtofh_xx(&fh.fh_generic, v3, &md, &dpos);
216 bcopy(fh.fh_generic.fh_fid.fid_data, &i->fh, sizeof(i->fh));
218 /* Content ourselves with zero offset for all but reads. */
219 if (procnum != NFSPROC_READ)
223 tl = nfsm_dissect_xx_nonblock(2 * NFSX_UNSIGNED, &md, &dpos);
226 i->offset = fxdr_hyper(tl);
228 tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, &md, &dpos);
231 i->offset = fxdr_unsigned(u_int32_t, *tl);
236 case NFSPROC_GETATTR:
239 case NFSPROC_READLINK:
241 case NFSPROC_READDIR:
242 case NFSPROC_READDIRPLUS:
243 i->locktype = LK_SHARED;
245 case NFSPROC_SETATTR:
249 case NFSPROC_SYMLINK:
257 case NFSPROC_PATHCONF:
260 i->locktype = LK_EXCLUSIVE;
265 static struct fha_hash_entry *
266 fha_hash_entry_new(u_int64_t fh)
268 struct fha_hash_entry *e;
270 e = malloc(sizeof(*e), M_NFS_FHA, M_WAITOK);
275 LIST_INIT(&e->threads);
281 fha_hash_entry_destroy(struct fha_hash_entry *e)
284 if (e->num_reads + e->num_writes)
285 panic("nonempty fhe");
290 fha_hash_entry_remove(struct fha_hash_entry *e)
293 LIST_REMOVE(e, link);
294 fha_hash_entry_destroy(e);
297 static struct fha_hash_entry *
298 fha_hash_entry_lookup(SVCPOOL *pool, u_int64_t fh)
300 struct fha_hash_entry *fhe, *new_fhe;
302 LIST_FOREACH(fhe, &g_fha.hashtable[fh % g_fha.hashmask], link)
307 /* Allocate a new entry. */
308 mtx_unlock(&pool->sp_lock);
309 new_fhe = fha_hash_entry_new(fh);
310 mtx_lock(&pool->sp_lock);
312 /* Double-check to make sure we still need the new entry. */
313 LIST_FOREACH(fhe, &g_fha.hashtable[fh % g_fha.hashmask], link)
318 LIST_INSERT_HEAD(&g_fha.hashtable[fh % g_fha.hashmask],
321 fha_hash_entry_destroy(new_fhe);
328 fha_hash_entry_add_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
331 LIST_INSERT_HEAD(&fhe->threads, thread, st_alink);
336 fha_hash_entry_remove_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
339 LIST_REMOVE(thread, st_alink);
344 * Account for an ongoing operation associated with this file.
347 fha_hash_entry_add_op(struct fha_hash_entry *fhe, int locktype, int count)
350 if (LK_EXCLUSIVE == locktype)
351 fhe->num_writes += count;
353 fhe->num_reads += count;
357 get_idle_thread(SVCPOOL *pool)
361 LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink) {
362 if (st->st_xprt == NULL && STAILQ_EMPTY(&st->st_reqs))
370 * Get the service thread currently associated with the fhe that is
371 * appropriate to handle this operation.
374 fha_hash_entry_choose_thread(SVCPOOL *pool, struct fha_hash_entry *fhe,
375 struct fha_info *i, SVCTHREAD *this_thread);
378 fha_hash_entry_choose_thread(SVCPOOL *pool, struct fha_hash_entry *fhe,
379 struct fha_info *i, SVCTHREAD *this_thread)
381 SVCTHREAD *thread, *min_thread = NULL;
382 int req_count, min_count = 0;
383 off_t offset1, offset2;
385 LIST_FOREACH(thread, &fhe->threads, st_alink) {
386 req_count = thread->st_reqcount;
388 /* If there are any writes in progress, use the first thread. */
389 if (fhe->num_writes) {
391 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
392 "fha: %p(%d)w", thread, req_count);
398 * Check for read locality, making sure that we won't
399 * exceed our per-thread load limit in the process.
401 offset1 = i->offset >> fha_ctls.bin_shift;
402 offset2 = STAILQ_FIRST(&thread->st_reqs)->rq_p3
403 >> fha_ctls.bin_shift;
404 if (offset1 == offset2) {
405 if ((fha_ctls.max_reqs_per_nfsd == 0) ||
406 (req_count < fha_ctls.max_reqs_per_nfsd)) {
408 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
409 "fha: %p(%d)r", thread, req_count);
416 * We don't have a locality match, so skip this thread,
417 * but keep track of the most attractive thread in case
418 * we need to come back to it later.
421 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
422 "fha: %p(%d)s off1 %llu off2 %llu", thread,
423 req_count, offset1, offset2);
425 if ((min_thread == NULL) || (req_count < min_count)) {
426 min_count = req_count;
432 * We didn't find a good match yet. See if we can add
433 * a new thread to this file handle entry's thread list.
435 if ((fha_ctls.max_nfsds_per_fh == 0) ||
436 (fhe->num_threads < fha_ctls.max_nfsds_per_fh)) {
438 * We can add a new thread, so try for an idle thread
439 * first, and fall back to this_thread if none are idle.
441 if (STAILQ_EMPTY(&this_thread->st_reqs)) {
442 thread = this_thread;
444 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
445 "fha: %p(%d)t", thread, thread->st_reqcount);
447 } else if ((thread = get_idle_thread(pool))) {
449 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
450 "fha: %p(%d)i", thread, thread->st_reqcount);
453 thread = this_thread;
455 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
456 "fha: %p(%d)b", thread, thread->st_reqcount);
459 fha_hash_entry_add_thread(fhe, thread);
462 * We don't want to use any more threads for this file, so
463 * go back to the most attractive nfsd we're already using.
472 * After getting a request, try to assign it to some thread. Usually we
473 * handle it ourselves.
476 fha_assign(SVCTHREAD *this_thread, struct svc_req *req)
481 struct fha_hash_entry *fhe;
484 * Only do placement if this is an NFS request.
486 if (req->rq_prog != NFS_PROG)
487 return (this_thread);
489 if (req->rq_vers != 2 && req->rq_vers != 3)
490 return (this_thread);
492 pool = req->rq_xprt->xp_pool;
493 fha_extract_info(req, &i);
496 * We save the offset associated with this request for later
499 fhe = fha_hash_entry_lookup(pool, i.fh);
501 req->rq_p2 = i.locktype;
502 req->rq_p3 = i.offset;
505 * Choose a thread, taking into consideration locality, thread load,
506 * and the number of threads already working on this file.
508 thread = fha_hash_entry_choose_thread(pool, fhe, &i, this_thread);
509 KASSERT(thread, ("fha_assign: NULL thread!"));
510 fha_hash_entry_add_op(fhe, i.locktype, 1);
516 * Called when we're done with an operation. The request has already
520 fha_nd_complete(SVCTHREAD *thread, struct svc_req *req)
522 struct fha_hash_entry *fhe = req->rq_p1;
525 * This may be called for reqs that didn't go through
526 * fha_assign (e.g. extra NULL ops used for RPCSEC_GSS.
531 fha_hash_entry_add_op(fhe, req->rq_p2, -1);
533 if (thread->st_reqcount == 0) {
534 fha_hash_entry_remove_thread(fhe, thread);
535 if (0 == fhe->num_reads + fhe->num_writes)
536 fha_hash_entry_remove(fhe);
540 extern SVCPOOL *nfsrv_pool;
543 fhe_stats_sysctl(SYSCTL_HANDLER_ARGS)
547 struct fha_hash_entry *fhe;
551 sbuf_new(&sb, NULL, 4096, SBUF_FIXEDLEN);
554 sbuf_printf(&sb, "NFSD not running\n");
558 mtx_lock(&nfsrv_pool->sp_lock);
560 for (i = 0; i <= g_fha.hashmask; i++)
561 if (!LIST_EMPTY(&g_fha.hashtable[i]))
565 sbuf_printf(&sb, "No file handle entries.\n");
569 for (i = 0; i <= g_fha.hashmask; i++) {
570 LIST_FOREACH(fhe, &g_fha.hashtable[i], link) {
571 sbuf_printf(&sb, "%sfhe %p: {\n", first ? "" : ", ", fhe);
573 sbuf_printf(&sb, " fh: %ju\n", (uintmax_t) fhe->fh);
574 sbuf_printf(&sb, " num_reads: %d\n", fhe->num_reads);
575 sbuf_printf(&sb, " num_writes: %d\n", fhe->num_writes);
576 sbuf_printf(&sb, " num_threads: %d\n", fhe->num_threads);
578 LIST_FOREACH(thread, &fhe->threads, st_alink) {
579 sbuf_printf(&sb, " thread %p (count %d)\n",
580 thread, thread->st_reqcount);
583 sbuf_printf(&sb, "}");
586 /* Limit the output. */
588 sbuf_printf(&sb, "...");
596 mtx_unlock(&nfsrv_pool->sp_lock);
599 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);