From bbe748063fa139949656fd7a8dec86ebdecddb6f Mon Sep 17 00:00:00 2001 From: Brian Silverman Date: Mon, 15 Feb 2016 16:15:24 +0000 Subject: Use hard links to create empty files. This speeds up my test of running a bunch of genrules with 3000 inputs by 2x on XFS. It might speed up ext4 a bit, but it definitely doesn't make it slower. XFS and ext4 are about the same speed with this. Test at . Refs #852 RELNOTES: Improved sandbox performance on XFS filesystems. -- Change-Id: Ifabc0442f9f919d50cb5fe80a6445890c6d7f03e Reviewed-on: https://bazel-review.googlesource.com/#/c/2890/1 MOS_MIGRATED_REVID=114693725 --- src/main/tools/namespace-sandbox.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/tools/namespace-sandbox.c b/src/main/tools/namespace-sandbox.c index 0153211b7b..b092cfec5d 100644 --- a/src/main/tools/namespace-sandbox.c +++ b/src/main/tools/namespace-sandbox.c @@ -400,6 +400,13 @@ static void CreateFile(const char *path) { CHECK_CALL(close(handle)); } +// Creates an empty file at 'path' by hard linking it from a known empty file. +// This is over two times faster than creating empty files via open() on +// certain filesystems (e.g. XFS). +static void LinkFile(const char *path) { + CHECK_CALL(link("tmp/empty_file", path)); +} + static void SetupDevices() { CHECK_CALL(mkdir("dev", 0755)); const char *devs[] = {"/dev/null", "/dev/random", "/dev/urandom", "/dev/zero", @@ -448,7 +455,7 @@ static int CreateTarget(const char *path, bool is_directory) { if (is_directory) { CHECK_CALL(mkdir(path, 0755)); } else { - CreateFile(path); + LinkFile(path); } return 0; @@ -499,6 +506,9 @@ static void SetupDirectories(struct Options *opt) { CHECK_CALL(CreateTarget(opt->create_dirs[i] + 1, true)); } + // This is used as the base for hardlinking the input files. + CreateFile("tmp/empty_file"); + // Mount all mounts. for (int i = 0; i < opt->num_mounts; i++) { struct stat sb; -- cgit v1.2.3