2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/sysproto.h>
34 #include <sys/kernel.h>
35 #include <sys/sysctl.h>
36 #include <sys/vnode.h>
37 #include <sys/malloc.h>
38 #include <sys/mount.h>
43 #include <nfs/nfs_fha.h>
45 static MALLOC_DEFINE(M_NFS_FHA, "NFS FHA", "NFS FHA");
48 * XXX need to commonize definitions between old and new NFS code. Define
49 * this here so we don't include one nfsproto.h over the other.
51 #define NFS_PROG 100003
54 fha_init(struct fha_params *softc)
58 for (i = 0; i < FHA_HASH_SIZE; i++)
59 mtx_init(&softc->fha_hash[i].mtx, "fhalock", NULL, MTX_DEF);
62 * Set the default tuning parameters.
64 softc->ctls.enable = FHA_DEF_ENABLE;
65 softc->ctls.read = FHA_DEF_READ;
66 softc->ctls.write = FHA_DEF_WRITE;
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 * Add sysctls so the user can change the tuning parameters.
74 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
75 OID_AUTO, "enable", CTLFLAG_RWTUN,
76 &softc->ctls.enable, 0, "Enable NFS File Handle Affinity (FHA)");
78 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
79 OID_AUTO, "read", CTLFLAG_RWTUN,
80 &softc->ctls.read, 0, "Enable NFS FHA read locality");
82 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
83 OID_AUTO, "write", CTLFLAG_RWTUN,
84 &softc->ctls.write, 0, "Enable NFS FHA write locality");
86 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
87 OID_AUTO, "bin_shift", CTLFLAG_RWTUN,
88 &softc->ctls.bin_shift, 0, "Maximum locality distance 2^(bin_shift) bytes");
90 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
91 OID_AUTO, "max_nfsds_per_fh", CTLFLAG_RWTUN,
92 &softc->ctls.max_nfsds_per_fh, 0, "Maximum nfsd threads that "
93 "should be working on requests for the same file handle");
95 SYSCTL_ADD_UINT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
96 OID_AUTO, "max_reqs_per_nfsd", CTLFLAG_RWTUN,
97 &softc->ctls.max_reqs_per_nfsd, 0, "Maximum requests that "
98 "single nfsd thread should be working on at any time");
100 SYSCTL_ADD_OID(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
101 OID_AUTO, "fhe_stats", CTLTYPE_STRING | CTLFLAG_RD, 0, 0,
102 softc->callbacks.fhe_stats_sysctl, "A", "");
107 fha_uninit(struct fha_params *softc)
111 sysctl_ctx_free(&softc->sysctl_ctx);
112 for (i = 0; i < FHA_HASH_SIZE; i++)
113 mtx_destroy(&softc->fha_hash[i].mtx);
117 * This just specifies that offsets should obey affinity when within
118 * the same 1Mbyte (1<<20) chunk for the file (reads only for now).
121 fha_extract_info(struct svc_req *req, struct fha_info *i,
122 struct fha_callbacks *cb)
126 static u_int64_t random_fh = 0;
128 int v3 = (req->rq_vers == 3);
132 * We start off with a random fh. If we get a reasonable
133 * procnum, we set the fh. If there's a concept of offset
134 * that we're interested in, we set that.
138 i->locktype = LK_EXCLUSIVE;
139 i->read = i->write = 0;
142 * Extract the procnum and convert to v3 form if necessary,
143 * taking care to deal with out-of-range procnums. Caller will
144 * ensure that rq_vers is either 2 or 3.
146 procnum = req->rq_proc;
148 rpcproc_t tmp_procnum;
150 tmp_procnum = cb->get_procnum(procnum);
151 if (tmp_procnum == -1)
153 procnum = tmp_procnum;
157 * We do affinity for most. However, we divide a realm of affinity
158 * by file offset so as to allow for concurrent random access. We
159 * only do this for reads today, but this may change when IFS supports
160 * efficient concurrent writes.
162 if (cb->no_offset(procnum))
165 i->read = cb->is_read(procnum);
166 i->write = cb->is_write(procnum);
168 error = cb->realign(&req->rq_args, M_NOWAIT);
172 dpos = mtod(md, caddr_t);
174 /* Grab the filehandle. */
175 error = cb->get_fh(&i->fh, v3, &md, &dpos);
179 /* Content ourselves with zero offset for all but reads. */
180 if (i->read || i->write)
181 cb->get_offset(&md, &dpos, v3, i);
184 cb->set_locktype(procnum, i);
187 static struct fha_hash_entry *
188 fha_hash_entry_new(u_int64_t fh)
190 struct fha_hash_entry *e;
192 e = malloc(sizeof(*e), M_NFS_FHA, M_WAITOK);
195 e->num_exclusive = 0;
197 LIST_INIT(&e->threads);
203 fha_hash_entry_destroy(struct fha_hash_entry *e)
206 mtx_assert(e->mtx, MA_OWNED);
207 KASSERT(e->num_rw == 0,
208 ("%d reqs on destroyed fhe %p", e->num_rw, e));
209 KASSERT(e->num_exclusive == 0,
210 ("%d exclusive reqs on destroyed fhe %p", e->num_exclusive, e));
211 KASSERT(e->num_threads == 0,
212 ("%d threads on destroyed fhe %p", e->num_threads, e));
217 fha_hash_entry_remove(struct fha_hash_entry *e)
220 mtx_assert(e->mtx, MA_OWNED);
221 LIST_REMOVE(e, link);
222 fha_hash_entry_destroy(e);
225 static struct fha_hash_entry *
226 fha_hash_entry_lookup(struct fha_params *softc, u_int64_t fh)
228 struct fha_hash_slot *fhs;
229 struct fha_hash_entry *fhe, *new_fhe;
231 fhs = &softc->fha_hash[fh % FHA_HASH_SIZE];
232 new_fhe = fha_hash_entry_new(fh);
233 new_fhe->mtx = &fhs->mtx;
235 LIST_FOREACH(fhe, &fhs->list, link)
240 LIST_INSERT_HEAD(&fhs->list, fhe, link);
242 fha_hash_entry_destroy(new_fhe);
247 fha_hash_entry_add_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
250 mtx_assert(fhe->mtx, MA_OWNED);
252 LIST_INSERT_HEAD(&fhe->threads, thread, st_alink);
257 fha_hash_entry_remove_thread(struct fha_hash_entry *fhe, SVCTHREAD *thread)
260 mtx_assert(fhe->mtx, MA_OWNED);
261 KASSERT(thread->st_p2 == 0,
262 ("%d reqs on removed thread %p", thread->st_p2, thread));
263 LIST_REMOVE(thread, st_alink);
268 * Account for an ongoing operation associated with this file.
271 fha_hash_entry_add_op(struct fha_hash_entry *fhe, int locktype, int count)
274 mtx_assert(fhe->mtx, MA_OWNED);
275 if (LK_EXCLUSIVE == locktype)
276 fhe->num_exclusive += count;
278 fhe->num_rw += count;
282 * Get the service thread currently associated with the fhe that is
283 * appropriate to handle this operation.
286 fha_hash_entry_choose_thread(struct fha_params *softc,
287 struct fha_hash_entry *fhe, struct fha_info *i, SVCTHREAD *this_thread)
289 SVCTHREAD *thread, *min_thread = NULL;
290 int req_count, min_count = 0;
291 off_t offset1, offset2;
293 LIST_FOREACH(thread, &fhe->threads, st_alink) {
294 req_count = thread->st_p2;
296 /* If there are any writes in progress, use the first thread. */
297 if (fhe->num_exclusive) {
299 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
300 "fha: %p(%d)w", thread, req_count);
305 /* Check whether we should consider locality. */
306 if ((i->read && !softc->ctls.read) ||
307 (i->write && !softc->ctls.write))
311 * Check for locality, making sure that we won't
312 * exceed our per-thread load limit in the process.
315 offset2 = thread->st_p3;
317 if (((offset1 >= offset2)
318 && ((offset1 - offset2) < (1 << softc->ctls.bin_shift)))
319 || ((offset2 > offset1)
320 && ((offset2 - offset1) < (1 << softc->ctls.bin_shift)))) {
321 if ((softc->ctls.max_reqs_per_nfsd == 0) ||
322 (req_count < softc->ctls.max_reqs_per_nfsd)) {
324 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
325 "fha: %p(%d)r", thread, req_count);
333 * We don't have a locality match, so skip this thread,
334 * but keep track of the most attractive thread in case
335 * we need to come back to it later.
338 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
339 "fha: %p(%d)s off1 %llu off2 %llu", thread,
340 req_count, offset1, offset2);
342 if ((min_thread == NULL) || (req_count < min_count)) {
343 min_count = req_count;
349 * We didn't find a good match yet. See if we can add
350 * a new thread to this file handle entry's thread list.
352 if ((softc->ctls.max_nfsds_per_fh == 0) ||
353 (fhe->num_threads < softc->ctls.max_nfsds_per_fh)) {
354 thread = this_thread;
356 ITRACE_CURPROC(ITRACE_NFS, ITRACE_INFO,
357 "fha: %p(%d)t", thread, thread->st_p2);
359 fha_hash_entry_add_thread(fhe, thread);
362 * We don't want to use any more threads for this file, so
363 * go back to the most attractive nfsd we're already using.
372 * After getting a request, try to assign it to some thread. Usually we
373 * handle it ourselves.
376 fha_assign(SVCTHREAD *this_thread, struct svc_req *req,
377 struct fha_params *softc)
381 struct fha_hash_entry *fhe;
382 struct fha_callbacks *cb;
384 cb = &softc->callbacks;
386 /* Check to see whether we're enabled. */
387 if (softc->ctls.enable == 0)
391 * Only do placement if this is an NFS request.
393 if (req->rq_prog != NFS_PROG)
396 if (req->rq_vers != 2 && req->rq_vers != 3)
399 fha_extract_info(req, &i, cb);
402 * We save the offset associated with this request for later
405 fhe = fha_hash_entry_lookup(softc, i.fh);
407 req->rq_p2 = i.locktype;
408 req->rq_p3 = i.offset;
411 * Choose a thread, taking into consideration locality, thread load,
412 * and the number of threads already working on this file.
414 thread = fha_hash_entry_choose_thread(softc, fhe, &i, this_thread);
415 KASSERT(thread, ("fha_assign: NULL thread!"));
416 fha_hash_entry_add_op(fhe, i.locktype, 1);
418 thread->st_p3 = i.offset;
421 * Grab the pool lock here to not let chosen thread go away before
422 * the new request inserted to its queue while we drop fhe lock.
424 mtx_lock(&thread->st_lock);
425 mtx_unlock(fhe->mtx);
430 mtx_lock(&this_thread->st_lock);
431 return (this_thread);
435 * Called when we're done with an operation. The request has already
439 fha_nd_complete(SVCTHREAD *thread, struct svc_req *req)
441 struct fha_hash_entry *fhe = req->rq_p1;
445 * This may be called for reqs that didn't go through
446 * fha_assign (e.g. extra NULL ops used for RPCSEC_GSS.
453 fha_hash_entry_add_op(fhe, req->rq_p2, -1);
455 KASSERT(thread->st_p2 >= 0, ("Negative request count %d on %p",
456 thread->st_p2, thread));
457 if (thread->st_p2 == 0) {
458 fha_hash_entry_remove_thread(fhe, thread);
459 if (0 == fhe->num_rw + fhe->num_exclusive)
460 fha_hash_entry_remove(fhe);
466 fhe_stats_sysctl(SYSCTL_HANDLER_ARGS, struct fha_params *softc)
470 struct fha_hash_entry *fhe;
471 bool_t first, hfirst;
474 sbuf_new(&sb, NULL, 65536, SBUF_FIXEDLEN);
477 sbuf_printf(&sb, "NFSD not running\n");
481 for (i = 0; i < FHA_HASH_SIZE; i++)
482 if (!LIST_EMPTY(&softc->fha_hash[i].list))
485 if (i == FHA_HASH_SIZE) {
486 sbuf_printf(&sb, "No file handle entries.\n");
491 for (; i < FHA_HASH_SIZE; i++) {
492 mtx_lock(&softc->fha_hash[i].mtx);
493 if (LIST_EMPTY(&softc->fha_hash[i].list)) {
494 mtx_unlock(&softc->fha_hash[i].mtx);
497 sbuf_printf(&sb, "%shash %d: {\n", hfirst ? "" : ", ", i);
499 LIST_FOREACH(fhe, &softc->fha_hash[i].list, link) {
500 sbuf_printf(&sb, "%sfhe %p: {\n", first ? " " : ", ", fhe);
502 sbuf_printf(&sb, " fh: %ju\n", (uintmax_t) fhe->fh);
503 sbuf_printf(&sb, " num_rw/exclusive: %d/%d\n",
504 fhe->num_rw, fhe->num_exclusive);
505 sbuf_printf(&sb, " num_threads: %d\n", fhe->num_threads);
507 LIST_FOREACH(thread, &fhe->threads, st_alink) {
508 sbuf_printf(&sb, " thread %p offset %ju "
510 thread->st_p3, thread->st_p2);
513 sbuf_printf(&sb, " }");
516 sbuf_printf(&sb, "\n}");
517 mtx_unlock(&softc->fha_hash[i].mtx);
524 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);