173 points · by todsacerdoti · 3y ago · 82 comments
Watchpoints, when they're suitable, are an incredibly powerful way to query your program's runtime behaviour (When does this happen? Why does this value end up here?) instead of stepping through and printing things / logging stuff.
They also fast, provided you watch values suitable for the CPU's logic to handle them directly. They need to be small-ish and located in memory for hardware support to handle it, otherwise GDB needs to single step.
(the `-l` flag is also important here and even less well known - it tells GDB you really care about the memory address the expression is stored in, not the expression itself)
I'm wondering if it's possible to put some directives into the generated C++ code that would map its debug info directly back to source lines in the Python - I can't find any docs to confirm my feeling this should be possible.
Edit: Maybe this? https://gcc.gnu.org/onlinedocs/cpp/Line-Control.html
So I think that would be cool, but I don't usually use a debugger in Python (or even C++ -- I tend to write small programs using shell as the "REPL").
Oil is extremely test-driven and shell's stdin/stdout type interfaces means it's pretty easy to exhaustively test. Examples: https://www.oilshell.org/release/0.13.1/test/spec.wwz/survey...
The GC is the one case we have where that kind of testing/debugging doesn't suffice! So I started watching the CppCon videos on debuggers a couple years ago and found some good talks :)
Which effectively combines a `watch -l`, plus a reverse-continue but also monitors when the underlying memory was allocated / freed.
It's quite nice, basically "git blame" for your variables.
I'm on the Undo mailing list as well -- nice to see that it's effective!
(Also should say that Clasp sounds very cool -- I'm a fan of anything that enables interop and reuse, rather than rebuilding the same thing in different languages, which may or may not be as good)
It succeeds because it only solves the GC problems that Oil has (and I mean this as a high complement).
Oil is single threaded, code runs in loops that are well understood, and the code is generated from a strongly typed subset of Python.
The GC then is run only between loop iterations, so there is no need for stack scanning. You never have to worry about a root being in a temporary (from the point of view of the C++ compiler) since there are just a few locals in the function running the loop, and any variables local to the loop are logically dead between iterations.
Since a goal of Oil is portability, not scanning registers and stacks is very important. Getting this right when the GC could be invoked at any allocation is potentially intractable with mypy semantics at least.
It's unusual because it's a precise collector in C++, and what I slowly realized is that that problem is basically impossible for any non-trivial software, without changing the C++ language itself :)
It seems like that hasn't happened, despite efforts over decades. I added this link about C++ GC support to the appendix, which also explains our unique constraints.
Garbage collection in the next C++ standard (Boehm 2009)
https://dl.acm.org/doi/abs/10.1145/1542431.1542437
http://www.oilshell.org/blog/2023/01/garbage-collector.html#...
---
The reason that precise GC can work for Oil is because it's a shell that links with extremely little 3rd-party code, and has relatively low perf requirements. We depend on libc and GNU readline, just like bash. And those libraries are basically old-school C functions which are easy to wrap with a GC.
(Also as Aidenn mentioned, shells use process-based concurrency, which means we don't have threads. The fact that it's mostly generated C++ code is also important, as mentioned in the post)
---
The funny thing is that one reason I started this project is because I worked with "big data" frameworks on clusters, but I found that you can do a lot on a single machine. (in spirit similar to the recent "Twitter on one machine post" https://news.ycombinator.com/item?id=34291191 )
I would just use shell scripts to saturate ~64 cores / 128 G of RAM, rather than dealing with slow schedulers and distributed file systems.
But garbage collectors and memory management are a main reason you can't use all of a machine from one process. There's just so much room for contention. Also the hardware was trending toward NUMA at the time, and probably is even more now, so processes make even more sense.
All of that is to say that I'm a little scared of multi-threaded GC ... especially when linking in lots of third party libraries.
And AFAIK heaps with tens or hundreds of gigabytes are still in the "not practical" range ... or they would take a huge amount of engineering effort
---
But of course there are many domains where you don't have embarrassingly parallel problems, and writing tight single- or multi-threaded code is the best solution.
Some more color here: https://old.reddit.com/r/oilshell/comments/109t7os/pictures_...
I wonder if Clasp has any support for multi-process programming? Beyond Unix pipes, you could also use shared memory and maybe some semaphores to synchronize access, and avoid copying. I think of that as sort of "inverting" the problem. Certain kinds of data like pointer-rich data is probably annoying to deal with in shared memory, but there are lots of representations for data and I imagine Lisps could take advantage of some of them, e.g. https://github.com/oilshell/oil/wiki/Compact-AST-Representat...
> gdb has had reversible debugging since release 7, in 2009. What does udb offer that it lacks?
GDB's built-in reversible debugging is cool (and it's helped raise awareness) but it doesn't scale well. We build on the same command set and serial protocol that GDB defined - UDB is GDB but with additional Python code hooking it up to our separate record/replay engine.
For UDB, I'd say we offer: 1. Performance & efficiency (orders of magnitude faster at runtime and lower in memory requirements). 2. Recordings can be saved to portable files (share with colleagues, receive from customers, etc). 3. Library API so applications can self-record with control of when to capture and save. 4. Wider support of modern software (proactively tracking modern CPU features, shared memory and device maps, etc). 5. Correctness (in the past we've found the reverse operations in GDB don't have as strong semantics as we'd hoped, though I'd also be happy to be wrong here)
FWIW, rr (https://rr-project.org/) also offers many similar benefits over GDB's built-in system (though not the library API in point 3) but with differences in what CPUs / systems are supported, ability to attach at runtime, etc.
If you're looking for an open source solution, I'd choose rr over GDB's built-in approach.
In my experience rr's recording is around x2 slower for single threaded programs than running the program on its own.
I think featurewise they are in parity.
I heard that udb is like rr, but better on some fronts (except price and software freedom).
rr's recording works across system calls, thread context switches, etc, but gdb's doesn't.
rr's recording creates a persistent recording on disk that you can even move around between machines. This permits workflows that gdb's doesn't.
(Disclaimer: I work on rr)
A thousand years from now when they're digging up texts and artifacts from this period, there's likely going to be a lot of confused academics.
Edit: this link posted by another commenter gives a nice overview of the tradeoffs between the methods and it has very nice graphs to boot https://spin.atomicobject.com/2014/09/03/visualizing-garbage...
That is, with ARC, the more garbage you generate, the more work will be spent on the GC vs normal program flow. In contrast, with tracing/copying, you can generate as much garbage as you want, without affecting GC time; but, the more memory you actually use, the more time you will spend in GC.
A is for Automatic, as in the compiler adds the calls needed to manage the reference counting.
If the language and compiler can guarantee a reference is strictly contained within a thread, it doesn't have to be atomic. Usually that is not the case, so atomic operations are used.
What do you mean by 'usage'? References don't change when reading or writing the data being reference counted, they change when being passed to a function or returned from one. In C++ it's rare that you would even use reference counting, since that essentially means you don't know the lifetime of your value. Most variables are going to be on the stack and the vast majority of dynamic allocation is going to be referenced from a single scope at one time.
The reality is that it takes gross incompetence to have a speed impact from reference counting.
What’s manual rc like?
E.g., the Arc type in Rust. Rust, for example, has a separate Rc type. Rc should outperform Arc, but cannot be shared across threads.