Posting here in particular since this uses some pretty new kernel features (specifically PSI, which is enabled in the default Arch kernel) that I think will be of interest to Arch Linux users. :-)
I think we've all had situations where the desktop environment has become so slow as to be unusable, usually because some application has started eating up system resources quickly. Fellow Chrome users, I'm especially looking at you.
My wife is certainly someone in that position. Often she gets into scenarios where unwieldy applications like Chrome would unexpectedly eat memory over time, eventually ending with her desktop suddenly slowing to a crawl. Usually the only way out of this situation is to manually trigger an OOM kill with Alt + SysRq + F, and even then that's not foolproof, since the OOM killer may not pick the right target to kill. Even if it does pick the right target to kill, system responsiveness after the fact can still be impacted since the thrashing has pushed many pages into swap.
Logging PSI metrics on her machine leading up to these incidents, it was pretty obvious that there was always an opportunity to alert her ahead of time that the situation was becoming a problem, and allow her to manually select which applications to kill, since it might not always just be the one using the most memory or with the highest memory usage in a desktop scenario. Since prioritisation of "what to kill" is so dynamic on the desktop, it's very hard to write a one-size-fits-all policy about what to do in these scenarios, so these notifications allow you to avoid that problem entirely.
To that extent, psi-notify will warn you using whatever desktop notifier you have running (eg. GNOME notifications, dunst, xfce4-notifyd) when resources are starting to become contended, so that you can do something about that. A facetious demo is shown in the readme.
Feature requests and bug reports are welcome. I've been using it for the last week and already found it useful in keeping death-by-Chrome at bay. :-)
Hey, this is really neat! Since it's on a similar vein, I'll share a little script I just finished writing because I was frustrated that my system was locking up when I'd compile large packages from the AUR. Now my window manager stays responsive because one of the CPUs isn't being hammered (they start counting at 0).
limit-cpu
#!/bin/bash
# systemd-run clears the environment so copy it manually
ENV="$(env | sed 's/\(.*\)/"\1"/' | tr '\n' ' ')"
systemd-run \
--user \
--pty \
--pipe \
--quiet \
--property Environment="$ENV" \
--property AllowedCPUs=1-7 \
"$@"
limit-cpu yay -S linux-lts419
Does this work better than nice? I realize the two work differently (controlling CPU affinity whereas nice controls thread priority), but it seems like nice would have the same end result.
It works much better. Using nice got me from completely non-responsive to barely usable. Basically, I would press a key or move the mouse and then wait several seconds for the input to register.
My theory is that the window manager idles at very low CPU usage even though it has a high priority because it isn't doing anything. Even though the build process has lower priority, it's free to grab the rest of the CPU usage because nothing is using it. Then I make some sort of input that the window manager needs to process. Switching workspaces redraws the whole screen, so there would normally be a small spike in CPU usage. But there isn't any free so it has to ask for it. The nice settings ensure that it gets it, but that evidently takes some time. After finishing redrawing, the window manager drops back to its idling state and the whole process starts over resulting in a jerky and delayed experience.
The script is literally telling whatever you run with it that it isn't allowed to touch the first processor, so assuming you aren't doing anything else CPU intensive, there should always be free processing power for the window manager to grab when it needs it.
I just wish Linux DEs could prevent runaway applications from eating all the memory. On macOS, it works really well. Instead of slowing the whole system to a halt, it allows user to kill the culprit before it's too late. Maybe have something similar for Linux? When some process eats almost all of the memory, pause it, ask user what to do next (continue/abort) and prevent those horrible stutters and slowdowns.
I'm a Chromium tab hoarder and thought this was a problem unique to me and my ten year old machine... I can usually tell it's coming when my cursor and text inputs start to lag a bit.
I'm torn between checking out the project and trying to fix the underlying problem, either my habits or limiting resource utilization by processes. Not entirely sure how to go about that though.
One option is systemd slices. You can set both CPU and memory limits for an individual program or a group of processes. Or libcgroup which comes with a set of command line tools to achieve the same things. They're both based on cgroups.
I just wanted to comment that you might want to separate your code to multiple files. I don't know the layout and workings of your code so I don't know if it's feasible. Also don't know much about C so take this with a grain of salt.
Personally, I just wanted to get a quick idea how the project works, but immediately backed out when I saw the 550 lines of code. Separating it out might make it more welcoming and get you more contributions down the line.
Thanks, and sure, I can see where you're coming from. There are definitely tradeoffs in both directions, but there are a few nice things about having everything in one file for small projects (let's say <1000 lines):
Ease of navigation. People don't have to worry about ctags, an IDE or whatever to find things quickly. They know that if it's provided by this project, it's in this file.
Reduction of Makefile complexity and dependency knowledge. Ok, so it doesn't add a whole lot more work to add some more objects to the mix in the Makefile, but I also don't see much reason to add them, either.
I'm not convinced splitting into more files makes things much clearer. If I were to do this, we'd probably end up with five files: notify.[ch], psi.[ch], and maybe cli.c. I'm not sure that really achieves much, which functions are part of which of these is already pretty clear.
psi-notify is fundamentally pretty simple in concept. If you trace through main(), I think you'll get there pretty fast :-)
All the functions pretty much do what they say they do:
These are some good points too. I will try it on my system and see if there is something I would like to contribute.
It's about time I learned C, as it seems I have been learning languages in sort of reverse order. From Python and Bash to Java to webdev and now quite recently Rust.
Why is this useful? I don't have a new computer but it still never becomes crippled even under heavy loads. One of the benefits of Linux has always been that the kernel is good at distributing resources. Perhaps try a low-latency kernel if you're having issues?
Hey! I actually work full time on Linux memory management and am one of the cgroup maintainers, so I'm pretty well versed in in this area. :-)
While it's true we're not bad at distributing resources, the question is about what to do when there is a scarcity of resources, and that's what PSI helps with -- identifying those scarcities as they start to occur and allowing some application or user to do something about it.
Low latency kernels are unlikely to help in these scenarios, because this isn't about latency or throughput, but instead actual resource shortages. That's what PSI has been designed to identify.
40 comments
On the AUR: https://aur.archlinux.org/packages/psi-notify/
Posting here in particular since this uses some pretty new kernel features (specifically PSI, which is enabled in the default Arch kernel) that I think will be of interest to Arch Linux users. :-)
I think we've all had situations where the desktop environment has become so slow as to be unusable, usually because some application has started eating up system resources quickly. Fellow Chrome users, I'm especially looking at you.
My wife is certainly someone in that position. Often she gets into scenarios where unwieldy applications like Chrome would unexpectedly eat memory over time, eventually ending with her desktop suddenly slowing to a crawl. Usually the only way out of this situation is to manually trigger an OOM kill with Alt + SysRq + F, and even then that's not foolproof, since the OOM killer may not pick the right target to kill. Even if it does pick the right target to kill, system responsiveness after the fact can still be impacted since the thrashing has pushed many pages into swap.
Logging PSI metrics on her machine leading up to these incidents, it was pretty obvious that there was always an opportunity to alert her ahead of time that the situation was becoming a problem, and allow her to manually select which applications to kill, since it might not always just be the one using the most memory or with the highest memory usage in a desktop scenario. Since prioritisation of "what to kill" is so dynamic on the desktop, it's very hard to write a one-size-fits-all policy about what to do in these scenarios, so these notifications allow you to avoid that problem entirely.
To that extent, psi-notify will warn you using whatever desktop notifier you have running (eg. GNOME notifications, dunst, xfce4-notifyd) when resources are starting to become contended, so that you can do something about that. A facetious demo is shown in the readme.
Feature requests and bug reports are welcome. I've been using it for the last week and already found it useful in keeping death-by-Chrome at bay. :-)
I am a linux noob here. But could you tell me what desktop notifier I could use on i3 for this to work?
No worries! We've all been there. :-)
You can use any notifier which implements the Desktop Notifications specification. Here are a couple of examples which should just work:
Look here: https://wiki.archlinux.org/index.php/Desktop_notifications#Standalone
I personally use https://wiki.archlinux.org/index.php/Dunst
Hey, this looks super neat - i'm considering pulling this in to the official arch linux repositories in the future.
Though, i've noticed the github repository doesn't ever mention licensing, and the AUR package states MIT - you might want to clarify this in the repo
Thanks! And whoops, let me add the license now :-)
I need this so much! Thank you! Here, have some ice cream (┛❍ᴥ❍)┛彡<3
This seems extremely useful! Thank you
Hey, this is really neat! Since it's on a similar vein, I'll share a little script I just finished writing because I was frustrated that my system was locking up when I'd compile large packages from the AUR. Now my window manager stays responsive because one of the CPUs isn't being hammered (they start counting at 0).
limit-cpu
Does this work better than
nice? I realize the two work differently (controlling CPU affinity whereas nice controls thread priority), but it seems like nice would have the same end result.It works much better. Using
nicegot me from completely non-responsive to barely usable. Basically, I would press a key or move the mouse and then wait several seconds for the input to register.My theory is that the window manager idles at very low CPU usage even though it has a high priority because it isn't doing anything. Even though the build process has lower priority, it's free to grab the rest of the CPU usage because nothing is using it. Then I make some sort of input that the window manager needs to process. Switching workspaces redraws the whole screen, so there would normally be a small spike in CPU usage. But there isn't any free so it has to ask for it. The nice settings ensure that it gets it, but that evidently takes some time. After finishing redrawing, the window manager drops back to its idling state and the whole process starts over resulting in a jerky and delayed experience.
The script is literally telling whatever you run with it that it isn't allowed to touch the first processor, so assuming you aren't doing anything else CPU intensive, there should always be free processing power for the window manager to grab when it needs it.
what font is that in the screenshot on the readme?
also: finally my computer won't shit the bed while I'm playing CSGO
It's Terminus, just blown up to a ridiculous size for the demo. Usually I run it at pixelsize=8: https://raw.githubusercontent.com/cdown/dwm/master/screenshot.png
That's really cool! Good work
Thanks! Installed.
Installed, now anxiously waiting for the Chrome overload. :-)
I just wish Linux DEs could prevent runaway applications from eating all the memory. On macOS, it works really well. Instead of slowing the whole system to a halt, it allows user to kill the culprit before it's too late. Maybe have something similar for Linux? When some process eats almost all of the memory, pause it, ask user what to do next (continue/abort) and prevent those horrible stutters and slowdowns.
Try earlyoom.
There’s also oomd, which is likely going to get pulled into systemd itself: https://www.phoronix.com/scan.php?page=news_item&px=Systemd-OOMD-April-WIP
I'm a Chromium tab hoarder and thought this was a problem unique to me and my ten year old machine... I can usually tell it's coming when my cursor and text inputs start to lag a bit.
I'm torn between checking out the project and trying to fix the underlying problem, either my habits or limiting resource utilization by processes. Not entirely sure how to go about that though.
One option is systemd slices. You can set both CPU and memory limits for an individual program or a group of processes. Or libcgroup which comes with a set of command line tools to achieve the same things. They're both based on cgroups.
Damn a little too late. You should have told me before I did this.
I did that mostly because I couldn't run a language server and a browser at the same time
What you've made is awesome and you are awesome. Aaaand it's installed.
I just wanted to comment that you might want to separate your code to multiple files. I don't know the layout and workings of your code so I don't know if it's feasible. Also don't know much about C so take this with a grain of salt.
Personally, I just wanted to get a quick idea how the project works, but immediately backed out when I saw the 550 lines of code. Separating it out might make it more welcoming and get you more contributions down the line.
Thanks, and sure, I can see where you're coming from. There are definitely tradeoffs in both directions, but there are a few nice things about having everything in one file for small projects (let's say <1000 lines):
psi-notify is fundamentally pretty simple in concept. If you trace through
main(), I think you'll get there pretty fast :-)All the functions pretty much do what they say they do:
I had no idea you could list out the definitions that ctags has found for a file. You have just blown my mind.
These are some good points too. I will try it on my system and see if there is something I would like to contribute.
It's about time I learned C, as it seems I have been learning languages in sort of reverse order. From Python and Bash to Java to webdev and now quite recently Rust.
For cases like this look into earlyoom too
Why is this useful? I don't have a new computer but it still never becomes crippled even under heavy loads. One of the benefits of Linux has always been that the kernel is good at distributing resources. Perhaps try a low-latency kernel if you're having issues?
Hey! I actually work full time on Linux memory management and am one of the cgroup maintainers, so I'm pretty well versed in in this area. :-)
While it's true we're not bad at distributing resources, the question is about what to do when there is a scarcity of resources, and that's what PSI helps with -- identifying those scarcities as they start to occur and allowing some application or user to do something about it.
Low latency kernels are unlikely to help in these scenarios, because this isn't about latency or throughput, but instead actual resource shortages. That's what PSI has been designed to identify.
You can find out more about why PSI is useful in my talk at SREcon last year. Discussion of PSI is around the 25 minute mark: https://www.youtube.com/watch?v=beefUhRH5lU&feature=youtu.be&t=1505
Ouch! :)
Well, thanks for the information, I will have a look at the discussion.
For some reason my brain flagged this as an ad and I almost downvoted... oops!