概要
NFS服务器默认的NFSD线程数为8,但是在某些压力较大的场景,8可能不够用。本文调研了动态调整NFSD线程数的方案,及对已上线业务的影响分析。
调整方案
修改/etc/sysconfig/nfs的RPCNFSDCOUNT,可以永久性的调整NFSD的数量。正常情况下systemctl restart nfs重启服务就可以生效。
但在线上环境,重启服务可能导致IO中断,造成业务中断。且在带压力的情况下,存在一定概率的IO卡住风险。所以不推荐直接重启服务。
通过执行以下命令,可以直接调整NFSD线程数(以调整到128为例):
/usr/sbin/rpc.nfsd 128
或 echo 128> /proc/fs/nfsd/threads
调整原理
实际这两个命令都是向nfsd提供的内核接口写入nfsd线程数,走的同样的流程:
nfsd_svc->svc_set_num_threads
static const struct svc_serv_ops nfsd_thread_sv_ops = {
.svo_shutdown = nfsd_last_thread,
.svo_function = nfsd,
.svo_enqueue_xprt = svc_xprt_do_enqueue,
.svo_setup = svc_set_num_threads,
.svo_module = THIS_MODULE,
};
设置NFSD数量的流程为通用的RPC SVC流程,在svc_set_num_threads中处理:
/*
* Create or destroy enough new threads to make the number
* of threads the given number. If `pool' is non-NULL, applies
* only to threads in that pool, otherwise round-robins between
* all pools. Caller must ensure that mutual exclusion between this and
* server startup or shutdown.
*
* Destroying threads relies on the service threads filling in
* rqstp->rq_task, which only the nfs ones do. Assumes the serv
* has been created using svc_create_pooled().
*
* Based on code that used to be in nfsd_svc() but tweaked
* to be pool-aware.
*/
int
svc_set_num_threads(struct svc_serv *serv, struct svc_pool *pool, int nrservs)
{
if (pool == NULL) {
/* The -1 assumes caller has done a svc_get() */
nrservs -= (serv->sv_nrthreads-1);
} else {
spin_lock_bh(&pool->sp_lock);
nrservs -= pool->sp_nrthreads; //指定的数量减去当前的数量,如果为nrservs>0说明需要新增,如果<0说明需要减少
spin_unlock_bh(&pool->sp_lock);
}
if (nrservs > 0)
return svc_start_kthreads(serv, pool, nrservs); //nrservs>0,新建nrservs个nfsd
if (nrservs < 0)
return svc_signal_kthreads(serv, pool, nrservs); //nrservs>0,kill掉nrservs个nfsd,发送SIGINT对对应的进程,效果同kill -9 nfsd
return 0;
}
从svc_set_num_threads函数可以看到,如果是增加nfsd线程,并不会对已有的线程执行任何动作。
测试验证
可以观察到通过rpc.nfsd修改时:
减少线程数量时,原有线程PID没有发生变化,仅减少了对应数量的线程。
增加线程数量时,原因线程PID同样没有发生变化,新的线程为新建的PID。

vdbench没有跌0:

测试也证明了该方案可安全的动态调整nfsd线程数,对在线业务没有影响。