context switch vs. signal delivery [was: Re: Accelerating user mode linux]
A message that is not in the archive (sent off-list or not yet received): 1028294887.18635.71.camel@irongate.swansea.linux.org.uk.suse.lists.linux.kernel
A message that is not in the archive (sent off-list or not yet received): Pine.LNX.4.44.0208031332120.7531-100000@localhost.localdomain.suse.lists.linux.kernel
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]'s
Quoted 13 lines · [email protected] (Linus Torvalds) writes:
Quoted 9 lines
This is because the signal save/restore does a lot of unnecessary stuff. One optimization I implemented at one time was adding a
SA_NOFPsignal bit that told the kernel that the signal handler did not intend to modify floating point state (few signal handlers need FP) It would not save the FPU state then and reached quite some speedup in signal latency.Linux got a lot slower in signal delivery when the SSE2 support was added. That got this speed back.
This will break _horribly_ when (if) glibc starts using SSE2 for things like
memcpy()etc.
Quoted 6 lines
I agree that it is really sad that we have to save/restore FP on signals, but I think it's unavoidable. Your hack may work for you, but it just gets really dangerous in general. having signals randomly subtly corrupt some SSE2 state just because the signal handler uses something like memcpy (without even realizing that that could lead to trouble) is bad, bad, bad.
I think the possibility at least for memcpy is rather remote. Any sane SSE memcpy would only kick in for really big arguments (for small memcpys it doesn't make any sense at all because of the context save/possible reformatting penalty overhead). So only people doing really
big memcpys could be possibly hurt, and that is rather unlikely.
But your point stands, one definitely needs to be very careful with it.
Also for special things like UML who can ensure their environment is sane it could be still an useful optimization. I did it originally for async IO handling in some project. At least offering the choice does not hurt. If it wcould speed up UML I think it would be certainly worth it.
After all Linux should give you enough rope to shot yourself in the foot ;)
Quoted 7 lines
In other words, "not intending to" does not imply "will not". It's just potentially too easy to change SSE2 state by mistake.
And yes, this signal handler thing is clearly visible on benchmarks. MUCH too clearly visible. I just didn't see any safe alternatives (and I still don't ;( )
In theory you could do a superhack: put the FP context into an unmapped page on the stack and only save with lazy FPU or access to the unmapped page. Unfortunately the details get too nasty
(where to find the unmapped page? is the tlb manipulation worth it if the page was mapped? how to store the address of the unmapped page for nested signal handlers for the page fault handler?) so I discarded this idea.
-Andi
Quoted 2 lines · [email protected] said:
Also for special things like UML who can ensure their environment is sane it could be still an useful optimization.
I use libc, and I haven't been able to convince myself that it isn't going to use FP instructions or registers on my behalf. I use it as little as possible, but it still makes me nervous.
Quoted 2 lines
If it wcould speed up UML I think it would be certainly worth it.
After Ingo's numbers, I like the idea of just having a separate address space and process for the UML kernel, and have that process ptrace UML processes and handle system calls and interrupts on their behalf. One context switch at the start of a system call and one at the end, as opposed to a signal delivery and sigreturn.
This also solves the jail mode mprotect performance horrors.
The one thing standing in my way is the need for the kernel process to be able to change the address space of its processes.
I made a proposal for that, and Alan didn't like it. So, we'll see what he likes better.
Jeff
Quoted 6 lines · Andi Kleen
I think the possibility at least for memcpy is rather remote. Any sane SSE memcpy would only kick in for really big arguments (for small memcpys it doesn't make any sense at all because of the context save/possible reformatting penalty overhead). So only people doing really
big memcpys could be possibly hurt, and that is rather unlikely.
And this is why the kernel _has_ to save the FP state.
It's the "only happens in a blue moon" bugs that are the absolute _worst_ bugs. I want to optimize the kernel until I'm blue in the face, but the kernel must NEVER EVER have a "non-stable" interface.
Signal handlers that don't restore state are hard as _hell_ to debug. Most of the time it doesn't really matter (unless the lack of restore is something really major like one of the most common integer registers), but then depending on what libraries you use, and just _exactly_ when the signal comes in, you get subtle data corruption that may not show up until much later.
At which point your programmer wonders if he mistakenly wandered into MS-Windows land.
No thank you. I'll take slow signal handlers over ones that _sometimes_ don't work.
Quoted 1 line
After all Linux should give you enough rope to shot yourself in the foot ;)
On purpose, yes. It's ok to take careful aim, and say "I'm now shooting myself in the foot".
And yes, it's also ok to say "I don't know what I'm doing, so I may be shooting myself in the foot" (this is obviously the most common foot-shooter).
And if you come to me and complain about how drunk you were, and how you shot yourself in the foot by mistake due to that, I'll just ignore you.
BUT - and this is a big BUT - if you are doing everything right, and you actually know what you're doing, and you end up shooting yourself in the foot because the kernel was taking a shortcut, then I think the kernel is _wrong_.
And I'd rather have a slow kernel that does things right, than a fast kernel which screws with people.
Quoted 3 lines
In theory you could do a superhack: put the FP context into an unmapped page on the stack and only save with lazy FPU or access to the unmapped page.
That would be extremely interesting especially with signal handlers that do a longjmp() thing.
The real fix for a lot of programs on x86 would be for them to never ever use FP in the first place, in which case the kernel would be able to just not save and restore it at all.
However, glibc fiddles with the fpu at startup, even for non-FP programs. Dunno what to do about that.
Linus
Re: gcc 2.95 vs 3.21 performance
In reply to a message not in the archiveQuoted 3 lines · [email protected] (Linus Torvalds) writes:
I'd love to see a small - and fast - C compiler, and I'd be willing to make kernel changes to make it work with it.
If you want small and fast use lcc.
Unfortunately it's not completely free (some weird license), doesn't really support real inline assembly and generates rather bad code compared to gcc.
I'm still looking forward to Open Watcom (http://www.openwatcom.org) - they are near self hosting on Linux. The inline assembly is very VC++ style though; very different from gcc and worse you have to write it in Intel syntax.
Another alternative would be TenDRA, but it also has no inline assembly and it's C understanding can be only described as "fascist".
If you don't care about free software you could also use the Intel compiler, which seems to be often faster in compile time than gcc now and can already compile kernels.
-Andi
Re: gcc 2.95 vs 3.21 performance
Quoted 2 lines · Andi Kleen
If you want small and fast use lcc.
lcc isn't really something I want to use, since the license is so strange, and thus can't be improved upon if there are issues with it.
Some people have used the Intel compiler - which obviously also cannot be improved upon, but which is likely to start off pretty good. I don't really want to use it myself - what I'd really like to see is gcc splitting up just the C compiler as a separate project with more attention to size and speed.
Linus
Re: gcc 2.95 vs 3.21 performance
From: Linus Torvalds <[email protected]>
Date: Tue, 4 Feb 2003 14:14:06 -0800 (PST)
Hi Linus,
Quoted 2 lines
lcc isn't really something I want to use, since the license is so strange, and thus can't be improved upon if there are issues with it.
what is the difference between compiler and source management system regarding licenses and improvements?
Signature · 4 lines
Pavel Janík
I think I started with hitting C-h a lot. Really a LOT.
-- Kai Grossjohann in gnu.emacs.help about Emacs knowledgeRe: gcc 2.95 vs 3.21 performance
Quoted 8 lines · On Wed, 5 Feb 2003, Pavel [iso-8859-2] Janík wrote:
Hi Linus,
Quoted 2 lines
lcc isn't really something I want to use, since the license is so strange, and thus can't be improved upon if there are issues with it.
what is the difference between compiler and source management system regarding licenses and improvements?
You snipped the part where I said that the intel compiler is likely to be more interesting to a number of people, since it's at a higher level. So no, I'm not religious about licenses.
But the real issue is "does it do what we want it to do?" and "do we have a choice?". There are no open-source SCM's that work for me. But there _is_ an open-source compiler that does work for me. At which point the license matters - simply because there is choice in the matter.
Gcc mostly works. But it's slower then I'd like. And it prioritizes things I don't care about. And competition is always good. So I would definitely love to see some alternatives.
And if you have issues with BK, maybe you can try to encourage the SCM people to see why I consider BK to not even have alternatives right now.
Linus
Re: gcc 2.95 vs 3.21 performance
In reply to Pavel JaníkQuoted 6 lines · [email protected] (Pavel =?iso-8859-2?q?Jan=EDk?=) said:
Quoted 2 lines · Linus Torvalds
lcc isn't really something I want to use, since the license is so strange, and thus can't be improved upon if there are issues with it.
what is the difference between compiler and source management system regarding licenses and improvements?
That bk was designed around Linus' and other head kernel hackers ideas of how it should work, and they are still bending over backwards to keep this biggest _*non*_customer of theirs happy.
OTOH, lcc as a project seems to be dead for all practical purposes (it looks like 4.2 will be the endo of the line, no patches or updates have shown up for quite some time). Its licence
<http://www.cs.princeton.edu/software/lcc/pkg/CPYRIGHT> is vaguely BSDish, but with a "you can't make money off this or any modified versions/software based on it" clause.
I've been inside lcc 4.1 (current version is 4.2, somewhat different, so YMMV...) myself a bit, and while it is a marvelous showpiece for classroom use, it is sorely lacking in what makes a _real_ C compiler (for kernel use). For one, it only knows about i486-ish ia32 CPUs, to get others supported in its current incarnation would be a massive excercise in duplication or macro-massaging the backend source; other than the (very good) optimal instruction selection there is very little optimization (what there is is a bit of strength reduction), the organization of the compiler makes adding aditional higher-level optimization almost impossible, a separate SSA or such intermediate form would have to retrofitted; the register selection is very simplistic and doesn't work correctly (some experimental patches we had for generating PIC code on ia32 kept it crashing by running out of registers the code for fixing this case up just doesn't work). No hint at scheduling instructions or such.
Signature · 4 lines
Dr. Horst H. von Brand User #22616 counter.li.org Departamento de Informatica Fono: +56 32 654431 Universidad Tecnica Federico Santa Maria +56 32 654239 Casilla 110-V, Valparaiso, Chile Fax: +56 32 797513
Re: gcc 2.95 vs 3.21 performance
In reply to Andi KleenQuoted 8 lines · Andi Kleen
If you want small and fast use lcc.
Unfortunately it's not completely free (some weird license), doesn't really support real inline assembly and generates rather bad code compared to gcc.
I'm still looking forward to Open Watcom (http://www.openwatcom.org) - they are near self hosting on Linux. The inline assembly is very VC++ style though; very different from gcc and worse you have to write it in Intel syntax.
Quoted 7 lines
Another alternative would be TenDRA, but it also has no inline assembly and it's C understanding can be only described as "fascist".
If you don't care about free software you could also use the Intel compiler, which seems to be often faster in compile time than gcc now and can already compile kernels.
There is also tcc (http://fabrice.bellard.free.fr/tcc/)
It claims to support gcc-like inline assembler, appears to be much smaller and faster than gcc. Plus it is GPL so the liscense isn't a problem either. Though, I am not really sure of the quality of code generated or of how mature it is.
-Jeff
Re: gcc 2.95 vs 3.21 performance
Quoted 17 lines · Jeff Muizelaar
Quoted 8 lines · Andi Kleen
If you want small and fast use lcc.
Unfortunately it's not completely free (some weird license), doesn't really support real inline assembly and generates rather bad code compared to gcc.
I'm still looking forward to Open Watcom (http://www.openwatcom.org) - they are near self hosting on Linux. The inline assembly is very VC++ style though; very different from gcc and worse you have to write it in Intel syntax.
Quoted 7 lines
Another alternative would be TenDRA, but it also has no inline assembly and it's C understanding can be only described as "fascist".
If you don't care about free software you could also use the Intel compiler, which seems to be often faster in compile time than gcc now and can already compile kernels.
Quoted 5 lines
There is also tcc (http://fabrice.bellard.free.fr/tcc/)
It claims to support gcc-like inline assembler, appears to be much smaller and faster than gcc. Plus it is GPL so the liscense isn't a problem either. Though, I am not really sure of the quality of code generated or of how mature it is.-Jeff
wow, looks like some teenage kid like me made it...
its a 170 kb gzipped tar!
nice for a C compiler...But i'm not sure if it could compile half of the linux kernel successfully...
Re: gcc 2.95 vs 3.21 performance
In reply to Jeff Muizelaar[Massive Cc: snippage]
Jeff Muizelaar <[email protected]> said:
[...]
Quoted 5 lines
There is also tcc (http://fabrice.bellard.free.fr/tcc/)
It claims to support gcc-like inline assembler, appears to be much smaller and faster than gcc. Plus it is GPL so the liscense isn't a problem either.
Though, I am not really sure of the quality of code generated
Horrible.
Quoted 2 lines
or of how
mature it is.
Nice for one-file throwaway C proggies. But then again, Perl is so much better at what you'd want to do most of the time...
Look, people, the gcc folks have recently redone the guts of the compiler to make more advanced optimizations possible/easier (look at the news for 2000-2002 at <http://gcc.gnu.org>). It still needs a lot of porting over of optimizations and developing new ones, plus tuning, AFAIU.
The other open(ish) C compilers I know about are mere toys.
Signature · 4 lines
Dr. Horst H. von Brand User #22616 counter.li.org Departamento de Informatica Fono: +56 32 654431 Universidad Tecnica Federico Santa Maria +56 32 654239 Casilla 110-V, Valparaiso, Chile Fax: +56 32 797513
Re: gcc 2.95 vs 3.21 performance
In reply to Jeff MuizelaarIn article <[email protected]>,
Quoted 7 lines · Jeff Muizelaar
There is also tcc (http://fabrice.bellard.free.fr/tcc/)
It claims to support gcc-like inline assembler, appears to be much smaller and faster than gcc. Plus it is GPL so the liscense isn't a problem either.
Though, I am not really sure of the quality of code generated or of how mature it is.
tcc is interesting. The code generation is pretty simplistic (read:
trivially horrible for most things), but it sure is fast and small. And judging by the changelog, Fabrice is trying to compile the kernel with it.
For a lot of problems, small-and-fast is good. Hell, some of the things I'd personally find interesting don't have any code generation part at all (static analysis of annotated source-code - stanford checker on the cheap). And development doesn't always need good code generation (right now some people use "gcc -O0" for that, because anything else hurts too much. Now, the code from tcc will probably look more like "-O-1", but at least you can test out things _quickly_).
Linus
Re: gcc 2.95 vs 3.21 performance
Quoted 7 lines
Quoted 6 lines
There is also tcc (http://fabrice.bellard.free.fr/tcc/)
It claims to support gcc-like inline assembler, appears to be much smaller and faster than gcc. Plus it is GPL so the liscense isn't a problem either.
Though, I am not really sure of the quality of code generated or of how mature it is.
Quoted 6 lines
tcc is interesting. The code generation is pretty simplistic (read:
trivially horrible for most things), but it sure is fast and small. And judging by the changelog, Fabrice is trying to compile the kernel with it.For a lot of problems, small-and-fast is good.
Maybe otcc is a better choice, then?
http://fabrice.bellard.free.fr/otcc/
:-)
John.
Re: gcc 2.95 vs 3.21 performance
In reply to Linus TorvaldsOn Wed, 5 Feb 2003, Linus Torvalds wrote:
| In article <[email protected]>, | Jeff Muizelaar <[email protected]> wrote: | > | >There is also tcc (http://fabrice.bellard.free.fr/tcc/) | >It claims to support gcc-like inline assembler, appears to be much | >smaller and faster than gcc. Plus it is GPL so the liscense isn't a | >problem either. | >Though, I am not really sure of the quality of code generated or of how | >mature it is. | | tcc is interesting. The code generation is pretty simplistic (read: | trivially horrible for most things), but it sure is fast and small. And | judging by the changelog, Fabrice is trying to compile the kernel with | it. | | For a lot of problems, small-and-fast is good. Hell, some of the things | I'd personally find interesting don't have any code generation part at | all (static analysis of annotated source-code - stanford checker on the | cheap).
Yep, that's exactly why I'm interested...
| And development doesn't always need good code generation (right | now some people use "gcc -O0" for that, because anything else hurts too | much. Now, the code from tcc will probably look more like "-O-1", but | at least you can test out things _quickly_).
Signature · 1 line
~Randy
Re: gcc 2.95 vs 3.21 performance
In reply to Jeff MuizelaarJeff Muizelaar wrote:-
Quoted 4 lines
There is also tcc (http://fabrice.bellard.free.fr/tcc/)
It claims to support gcc-like inline assembler, appears to be much smaller and faster than gcc. Plus it is GPL so the liscense isn't a problem either.
It doesn't expand macros correctly, however, and accepts an enormous range of invalid code without a single diagnostic. I'm pretty sure it's arithmetic rules are incorrect, too. It's certainly nowhere near C89 compliance.
Neil.
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: gcc 2.95 vs 3.21 performance
Quoted 13 lines · Neil Booth
[email protected] wrote:-
Quoted 1 line
Maybe thats why its a 0.9* version, and the auther has stated on his site that not all C98 features are implimented...but then even GCC doesn't impliment them...
No, I said C89. He's got a *long* way to go for that. Forget C99.
However, he does claim C89 compliance, which is quite disingenuous.
Quoted 2 lines
I checked tcc out, and its damn fast, much much much much faster than gcc.
gcc is bloated and its slow even on my pentium 4 machine, let alone my 1.2 celeron. It takes 20 minutes to compile a new kernel on that, now if you're gonna test kernels/patches, you can wait 20 minutes for every compile!I agree. I'm trying to fix it.
Quoted 8 lines
GCC is larger for a reason: it does things properly. It's easy to be fast if you're willing to be wrong, and not emit warnings or errors, and not implement half the standard. And not optimize.
Quoted 2 lines
Even icc is much better than gcc, but its very perticular about code (and its not gcc compatible as the intel site says) And its non-free also...
Only better in terms of compile speed.
Cool (you're trying to fix it), maybe you can modify tcc so it is optimized for compiling linux (optimized for compiling speed and runtime speed for linux). I think it'll be easier and quicker to just make it compile linux properly first, then do the testing/fixing for other things, as they are so many compilers for other things anyway...And maybe it can be called "Linux C Compiler"? lol.
Re: gcc 2.95 vs 3.21 performance
Quoted 8 lines · [email protected] said:
Quoted 6 lines · Neil Booth
[email protected] wrote:-
Quoted 3 lines
Maybe thats why its a 0.9* version, and the auther has stated on his site that not all C98 features are implimented...but then even GCC doesn't impliment them...
No, I said C89. He's got a *long* way to go for that. Forget C99.
Quoted 7 lines
Quoted 7 lines
However, he does claim C89 compliance, which is quite disingenuous.
Quoted 5 lines
I checked tcc out, and its damn fast, much much much much faster than gcc. gcc is bloated and its slow even on my pentium 4 machine, let alone my 1.2 celeron. It takes 20 minutes to compile a new kernel on that, now if you're gonna test kernels/patches, you can wait 20 minutes for every compile!
Come on, quit whining already. When I started out fooling around with egcs and the kernel, it took 45 to 60 minutes to build a kernel for me. And the kernel was a lot smaller, and the compiler much faster.
Quoted 9 lines
Quoted 9 lines
I agree. I'm trying to fix it.
GCC is larger for a reason: it does things properly. It's easy to be fast if you're willing to be wrong, and not emit warnings or errors, and not implement half the standard. And not optimize.
Quoted 3 lines
Even icc is much better than gcc, but its very perticular about code (and its not gcc compatible as the intel site says)
And its non-free also...
Pour manpower and people who _know_ that _one_ CPU you are targeting in and out into the project, it sure will get further along...
Quoted 5 lines
Quoted 1 line
Only better in terms of compile speed.
Cool (you're trying to fix it), maybe you can modify tcc so it is optimized for compiling linux (optimized for compiling speed and runtime speed for linux).
Sorry, can pick just one. Either you compile very fast (because you don't analyze the code you are compiling very much, i.e., generate lousy code) or generate excelent code (that requires complex analysis, large data structures to build and use, and takes time).
Quoted 4 lines
I think it'll be easier and quicker to just make it compile linux
properly first, then do the testing/fixing for other things, as they are so many compilers for other things anyway...And maybe it can be called "Linux C Compiler"? lol.
"Easier and quicker" as in 5 or 6 years of hard work. Sure enough, come back when you're done.
Signature · 4 lines
Dr. Horst H. von Brand User #22616 counter.li.org Departamento de Informatica Fono: +56 32 654431 Universidad Tecnica Federico Santa Maria +56 32 654239 Casilla 110-V, Valparaiso, Chile Fax: +56 32 797513
Re: gcc 2.95 vs 3.21 performance
In reply to [email protected][email protected] wrote:-
Quoted 6 lines
Cool (you're trying to fix it), maybe you can modify tcc so it is optimized for compiling linux (optimized for compiling speed and runtime speed for linux). I think it'll be easier and quicker to just make it compile linux properly first, then do the testing/fixing for other things, as they are so many compilers for other things anyway...And maybe it can be called "Linux C Compiler"? lol.
Sorry, I only care about GCC.
Neil.
Re: gcc 2.95 vs 3.21 performance
In reply to Neil BoothQuoted 11 lines · Neil Booth
Jeff Muizelaar wrote:-
Quoted 2 lines
There is also tcc (http://fabrice.bellard.free.fr/tcc/)
It claims to support gcc-like inline assembler, appears to be much smaller and faster than gcc. Plus it is GPL so the liscense isn't a problem either.It doesn't expand macros correctly, however, and accepts an enormous range of invalid code without a single diagnostic. I'm pretty sure it's arithmetic rules are incorrect, too. It's certainly nowhere near C89 compliance.
100% agreed.
However, for our purposes, TinyCC is only missing two pieces needed for successfully building a bootable kernel:
* __builtin_constant_p
* function inlining
Given the existing TinyCC source base, function inlining is a big step (since tcc doesn't do AST-like things currently), so don't expect that very soon. TinyCC is a fun little project to watch and play around with, though, and can compile most major open source projects, as well as itself.
Jeff
Re: gcc 2.95 vs 3.21 performance
Quoted 7 lines
Given the existing TinyCC source base, function inlining is a big step (since tcc doesn't do AST-like things currently), so don't expect that very soon. TinyCC is a fun little project to watch and play around with, though, and can compile most major open source projects, as well as itself.
I wonder how that can be, though, because I've failed getting it to compile code as trivial as
walk_de = (dirent_t *) debug_malloc(sizeof(dirent_t));
where dirent_t is a simple structure and debug_malloc is prototyped to void *debug_malloc(size_t size);
Signature · 1 line
Tomas Szepe <[email protected]>
Re: [PATCH] New dcache / inode hash tuning patch
In reply to a message not in the archive[email protected] (Linus Torvalds) writes:
[please read the whole post before answering. thanks]
Quoted 4 lines
But quite frankly, if the hash list heads are actually noticeable memory users, the hash is likely to be _way_ too big. The list heads are _much_ smaller than the entries they point to, the hash list just shouldn't be big enough that it really matters.
On big machines it is currently 1+MB. IMHO this is way too big.
Quoted 8 lines
- hash list cache footprint
Again: the hash head array itself is at least dense in the cache, and each entry is _much_ smaller than the actual data structures it points to. So even if you improve the hash heads to be better from a cache standpoint, you're only getting a very small percentage of the real cache costs.
It would be possible to cache line optimize the layout of struct dentry in addition. May be an interesting add-on project for someone... But for lookup walking even one cache line - the one containing d_hash - should be needed. Unless d_hash is unlucky enough to cross a cache line for its two members ... but I doubt that.
Quoted 7 lines
So let's say that the cache costs of the dcache is 4% (according to the oprofile run), then saving a few procent of that is not actually going to be noticeable at a user level.
And the downsides of the hash list is that addition/removal is costlier due to the conditionals, and a non-conditional version (a common
But the list walking is faster. Testing for NULL generates much better code on i386 than having to dedicate a register for storing the head to test against. List walking happens more often than insertion/deletion.
I believe the conditionals are completely left in the noise compared to the cache misses the two pointer head version causes. You can execute a lot of conditionals in the time needed to serve one cache miss!
Please take a look at the x86 assembly generated by list_for_each vs hlist_for_each. hlist_for_each looks much nicer, especially when you can use the register normally wasted on the head for something else in the loop body.
In case of dcache rcu it also made things simpler/faster because it didn't require the complicated is_bucket race breaker check.
Quoted 4 lines
In other words: it may be that our current dentry hashes are too big, and that is certainly worth fixing if so. But the "hlist" approach very fundamentally cannot really help the _real_ problem very much, and it will (slightly) hurt the case where the hashes are actually cached.
I admit it is a kind of micro optimization, but I believe it is an useful one. Frankly wasting two pointers for a hash bucket in a potentially big hash table is just s*d.
Quoted 4 lines
So I really think that the only longterm fix is to make the lookup data structures be "local" to the base of the lookup, in order to get away from the inherently non-local nature of the current hash lookups.
Yes, that may be a good idea. I don't have time to work on this though.
Still even with local lookups single pointer buckets will likely help ;)
Also isn't it a bit late in the 2.5 cycle to think about such radical changes like local lookup? It sounds more like a nice 2.7 project. I believe my patch with a bit more tweaking (my current 64K hash table seems to be too small) is suitable even for an soon to be stable kernel.
Also my patch had some other changes that I believe should be included anyways because they're independent and improvement. It replaces the max_dentries race break hack with a better algorithm to detect cycles on walk.
Also it does more prefetches while list walking which I believe to be useful.
-Andi
Re: [Lse-tech] Re: [PATCH] New dcache / inode hash tuning patch
Quoted 3 lines
It would be possible to cache line optimize the layout of struct dentry in addition. May be an interesting add-on project for someone...
I played with this a few months ago, and sent a preliminary patch to linux-kernel, but in my (fairly brief) testing on a 4-way system I wasn't able to produce a measurable benefit from it. I think part of this may have been due to contention on dcache_lock, so maybe dcache_rcu will help there.
The main changes were to bring together all the data needed for checking a non-matching dcache hash entry (modulo hash collisions) into one cacheline, and to separate out the mostly-read-only fields from the change-frequently fields.
The original patch is at
http://marc.theaimsgroup.com/?l=linux-kernel&m=102650654002932&w=2
Quoted 3 lines
But for lookup walking even one cache line - the one containing
d_hash- should be needed. Unlessd_hashis unlucky enough to cross a cache line for its two members ... but I doubt that.
No, but on a 32-byte cache line system, d_parent, d_hash and d_name are all on different cache lines, and they're used when checking each entry. On 64-byte systems, d_parent and d_hash will be on the same line, but d_name is still on a separate line and d_name.hash gets checked before d_parent. So bringing these three fields on to the same cacheline would theoretically be a win.
Paul
Re: [Lse-tech] Re: [PATCH] New dcache / inode hash tuning patch
Quoted 6 lines · Paul Menage
Quoted 3 lines
But for lookup walking even one cache line - the one containing
d_hash- should be needed. Unlessd_hashis unlucky enough to cross a cache line for its two members ... but I doubt that.No, but on a 32-byte cache line system,
d_parent,d_hashandd_nameare all on different cache lines, and they're used when checking each entry.
... and dcache RCU checks d_bucket and d_move_count too in the hash walking loop.
Quoted 4 lines
On 64-byte systems,
d_parentandd_hashwill be on the same line, butd_nameis still on a separate line and d_name.hash gets checked befored_parent. So bringing these three fields on to the same cacheline would theoretically be a win.
Ok you're right. Optimizing the layout a bit would be probably a good idea. I won't include it in the hash patchkit for now to not do too many things with the same patch.
-Andi
Re: [PATCH] New dcache / inode hash tuning patch
In reply to Andi KleenQuoted 3 lines · Andi Kleen
Also isn't it a bit late in the 2.5 cycle to think about such radical changes like local lookup?
Look again at my suggestion: forcing locality by just changing the hash function to _intentionally_ bunch directory hashes together.
Yes, it's "pseudo-locality" (and my example hash was truly broken: we must _not_ use the directory dentry hash value as part of the hash, as the hash needs to be stable over the whole lifetime of the directory dentry), but the point is that by just changing the hashing algorithm you can potentially get a good portion of the locality we want.
Right now the dcache hash is often something like 17 bits - and we could easily make it so that roughly "half" the bits would be based purely on the directory. That would still give each directory ~8 bits worth of "local hashing", which is fairly reasonable.
Quoted 1 line
It sounds more like a nice 2.7 project.
It sounds more like changing two lines of code to me.
Quoted 3 lines
I believe my patch with a bit more tweaking (my current 64K hash table seems to be too small) is suitable even for an soon to be stable kernel.
Quite frankly, right now the only report I've seen about your patch is that it made things slightly _slower_.
For a patch that is supposed to speed stuff up, that's a damn bad track record. Sorry.
I'd suggest you drop the hash size changes, and try with _just_ the hlist stuff, and once that is verified to perform well, _then_ worry about hashing changes. Because quite frankly, I suspect my "local hash" thing performs better than "make the hashes smaller". And the hash algorithm and size is _totally_ independent from whether it uses the regular lists or the hlists..
Linus
Re: [PATCH] New dcache / inode hash tuning patch
Quoted 4 lines · Linus Torvalds
Right now the dcache hash is often something like 17 bits - and we could easily make it so that roughly "half" the bits would be based purely on the directory. That would still give each directory ~8 bits worth of "local hashing", which is fairly reasonable.
Ok I will see if that helps.
Quoted 7 lines
Quoted 4 lines
I believe my patch with a bit more tweaking (my current 64K hash table seems to be too small) is suitable even for an soon to be stable kernel.
Quite frankly, right now the only report I've seen about your patch is that it made things slightly _slower_.
Actually that's not quite true. The report had a completely different profile (lots of other functions had different percentages), so it likely wasn't a comparable workload. I also don't think the NUMAQs are a good test platform for this because they have 2MB of fast cache per CPU, while the typical linux multiprocessor machine has much less. Yes you can fit an 1MB hash table into a 2 MB cache....
I'll generate some new numbers here locally over the weekend on P4, but I only have a dual to test on and see how it performs.
-Andi
Re: [PATCH] New dcache / inode hash tuning patch
Quoted 6 lines · Andi Kleen
Quoted 2 lines
Quite frankly, right now the only report I've seen about your patch is that it made things slightly _slower_.
Actually that's not quite true. The report had a completely different profile (lots of other functions had different percentages), so it likely wasn't a comparable workload. I also don't think the NUMAQs are a good test
Did you read any of the comments on your patch?
Look at slabinfo to see how much space those objects are actually using.
Dentry cache hash table entries: 131072 (order: 8, 1048576 bytes) Inode cache hash table entries: 65536 (order: 7, 524288 bytes)
inode_cache 33977 82901 512 11810 11843 1 : 124 62 dentry_cache 14490 14490 128 483 483 1 : 252 126
So the actual amount of memory used by f.i. the inodes is about 47MB (11810 * 4KB).
The dentry cache hashtable currently has a fill-rate of about 10%, but I've been running a lot of compilations and updatedb hasn't been around for a long time. The VM has already aggressivly pruned it, which is to be expected. The inode cache hash table is at about 50%. So if the hash function is good we can assume that most hash chains are either empty or only contain a single object and any hit will give us either a pointer to the likely candidate or we know that the object isn't cached.
Now from what I read of your patch, it limits the size of the inode hash table to 64KB, but with single pointers it can still address about 16000 hash chains. Assuming the same perfect hash distribution, all chains will contain about 2 objects. So although the hash table fits much better, each time we are looking for an uncached object we need to walk 2 objects that are spread across that 47MB chunk of allocated inodes, instead of basically having a 'yes or no' answer pretty much from looking at the hash table.
Quoted 3 lines
platform for this because they have 2MB of fast cache per CPU, while the typical linux multiprocessor machine has much less. Yes you can fit an 1MB hash table into a 2 MB cache....
Yes but you will never fit 47MB worth of inodes into that same 2MB cache. And I realize that you can align things so that you don't have to pull in more than a few cache-lines per object, but traversing ~0.5 pointers versus 2 pointers for every L1/L2 cache miss will be felt because each traversal will hit another L2 cache-line.
It is probably far more useful to restrain the sizes of the inode and dentry caches. Why would my system need 2-3 times the number of inodes compared to dentries, while you pretty much always need to go through the dentry cache to find the inodes. So a lot of those inodes are likely unreferenced and we already have to hit the disk for the dentry lookup anyways.
Jan
Re: [PATCH] New dcache / inode hash tuning patch
Quoted 5 lines
The dentry cache hashtable currently has a fill-rate of about 10%, but I've been running a lot of compilations and updatedb hasn't been around for a long time. The VM has already aggressivly pruned it, which is to be expected. The inode cache hash table is at about 50%. So if the hash function is good we can assume that most hash chains are either empty or
The hash function isn't good and the hash chains are far from evenly distributed. There are typically 30-40% empty buckets, while some others are rather long.
In addition I think only a small fraction of the dentries are actually hot. (no numbers on that, sorry)
Your 47MB number doesn't make much sense for lookup tuning because these are not walked all the time. Only a few cache lines of the dentry are accessed for a lookup.
If you think you have a better concept than me to tune it then feel free to post a patch.
Quoted 8 lines
Now from what I read of your patch, it limits the size of the inode hash table to 64KB, but with single pointers it can still address about 16000 hash chains. Assuming the same perfect hash distribution, all chains will contain about 2 objects. So although the hash table fits much better, each time we are looking for an uncached object we need to walk 2 objects that are spread across that 47MB chunk of allocated inodes, instead of basically having a 'yes or no' answer pretty much from looking at the hash table.
Apply the latest hash patch and run some statistics over the bucket distributions from /proc/dcache. You'll quickly see that your ideal model does not match reality at all.
Quoted 2 lines
Yes but you will never fit 47MB worth of inodes into that same 2MB cache. And I realize that you can align things so that you don't have
The overall cached inodes are not too interesting - it is a long known problem of linux that it caches too many inodes. But it doesn't matter here because the dcache does all the hard lookup work and it hash direct pointers to the inodes (no inode hash lookup needed). Most of them are just wasted memory, but luckily not wasted cache.
What's interesting is to make the dentry lookup as fast as possible.
Quoted 3 lines
to pull in more than a few cache-lines per object, but traversing ~0.5 pointers versus 2 pointers for every L1/L2 cache miss will be felt because each traversal will hit another L2 cache-line.
Sorry but the average 2 bucket length number is completely unrealistic. It doesn't make any sense to use it in an argument.
Quoted 3 lines
It is probably far more useful to restrain the sizes of the inode and dentry caches. Why would my system need 2-3 times the number of inodes
Limiting inode caches is probably a good idea. It's a long standing known bug. At least definitely do prune inodes not referenced by the dcache I believe some 2.4 trees fixed that in fact, but these fixes have likely not moved to 2.5 yet. But really, it makes no difference for these benchmarks, which have enough memory.
Limiting dcache is probably a bad idea. Recreating the dcache is far slower than any hash lookup. And a lot of Linux's good interactive performance comes from the aggressive dcache caching.
-Andi
Re: [PATCH] New dcache / inode hash tuning patch
Quoted 6 lines · Andi Kleen
The hash function isn't good and the hash chains are far from evenly distributed. There are typically 30-40% empty buckets, while some others are rather long.
In addition I think only a small fraction of the dentries are actually hot. (no numbers on that, sorry)
I wonder what would happen if you reordered the chains moving a 'found' dentry to the front of the chain? If this could be done without excessive locking it might help keep hot entries quickly accessable. This operation should be cheaper given you are using hlists.
Ed Tomlinson
Re: [PATCH] New dcache / inode hash tuning patch
Quoted 4 lines
I wonder what would happen if you reordered the chains moving a 'found' dentry to the front of the chain? If this could be done without excessive locking it might help keep hot entries quickly accessable. This operation should be cheaper given you are using hlists.
You would need to fetch a spinlock for LRU, while the current lookup runs completely lockless.
-Andi
Re: [PATCH] New dcache / inode hash tuning patch
Quoted 7 lines · Andi Kleen
Quoted 4 lines
I wonder what would happen if you reordered the chains moving a 'found' dentry to the front of the chain? If this could be done without excessive locking it might help keep hot entries quickly accessable. This operation should be cheaper given you are using hlists.
You would need to fetch a spinlock for LRU, while the current lookup runs completely lockless.
You would have to lock the chain IF the dentry was not at the head. Would be interesting to see if this locking would hurt much - since hot dentries would not require a lock...
Ed
Re: [PATCH] New dcache / inode hash tuning patch
In reply to Ed TomlinsonIn article <[email protected]>,
Quoted 4 lines · Ed Tomlinson
I wonder what would happen if you reordered the chains moving a 'found' dentry to the front of the chain? If this could be done without excessive locking it might help keep hot entries quickly accessable.
The original dentry code actually did that. It sucks.
The reason it sucks is not so much just the locking, but the fact that you dirty the cache lines, which means that not only are you blowing your cache on that CPU, you also caused the other CPU's to blow _their_ caches (the lines that are in the cache can no longer be shared) AND you caused excessive bus traffic for the writeouts.
In other words: it makes sense if there is one or two really hot entries. But it does not make sense in general. But you might have some heuristic that does it "every 1000 lookups" or something like that, to avoid the problems but still statistically getting the really hot entries closer to the top.
This cache behaviour is, btw, something that rcu made worse - with the pre-rcu stuff, we avoided taking the dcache locks and incrementing the dcache counters for intermediate cached lookups, and we only did it for the leaf entry (or misses).
I hope that we can re-do that optimization _with_ rcu in 2.7.x.
Linus