LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 07-02-2009, 03:25 PM   #1
ranthal
LQ Newbie
 
Registered: Jun 2009
Location: Carlsbad, CA
Posts: 24

Rep: Reputation: 15
Question on execution of unlikely()


So unlikely is a nice way to cut down on how the kernel executes certain tests that you doubt will fail.

For instance I kmalloc a piece of memory and it's unlikely I still have a null pointer-
Code:
s_ptr = kmalloc(sizeof(struct some_struct), GFP_KERNEL)
if(unlikeley(!s_ptr))
{
    ...
}
But how efficient is it if I want to test the execution of a certain function? Going with the last example, say I did it like this-
Code:
if(unlikely(!(s_ptr = kmalloc(sizeof(struct some_struct), GFP_KERNEL)))
{
    ...
}
This isn't exactly how I'm planning to use it, just an example, but just if I applied it to a function I'm expecting return 0 in pretty much all cases unless something really goes wrong.
 
Old 07-05-2009, 07:04 PM   #2
JimHughen
Member
 
Registered: Jun 2009
Location: Austin, Texas
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 16
A discussion about these conditional branching mechanisms is interesting. I have read many of the macro's to unravel 'what the intention is'. I still don't see it very clearly. I saw a double explanation point in front of a conditional. "!!(cond)" I'm not sure of this usage, but curious.
 
Old 07-07-2009, 09:09 AM   #3
cladisch
Member
 
Registered: Oct 2008
Location: Earth
Distribution: Slackware
Posts: 228

Rep: Reputation: 54
Quote:
Originally Posted by ranthal View Post
So unlikely is a nice way to cut down on how the kernel executes certain tests that you doubt will fail.
[...]
But how efficient is it if I want to test the execution of a certain function?
This depends on how often that code is executed and on how well the compiler can optimize your code.
You'll have to measure.
 
Old 07-07-2009, 09:11 AM   #4
cladisch
Member
 
Registered: Oct 2008
Location: Earth
Distribution: Slackware
Posts: 228

Rep: Reputation: 54
Quote:
Originally Posted by JimHughen View Post
I saw a double explanation point in front of a conditional. "!!(cond)" I'm not sure of this usage, but curious.
The purpose of this idiom is to convert from an integer to a boolean with a deterministic 'true' value, i.e., any non-zero value results in 1.
 
Old 07-07-2009, 09:34 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by ranthal View Post
So unlikely is a nice way to cut down on how the kernel executes certain tests that you doubt will fail.
I believe it is not the test itself, but the code executed after the test that can be optimized.

The test itself needs to be executed regardless of the low probability of a true result. But the compiler could change the code that would be executed after true is seen to optimize the typical flow:

1) The simple and obvious optimization is to put that code out of line from the main flow, hopefully in a different cache line. So the code that isn't normally executed won't be read into the cache and won't push more useful code out of the cache.

2) I don't think GCC does this (but I think it ought to): Within sections of code whose execution is unlikely, regardless of any overall optimization setting biasing in favor of speed over space, there should be a local optimization of space over speed. Simply put, compile the unlikely to be executed code into as small binary as possible to further minimize its impact on the cache, on virtual memory paging, etc.

Quote:
say I did it like this-
I'm not 100% sure, but I don't think the difference between your two versions of that code would matter at all to the optimizer.
Burying the assignment inside the test makes the code harder to read, harder to maintain and harder to debug. If there is any useful information handed to the optimizer by burying the assignment that way, I expect the optimizer would have found the same information by itself from the more readable form.
 
Old 07-07-2009, 11:39 AM   #6
JimHughen
Member
 
Registered: Jun 2009
Location: Austin, Texas
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 16
That makes sense.

The compiler has the possibility to 'not branch' for the 'likely' case, and then to 'branch' for the 'unlikely' case.

For the 'likely' case, no branch is taken and the following instructions have probably already been fetched by the bus processor look-ahead/prefetch. I think the prefetch advantage is in addition to the cache optimization John discussed.

Very nice idea, even if the compiler hasn't taken advantage of it yet.

Quote:
The purpose of this idiom is to convert from an integer to a boolean with a deterministic 'true' value, i.e., any non-zero value results in 1.
It is necessary that the first "!(cond)" convert the integer to a boolean with the same assumption that the last/outside expression avoids.

Maybe "(cond != 0)" might be more concise.
 
Old 07-07-2009, 01:38 PM   #7
ranthal
LQ Newbie
 
Registered: Jun 2009
Location: Carlsbad, CA
Posts: 24

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by johnsfine View Post
Burying the assignment inside the test makes the code harder to read, harder to maintain and harder to debug. If there is any useful information handed to the optimizer by burying the assignment that way, I expect the optimizer would have found the same information by itself from the more readable form.
I think for the purposes of my module this is the best solution.

However, I was mostly using the assignment as an example, such cases might exist where I think burying it is more readable. Something quick that comes to mind is if(isalpha(c)). I feel like the readability of that better describes the operation rather than assigning the result of isalpha() to a variable then testing the variable.

Either way, an interesting discussion so far on optimizing.
 
Old 07-07-2009, 02:06 PM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by ranthal View Post
such cases might exist where I think burying it is more readable. Something quick that comes to mind is if(isalpha(c)).
I wasn't objecting to a function call inside a condition. That's ordinary programming. I almost never prefer adding an extra bool variable over directly testing the condition where you care about it.

I objected to unnecessarily burying the assignment operator inside the condition.

I also do bury a lot of assignment operators inside conditions, but only when the flow of code would be broken up if you didn't, for example:

Code:
   if (   pnt == 0
       || (x = pnt->bar) != 0 )
   {
      ...
   }
Depending on surrounding code and other details I left out, it might be very messy to pull the assignment operator out of the middle of the condition.

Then I try to minimize the inherent unreadability by making very clear the '=' wasn't a typo for '==' and with a line break to highlight the extra operation.

Last edited by johnsfine; 07-07-2009 at 02:08 PM.
 
Old 07-07-2009, 04:06 PM   #9
ranthal
LQ Newbie
 
Registered: Jun 2009
Location: Carlsbad, CA
Posts: 24

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by johnsfine View Post
I wasn't objecting to a function call inside a condition. That's ordinary programming. I almost never prefer adding an extra bool variable over directly testing the condition where you care about it.

I objected to unnecessarily burying the assignment operator inside the condition.
Yeah I figured that was the case, just thought I'd say something explicitly to keep the discussion going since in the original post I wasn't so concerned with the assignment as I was about how the optimizer treated the function execution were it placed inside the unlikely().
 
Old 07-07-2009, 07:45 PM   #10
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by ranthal View Post
in the original post I wasn't so concerned with the assignment as I was about how the optimizer treated the function execution were it placed inside the unlikely().
Probably you already understand from what I said before. I'm pretty sure it will make no difference to the optimizer whether the function call is inside the unlikely() vs. before it.

I tend to think about the optimizer reaction first. After I decide a choice will make no difference to the optimizer, then I try to arrange it for maximum maintainability.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
command execution gr8linux Linux - Newbie 14 05-28-2009 02:28 AM
PHP & HTML conditional execution question bostonantifan Programming 1 05-19-2009 07:32 AM
Threads execution question mvorlov Programming 7 09-19-2006 08:13 AM
How to tell where the execution is going on right now? bahadur Programming 7 04-03-2005 03:42 AM
Thorny remote execution question davidcrawley Linux - Software 3 09-03-2004 02:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 06:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration