f



getting rid of a while(1) "poll"

Typical problem where a while(1) consumes gobs of CPU.  Not as 
straightforward (to me) to figure out how to replace it with 
something more CPU friendly.

The overall system has three daemons:

1) suck_packets_d
2) first_order_packet_filter_and_packet_buffer_d
3) extract_interesting_packet_features_and_generate_report_d

Using some sort of blocking calls in 1) or 2) seems like a bad idea, 
because packet volume can be high and I want to keep up with it as 
much as possible.  

I could use some help thinking this through if anyone is so inclined.

C / Linux

pseudocode for daemon (2) (biggest CPU consumer)

main()
{
	while(1)
	{
		retcode = function() ;
		switch retcode 
		{
			case	errortype1
			case	errortype2
			case	errortype3
		}
	}
	
	return 0 ;
}

int function()
{
	value = 0 ;

	// value is pass-by-result here
	read_incoming_svs_v_message_queue( value ) ; 

	if keep_new_data( value )
		return write_outgoing_sys_v_message_queue() ;
	else
		return	NO_OP ;	
}

int read_incoming_svs_v_message_queue( value ) 
{
	if msgrcv() < 0
		if errno != ENOMSG
			return ERROR
		else
			return	NO_OP
	else
	{
		read_inbound_posix_shared_memory() ;
		
		while (1)
		{
			if fcntl() GET LOCK < 0
			{
				if errno == EAGAIN or errno == EACCES
					continue ;
				else 
					return	ERROR
			}
		}

		value = read locked resource

		if fcntl() UNLOCK < 0 
			handle_error()		
	}
}

int write_outgoing_sys_v_message_queue()
{
	while (1)
	{
		if fcntl() GET LOCK < 0
		{
			if errno == EAGAIN or errno == EACCES
				continue ;
			else 
				return	ERROR
		}
	}

	write to locked resource

	if fcntl() UNLOCK < 0 
		handle_error()

	if msgsnd() fails
		return	ERROR ;

	write to posix shared memory 
}


0
Vendor
12/15/2004 6:41:57 PM
comp.unix.programmer 10848 articles. 0 followers. kokososo56 (350) is leader. Post Follow

10 Replies
1031 Views

Similar Articles

[PageSpeed] 10

"Vendor Neutral" <neutral@domain.invalid> wrote in message
news:MPG.1c2a4f80ebc7ecbf989f9f@news.verizon.net...
> Typical problem where a while(1) consumes gobs of CPU.  Not as
> straightforward (to me) to figure out how to replace it with
> something more CPU friendly.
>
> The overall system has three daemons:
>
> 1) suck_packets_d
> 2) first_order_packet_filter_and_packet_buffer_d
> 3) extract_interesting_packet_features_and_generate_report_d
>
> Using some sort of blocking calls in 1) or 2) seems like a bad idea,
> because packet volume can be high and I want to keep up with it as
> much as possible.

Sorry, but "blocking calls" are designed for this purpose.  Why is it a bad
idea?  If the data is there to process then you are unblocked.  These types
of semaphores are generally designed to work with the I/O and CPU to create
the best possible scenario.


0
Bryan
12/15/2004 6:53:20 PM
On Wed, 15 Dec 2004, Vendor Neutral wrote:

> Using some sort of blocking calls in 1) or 2) seems like a bad idea,

Why do you think that?

> because packet volume can be high and I want to keep up with it as
> much as possible.

Umm, if there is data available, blocking functions won't block: they'll
return data immediately.

Using functions that block if no data is available is the correct
thing to do.

-- 
Rich Teer, SCNA, SCSA, author of "Solaris Systems Programming"

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich
0
Rich
12/15/2004 7:29:25 PM
rich.teer@rite-group.com wrote...
> On Wed, 15 Dec 2004, Vendor Neutral wrote:
> 
> > Using some sort of blocking calls in 1) or 2) seems like a bad idea,
> 
> Why do you think that?

Mmmmm...because I'm a dope?

And, being a dope, I was concerned about the latency of the 
transition from sleep to getting placed in the run queue, when there 
are data waiting.

> > because packet volume can be high and I want to keep up with it as
> > much as possible.
> 
> Umm, if there is data available, blocking functions won't block: they'll
> return data immediately.
> 
> Using functions that block if no data is available is the correct
> thing to do.

0
Vendor
12/15/2004 7:58:00 PM
On Wed, 15 Dec 2004, Vendor Neutral wrote:

> Mmmmm...because I'm a dope?

The fact that you asked the question suggests otherwise.  :-)

> And, being a dope, I was concerned about the latency of the
> transition from sleep to getting placed in the run queue, when there
> are data waiting.

That would only be an issue on a R E A L L Y heavily loaded system,
in which case, you've got other problems, and probably wouldn't be
able to process the data in a timely fashion anyway.

Any reasonably modern machine can process packets from a fully
saturated 100 baseT interface.  And processing data as fast as
the wire can get it to you is the best you can do!

-- 
Rich Teer, SCNA, SCSA, author of "Solaris Systems Programming"

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich
0
Rich
12/15/2004 8:03:30 PM
Vendor Neutral <neutral@domain.invalid> writes:

>Mmmmm...because I'm a dope?

>And, being a dope, I was concerned about the latency of the 
>transition from sleep to getting placed in the run queue, when there 
>are data waiting.

You should be more worried about your application burning CPU cycles
your network driver may need to process incoming packets.

Casper
-- 
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
0
Casper
12/15/2004 8:30:08 PM
rich.teer@rite-group.com wrote...
> On Wed, 15 Dec 2004, Vendor Neutral wrote:
> 
> > Mmmmm...because I'm a dope?
> 
> The fact that you asked the question suggests otherwise.  :-)
> 
> > And, being a dope, I was concerned about the latency of the
> > transition from sleep to getting placed in the run queue, when there
> > are data waiting.
> 
> That would only be an issue on a R E A L L Y heavily loaded system,
> in which case, you've got other problems, and probably wouldn't be
> able to process the data in a timely fashion anyway.
> 
> Any reasonably modern machine can process packets from a fully
> saturated 100 baseT interface.  And processing data as fast as
> the wire can get it to you is the best you can do!

I switched to blocking versions of msgsnd() and msgrcv() (by removing 
the IPC_NOWAIT flag and adjusting < 0 handling to deal with EIDRM and 
EINTR errno returns) and I am literally stunned at the reduction in 
CPU across the board.  For example, from 90+% to 0.7% on my second 
daemon, with no loss of throughput.

Man, what an education I just got.

Many, many thanks!
0
Vendor
12/15/2004 9:48:33 PM
Casper.Dik@Sun.COM wrote...
> Vendor Neutral <neutral@domain.invalid> writes:
> 
> >Mmmmm...because I'm a dope?
> 
> >And, being a dope, I was concerned about the latency of the 
> >transition from sleep to getting placed in the run queue, when there 
> >are data waiting.
> 
> You should be more worried about your application burning CPU cycles
> your network driver may need to process incoming packets.

That, of course, is an extremely good point.
0
Vendor
12/15/2004 9:49:04 PM
On Wed, 15 Dec 2004, Vendor Neutral wrote:

> Man, what an education I just got.
>
> Many, many thanks!

You're welcome.

You might want to have a look at my book, Solaris Systems Programming.*
Despite the word "Solaris" in its title, most of the material applies
to most UNIX and UNIX-like OSes.

The book's home page, www.rite-group.com/rich/ssp contains a lot of
info about it, including a sample chapter.

* Or preferably, buy a copy!  :-)

-- 
Rich Teer, SCNA, SCSA, author of "Solaris Systems Programming"

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich
0
Rich
12/15/2004 10:17:32 PM
rich.teer@rite-group.com wrote...
> On Wed, 15 Dec 2004, Vendor Neutral wrote:
> 
> > Man, what an education I just got.
> >
> > Many, many thanks!
> 
> You're welcome.
> 
> You might want to have a look at my book, Solaris Systems Programming.*
> Despite the word "Solaris" in its title, most of the material applies
> to most UNIX and UNIX-like OSes.
> 
> The book's home page, www.rite-group.com/rich/ssp contains a lot of
> info about it, including a sample chapter.
> 
> * Or preferably, buy a copy!  :-)

Ordered via your link w/Amazon.  Merry Christmas to me.

Thanks again, Rich.  Happy holidays.

0
Vendor
12/15/2004 10:44:56 PM
On Wed, 15 Dec 2004, Vendor Neutral wrote:

> Ordered via your link w/Amazon.  Merry Christmas to me.

Thank you!

> Thanks again, Rich.  Happy holidays.


My pleasure - and Merry Christmas to you, too.

WHich reminds me--it's way past the time I should dig out
my Xmas signature.  I'll rectify that right now!

-- 
Rich Teer, SCNA, SCSA, author of "Solaris Systems Programming"

                                                    .  *   * . * .* .
                                                     .   *   .   .*
President,                                          * .  . /\ ( .  . *
Rite Online Inc.                                     . .  / .\   . * .
                                                    .*.  / *  \  . .
                                                      . /*   o \     .
Voice: +1 (250) 979-1638                            *   '''||'''   .
URL: http://www.rite-online.net                     ******************
0
Rich
12/15/2004 11:26:46 PM
Reply:

Similar Artilces:

how to get rid of "today" "yesterday" etc bars in kmail 1.12.4?
Hi: I looked around in the setup of kmail, I googled but for the life of me I can't find any way to get rid of the superfluous and molesting bars which devide my message list into "today", "yesterday", "last week", "NAME OF MONTH". etc [rant] I am able to read the dates and sort against the dates column - yessir! I don't want such "Outlook-Mania for dummies". Oh wait! Maybe it's not for dummies - 'cause dummies aren't able to get rid of it. [rant] And yes, I would like to stay with kmail. Kind regards, Eike -- "T...

""""""""""""""""""""""ADD ME""""""""""""""""""""
Hi , Hope you are doing great. Please let me take this opportunity to introduce myself, Iam Karthik working with BhanInfo Inc, a NY based company. We have consultants on our bench on various technologies, my request is to add me to your distribution list and kindly do send me the requirements. i have the below list available 1. Mainframe 2. Java 3.. Financial Analyst 4. Data Architect If there is any vendor ship agreement which has to be signed then I would like to take an opportunity to represent my company and expect your cooperation... We look forward to build a ve...

"""""""""ADD ME""""""""""
Hi , Hope you are doing great. Please let me take this opportunity to introduce myself, Iam Karthik working with BhanInfoi Inc, a NY based company. We have consultants on our bench on various technologies, my request is to add me to your distribution list and kindly do send me the requirements. i have the below list available 1. Mainframe 2. Java 3.. Financial Analyst 4. Data Architect If there is any vendor ship agreement which has to be signed then I would like to take an opportunity to represent my company and expect your cooperation... ...

Replacing "RADIO" with "RADIO" NAME="1" VALUE="1", etc..
I am running code below so that "RADIO" gets replaced by "RADIO" NAME="1" VALUE="1" etc. Actually there are 5 RADIO buttons for each question, and the initial web page I generate somehow dord not have NAME="1" VALUE="1", etc. after "RADIO". So I am using Perl to replace simple "RADIO" with "RADIO" NAME="1" VALUE="1", "2", "3", "4", and "5" and for the choices for the problem number 2, "RADIO" NAME="2" VALUE="1", &q...

a = [ "1", "2", "3" ] v/s a = new Array ( "1", "2", "3" ) identical in all ways?
Do these result in identical objects? a = [ "1", "2", "3" ] a = new Array ( "1", "2", "3" ) On Aug 25, 1:30=A0pm, okey wrote: > Do these result in identical objects? > > a =3D [ "1", "2", "3" ] > a =3D new Array ( "1", "2", "3" ) Yes, but:- a =3D [ 2 ]; a =3D new Array( 2 ); - do not. Richard. okey wrote: > Do these result in identical objects? > > a = [ "1", "2", "3" ] > a = new Array ( "1", "2"...

LV7.1 Troubles to get a "Region of Interest" with the "get last event" method
Hello ! &nbsp; When using the "get last event" method on a displayed image, I can't get a ROI input : the method never returns a&nbsp;type=2 and doesn't draw the rectangle on the image. &nbsp; Moreover, I experience a behaviour I don't quite understand with the command array for the "which event" parameter : the left hand&nbsp;value is a digit, and switches the right hand value from "no event"&nbsp; to "draw event" and son on,.. but I can't&nbsp;understand what the left hand digit is exactly used for. I want the m...

Urgent Requirement in """""""""""""NEW YORK""""""""""""""""
Hello Partners, Please find the requirement below. Please send the updated resume along with rate and contact no. REQ#1: Title : Java Developer ( Rating Project) Duration : 6 months Rate : open Location : NY strong java, WebLogic 9.2, Web Services, Oracle REQ#2: Title : Java Developer Duration : 4 months Rate : open Location : NY Strong java, SQL REQ#3: Title : VB.Net Consultant Location : NY Duration : 4 months Rate : open Primarily looking at someone who has Excel, VB.net a...

"my" and "our"
Hi, while testing a program, I erroneously declared the same variable twice within a block, the first time with "my", the second time with "our": { my $fz = 'VTX_Link'; .... ( around 200 lines of code, all in the same block) our $fz = 'VTX_Linkset'; ... } So the initial contents of the $fz declared with "my" is lost, because "our" creates a lexical alias for the global $fz, thus overwriting the previous "my" declaration. It was my error, no question. But I wonder why Perl doesn't mention this - even with "use s...

"out" and "in out"
Hi i found the following explaination: In Ada, "in" parameters are similar to C++ const parameters. They are effectively read-only within the scope of the called subprogram. Ada "in out" parameters have a reliable initial value (that passed in from the calling subprogram) and may be modified within the scope of the called procedure. Ada "out" parameters have no reliable initial value, but are expected to be assigned a value within the called procedure. What does "have no reliable initial value" mean when considering the "out" parameter? By c...

"/a" is not "/a" ?
Hi everybody, while testing a module today I stumbled on something that I can work around but I don't quite understand. >>> a = "a" >>> b = "a" >>> a == b True >>> a is b True >>> c = "/a" >>> d = "/a" >>> c == d True # all good so far >>> c is d False # eeeeek! Why c and d point to two different objects with an identical string content rather than the same object? Manu Emanuele D'Arrigo wrote: >>>> c = "/a" >>>&...

"If then; if then;" and "If then; if;"
I have a raw data set which is a hierarchical file: H 321 s. main st P Mary E 21 F P william m 23 M P Susan K 3 F H 324 S. Main St I use the folowing code to read the data to creat one observation per detail(P) record including hearder record(H): data test; infile 'C:\Documents and Settings\retain.txt'; retain Address; input type $1. @; if type='H' then input @3 Address $12.; if type='P' then input @3 Name $10. @13 Age 3. @16 Gender $1.; run; but the output is not what I want: 1 321 s. main H 2 321 s. main P Mary E 21 F 3 321 s...

about "++" and "--"
why this program snippet display "8,7,7,8,-7,-8" the program is: main() { int i=8; printf("%d\n%d\n%d\n%d\n%d\n%d\n",++i,--i,i++,i--,-i++,-i--); } > why this program snippet display "8,7,7,8,-7,-8" Ask your compiler-vendor because this result is IMHO implementation-defined. Check this out: http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.15 http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.16 Regards, Irina Marudina fxc123@gmail.com wrote: > why this program snippet display "8,7,7,8,-7,-8&q...

"or" and "and"
Hi, I'm just getting to discover ruby, but I find it very nice programming language. I just still don't understand how the "or" and "and" in ruby... I was playing with ruby and for example made a def to print Stem and Leaf plot (for those who didn't have a statistics course or slept on it, e.g. http://cnx.org/content/m10157/latest/) Here is the Beta version of it: class Array def n ; self.size ; end def stem_and_leaf(st = 1) # if st != (2 or 5 or 10) then ; st = 1 ; end k = Hash.new(0) self.each {|x| k[x.to_f] += 1 } k = k.sort{|a, b| a[0].to_f <=&g...

"C" programm in "Matlab" Programm
Hi, i habe a matlab code which is looking like that " in C: unsigned char i, Checksum; unsigned char Command[9]; //Set the “Command” array to the desired command Checksum = Command[0]; for(i=1; i<8; i++) Checksum+=Command[i]; Command[8]=Checksum; //insert checksum as last byte of the command //Now, send it to the module how can i convert it to matlab? thanks for you answer melano "Miland " <miland17@yahoo.de> wrote in message <j0mb4f$48u$1@newscl01ah.mathworks.com>... > Hi, > i habe a matlab code which is looking like that "...

Web resources about - getting rid of a while(1) "poll" - comp.unix.programmer

Resources last updated: 2/14/2016 8:30:42 AM