From nobody Sun May 19 07:11:56 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1662636325; cv=none; d=zohomail.com; s=zohoarc; b=TyETdod7skxyEr2QePIhMt2KSLBrF2X9ioeOp3poLDq/nXGFjPdgfWG9LfziQ3eb4qeiCR4VnmNx7/KLNBAat8zIl4GL65khRLi2pIpLJkitSL3jgjeXXov+3NGP/vBg21NCI+V7sXuGt38XxFxkOQdgcF9aArVbQ/vsJDFlsHQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1662636325; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=yOF7obn7bO8+DOYaGQXpTxRC0oFiXOne6VYAceKYfZ8=; b=Y87L6OYjTnmYn4OjZ+OvQ/14d1w8ZH+rTHBXeShTdL4Rbqr/36P8wGfVusCWwz70JupTMAiTtadDp8Vq7I0xzK3oJcDB4WEFCX/ujWomZeJ5GZSI+/6ilG6fGT8C6VIo7zMl23Zn1kpLeotzTDDjB+kl3HZ5xUDrUp0w+IMtRx8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1662636325486924.8376447568029; Thu, 8 Sep 2022 04:25:25 -0700 (PDT) Received: from localhost ([::1]:34268 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oWFeu-0008GH-1Z for importer@patchew.org; Thu, 08 Sep 2022 07:25:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55734) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oWFdg-0007SQ-VF; Thu, 08 Sep 2022 07:24:08 -0400 Received: from sosiego.soundray.org ([2a01:4f8:c2c:a9a0::1]:38032) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oWFdc-0004La-7O; Thu, 08 Sep 2022 07:24:08 -0400 From: Linus Heckemann DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sphalerite.org; s=sosiego; t=1662636236; bh=yOF7obn7bO8+DOYaGQXpTxRC0oFiXOne6VYAceKYfZ8=; h=From:To:Cc:Subject:Date; b=0IE/S8kt/pOUmxljO8rNBY8G2iq+i6pf3EZnH+WQhcJdhBMbJTPlaqkafIAfVRfEg aCVYX/sm8qg2+V1JOku538VITApHpez7hYW0NlSe2VnYTvu0OwKhtbbTvIb5gAYnRb Rn3ZiyDHKaH63bEWrvHJImZnN49kQ9b+9BN964HQ= To: qemu-devel@nongnu.org Cc: Christian Schoenebeck , Qemu-block , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Greg Kurz , Linus Heckemann Subject: [PATCH v3] 9pfs: use GHashTable for fid table Date: Thu, 8 Sep 2022 13:23:53 +0200 Message-Id: <20220908112353.289267-1-git@sphalerite.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a01:4f8:c2c:a9a0::1; envelope-from=linus@sphalerite.org; helo=sosiego.soundray.org X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @sphalerite.org) X-ZM-MESSAGEID: 1662636328163100001 The previous implementation would iterate over the fid table for lookup operations, resulting in an operation with O(n) complexity on the number of open files and poor cache locality -- for every open, stat, read, write, etc operation. This change uses a hashtable for this instead, significantly improving the performance of the 9p filesystem. The runtime of NixOS's simple installer test, which copies ~122k files totalling ~1.8GiB from 9p, decreased by a factor of about 10. Signed-off-by: Linus Heckemann Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Greg Kurz --- Greg Kurz writes: > The comment above should be adapted to the new situation : no need I've removed it completely, since the logic is simple enough that only the shortened comment below remains necessary. > With the new logic, this should just be: now is :) > g_hash_table_steal_extended() [1] actually allows to do just that. g_hash_table_steal_extended unfortunately isn't available since it was introduced in glib 2.58 and we're maintaining compatibility to 2.56. > You could just call g_hash_table_iter_remove(&iter) here Applied this suggestion, thanks! > Well... finding at least one clunked fid state in this table is > definitely a bug so I'll keep the BUG_ON() anyway. Christian Schoenebeck writes: > Yeah, I think you are right, it would feel odd. Just drop BUG_ON() for > now. I still prefer dropping it, but if we were to keep it I think it should be in v9fs_reclaim_fd where we iterate and can thus check the whole table. Greg Kurz and Philippe Mathieu-Daud=C3=A9 write: > [patch versioning] Whoops. I used -v2 on git send-email, which just ignored the option, rather than git format-patch, by accident. This one _should_ now be v3! hw/9pfs/9p.c | 140 +++++++++++++++++++++++++-------------------------- hw/9pfs/9p.h | 2 +- 2 files changed, 70 insertions(+), 72 deletions(-) diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c index aebadeaa03..98a475e560 100644 --- a/hw/9pfs/9p.c +++ b/hw/9pfs/9p.c @@ -282,33 +282,31 @@ static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pd= u, int32_t fid) V9fsFidState *f; V9fsState *s =3D pdu->s; =20 - QSIMPLEQ_FOREACH(f, &s->fid_list, next) { - BUG_ON(f->clunked); - if (f->fid =3D=3D fid) { - /* - * Update the fid ref upfront so that - * we don't get reclaimed when we yield - * in open later. - */ - f->ref++; - /* - * check whether we need to reopen the - * file. We might have closed the fd - * while trying to free up some file - * descriptors. - */ - err =3D v9fs_reopen_fid(pdu, f); - if (err < 0) { - f->ref--; - return NULL; - } - /* - * Mark the fid as referenced so that the LRU - * reclaim won't close the file descriptor - */ - f->flags |=3D FID_REFERENCED; - return f; + f =3D g_hash_table_lookup(s->fids, GINT_TO_POINTER(fid)); + if (f) { + /* + * Update the fid ref upfront so that + * we don't get reclaimed when we yield + * in open later. + */ + f->ref++; + /* + * check whether we need to reopen the + * file. We might have closed the fd + * while trying to free up some file + * descriptors. + */ + err =3D v9fs_reopen_fid(pdu, f); + if (err < 0) { + f->ref--; + return NULL; } + /* + * Mark the fid as referenced so that the LRU + * reclaim won't close the file descriptor + */ + f->flags |=3D FID_REFERENCED; + return f; } return NULL; } @@ -317,12 +315,9 @@ static V9fsFidState *alloc_fid(V9fsState *s, int32_t f= id) { V9fsFidState *f; =20 - QSIMPLEQ_FOREACH(f, &s->fid_list, next) { + if (g_hash_table_contains(s->fids, GINT_TO_POINTER(fid))) { /* If fid is already there return NULL */ - BUG_ON(f->clunked); - if (f->fid =3D=3D fid) { - return NULL; - } + return NULL; } f =3D g_new0(V9fsFidState, 1); f->fid =3D fid; @@ -333,7 +328,7 @@ static V9fsFidState *alloc_fid(V9fsState *s, int32_t fi= d) * reclaim won't close the file descriptor */ f->flags |=3D FID_REFERENCED; - QSIMPLEQ_INSERT_TAIL(&s->fid_list, f, next); + g_hash_table_insert(s->fids, GINT_TO_POINTER(fid), f); =20 v9fs_readdir_init(s->proto_version, &f->fs.dir); v9fs_readdir_init(s->proto_version, &f->fs_reclaim.dir); @@ -424,12 +419,11 @@ static V9fsFidState *clunk_fid(V9fsState *s, int32_t = fid) { V9fsFidState *fidp; =20 - QSIMPLEQ_FOREACH(fidp, &s->fid_list, next) { - if (fidp->fid =3D=3D fid) { - QSIMPLEQ_REMOVE(&s->fid_list, fidp, V9fsFidState, next); - fidp->clunked =3D true; - return fidp; - } + fidp =3D g_hash_table_lookup(s->fids, GINT_TO_POINTER(fid)); + if (fidp) { + g_hash_table_remove(s->fids, GINT_TO_POINTER(fid)); + fidp->clunked =3D true; + return fidp; } return NULL; } @@ -439,10 +433,15 @@ void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) int reclaim_count =3D 0; V9fsState *s =3D pdu->s; V9fsFidState *f; + GHashTableIter iter; + gpointer fid; + + g_hash_table_iter_init(&iter, s->fids); + QSLIST_HEAD(, V9fsFidState) reclaim_list =3D QSLIST_HEAD_INITIALIZER(reclaim_list); =20 - QSIMPLEQ_FOREACH(f, &s->fid_list, next) { + while (g_hash_table_iter_next(&iter, &fid, (gpointer *) &f)) { /* * Unlink fids cannot be reclaimed. Check * for them and skip them. Also skip fids @@ -518,23 +517,19 @@ static int coroutine_fn v9fs_mark_fids_unreclaim(V9fs= PDU *pdu, V9fsPath *path) { int err; V9fsState *s =3D pdu->s; - V9fsFidState *fidp, *fidp_next; + V9fsFidState *fidp; + gpointer fid; + GHashTableIter iter; =20 - fidp =3D QSIMPLEQ_FIRST(&s->fid_list); - if (!fidp) { - return 0; - } + g_hash_table_iter_init(&iter, s->fids); + + while (g_hash_table_iter_next(&iter, &fid, (gpointer *) &fidp)) { + /* + * Ensure the fid survives a potential clunk request during + * v9fs_reopen_fid. + */ + fidp->ref++; =20 - /* - * v9fs_reopen_fid() can yield : a reference on the fid must be held - * to ensure its pointer remains valid and we can safely pass it to - * QSIMPLEQ_NEXT(). The corresponding put_fid() can also yield so - * we must keep a reference on the next fid as well. So the logic here - * is to get a reference on a fid and only put it back during the next - * iteration after we could get a reference on the next fid. Start with - * the first one. - */ - for (fidp->ref++; fidp; fidp =3D fidp_next) { if (fidp->path.size =3D=3D path->size && !memcmp(fidp->path.data, path->data, path->size)) { /* Mark the fid non reclaimable. */ @@ -548,16 +543,6 @@ static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsP= DU *pdu, V9fsPath *path) } } =20 - fidp_next =3D QSIMPLEQ_NEXT(fidp, next); - - if (fidp_next) { - /* - * Ensure the next fid survives a potential clunk request duri= ng - * put_fid() below and v9fs_reopen_fid() in the next iteration. - */ - fidp_next->ref++; - } - /* We're done with this fid */ put_fid(pdu, fidp); } @@ -569,18 +554,20 @@ static void coroutine_fn virtfs_reset(V9fsPDU *pdu) { V9fsState *s =3D pdu->s; V9fsFidState *fidp; + gpointer fid; + GHashTableIter iter; + + g_hash_table_iter_init(&iter, s->fids); =20 /* Free all fids */ - while (!QSIMPLEQ_EMPTY(&s->fid_list)) { - /* Get fid */ - fidp =3D QSIMPLEQ_FIRST(&s->fid_list); + while (g_hash_table_iter_next(&iter, &fid, (gpointer *) &fidp)) { fidp->ref++; =20 /* Clunk fid */ - QSIMPLEQ_REMOVE(&s->fid_list, fidp, V9fsFidState, next); fidp->clunked =3D true; =20 put_fid(pdu, fidp); + g_hash_table_iter_remove(&iter); } } =20 @@ -3205,6 +3192,8 @@ static int coroutine_fn v9fs_complete_rename(V9fsPDU = *pdu, V9fsFidState *fidp, V9fsFidState *tfidp; V9fsState *s =3D pdu->s; V9fsFidState *dirfidp =3D NULL; + GHashTableIter iter; + gpointer fid; =20 v9fs_path_init(&new_path); if (newdirfid !=3D -1) { @@ -3238,11 +3227,13 @@ static int coroutine_fn v9fs_complete_rename(V9fsPD= U *pdu, V9fsFidState *fidp, if (err < 0) { goto out; } + /* * Fixup fid's pointing to the old name to * start pointing to the new name */ - QSIMPLEQ_FOREACH(tfidp, &s->fid_list, next) { + g_hash_table_iter_init(&iter, s->fids); + while (g_hash_table_iter_next(&iter, &fid, (gpointer *) &tfidp)) { if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) { /* replace the name */ v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data)= ); @@ -3320,6 +3311,8 @@ static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *p= du, V9fsPath *olddir, V9fsPath oldpath, newpath; V9fsState *s =3D pdu->s; int err; + GHashTableIter iter; + gpointer fid; =20 v9fs_path_init(&oldpath); v9fs_path_init(&newpath); @@ -3336,7 +3329,8 @@ static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *p= du, V9fsPath *olddir, * Fixup fid's pointing to the old name to * start pointing to the new name */ - QSIMPLEQ_FOREACH(tfidp, &s->fid_list, next) { + g_hash_table_iter_init(&iter, s->fids); + while (g_hash_table_iter_next(&iter, &fid, (gpointer *) &tfidp)) { if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) { /* replace the name */ v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data)); @@ -4226,7 +4220,7 @@ int v9fs_device_realize_common(V9fsState *s, const V9= fsTransport *t, s->ctx.fmode =3D fse->fmode; s->ctx.dmode =3D fse->dmode; =20 - QSIMPLEQ_INIT(&s->fid_list); + s->fids =3D g_hash_table_new(NULL, NULL); qemu_co_rwlock_init(&s->rename_lock); =20 if (s->ops->init(&s->ctx, errp) < 0) { @@ -4286,6 +4280,10 @@ void v9fs_device_unrealize_common(V9fsState *s) if (s->ctx.fst) { fsdev_throttle_cleanup(s->ctx.fst); } + if (s->fids) { + g_hash_table_destroy(s->fids); + s->fids =3D NULL; + } g_free(s->tag); qp_table_destroy(&s->qpd_table); qp_table_destroy(&s->qpp_table); diff --git a/hw/9pfs/9p.h b/hw/9pfs/9p.h index 994f952600..10fd2076c2 100644 --- a/hw/9pfs/9p.h +++ b/hw/9pfs/9p.h @@ -339,7 +339,7 @@ typedef struct { struct V9fsState { QLIST_HEAD(, V9fsPDU) free_list; QLIST_HEAD(, V9fsPDU) active_list; - QSIMPLEQ_HEAD(, V9fsFidState) fid_list; + GHashTable *fids; FileOperations *ops; FsContext ctx; char *tag; --=20 2.36.0