nanosleep accuracy / threads

I have a client app that runs a high number of threads and pumps tcp/ip
requests to a server at high intensity.  To slow down the app, I've
tried to introduce nanosleep, but have found that it doesn't result in
predictable delays.  Is there another method to use on the threads that
would be more predictable?

Thanks.

0
morty3e (4)
10/27/2005 2:13:51 PM
comp.unix.programmer 10729 articles. 0 followers. kokososo56 (349) is leader. Post Follow

6 Replies
494 Views

Similar Articles

[PageSpeed] 42
morty3e@gmail.com schrieb:
> I have a client app that runs a high number of threads and pumps tcp/ip
> requests to a server at high intensity.  To slow down the app, I've
> tried to introduce nanosleep, but have found that it doesn't result in
> predictable delays.  Is there another method to use on the threads that
> would be more predictable?
> 
> Thanks.
> 

I suppose that you are using Linux due to your problem with the nanosleep().
The timer of the vanilla Kernel depends on the system speed and is thus
not quite precise. You could:
1. tune the system frequence high (1000 Hz max. since 2.6.12)
2. apply the Preemption-Realtime patch of Ingo Molnar.
http://people.redhat.com/mingo/realtime-preempt/
The timer usage of nanosleep is directly mapped to the ktimer of the
patch. It's a high resolution realtime clock and will hopefully meat
your requirement.

Good luck
Cheers
Luotao Fu
0
Luotao
10/27/2005 3:29:13 PM

morty3e@gmail.com wrote On 10/27/05 10:13,:
> I have a client app that runs a high number of threads and pumps tcp/ip
> requests to a server at high intensity.  To slow down the app, I've
> tried to introduce nanosleep, but have found that it doesn't result in
> predictable delays.  Is there another method to use on the threads that
> would be more predictable?

    For high accuracy you need a real-time system.  (What
"high accuracy" means is application-dependent.)

    To get a rate that's fairly good (another application-
dependent term), you could create a "governor" thread.  It
would check the current time with a high-resolution timer
(e.g., gethrtime() on Solaris), then sleep for a nominal
interval, then use the high-resolution timer to get the
actual time upon awakening.  From the actual elapsed time
and the desired message rate it would calculate the number
of messages that are now "due," and increment a semaphore
that many times.  Pseudocode:

	quota = 0;
	start = gethrtime();
	while (running) {
	    sleep_a_while();
	    goal = (gethrtime() - start) * message_rate;
	    increment_semaphore(goal - quota);
	    quota = goal;
	}

    Meanwhile, the message-generating threads spew requests
as fast as they can, but decrement the semaphore before each
transmission.  They'll wait until the governor says it's time
for more requests, and they will never exceed the quota.

    The overall effect will be a bit bursty: You'll have a
few moments of silence until the governor calls for more
traffic, then a flurry of requests as all the requestors
burn through the quota, then another interval of silence,
and so on.  If the governor's sleep intervals are brief
enough this may be acceptable.  You could also slow down the
requestors a bit by introducing sleeps as you've already done,
but with the governor throttling the overall rate of progress
you're no longer so dependent on the accuracy of the sleep
intervals.  You could even "tune" the requestors' nominal
sleep times by keeping track of how long a requestor thread
waits for the semaphore: if it always winds up waiting then
it's probably sleeping for too short a time, but if it never
waits it's probably sleeping too long.

    Whether any of this is tenable depends on just how tightly
you need to control the request generation rate.  As I said at
the beginning, for extremely tight control you'll need features
that go beyond what a general-purpose O/S is designed to deliver.

-- 
Eric.Sosman@sun.com

0
Eric
10/27/2005 3:52:58 PM
morty3e@gmail.com wrote:
> I have a client app that runs a high number of threads and pumps tcp/ip
> requests to a server at high intensity.  To slow down the app, I've
> tried to introduce nanosleep, but have found that it doesn't result in
> predictable delays.  Is there another method to use on the threads that
> would be more predictable?
> 
> Thanks.

You could try to count the requests you sent.
(maybe in the last n seconds)

HTH,
AvK
0
moi
10/29/2005 11:52:17 AM
I've been thinking of doing that, but using nanosleep() after reaching
that (one second) limit would still rely on nanosleep() and its
unpredictable results.

Here are some numbers I got when doing a quick test.

Delay,Write,Read,Puts,Threads,Read TPS
0,2,16,100,1,6250
1,100,109,100,1,917
10,102,111,100,1,901
100,138,150,100,1,667
1000,982,983,100,1,102
10000,994,995,100,1,101
10000,992,993,100,1,101
100000,993,994,100,1,101
1000000,1835,1836,100,1,54
10000000,10946,10947,100,1,9

0,12,55,1000,1,18182
1,993,994,1000,1,1006
10,1067,1076,1000,1,929
100,1034,1044,1000,1,958
1000,9927,9928,1000,1,101
10000,9991,9992,1000,1,100
10000,9996,9997,1000,1,100
100000,9993,9994,1000,1,100
1000000,19102,19103,1000,1,52
10000000,109707,109708,1000,1,9

0,13,34,100,10,29412
1,75,83,100,10,12048
10,80,88,100,10,11364
100,107,125,100,10,8000
1000,990,991,100,10,1009
10000,993,994,100,10,1006
10000,994,995,100,10,1005
100000,1001,1002,100,10,998
1000000,1711,1719,100,10,582
10000000,10942,10943,100,10,91

0,77,277,1000,10,36101
1,1337,1340,1000,10,7463
10,1270,1278,1000,10,7825
100,1221,1229,1000,10,8137
1000,9977,9978,1000,10,1002
10000,9993,9994,1000,10,1001
10000,9997,9998,1000,10,1000
100000,9996,9997,1000,10,1000
1000000,19066,19067,1000,10,524
10000000,109808,109809,1000,10,91

0
morty3e
11/3/2005 3:00:21 PM
<morty3e@gmail.com> wrote in message 
news:1130422431.532259.144650@g49g2000cwa.googlegroups.com...

>I have a client app that runs a high number of threads and pumps tcp/ip
> requests to a server at high intensity.  To slow down the app, I've
> tried to introduce nanosleep, but have found that it doesn't result in
> predictable delays.  Is there another method to use on the threads that
> would be more predictable?

    Why do you need to slow down the app? TCP/IP has its own pacing, you 
should be able to read/write data to the TCP/IP connections at full speed 
without any problem.

    DS


0
David
11/3/2005 7:15:40 PM
On 2005-11-03, David Schwartz <davids@webmaster.com> wrote:
[...]
>     Why do you need to slow down the app? TCP/IP has its own pacing, you 
> should be able to read/write data to the TCP/IP connections at full speed 
> without any problem.

Sometimes, there're some "funny" requirements saying that this
particular code must not process input faster than this and this :) 
For example we have multiple links, one link pushes lots of data, thus
pulling on itself CPU, bandwith etc. We want to slow it down. Another
example, our peer server is buggy, and if we feed the data too fast, then
that server hits some race condition and crashes. They can't fix the
server (commercial product), so instead we have to push data slower.

I guess there are lots of examples in real world that put the
requirement to slow down TCP stream. Fortunately, so far I didn't hit
the requirement to do it with nanosecond accuracy :)

-- 
Minds, like parachutes, function best when open
0
Andrei
11/4/2005 10:15:06 AM
Reply:
Similar Artilces:

I cannot copy a file line by line through a thread
I have this example I am trying to build just to understand threads. The example is : the main program code prints "hello main" on the terminal. Meanwhile, a single thread is copying a file line by line. I am copying from j.txt to f.txt . All I see is that f.txt is still empty. So what should I do to make f.txt filled ? It's important that the copying works this way. I might think of skipping some lines of j.txt while it's copied to f.txt . #!/usr/bin/tclsh package require Thread set thread1 [ thread::create ] set fid [ open "j.txt" "r" ] t...

metaprogramming with the preprocessor: when is it too much? (inspired by the iterators thread)
Hi c.l.c, after reading through a sufficient part of the iterator-thread, I remembered an iterator-like preprocessing facility which I wrote a while back. The aim was to provide zero-overhead ad-hoc iterator functionality through the C preprocessor. I will roughly depict below what I came up with: DISCLAIMER: as the preprocessor symbols are heavily used in the code and not everyone likes being constantly shouted at when reading source, please mentally replace the all-caps notation with something more friendly. And in case you come up with much nicer or more logical names for the ...

C/C++ Threads in Tandem
Hi all, Can anyone help me on how I can use threads on my C/C++ programs in Tandem? Just as I use pthread.h on linux, or types.h and stub.h to use 'thread_create'. I tried searching for these header files on our tandem machine but have'nt found any... any thoughts and opinions? many thanks guys and I really appreciate your help! Enjoy you Day!!! On Mar 5, 12:46 am, "mau_infrance" <mau_infra...@yahoo.com> wrote: > Hi all, > > Can anyone help me on how I can use threads on my C/C++ programs in > Tandem? Just as I use pthread.h on linux, or types.h and...

Accuracy
Hello, I tried to get the machine epsilon whith a little Pascal program. The result is a var extended, with an accuracy of 10e-18, though it was internally calculated with an accuracy of 10e-20. Does anybody know, how one can get those last two digits ? regards udo Udo Stenger wrote: > Hello, > I tried to get the machine epsilon whith a little Pascal program. The > result is a var extended, with an accuracy of 10e-18, though it was > internally calculated with an accuracy of 10e-20. > Does anybody know, how one can get those last two digits ? > > regard...

worker thread crashes on exit when writing to cerr (pthread + gcc 4.3.2 + sunos 5.10)
In psuedo-code I essentially have the following situation, void workerThread( ... ) { ... code ... // std::cerr << ..... << std::endl; <<== un-commenting this causes bus error inside pthread_exit() .... code ... pthread_exit( 0 ); return 0; } int main(...) { pthread_create( ..., workerThread, .... ); pthread_join( ... ); // wait for worker thread return 0; } ideas? On 04/16/10 01:18 PM, Shane wrote: > In psuedo-code I essentially have the following situation, > > void workerThread( ... ) > { > ......

Re: OT: the enlightenment thread #5
Stan responds to previous Biblical quotations: > > What is your response to Leviticus 18:22 - You shall not lie with a male as > > with a woman; it is an abomination. No, no, no. The United States is a bombin' nation. Wirt Atmar * To join/leave the list, search archives, change list settings, * * etc., please visit http://raven.utc.edu/archives/hp3000-l.html * ...

external thread #2
How can I make external thread visible by bolts. I am just starting with SW. Thanks in advance AD Yeah it's real processor intensive so I would not recommend doings this to simple nuts and bolts, especially in an assembly. But if you must check your mail. I didn't write this. One of this ng's main benefactors wrote it. Mike "Alain" <Alain.Degryse@pandora.be> wrote in message news:kRCig.227428$Gz1.161010@blueberry.telenet-ops.be... > How can I make external thread visible by bolts. > > I am just starting with SW. > > Thanks in advance > &g...

Auto kill thread in MacSoup
I've just downloaded MacSoup 2.8b2 and the auto kill thread shortcut keyboad combination doesn't work. The key cmbination should be alt+command + - Has anyone else noticed this and is there a way around it. -- Please remove AT to reply Peter James <pfjamesAT@clara.co.uk> wrote: > I've just downloaded MacSoup 2.8b2 and the auto kill thread shortcut > keyboad combination doesn't work. The key cmbination should be > alt+command + - > Has anyone else noticed this and is there a way around it. Uh, I have always used alt+ or alt-, ie. without command. -- ...

[tao-users] Corba threads hang issue
Hi Experts, I'm new to ACE+TAO. OS: FC2 with Linux kernel 2.6.13. ACE TAO Version: ACE-5.4.4+TAO-1.4.4 Problem: I have 32 corba threads and 200 application threads. If the number of threads are very less, for e.g 10 corba threads and 30 application threads, it just works fine. When the no.of threads are more some how it goes into dead lock and the application hangs. Has this been observed before? It is difficult to debug as this issue is not reproducible with lesser number of threads. Please let me know if someone has already faced such issue. I coulnd't ...

FUTEX_WAIT on all threads
Hello all! I considered that my multi-threaded application sometimes hangs. I used 'ps' utility to show info about threads: user@my_host:~$ ps -eLf | grep my_prog | grep -v grep 1005 10778 1 10778 0 5 Feb26 ? 00:00:01 ./my_prog 1005 10778 1 10779 0 5 Feb26 ? 00:00:22 ./my_prog 1005 10778 1 26660 0 5 Feb27 ? 00:00:00 ./my_prog 1005 10778 1 26661 0 5 Feb27 ? 00:00:00 ./my_prog 1005 10778 1 26662 0 5 Feb27 ? 00:00:00 ./my_prog user@my_host:~$ As you can see there are 5 threa...

machine threads
I wonder how you would make the threads of a screw in PLE. Any info would help. Thanks "Marky" <markajacobson@comcast.net> wrote in news:lL2dnSUBzdlED5rdRVn- vA@comcast.com: > I wonder how you would make the threads of a screw in PLE. Any info would > help. Thanks if you are using nurbs, the basic idea is to make one revolution (imagine a cylinder, but the edge is offset so the top of one side meets the bottom of the other), copy it and stack the next one on top, and so on. Then join them all together either via the stacked edges, or where the thr...

Numeric accuracy questions
Hi, I'm performing some calculations for which I need to compute n "choose" k for n=32 (say). Matlab's documentation for the nchoosek() function states that "this function is only practical for situations where n is less than about 15", without giving any additional explanations. My m-file is producing unexpected results and I think it's because I'm exceeding the allowable range for n. My questions are: 1) Why isn't nchoosek() "practical" for n > 15? The results still appear to be accurate and don't exceed the allowable double range...

thread programming in C
please give me some tips how to start programming using threads in C.... What what do i need to learn first... On 22/08/2014 06:22, naobi wrote: > please give me some tips how to start programming using threads in C. If you're interested in Unix threads, then look for POSIX threads (aka pthreads). https://computing.llnl.gov/tutorials/pthreads/ If you're interested in Windows threads, then look for Win32 threads on MSDN or elsewhere. http://msdn.microsoft.com/en-us/library/windows/desktop/ms682516%28v=vs.85%29.aspx If you're interested in "generic&...

Hyper Threading / Multi-Threaded Queue / Application Freezes
have an MFC application which contains a queue which is accessed by multiple threads. The main application thread is used to add items to the queue, while I have a worker thread which is used for taking items from the queue and passing them to another application. The queue is implemented using the MFC CList class, and add/remove operations are guarded by a critical section, therefore access to the queue is synchronized ( I hope ). My problem is this program has run great on 100's of machines for well over 12months, however recently has been tested on a Hyper-Threaded XP SP2...

Coding Problem (arbitrary thread sych)
Hi, Here is a code example to visualize my problem. ---------------------------------------------------------------------------------- import thread import threading from time import sleep def a(): print "exec a" sleep(1) def b(): print "exec b" sleep(4) def c(): print "exec c" sleep(3) def d(): print "exec d" sleep(2) def e(): print "exec e" sleep(2) def s(): print "exec s" # ... # code which synchronize the incoming three threads # .... print "in synch" def main(sstr): nodes = sstr.split(",&q...

YARHT (Yet Another Rails Hosting Thread)
I have seen many different people asking for free Rails hosting. As I understand it, these people are looking for a free/very cheap host to display their Rails apps on the public. If/when those apps gain a large following, I am sure that the developer of the app would migrate it to a higher-end, paid server. Until that point, however, their seems to be a fair amount of people who could use some cheap/free Rails hosting. So... I pose the question: Would you utilize a service that offered Rails hosting for a fee averaging around $1 a month? This would be a fairly fast machine running ...

serialport won't thread (new to ruby)
def readSerial while 1 puts "reading line from TTY\n" TTY.read_timeout=1 lineTTY = TTY.read.unpack("H*") lineTTY.each { |l| if l.sub!(/^0262([0-9A-F]{12})06/i, "") puts "Got ACK on: #{$1}\n" #This message is simply an ACK from the PLM of a message I sent #to the PLM. because I incorporate error-detection elsewhere, #I assume I send good data to the PLM, and only get ACKs. #NAKs from the PLM are "15" inst...

Time accuracy in relation to position accuracy
I have a real scenario problem regarding GPS/NTP time synchronisation and since I am not such an expert I'd like to ask the GPS wizards here for help. As discussed in this group before there are GPS based reference clocks which can be put into so called "fixed" or "stationary" mode. In simple terms this means that the GPS will calculate its precise position only during startup and not continuosly during operation. In return it will happily provide GPS based time even if only a single satellite is in view. Since the GPS receiver is stationary, it simply remembers its po...

thread with singleton class issue
I have a simple singleton class and have problem to use thread with it. It will be compiled without any problem but always crashed at the activate() call. There is NO problem if it is not a singleton class. Anybody can tell me what is wrong with my class? thanks, James Mei #ifndef ACETHREAD_H_ #define ACETHREAD_H_ #include "ace/OS.h" #include "ace/Time_Value.h" #include "ace/Task.h" #include <iostream> class ACEThread : public ACE_Task_Base { public: void startThread(); virtual int svc(); static ACEThread* i...

Poor thread support in Linux.
Win32 programmers typically create threads. Linux programmers create new processes with fork(). Threads have a lower overhead than processes. Why are threads so poorly implimented in the Linux kernel? begin Mike Cox wrote: > Win32 programmers typically create threads. Linux programmers create new > processes with fork(). > > Threads have a lower overhead than processes. > Fine. Threads do exist in linux > Why are threads so poorly implimented in the Linux kernel? Why are you such a stupid twit? -- You are a shining example for the advances in artificial stupidity...

Problem with autoflush on Semaphore-threads for Windows
Dear community, having written a working example for using Semphores on a windows client, I created a little "server-application" that does the following: A Server continously "listens/tails" a command-file for new commands. If a new commands arrives a split of the workload to threads is under- taken that "control themselves" via Semaphore and are joined finally. It works. However, the aspects of logging "I am done" into a log-file does mysteriously not work synchronised although: - all threads are definitely joined (see below) - autoflush is act...

Data-path accuracy in IIR filters?
I am working on a project where I need to implement 6-th order Butterworth low-pass filters in an FPGA. In some the bandwidth is low relative to the input data rate, whereas others have higher bandwidth. I can use ScopeIIR or Matlab to give me a good idea of coefficient accuracy for any given ratio of bandwidth to input sample rate. However, I'm not sure what data-path accuracy I need (for 20-bit input / output accuracy). Is there a rule-of-thumb I can use, or do I just have to simulate the filter with real data and see what gives me low enough noise? I was planning on usi...

Tomcat thread
Hi, Can someone tell me if there is any way of measuring the thread utilisation of Tomcat using Java program? I am using Tomcat 5.0.19 thanks and rgds, Prashant You can use write a Servlet that analyzes System.Diagnostics.Process Karl pvsnmp@yahoo.com wrote: > Hi, > Can someone tell me if there is any way of measuring the thread > utilisation of Tomcat using Java program? I am using Tomcat 5.0.19 > > thanks and rgds, > Prashant > ...

Exceptions in threads?
What is supposed to happen when an exception is raised and not caught in a thread? The Reference Manual (section 4.2) states "When an exception is not handled at all, the interpreter terminates execution of the program, or returns to its interactive main loop", but it looks like what really happens is the thread is terminated, not the whole program. If I run the following: ------- #!/usr/bin/env python import time import thread def bogus (): raise Exception thread.start_new_thread (bogus, ()) while 1: time.sleep (1) print "still alive" ------- It kee...

nanosleep() hung
The following is my sleep function: long nap(long period) { timespec_t rqtp, rmtp; rqtp.tv_sec = period / 1000; rqtp.tv_nsec = 1000000 * (period % 1000); nanosleep(&rqtp, &rmtp); } When execute nap(10), sometimes it will be hung up. My environment is SunOS 5.7 + gcc 2.8.1 -- Thanks, Richard whererush wrote: > The following is my sleep function: > > long nap(long period) > { > timespec_t rqtp, rmtp; > > rqtp.tv_sec = period / 1000; > rqtp.tv_nsec = 1000000 * (period % 1000); > nanosleep(&rqtp, &rmtp); > } ...