Alternative To the 200-Line Linux Kernel Patch
climenole writes "Phoronix recently published an article regarding a ~200 line Linux Kernel patch that improves responsiveness under system strain. Well, Lennart Poettering, a Red Hat developer, replied to Linus Torvalds on a mailing list with an alternative to this patch that does the same thing yet all you have to do is run 2 commands and paste 4 lines in your ~/.bashrc file."
There is a right way to do things and a hackish way. I wonder which one is which
Will this work on more than just x86, for example a rooted Android phone? I might try this now.
After logging in slashdot still does not take you back to the page you were on. It's been that way for 20 years.
Is there a catch?
Yes. To quote Linus:
User-level configuration for something that should just work is annoying. We can do better.
I think I will use an actual kernel patch rather then this hack in user space.
"Maybe this world is another planet's hell"
Aldous Huxley
I heard that if you stick a penny to the top of your case it speeds up everything by 200%.
this is definitely one of those things that I add now, then forget about later, and it becomes a condition that may or may not work when I apply upgrades & patches in the future. Whether or not the .bashrc approach is faster, I think that going down the kernel route makes it more consistently usable.
I didn't see any Try's, so I don't think so.
My understanding of the original kernel patch is that it just puts stuff from different ttys into different groups for scheduling purposes so that they're less able to hog each other's resources. This alternative just makes your shell sort it out itself when it starts i.e. when you're running a new terminal. So this should basically be equivalent.
See this comment from the latest article for Linus' take on putting this stuff in-kernel:
http://www.webupd8.org/2010/11/alternative-to-200-lines-kernel-patch.html#comment-98834842
The comment here is very important to remember though:
http://linux.slashdot.org/comments.pl?sid=1870628&cid=34241622
another comment on that article (which I can't now find - anybody know where it is?) basically said that the patch suits Linus's own use of compiling kernels whilst surfing the web. Sounds like a reasonably accurate assessment really so for now it's far from the magical boost to general interactivity some may have hoped for. In some sense there's no such thing anyhow.
Nonetheless the comment linked above also has Linus talking about increasing the scope of the automatic grouping heuristics in the future so hopefully the "just works" nature of this should become available to more people eventually.
The original kernel patch (and this alternative) aren't magically making everything respond better, they just improve certain usecases.
Poettering wants scheduling to be handled by his "systemd", a replacement to init/upstart. This, by the way, is the developer of Pulseaudio, so those of you who've experienced broken sound in recent years can now look forward to broken system initialization, coming soon to a Linux distribution near you...
This isn't a nasty hack like some userspace bodges round kernel problems can be. The functionality to schedule the CPU in controlled ways to different groups of processes has been in the kernel for some time now and simply needs configuring from userspace. The 200 line patch adds some default configuration of this mechanism to the kernel; this alternative uses the existing functionality to do the same thing. The same kernel mechanism should end up handling it.
It's good if the kernel can do more stuff so that the user doesn't have to - where it makes sense. But this is actually a reasonably neat solution if you want the same behaviour without upgrading your kernel.
Man, after reading some of that thread, those folks in kernel development make Slashdot users seem downright well-mannered.
There's a 68.71% chance you're right.
Two things:
1) There isn't a difference between the kernel patch and the command line hack. They are equivalent. The command line bit was known beforehand because that was the method used to figure out if this kernel hack would be a good idea. The kernel hack just makes the process transparent.
Linus says: Right. And that's basically how this "patch" was actually tested originally - by doing this by hand, without actually having a patch in hand. I told people: this seems to work really well.
2) Linus recommends the kernel patch:
Linus also says:Put another way: if we find a better way to do something, we should _not_ say "well, if users want it, they can do this *technical thing here*". If it really is a better way to do something, we should just do it. Requiring user setup is _not_ a feature.
Source.
Weaselmancer
rediculous.
It makes every process spawned by the user that passes through the bash shell add their process ID to a per-user task control group. See the documentation on control groups for more information about exactly what that means, and what what some of the commands involved aim to do. I'm not sure if this is exactly the same impact as the kernel-level patch, which aimed at per-TTY control groups. That might includes some processes that don't pass through something that executes the .bashrc file along the way.
Following the instructions for Ubuntu as detailed in the post will give you an error message everytime you open gnome-terminal.
One of the comments left by Ricardo Ferreira on that page solved my problem (after rebooting again):
Edit your rc.local file with sudo gedit /etc/rc.local and delete the following line:
echo "1" > /dev/cgroup/cpu/user/notify_on_release
Save and exit gedit. Then, run gedit ~/.bashrc and add the following inside your if statement:
echo "1" > /dev/cgroup/cpu/user/$$/notify_on_release
So it should look like this:
if [ "$PS1" ] ; then /dev/cgroup/cpu/user/$$ /dev/cgroup/cpu/user/$$/tasks /dev/cgroup/cpu/user/$$/notify_on_release
mkdir -m 0700
echo $$ >
echo "1" >
fi
Do what thou wilt shall be the whole of the Law
You can only put a try on the 22nd catch.
The kernel has a mechanism to schedule groups of processes, and it has for years. By grouping tasks together, you can make one process (video playing) get the same cpu share as a group of processes put together (compiling code). By doing this (instead of the video processing being equal to just one of the compiling processes), everything feels more interactive, even though it's actually slightly slower.
No one uses scheduling groups because they have to be setup by root and it's not the easiest thing in the world (you have to write stuff into sysfs, I think). No distributions set them up.
The magic kernel patch just adds a simple rule to the scheduler. When a process starts, it goes into a group with the rest of the processes in that TTY (virtual terminal). This means the user doesn't have to do anything and the groups are setup automatically.
Poettering thinks this is somewhat hackish, and that things shouldn't be based on what TTY a process is started on. He made the little script to prove that this can easily be done in userspace.
Linus has rejected this, basically saying that we've had years for people to make something like this and no one did until the kernel patch came along. The patch is simple, reasonable, and doesn't require distributors to ship updated userland files to put processes in groups.
I should note that my understanding comes from LWN, which has had excellent coverage of this on their kernel page, as always. You'll be able to see their articles in two weeks if you're not a member (which is worth it if you like this kind of stuff).
Comment forecast: Bits of genius surrounded by a sea of mediocrity.
So the 22nd catch is the exception?
With my new 100/100mbit broadband, and the fastest SSD, and this hack, I'm not sure I'm noticing anything ;O
I still use csh(1), you insensitive clod!
Imagine you have an app that launches just one process, like a music player, and an app that launches 3 (for example, Firefox, which launches a new one for each plugin).
Since each process has the same priority, the second app - firefox - will effectively have 3x more CPU time than the media player, and possibly stutter the music.
The kernel has something called cgroups, which enables more than one process to be grouped, and each group will have the same CPU time. So the group (Firefox+plugins) would have the same CPU time than the media player.
This kernel patch and terminal code enables each terminal you launch to have a different group, so if you launch Firefox from one terminal and the music player from another, they'll have different groups.
Dilbert RSS feed
/etc/bash.bashrc
Global bashrc file.
Non impediti ratione cogitationus.
Sure.
Hopefully you know what a TTY is, but in case you don't it it a virtual or real terminal. When you open up an xterm you create one. If you don't have x-windows installed, you reach one, etc.
Well Linus had an idea about using a grouping functionality that was already in the Kernel to allow all the processes (technically actually all the kernel threads) running from one TTY to be grouped together for scheduling.
The result of that is that if you are running 99 processes in one xterm that could consume all of your CPU, and you open another xterm, one one just one process that wants 100% CPU, each xterm's processes gets 50% of the CPU, rather than one getting 99% and the other getting only 1%.
But lets say you only had that first xterm. Since each of those processes are not getting nearly the processor amount they desire, normally the scheduler sees them as nearly starved, and the next process that only wants 5% of CPU does not get much preferential treatment for giving up most of it's time. However, with the grouping, the scheduler can see that those 99 processes are related, and they are not really starved, since as a group they are getting 100%. So now when this other app that wants only 5% comes along, the scheduler might give it pretty much all of that 5% rather than the mere 1% it would have been getting before, and so that app (probably a web browser or something) remains nice and responsive.
That is not 100% accurate, since I've simplified some things a little, especially with regard to the working of the scheduler, but it should give you the idea.
Eventually, more heuristics might be added, so that a GUI application that launches a bunch of threads and hogs the CPU might have all it's threads grouped, so they don't hurt responsiveness of interactive apps either.
Stylish sheet to fix many problems in Slashdot's D3: https://gist.github.com/801524
Thanks for the nice words about LWN! Here's a special link to the LWN article on per-tty group scheduling for Slashdot folks. Hopefully a few of you will like what you see and decide to subscribe.
Jonathan Corbet, LWN.net
I tried that it didn't catch on...
sigs... don't talk to me about sigs....
Gooooooood make -j64 whatever.
Roll over!
People come up with the oddest names for their pets sometimes.
You were mod'ed "funny", but seriously, I've been using tcsh (interactively) since the 80s and prefer it to bash. I also tend to write scripts in ksh as that's been more portable and available (native) than bash on non-Linux systems - though that's changing.
It must have been something you assimilated. . . .
>> Just curious what others think.
'Curious' my ass. You have a history of trolling anything and everything against Apple or pro google or pro open source. If you want to troll, there are more subtle ways.
The kernel patch is the hackish way to do it. They're hard-coding policy settings into a kernel patch. Dumb. The kernel is there to provide the knobs, not to twiddle them for you.
Lennart's argument is that policy should not be hard-coded into the kernel. He's not saying "everyone should do this in a bash script". He's saying "leave policy settings to userspace mechanisms that can handle them better." Say, systemd for instance.
Users would be better served by Lennart's approach, I think.
Funny thing is, most desktop users will not see the benefits of the patch, since most of them never use the terminal to run cpu-hogging kernel builds. All desktop apps share the same cgroup.
That won't stop hordes of n00bs from claiming ZOMG MAI SYSTEM IS SO MUCH FA$TER NOW OMG!
Thanks for the nice words about LWN! Here's a special link to the LWN article on per-tty group scheduling for Slashdot folks. Hopefully a few of you will like what you see and decide to subscribe.
You've got a per-tty mouth.
I'm a good cook. I'm a fantastic eater. - Steven Brust
Considering that Lennart wrote the steaming, broken pile known as PulseAudio, his solution makes perfect sense...
So then why is your UID so high?
I will get off your lawn now.
In theory you could alter the 'launch' process for running software & check a database for 'nice' priorities so that they automatically launch with a preset 'nice' rating.
Currently, the kernel is very egalitarian - everything runs at 'nice 0' unless the user wants something different. If YOU think that extinguish_fire should have more of a priority than watch_tv, then YOU should handle the issue.
However, that isn't the issue addressed by either the patch or the userspace scripts. While adjusting niceness may help in a gross sense, it's not going to handle proper timeslicing of software that's spawning a huge number of threads and lagging other applications.
As an example, we need to run extinguish_fire and evacuate_building at the same time. extinguish_fire spawns a thread for each bucket in the brigade, while evacuate_building only spawns a thread for each escape route. Now, if there are 96 buckets & 4 escape routes, extinguish fire will consume 96% of the CPU & choke out the evacuate_building threads.
You could try to guess the appropriate level of 'nice' for each program when you launch it, but it's not going to be pretty. To get even timing, you would be pushing evacuate_building to nice -19 - an act that would make it next to impossible to establish any control over the bucket brigades.
By grouping all of the threads from a program, extinguish_fire and evacuate_building get equal footing regardless of the number of threads they spawn. Both of them remain responsive to commands without taking the huge hits you get from drastic nice levels. If both processes aren't running smoothly, you can renice the group rather than take the nice hit 'threadcount' times.
I see the GP is well on his way to earning the elusive (Score:5, Troll) achievement which is one of the rarest drops on Slashdot (BTW, when did Slashdot turn into XBox Live with achievements? Now, get off my lawn...)
Just an FYI, your ad hominem attack does not detract from his legitimate point. I am well aware of the technical issues involved, but at some point you have to stop giving Linux & X Windows a pass just because Unix/X was crappily designed in this regard back in the 70's (tty's and client/server GUI apps). If stuttering media playback is a side effect of the present modular paradigm, then perhaps it is time to stop making excuses for it.
This is almost as bad as when people stepped up to defend the ext4 data loss / commit interval issue to say it was "by design". Yeah, it is trivial to avoid by calling fsync in your app all the time, but watch that destroy perf because it flushes the whole I/O queue for the system. Ordered data mode just makes sense... strange that wouldn't be default, or even an option to turn off if you think about it. So, what's a cautious app designer to do? Fsync all the time? Can a userspace app even query this flag? And no, using SQLite is not a legitimate workaround.
Same damn thing when Linux zealots call ZFS a "rampant layering violation" and then define away innovative capabilities like RAIDZ and cheap, versatile COW snapshots as "nonfeatures" because they can't be replicated while abiding within the Linux layering paradigm. Sour grapes, indeed. Btfs won't be able to replicate these features unless they break the layering paradigm, so who wants those features anyway?
Soooooo... watch me get flayed alive for my heresy: I say if the extant paradigm isn't optimal, then maybe it's time to consider changing it. At the very least, it's intellectually dishonest to claim that these aren't problems just because there are no simple fixes in the present model. As noted by others, this "fix" is essentially a heuristic with improved granularity for the given problem. Good, at least that's something. Now, how about the real, underlying problem?
I was busy - working.
It must have been something you assimilated. . . .
Every grot knows da red onez go fasta!
The road to tyranny has always been paved with claims of necessity.
An early comment on LWN captured the technical argument best, I think, which I guess illustrates both the quality of the articles and posters on LWN. The background to this is we are discussing CPU scheduling. If you don't know what CPU scheduling is, think of it as form of mind reading. I'll illustrate.
Lets say you have asked your computer to do several things, in fact so many that if it follows the usual method of simply dividing its time equally between them it is going to annoy you. The video you watching might start flickering, or the music you are listening will drop out. So obviously the computer must now give more CPU time to playing your movie and less to whatever background task you started, such as that MP3 transcode of your 20,000 song library. Except how is the computer is supposed to know this? This is how we get to mind reading.
The hack we are discussing is essentially the discovery of a way to read the minds of one particular type of computer user - the Linux Kernel developer. The Linux Kernel developer is in the habit of starting huge background jobs called kernel compiles. These kernel compiles take a looong while, so the kernel developers, being very clever people, have invented all sorts of ways of speeding them up. One of those ways is to divide the task into lots of little bits, and then fire off separate tasks to do each. This takes maximum advantage of available CPU cores, soaking up every skerrick of available CPU time. This naturally enough leaves none left over for other important tasks like watching a movie while waiting your kernel compile. In this particular case the default CPU scheduling strategy of giving each task an equal share of CPU is woefully poor, because there might be 20 kernel compile tasks and just one movie watching task, so the movie player ends up with 1/20 of the available CPU time. This isn't enough to play a movie.
The mind reading trick discovered boils down to this: Linux Kernel developers use the linux command line interface to fire off the kernel compile. And it turns out that for years now the kernel has been able group the tasks started from a command line and give that group a single portion of CPU time, as opposed to a equal portion to each task in the group. Thus you only have to split up the CPU time into 2, one portion going to the kernel compiler group and the other going to the movie player. Naturally enough the movie player works real well with a 50% allocation of CPU, and so we have a happy kernel developer.
Now we come to the merits of the two hacks. They both do the job I just described equally well. The difference between them is that one, the kernel patch, is automagic, meaning it happens automatically without anybody having to lift a finger. But it comes at the expense of bloating linux kernel a tiny bit, even for users who won't benefit from it. The other way currently has to be done applied manually using a process the vast majority of Linux users will at best find difficult, tedious and error prone.
Seems like a simple decision eh - lets take the tiny bloat hit and not inflict our long suffering desktop users with yet another Linux user-unfriendly idiosyncrasy. But here is the rub: it doesn't help them. In fact, for some it might have a negative impact (a gstreamer pipeline started from the command line strings to mind). The people who will benefit from this are the ones that use the command line heavily and regularly. People like Linus. Which is why he liked it so much I guess. But these are precisely the people who will have no absolutely no trouble doing it the manual way.
Because you can hack together a close approximation of a feature that's in an unreleased upcoming kernel? A feature that every other OS has had for 3+ years now?
Yah, I'm bowled over.
Comment of the year
Seems a Major Major bug to me.
Hack your mind out of its sandbox.
the steaming, broken pile known as PulseAudio
That was the case a couple of years ago, but have you tried it recently? I haven't actually had a single audio problem since switching from debian/alsa to ubuntu 10.04/PA, and I now have a ton of useful features on top :-) (per-app volume, per-app output devices, network streaming, seamless switching between headphones and HDMI, etc)
I mod down anyone who says "I will be modded down for this", regardless of the rest of their comment
I am TheRaven on Soylent News
This was fixed in 2004, with Ubuntu's Code of Conduct. Telling people RTFM is forbidden, either you help or shut up. People sometimes wonder whats the big deal with Ubuntu, and I'm positive this is one of the main reasons. You can check the forum http://ubuntuforums.org/ or hop to Freenode's #ubuntu channel to see this policy in action. No matter how repeated or simple a question is, it is allowed and if you reply, it is to help, even if thats pointing someone to a well written help page (like the many at help.ubuntu.com).
That policy is written in detail here: http://www.ubuntu.com/community/conduct
Artix
Your Linux, your init.