janestreet / memtrace

Streaming client for OCaml's Memprof
MIT License
65 stars 13 forks source link

Better error handling during trace opening #12

Closed stedolan closed 3 years ago

stedolan commented 3 years ago

Lock the trace file, to avoid corruption from multiple PIDs, and give a vaguely reasonable error message if it can't be opened.

Signed-off-by: Stephen Dolan sdolan@janestreet.com

stedolan commented 3 years ago

I had thought that calling ftruncate was equivalent to O_TRUNC (I don't want to truncate if it turns out we don't have the lock), but it's not. (No two sequences of POSIX calls are equivalent, as far as I can tell).

If called on a non-regular file, ftruncate fails while O_TRUNC is a no-op. Concretely, this breaks MEMTRACE=/dev/null ./foo, which is a slightly weird usage but one I'd prefer not to break. (Maybe someone wants to test the perf overhead of memtrace with a large sample rate, without actually making a big file).

So, I'm going to call ftruncate, but ignore any errors. I think this is the best simulation of O_TRUNC.