From 21cfa1bde212e0f502779f0e4805eb6a1c1fa091 Mon Sep 17 00:00:00 2001 From: Micheal Smith Date: Mon, 23 Mar 2026 12:22:03 -0500 Subject: [PATCH] Initial commit --- .gitignore | 3 ++ LICENSE | 11 +++++ README.md | 34 +++++++++++++ compile_commands.json | 1 + meson.build | 26 ++++++++++ xdu.c | 110 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 185 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 120000 compile_commands.json create mode 100644 meson.build create mode 100644 xdu.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61bfbff --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +build/ +.cache/ +.clang-format diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b040710 --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ +Copyright 2026 Micheal Smith + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Redistributions in any form must retain this license verbatim. No additional licensing terms, including but not limited to the GNU General Public License, may be imposed on the original or modified work. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..06a47bf --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# xdu + +Just a tool that implements 'du -s' behavior using [OpenMP](https://www.openmp.org). +This conforms to [IEEE Std POSIX 1003.1-2024](https://publications.opengroup.org/standards/unix/c243) rather +than any specific OS, or distribution behaviors. This shouldn't impact much as most implementations +I know of generally conform to POSIX. + +## Building + +Just a standard meson [meson](https://mesonbuild.com) project. + +``` shell +meson setup build +meson compile -C build +``` + +## *Very* anecdotal performance findings + +I just ran this, and diskus in my home directory and measured with time. +I did it numerous times. Here are just the last results I got. + +``` shell +diskus 2.67s user 85.20s system 683% cpu 12.857 total +xdu 23.47s user 46.78s system 878% cpu 7.998 total +``` + +So around about a 40% improvement give or take on each run. I'm not +really looking to run an entire benchmark on du implementations here I +just wanted to see how OpenMP would perform, and it performs pretty +well. + +There are further improvement possibilities with batching tasks and +such, but I'll leave that in case I get some crazy idea into making this +a full du implementation. diff --git a/compile_commands.json b/compile_commands.json new file mode 120000 index 0000000..25eb4b2 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1 @@ +build/compile_commands.json \ No newline at end of file diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..0eda51c --- /dev/null +++ b/meson.build @@ -0,0 +1,26 @@ +project( + 'xdu', + 'c', + meson_version : '>= 1.3.0', + version : '0.1', + default_options : ['warning_level=3'], +) + +omp = dependency('openmp') + +dependencies = [ + omp +] + +sources = [ + 'xdu.c', +] + +exe = executable( + 'xdu', + sources, + dependencies : dependencies, + install : true, +) + +test('basic', exe) diff --git a/xdu.c b/xdu.c new file mode 100644 index 0000000..6f62843 --- /dev/null +++ b/xdu.c @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * Only a directory should be passed here currently as it's + * very naive. + */ +ssize_t sizeof_dir(const char *restrict path) { + DIR *dir = NULL; + char estr[LINE_MAX] = {'\0'}; + struct dirent *de = NULL; + ssize_t sz = 0; + struct stat st; + int err = 0; + + if ((dir = opendir(path)) == NULL) { + strerror_r(errno, estr, sizeof estr); + fprintf(stderr, "Error opening directory '%s': %s\n", path, estr); + return -1; + } + + if (lstat(path, &st) != 0) { + strerror_r(errno, estr, sizeof estr); + fprintf(stderr, "Error callings stat on '%s': %s\n", path, estr); + closedir(dir); + return -1; + } + + /* No tasks spawned yet, plain write is safe. */ + sz += st.st_blocks * 512; + + while ((de = readdir(dir)) != NULL) { + if ((strcmp(de->d_name, ".") == 0) || (strcmp(de->d_name, "..") == 0)) + continue; + + char fullpath[PATH_MAX]; + snprintf(fullpath, sizeof fullpath, "%s/%s", path, de->d_name); + + if (lstat(fullpath, &st) != 0) { + strerror_r(errno, estr, sizeof estr); + fprintf(stderr, "Error callings stat on '%s': %s\n", de->d_name, estr); + #pragma omp atomic write + err = 1; + break; + } + + if (!S_ISDIR(st.st_mode)) { + #pragma omp atomic + sz += st.st_blocks * 512; + } else { + char *fp = strdup(fullpath); + if (!fp) { + fprintf(stderr, "strdup failed\n"); + #pragma omp atomic write + err = 1; + break; + } + + #pragma omp task firstprivate(fp) shared(sz, err) + { + ssize_t result = sizeof_dir(fp); + free(fp); + if (result == -1) { + #pragma omp atomic write + err = 1; + } else { + #pragma omp atomic + sz += result; + } + } + } + } + + closedir(dir); + #pragma omp taskwait + + return err ? -1 : sz; +} + +/* + * Just a *possible* proof of concept. + */ +int main(int argc, char **argv) { + ssize_t sz = 0; + + if (argc < 2) { + fprintf(stderr, "Insufficient arguments.\n"); + exit(EXIT_FAILURE); + } + + #pragma omp parallel + #pragma omp single + sz = sizeof_dir(argv[1]); + + if (sz == -1) + fprintf(stderr, "Failed.\n"); + + printf("Size = %zd\n", sz); + + return 0; +}