Do you guys know some kind of runtime that monitor...
# thinking-together
f
Do you guys know some kind of runtime that monitors memory usage / runtime and warns / asks whether to continue when either is unreasonably high? Wouldn't it be cool if your running program could ask things like "the for loop X will take ~5 hours to complete, are you sure you want to continue?" or "your program is about to allocate 20 gigabytes of memory, continue?"? My current approach is to just run my programs and hope for the best. If it takes a long time I'll probably abort and restart with print statements inserted at appropriate places and if the computer becomes unresponsive I'll try to kill the process before its memory usage crashes my OS. It'd be really nice if a runtime would detect these things and warn me, at least during development. A concrete example where I would find such a system helpful is the Advent of Code (https://adventofcode.com/). For most of its challenges, there are easy but slow and more complicated but fast solutions. Implementing a simple solution first and checking whether it works for the given input would be much easier using a runtime like the one I described above.
Detection of infinite loops might also be an interesting feature
s
There's an interesting functional programming language experiment I found on Twitter but can't seem to find now. It adds efficiency concerns into the type system so you can explicitly reason about performance, like memory and time details. I think it's somewhat similar to Rust in its linear types that specify the number of times something can be accessed but I don't know almost anything about Rust so I may be mistaken
m
the erlang vm (BEAM) allows you to spawn a lightweight process with a limit size heap, if the process tries to allocate more it crashes, you can also start a timer from the spawning process and kill it via its reference when it goes over your limit
regarding detecting infinite loops and "seing resource usage" before execution, it sounds like the halting problem 🙂
👍 5
you can guess/detect the basic ones, but all of them I think it's not possible
note: crashing in erlang is not a bad thing, also process is more like people know as green threads
f
@Mariano Guerra detecting the basic ones would probably be enough for a lot of use cases. The Erlang approach seems reasonable, but still requires manual care to make a program detect excessive resource usage (much like print debugging) if I understood you correctly. Are there systems that have these "guard rails" built-in?
@stevekrouse you're right, I remember having seem that language as well. Lets see if I can find it in my bookmarks. I've used Rust almost daily for the past three months and don't know of any features in that direction.
Do you agree with the following principle? "A programming environment should prevent users from executing programs that accidentally use up all their systems' resources."
s
Maybe DTrace could help? (http://dtrace.org/blogs/about/) I've never used it directly, but it's the infrastructure that Xcode and Instruments build on to do all kinds of analysis during runtime. That’s not exactly what you asked for, but the CPU and memory gauges that Xcode displays when you run an app, that's exactly to get a feeling for potential memory leaks or see when a single thread maxes out the CPU for too long. As far as I know that's all based on DTrace and custom probes and there are some ancient mystics who I know can do magical things with it. Sadly, I’m not one of them.
m
@Felix Kohlgrüber how often does that happen? also, if not on the language runtime you can enforce process limits at the OS level, with cgroups in linux for example
Copy code
# Allows only 1ms every 100ms to simulate a slow system
cgset -r cpu.cfs_period_us=100000 -r cpu.cfs_quota_us=1000 sandbox
 
# Limit usage at 10% for a multi core system
cgset -r cpu.cfs_period_us=100000 \
     -r cpu.cfs_quota_us=$[ 10000 * $(getconf _NPROCESSORS_ONLN) ] \
           sandbox`

# Set a limit of 2Gb
cgset -r memory.limit_in_bytes=2G sandbox
 
# Get memory stats used by the cgroup
cgget -r memory.stat sandbox
continuing with the erlang promotion 😛 in erlang you can trace processes dynamically https://github.com/andytill/erlyberly (that's a UI on top of the vm libraries)
s
Do you agree with the following principle?
"A programming environment should prevent users from executing programs that accidentally use up all their systems' resources." Yes I agree with that on the grounds that it is part of the more general principle that the computer should communicate to you the potentially undesirable, unforeseen consequences of what you’re asking of it (https://twitter.com/stevekrouse/status/1083891157858426886?s=09) Also I found it: https://granule-project.github.io/
👍 1
n
Maybe the biggest difference between programming and just using computer is that while programming you are making all the decisions beforehand. Uncatched exceptions are just situations where programmer has not made decision what to do. Distinction between user and programmer could be much smaller. When unmade decission is met simply give control back to user (instead of crashing). What if WriteFileException would always open normal save dialog without extra effort so that user can save for somewhere else if default location was unavailable and same if reading fails.
j
I recall Zig being similar to Rust but caring a lot about the possibility of memory allocation failure: https://ziglang.org/