All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mark Hatle <mark.hatle@kernel.crashing.org>
To: yocto-patches@lists.yoctoproject.org
Subject: [pseudo][PATCH 32/34] pseudo_util: Skip unneeded lstat calls when canonicalizing paths
Date: Tue, 10 Nov 2026 22:21:00 -0600	[thread overview]
Message-ID: <1794370862-30908-33-git-send-email-mark.hatle@kernel.crashing.org> (raw)
In-Reply-To: <1794370862-30908-1-git-send-email-mark.hatle@kernel.crashing.org>

From: Mark Hatle <mark.hatle@amd.com>

Path canonicalization called lstat on the base directory and on every
element, including a final element that is not followed, whose result
is never used.  On a tar/chown/find/ls/rm workload of 40,000 files
this was 1.43M lstat calls.

Skip the lstat of a final element that is left alone, and only stat
the base when the first element is "", "." or "..", which are the only
cases where it matters whether the base is a directory.  For any other
name, lstat of the element fails with ENOTDIR under a non-directory,
giving the same result.  This drops the workload to 0.61M lstat calls.

AI-Generated: Uses GitHub Copilot
Signed-off-by: Mark Hatle <mark.hatle@amd.com>
---
 pseudo_util.c | 40 ++++++++++++++++++++++++++++------------
 1 file changed, 28 insertions(+), 12 deletions(-)

diff --git a/pseudo_util.c b/pseudo_util.c
index 1e29ba5..d9312d0 100644
--- a/pseudo_util.c
+++ b/pseudo_util.c
@@ -726,9 +726,13 @@ pseudo_append_element(char *newpath, char *root, size_t allocated, char **pcurre
 	/* if we are not on the last element of a path and supposed to leave
 	 * it alone (for SYMLINK_NOFOLLOW type cases), and we are not trying to
 	 * go further under a regular file, we want to know whether this is a symlink.
-	 * either way, we want to update buf to show the correct state of the file.
+	 * otherwise, update buf to show the correct state of the file.  An
+	 * element we leave alone is always the last one, so nothing uses
+	 * its state and we skip the lstat.
 	 */
-	if (!pseudo_real_lstat || (pseudo_real_lstat(newpath, buf) == -1)) {
+	if (leave_this) {
+		buf->st_mode = 0;
+	} else if (!pseudo_real_lstat || (pseudo_real_lstat(newpath, buf) == -1)) {
 		// if we can't stat it, zero mode so we don't think it's
 		// known to be a link or a regular file.
 		buf->st_mode = 0;
@@ -846,6 +850,7 @@ pseudo_append_elements(char *newpath, char *root, size_t allocated, char **curre
 	/* a shareable buffer so we can cache stat results while walking the path */
 	PSEUDO_STATBUF buf;
 	buf.st_mode = 0;
+	int stat_base = 0;
 	const char * start = path;
 	if (!newpath || !root ||
 	    !current || !*current ||
@@ -861,16 +866,18 @@ pseudo_append_elements(char *newpath, char *root, size_t allocated, char **curre
 		 * then get shared by anything we call.
 		 */
 		sbuf = &buf;
-		if (*current > root) {
-			if (!pseudo_real_lstat || (pseudo_real_lstat(newpath, sbuf) == -1)) {
-				sbuf->st_mode = 0;
-			}
-		} else {
-			/* Don't call lstat on an empty path, or at all when we
-			 * know that "root" is always directory-like.
-			 */
-			sbuf->st_mode = S_IFDIR;
-		}
+		/* Don't call lstat on an empty path, or at all when we
+		 * know that "root" is always directory-like.
+		 */
+		sbuf->st_mode = S_IFDIR;
+		/* The base is only stat'd if the first element is "", "."
+		 * or "..", the only cases where it matters whether the base
+		 * is really a directory.  For any other name, lstat of the
+		 * appended element fails with ENOTDIR under a non-directory,
+		 * giving the same result as stat'ing the base.
+		 */
+		if (*current > root)
+			stat_base = 1;
 	}
 	pseudo_debug(PDBGF_PATH | PDBGF_VERBOSE, "paes: newpath %s, element list <%.*s>\n",
 		newpath, (int) elen, path);
@@ -889,6 +896,15 @@ pseudo_append_elements(char *newpath, char *root, size_t allocated, char **curre
 		 */
 		pseudo_debug(PDBGF_PATH | PDBGF_VERBOSE, "element to add: '%.*s'\n",
 			(int) this_elen, path);
+		if (stat_base) {
+			stat_base = 0;
+			if (this_elen == 0 || (path[0] == '.' &&
+			    (this_elen == 1 || (this_elen == 2 && path[1] == '.')))) {
+				if (!pseudo_real_lstat || (pseudo_real_lstat(newpath, sbuf) == -1)) {
+					sbuf->st_mode = 0;
+				}
+			}
+		}
 		if (pseudo_append_element(newpath, root, allocated, current, path, this_elen, sbuf, leave_this) == -1) {
 			retval = -1;
 			break;
-- 
1.8.3.1


  parent reply	other threads:[~2026-11-11  4:21 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11  4:20 [pseudo][PATCH 00/34] Various fixes Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 01/34] pseudo_client: serialize path changes across clients Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 02/34] ports: hold the path lock in create and metadata wrappers Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 03/34] ports: check created files under the path lock before recording them Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 04/34] test-parallel-ops: Add a parallel create/rename/link/chmod test Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 05/34] ports: Remove PSEUDO_NO_REAL_AT_FUNCTIONS Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 06/34] ports/unix: Remove guts overridden by the Linux port Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 07/34] ports: Let the port define how pseudo stats files Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 08/34] patches/yocto-older-glibc-symbols.patch: Update to latest YP version Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 09/34] Remove unused #if 0 debug code Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 10/34] Replace #if 0 strerror() debug messages with a comment Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 11/34] pseudo_util: Remove stale with_libpseudo() comment Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 12/34] ports/common/subports: Show the unknown OS name in the error Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 13/34] ports/linux: Drop redundant PSEUDO_LINK_SYMLINK_BEHAVIOR override Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 14/34] pseudo_server: Skip events for clients closed earlier in the batch Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 15/34] pseudo_server: Drop an idle client when out of file descriptors Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 16/34] pseudo_server: Always use epoll Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 17/34] configure: Fix --disable-* options Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 18/34] pseudo_server: Lower highest_client when the highest client closes Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 19/34] pseudo_server: Set highest_client to the new client when growing Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 20/34] pseudo_util: Store PSEUDO_SEVERITY as severity names Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 21/34] pseudo: Fix the -k severity option Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 22/34] pseudo_util: Make -v and numeric PSEUDO_DEBUG add debug flags Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 23/34] AGENTS.md: Add to assist future developers Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 24/34] pseudo_util: Fix the date format in the event log dump Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 25/34] pseudo: List -p in the usage message Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 26/34] pseudo_util: Write the event log to PSEUDO_EVLOG_FILE Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 27/34] pseudo.1: Update to match the current code Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 28/34] ports/linux: Don't require <linux/mount.h> Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 29/34] configure: Require GCC 5 or later Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 30/34] doc: Update README and doc/ to match the code Mark Hatle
2026-11-11  4:20 ` [pseudo][PATCH 31/34] configure: Fix the --enable-profiling default Mark Hatle
2026-11-11  4:21 ` Mark Hatle [this message]
2026-11-11  4:21 ` [pseudo][PATCH 33/34] wrappers: Don't re-block signals in nested wrapper calls Mark Hatle
2026-11-11  4:21 ` [pseudo][PATCH 34/34] Wrap execvpe(), fexecve() and execveat() Mark Hatle

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1794370862-30908-33-git-send-email-mark.hatle@kernel.crashing.org \
    --to=mark.hatle@kernel.crashing.org \
    --cc=yocto-patches@lists.yoctoproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.