Initial commit

This commit is contained in:
Micheal Smith 2026-03-23 12:22:03 -05:00
commit 21cfa1bde2
Signed by: xulfer
GPG key ID: 1BBF282038A8EC3E
6 changed files with 185 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
build/
.cache/
.clang-format

11
LICENSE Normal file
View file

@ -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.

34
README.md Normal file
View file

@ -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.

1
compile_commands.json Symbolic link
View file

@ -0,0 +1 @@
build/compile_commands.json

26
meson.build Normal file
View file

@ -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)

110
xdu.c Normal file
View file

@ -0,0 +1,110 @@
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/dirent.h>
#include <sys/stat.h>
#include <sys/syslimits.h>
/*
* 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;
}