Skip to content

Strace cache#128

Closed
branc116 wants to merge 12 commits intotsoding:mainfrom
branc116:strace-cache
Closed

Strace cache#128
branc116 wants to merge 12 commits intotsoding:mainfrom
branc116:strace-cache

Conversation

@branc116
Copy link

So I've implemented caching system that uses strace to automatically find out which are input/output files.

Example on how to use it.

#define NOB_IMPLEMENTATION
#include "nob.h"

int main(void)
{
    Nob_Cmd cmd = { 0 };
    Nob_Strace_Cache cache = { .file_path = "nob.cache" };

    nob_cmd_append(&cmd, "cc", "-o", "main", "main.c");
    nob_cmd_run(&cmd, .strace_cache = &cache);

    nob_strace_cache_finish(cache);
    nob_cmd_free(cmd);
}

First time you run it, it will generate nob.cache file that contains something like this:

main: main.c
  cc -o main main.c

When you run nob again it will read and parse nob.cache file and will only run the command if the main.c (or anything included by main.c) was modified (nob_needs_rebuild).

This has a few TODOs still:

    • Use in combination with stdin/out - easy
    • Use in combination with stderr -hard
    • Use in combination with async - Idk how...
    • Think about other syscalls that a command can make that can invalidate cache:
      • getenv
      • sockets
      • Do we wanna even support those commands?
      • Maybe store hashes of each files in
    • Make the output format such that the first line of command is cd command to the pwd of command. [how_to/nob.c not cachable because of this]
    • How to do this on BSDs?
    • How to do this on Mac?
    • How to do this on Windows?
    • Check how stable strace output is.
    • realpath expands symlinks and that means that if the symlink changes, cache will not be invalidated. Do something about this..
    • Use <sys/ptrace.h> insted of strace utility. Check how to use <sys/ptrace.h> at https://github.com/strace/strace
      strace is just a program that is not on all distributions. <sys/ptrace.h> is part of glibc and should be more cross platform.
    • Do something about programs that don't read or write anything..

Motivation:

You have for example N png files.
You have a tool to generate atlas from png files.
You don't want to build the tool every time.
And you don't want to run the tool every time.
But you don't want to manually specify which .png will be used by the tool.

Coolest thing I found out when looking at the output was this:

.generated/temp1: /home/branimir/.gitconfig /home/branimir/Documents/source/branc116/brplot/.git/HEAD .git/config .git/HEAD .git/refs/heads/master bin/redirect_output
	bin/redirect_output .generated/temp1 git branch --show-current

.generated/temp2: /home/branimir/.gitconfig /home/branimir/Documents/source/branc116/brplot/.git/HEAD .git/config .git/HEAD .git/refs/heads/master .git/packed-refs bin/redirect_output
	bin/redirect_output .generated/temp2 git rev-parse HEAD

I'm running git commands during build process to get the current branch and commit hash and it found the dependencies for those commands.
I didn't know files .git/HEAD, .git/refs/heads/master, and .git/packed-refs existed, but the tool I made uses those files to do something. Magic.

@yuI4140
Copy link
Contributor

yuI4140 commented Sep 1, 2025

Use make instead of nob.
I think adding cache is unecesary. Therefore in your project, you can implement "cache" like system, if build directory exists then not compile;things like that.

@branc116
Copy link
Author

branc116 commented Sep 1, 2025

I'd use make, but even make can't do what I implemented in this pr.
With make you have to specify dependencies for each command, this finds dependencies automaticaly.
Also I was lookimg at the ptrace ( the syscall that's used by strace ) And an implementation would be simpler. so I'd like to reimplement this with ptrace.

@branc116 branc116 closed this Sep 1, 2025
@branc116 branc116 mentioned this pull request Sep 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants