From patchwork Mon Mar 18 12:58:48 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 2291141 Return-Path: X-Original-To: patchwork-linux-nfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 8656A3FD8C for ; Mon, 18 Mar 2013 12:58:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751376Ab3CRM6z (ORCPT ); Mon, 18 Mar 2013 08:58:55 -0400 Received: from mail-ob0-f178.google.com ([209.85.214.178]:56602 "EHLO mail-ob0-f178.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750899Ab3CRM6z (ORCPT ); Mon, 18 Mar 2013 08:58:55 -0400 Received: by mail-ob0-f178.google.com with SMTP id wd20so5164166obb.23 for ; Mon, 18 Mar 2013 05:58:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:sender:from:to:cc:subject:date:message-id:x-mailer :x-gm-message-state; bh=A+Hngu+WrcS5NdUFKZajugPUT5y/NqeQ+40NnDVXc+I=; b=kiUcEiff5/XUzTPcAPYKUClqV+6lxH0Tck/u6PS3yKY3FxeX4O96JtzyD3j44LGCaA 4hmF7v02dwRp0UMvoHHZqQc1AHV54sw2MgrmPu65NrmYgtOaCj0JYG2SNE5FqJFP3PDd rwrmwoL7XuMmCRNVGN5fKPCIR3s1gpDlZH87lS2JEtNjjN5KsCKTWzEHQPYxLKJxXMvq KhqwWXV5dM2JHy6IsCeqvpw9bNvJ/C4kLCWbO9nT4+NJwwurSLzkx6vdAIbc8omZhxQ/ SvSErH6/DD37c3QWmBIC03JRYJ7dMR94XHQkn5/5MqxNTVo+qDVMkcVgIDRcBTaItvqn sXLQ== X-Received: by 10.60.27.136 with SMTP id t8mr6547965oeg.92.1363611534711; Mon, 18 Mar 2013 05:58:54 -0700 (PDT) Received: from salusa.poochiereds.net (cpe-107-015-113-143.nc.res.rr.com. [107.15.113.143]) by mx.google.com with ESMTPS id bq8sm6148170obc.9.2013.03.18.05.58.53 (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 18 Mar 2013 05:58:53 -0700 (PDT) From: Jeff Layton To: bfields@fieldses.org Cc: linux-nfs@vger.kernel.org Subject: [PATCH] nfsd: eliminate one of the DRC cache searches Date: Mon, 18 Mar 2013 08:58:48 -0400 Message-Id: <1363611528-21522-1-git-send-email-jlayton@redhat.com> X-Mailer: git-send-email 1.7.11.7 X-Gm-Message-State: ALoCoQm+ULlvrCAKQa6rs2hKmlxgUrnR/MB4qGY2JSfceMAf7Cx1IC4MAZNy7gKORkWJ11EYpog/ Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org The most common case is to do a search of the cache, followed by an insert. In the case where we have to allocate an entry off the slab, then we end up having to redo the search, which is wasteful. Better optimize the code for the common case by eliminating the initial search of the cache and always preallocating an entry. In the case of a cache hit, we'll end up just freeing that entry but that's preferable to an extra search. Signed-off-by: Jeff Layton --- fs/nfsd/nfscache.c | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/fs/nfsd/nfscache.c b/fs/nfsd/nfscache.c index f5ad28e..1c4e95e 100644 --- a/fs/nfsd/nfscache.c +++ b/fs/nfsd/nfscache.c @@ -317,55 +317,53 @@ nfsd_cache_lookup(struct svc_rqst *rqstp) __wsum csum; unsigned long age; int type = rqstp->rq_cachetype; - int rtn; + int rtn = RC_DOIT; rqstp->rq_cacherep = NULL; if (type == RC_NOCACHE) { nfsdstats.rcnocache++; - return RC_DOIT; + return rtn; } csum = nfsd_cache_csum(rqstp); + /* + * Since the common case is a cache miss followed by an insert, + * preallocate an entry. First, try to reuse the first entry on the LRU + * if it works, then go ahead and prune the LRU list. + */ spin_lock(&cache_lock); - rtn = RC_DOIT; - - rp = nfsd_cache_search(rqstp, csum); - if (rp) - goto found_entry; - - /* Try to use the first entry on the LRU */ if (!list_empty(&lru_head)) { rp = list_first_entry(&lru_head, struct svc_cacherep, c_lru); if (nfsd_cache_entry_expired(rp) || num_drc_entries >= max_drc_entries) { lru_put_end(rp); prune_cache_entries(); - goto setup_entry; + goto search_cache; } } - /* Drop the lock and allocate a new entry */ + /* No expired ones available, allocate a new one. */ spin_unlock(&cache_lock); rp = nfsd_reply_cache_alloc(); - if (!rp) { - dprintk("nfsd: unable to allocate DRC entry!\n"); - return RC_DOIT; - } spin_lock(&cache_lock); - ++num_drc_entries; + if (likely(rp)) + ++num_drc_entries; - /* - * Must search again just in case someone inserted one - * after we dropped the lock above. - */ +search_cache: found = nfsd_cache_search(rqstp, csum); if (found) { - nfsd_reply_cache_free_locked(rp); + if (likely(rp)) + nfsd_reply_cache_free_locked(rp); rp = found; goto found_entry; } + if (!rp) { + dprintk("nfsd: unable to allocate DRC entry!\n"); + goto out; + } + /* * We're keeping the one we just allocated. Are we now over the * limit? Prune one off the tip of the LRU in trade for the one we @@ -375,7 +373,6 @@ nfsd_cache_lookup(struct svc_rqst *rqstp) nfsd_reply_cache_free_locked(list_first_entry(&lru_head, struct svc_cacherep, c_lru)); -setup_entry: nfsdstats.rcmisses++; rqstp->rq_cacherep = rp; rp->c_state = RC_INPROG;