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/nfs_fha.h>
43 static MALLOC_DEFINE(M_NFS_FHA, "NFS FHA", "NFS FHA");
46 * XXX need to commonize definitions between old and new NFS code. Define
47 * this here so we don't include one nfsproto.h over the other.
49 #define NFS_PROG 100003
52 fha_init(struct fha_params *softc)
57 * A small hash table to map filehandles to fha_hash_entry
60 softc->g_fha.hashtable = hashinit(256, M_NFS_FHA,
61 &softc->g_fha.hashmask);
64 * Set the default tuning parameters.
66 softc->ctls.enable = FHA_DEF_ENABLE;
67 softc->ctls.bin_shift = FHA_DEF_BIN_SHIFT;
68 softc->ctls.max_nfsds_per_fh = FHA_DEF_MAX_NFSDS_PER_FH;
69 softc->ctls.max_reqs_per_nfsd = FHA_DEF_MAX_REQS_PER_NFSD;
72 * Allow the user to override the defaults at boot time with
75 snprintf(tmpstr, sizeof(tmpstr), "vfs.%s.fha.enable",
77 TUNABLE_INT_FETCH(tmpstr, &softc->ctls.enable);
78 snprintf(tmpstr, sizeof(tmpstr), "vfs.%s.fha.bin_shift",
80 TUNABLE_INT_FETCH(tmpstr, &softc->ctls.bin_shift);
81 snprintf(tmpstr, sizeof(tmpstr), "vfs.%s.fha.max_nfsds_per_fh",
83 TUNABLE_INT_FETCH(tmpstr, &softc->ctls.max_nfsds_per_fh);
84 snprintf(tmpstr, sizeof(tmpstr), "vfs.%s.fha.max_reqs_per_nfsd",
86 TUNABLE_INT_FETCH(tmpstr, &softc->ctls.max_reqs_per_nfsd);
89 * Add sysctls so the user can change the tuning parameters at
92 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
93 OID_AUTO, "enable", CTLFLAG_RW,
94 &softc->ctls.enable, 0, "Enable NFS File Handle Affinity (FHA)");
96 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
97 OID_AUTO, "bin_shift", CTLFLAG_RW,
98 &softc->ctls.bin_shift, 0, "For FHA reads, no two requests will "
99 "contend if they're 2^(bin_shift) bytes apart");
101 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
102 OID_AUTO, "max_nfsds_per_fh", CTLFLAG_RW,
103 &softc->ctls.max_nfsds_per_fh, 0, "Maximum nfsd threads that "
104 "should be working on requests for the same file handle");
106 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
107 OID_AUTO, "max_reqs_per_nfsd", CTLFLAG_RW,
108 &softc->ctls.max_reqs_per_nfsd, 0, "Maximum requests that "
109 "single nfsd thread should be working on at any time");
111 SYSCTL_ADD_OID(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
112 OID_AUTO, "fhe_stats", CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
113 softc->callbacks.fhe_stats_sysctl, "A", "");
118 fha_uninit(struct fha_params *softc)
120 sysctl_ctx_free(&softc->sysctl_ctx);
121 hashdestroy(softc->g_fha.hashtable, M_NFS_FHA, softc->g_fha.hashmask);
125 * This just specifies that offsets should obey affinity when within
126 * the same 1Mbyte (1<<20) chunk for the file (reads only for now).
129 fha_extract_info(struct svc_req *req, struct fha_info *i,
130 struct fha_callbacks *cb)
135 static u_int64_t random_fh = 0;
137 int v3 = (req->rq_vers == 3);
141 * We start off with a random fh. If we get a reasonable
142 * procnum, we set the fh. If there's a concept of offset
143 * that we're interested in, we set that.
147 i->locktype = LK_EXCLUSIVE;
150 * Extract the procnum and convert to v3 form if necessary,
151 * taking care to deal with out-of-range procnums. Caller will
152 * ensure that rq_vers is either 2 or 3.
154 procnum = req->rq_proc;
156 rpcproc_t tmp_procnum;
158 tmp_procnum = cb->get_procnum(procnum);
159 if (tmp_procnum == -1)
161 procnum = tmp_procnum;
165 * We do affinity for most. However, we divide a realm of affinity
166 * by file offset so as to allow for concurrent random access. We
167 * only do this for reads today, but this may change when IFS supports
168 * efficient concurrent writes.
170 if (cb->no_offset(procnum))
173 error = cb->realign(&req->rq_args, M_NOWAIT);
177 dpos = mtod(md, caddr_t);
179 /* Grab the filehandle. */
180 error = cb->get_fh(&fh, v3, &md, &dpos);
184 bcopy(fh.fh_fid.fid_data, &i->fh, sizeof(i->fh));
186 /* Content ourselves with zero offset for all but reads. */
187 if (cb->is_read(procnum) || cb->is_write(procnum))
188 cb->get_offset(&md, &dpos, v3, i);
191 cb->set_locktype(procnum, i);
194 static struct fha_hash_entry *
195 fha_hash_entry_new(u_int64_t fh)
197 struct fha_hash_entry *e;
199 e = malloc(sizeof(*e), M_NFS_FHA, M_WAITOK);
202 e->num_exclusive = 0;
204 LIST_INIT(&e->threads);
210 fha_hash_entry_destroy(struct fha_hash_entry *e)
213 if (e->num_rw + e->num_exclusive)
214 panic("nonempty fhe");
219 fha_hash_entry_remove(struct fha_hash_entry *e)
222 LIST_REMOVE(e, link);
223 fha_hash_entry_destroy(e);
226 static struct fha_hash_entry *
227 fha_hash_entry_lookup(struct fha_params *softc, u_int64_t fh)
233 struct fha_hash_entry *fhe, *new_fhe;
235 LIST_FOREACH(fhe, &softc->g_fha.hashtable[fh % softc->g_fha.hashmask],
241 /* Allocate a new entry. */
242 mtx_unlock(&pool->sp_lock);
243 new_fhe = fha_hash_entry_new(fh);
244 mtx_lock(&pool->sp_lock);
246 /* Double-check to make sure we still need the new entry. */
248 &softc->g_fha.hashtable[fh % softc->g_fha.hashmask], link)
254 &softc->g_fha.hashtable[fh % softc->g_fha.hashmask],
257 fha_hash_entry_destroy(new_fhe);
264 fha_hash_entry_add_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
267 LIST_INSERT_HEAD(&fhe->threads, thread, st_alink);
272 fha_hash_entry_remove_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
275 LIST_REMOVE(thread, st_alink);
280 * Account for an ongoing operation associated with this file.
283 fha_hash_entry_add_op(struct fha_hash_entry *fhe, int locktype, int count)
286 if (LK_EXCLUSIVE == locktype)
287 fhe->num_exclusive += count;
289 fhe->num_rw += count;
293 get_idle_thread(SVCPOOL *pool)
297 LIST_FOREACH(st, &pool->sp_idlethreads, st_ilink) {
298 if (st->st_xprt == NULL && STAILQ_EMPTY(&st->st_reqs))
306 * Get the service thread currently associated with the fhe that is
307 * appropriate to handle this operation.
310 fha_hash_entry_choose_thread(struct fha_params *softc,
311 struct fha_hash_entry *fhe, struct fha_info *i, SVCTHREAD *this_thread);
314 fha_hash_entry_choose_thread(struct fha_params *softc,
315 struct fha_hash_entry *fhe, struct fha_info *i, SVCTHREAD *this_thread)
317 SVCTHREAD *thread, *min_thread = NULL;
319 int req_count, min_count = 0;
320 off_t offset1, offset2;
324 LIST_FOREACH(thread, &fhe->threads, st_alink) {
325 req_count = thread->st_reqcount;
327 /* If there are any writes in progress, use the first thread. */
328 if (fhe->num_exclusive) {
330 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
331 "fha: %p(%d)w", thread, req_count);
337 * Check for read locality, making sure that we won't
338 * exceed our per-thread load limit in the process.
341 offset2 = STAILQ_FIRST(&thread->st_reqs)->rq_p3;
343 if (((offset1 >= offset2)
344 && ((offset1 - offset2) < (1 << softc->ctls.bin_shift)))
345 || ((offset2 > offset1)
346 && ((offset2 - offset1) < (1 << softc->ctls.bin_shift)))) {
347 if ((softc->ctls.max_reqs_per_nfsd == 0) ||
348 (req_count < softc->ctls.max_reqs_per_nfsd)) {
350 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
351 "fha: %p(%d)r", thread, req_count);
358 * We don't have a locality match, so skip this thread,
359 * but keep track of the most attractive thread in case
360 * we need to come back to it later.
363 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
364 "fha: %p(%d)s off1 %llu off2 %llu", thread,
365 req_count, offset1, offset2);
367 if ((min_thread == NULL) || (req_count < min_count)) {
368 min_count = req_count;
374 * We didn't find a good match yet. See if we can add
375 * a new thread to this file handle entry's thread list.
377 if ((softc->ctls.max_nfsds_per_fh == 0) ||
378 (fhe->num_threads < softc->ctls.max_nfsds_per_fh)) {
380 * We can add a new thread, so try for an idle thread
381 * first, and fall back to this_thread if none are idle.
383 if (STAILQ_EMPTY(&this_thread->st_reqs)) {
384 thread = this_thread;
386 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
387 "fha: %p(%d)t", thread, thread->st_reqcount);
389 } else if ((thread = get_idle_thread(pool))) {
391 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
392 "fha: %p(%d)i", thread, thread->st_reqcount);
395 thread = this_thread;
397 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
398 "fha: %p(%d)b", thread, thread->st_reqcount);
401 fha_hash_entry_add_thread(fhe, thread);
404 * We don't want to use any more threads for this file, so
405 * go back to the most attractive nfsd we're already using.
414 * After getting a request, try to assign it to some thread. Usually we
415 * handle it ourselves.
418 fha_assign(SVCTHREAD *this_thread, struct svc_req *req,
419 struct fha_params *softc)
424 struct fha_hash_entry *fhe;
425 struct fha_callbacks *cb;
427 cb = &softc->callbacks;
429 /* Check to see whether we're enabled. */
430 if (softc->ctls.enable == 0)
431 return (this_thread);
434 * Only do placement if this is an NFS request.
436 if (req->rq_prog != NFS_PROG)
437 return (this_thread);
439 if (req->rq_vers != 2 && req->rq_vers != 3)
440 return (this_thread);
442 pool = req->rq_xprt->xp_pool;
443 fha_extract_info(req, &i, cb);
446 * We save the offset associated with this request for later
449 fhe = fha_hash_entry_lookup(softc, i.fh);
451 req->rq_p2 = i.locktype;
452 req->rq_p3 = i.offset;
455 * Choose a thread, taking into consideration locality, thread load,
456 * and the number of threads already working on this file.
458 thread = fha_hash_entry_choose_thread(softc, fhe, &i, this_thread);
459 KASSERT(thread, ("fha_assign: NULL thread!"));
460 fha_hash_entry_add_op(fhe, i.locktype, 1);
466 * Called when we're done with an operation. The request has already
470 fha_nd_complete(SVCTHREAD *thread, struct svc_req *req)
472 struct fha_hash_entry *fhe = req->rq_p1;
475 * This may be called for reqs that didn't go through
476 * fha_assign (e.g. extra NULL ops used for RPCSEC_GSS.
481 fha_hash_entry_add_op(fhe, req->rq_p2, -1);
483 if (thread->st_reqcount == 0) {
484 fha_hash_entry_remove_thread(fhe, thread);
485 if (0 == fhe->num_rw + fhe->num_exclusive)
486 fha_hash_entry_remove(fhe);
491 fhe_stats_sysctl(SYSCTL_HANDLER_ARGS, struct fha_params *softc)
495 struct fha_hash_entry *fhe;
500 sbuf_new(&sb, NULL, 4096, SBUF_FIXEDLEN);
505 sbuf_printf(&sb, "NFSD not running\n");
510 mtx_lock(&pool->sp_lock);
512 for (i = 0; i <= softc->g_fha.hashmask; i++)
513 if (!LIST_EMPTY(&softc->g_fha.hashtable[i]))
517 sbuf_printf(&sb, "No file handle entries.\n");
521 for (i = 0; i <= softc->g_fha.hashmask; i++) {
522 LIST_FOREACH(fhe, &softc->g_fha.hashtable[i], link) {
523 sbuf_printf(&sb, "%sfhe %p: {\n", first ? "" : ", ", fhe);
525 sbuf_printf(&sb, " fh: %ju\n", (uintmax_t) fhe->fh);
526 sbuf_printf(&sb, " num_rw: %d\n", fhe->num_rw);
527 sbuf_printf(&sb, " num_exclusive: %d\n", fhe->num_exclusive);
528 sbuf_printf(&sb, " num_threads: %d\n", fhe->num_threads);
530 LIST_FOREACH(thread, &fhe->threads, st_alink) {
531 sbuf_printf(&sb, " thread %p offset %ju "
532 "(count %d)\n", thread,
533 STAILQ_FIRST(&thread->st_reqs)->rq_p3,
534 thread->st_reqcount);
537 sbuf_printf(&sb, "}");
540 /* Limit the output. */
542 sbuf_printf(&sb, "...");
550 mtx_unlock(&pool->sp_lock);
553 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);