nanosleep accuracy / threads

  • Permalink
  • submit to reddit
  • Email
  • Follow


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
Reply morty3e (4) 10/27/2005 2:13:51 PM

See related articles to this posting

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
Reply 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
Reply 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
Reply 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
Reply 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
Reply 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
Reply Andrei 11/4/2005 10:15:06 AM
comp.unix.programmer 10605 articles. 64 followers. Post

6 Replies
388 Views

Similar Articles

[PageSpeed] 10

  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

nanosleep()-> -lrt or -lposix4? Different for Solaris versions, Single/Multi-threaded cases?
I use nanosleep() in my code. There are 2 shared libraries associated with it i.e. librt.so and libposix4.so Which library should I link with? I use Solaris 8. Does it vary for the Solaris 9? Does the linking differ(among librt.so and libposix4.so) for single and multithreaded program? Which one I should use for each of the case(single, multi-threaded)? qazmlp1209@rediffmail.com writes: >I use nanosleep() in my code. There are 2 shared libraries associated >with it i.e. librt.so and libposix4.so >Which library should I link with? >I use Solaris 8. Does it vary for the Solaris 9...

thread, threading; how to kill a thread?
Greetings Pythonists; I have limited experience with threaded apps and plenty with old style forked heavyweight multi-processing apps. Using Python 2.3.3 on a Redhat 7.x machine. Wondering if there is a simple way from a main python program to kill a running thread? I see with the 'threading' module the way daemonic threads behave when the main program finishes. But suppose we have a simple thread running a function like; def timedLoop(): while True: time.sleep(10) doSomething() All I am trying to do is stop that thread immediatly from the main program and i...

To thread or not to thread ?
The threads of discussion about threads have been enlightening in exposing some of the low level issues which could possibly affect MT code and, more importantly, served also to bring awareness to the unsuspecting (me for one). It leaves me wondering if writing MT code is even to be pursued at this point in time, or at least "where's the guidebook to read is that will facilitate the writing of MT code without engaging compiler-level gotchas?". Can the low level issues be dealt with in the short term by following certain rules, perhaps platform-specific ones, or is robust MT code ...

Thread or threads.
thinking of writing a perl script with concurrent abilities. which Module is better 'Thread' or 'threads' ? thx. On Mon, 30 Jun 2008 05:24:19 -0700 (PDT), nadav <nadavh@gmail.com> wrote: >thinking of writing a perl script with concurrent abilities. >which Module is better 'Thread' or 'threads' ? > >thx. Thread (capital T) is obsolete. -- I'm not really a human, but I play one on earth. http://zentara.net/CandyGram_for_Mongo.html nadav wrote: > thinking of writing a perl script with concurrent abilities. > which Module is bette...

threading a thread
I have a program written in Python that checks a class B network (65536 hosts) for web servers. It does a simple TCP socket connect to port 80 and times out after a certain periods of time. The program is threaded and can do all of the hosts in about 15 minutes or so. I'd like to make it so that I can check for other open ports (instead of just port 80) and I'd like to thread the port checks as well. Right now I'm just prototyping and the threaded hosts portion works very well for my needs. I'd just like to add a threaded ports check and wanted to know if anyone had don...

To thread or not to thread
Hello, I was recently reading an article on threading in python and I came across Global Interpreter Lock,now as a novince in python I was cusrious about 1.Is writing a threaded code in python going to perform well than a normal python code.If so on what basis can it performance be measured. 2.Is writing a threaded code in python better than a code written in C/C++ using PTHREADS. If someone can comment on these questions, it would be great. On 2005-07-28, Sidd <iamsidd@gmail.com> wrote: > I was recently reading an article on threading in python and I > came across Glo...

thread or threads ?
For some time now, I've been checking out tcl, tk, tls and threads from sourceforge. Recently, my cvs went berserk (I passed it a subdirectory with trailing slash for update, but I'm not sure if that was really the reason), and instead of updating just that directory, it checked out all modules. Among these modules was "thread". I checked for what was more recent, and it looked like thread had more recent checkins than threads. But "thread" doesn't even configure: > ../../../cvs/sourceforge/thread/configure checking for correct TEA configuration... ok (T...

To thread or not to thread
My app needs to check a number of things on the web. This can be done in the background. I use idHTTP to pull data in. This process is broken down into small parts and the subroutinen are called in the OnIdle handler of the main form. I notice that sometimes idHTTP needs a considerable time to build the connection/fetch the data and the application doesn't repond to user actions during such periods. To cope with this I have now implemented the web data fetching in a thread with priority tpIdle. I think the problem of non-response has gone, ut it is difficult to test. Am...

About threading.Thread
Hello guys: I have an question on threading.Thread My code is here: File1: a.py import threading import time def hello(): while True: print('hello') threads =3D threading.enumerate() for thread in threads: print(thread.getName()) time.sleep(3) def fun(): a =3D threading.Thread(target=3Dhello(), name=3D'hello') print("a file: I won't display!") a.start() File2: b.py import time from a import fun fun() print("b file: I won't display!") When I run: python b.py (python version: 2.7.6, 3.3.5, 3.4....

thread::release [thread::id] of main thread
I would like to put this into discussion: In the main thread write: package re Thread thread::release [thread::id] Shall a scripting language take precautions for making such errors and throw an error? or shall it simply do what the user requests? I already mentioned this at sourceforge but didn't write an explicit bug-report. thx ...

threads without threads
Hello, I have a counter loop which actually executes in a different thread. The thread is there to check for a connection time out. However due to some limitations, I cannot use pthreads or any OS dependant components (my code should run on both windows and linux). I have to relegiously stick on to ANSIC standard. Does ANSI C provide any inherent function which could accomplish what I intend to do. Or putting it otherway, how can I just deviate from my direct execution path and just return to it after a stipulated time has elapsed. ~saraca sindica@gmail.com scribbled the following: > Hel...

nuby threading on threads
Hi Friends, This is my first touch on threads, so be gentle pls :-) I am trying to execute loop a command by using threads (tested them on windows and I find them much faster than doing one at a time :). However, there are some commands that execuite very long time and are memory intensive, so I want that I only execute 10 threads at a time. eg. LIMIT= 10 tlist = [] loop { if tlist.size< LIMIT tlist << Thread.new { p "test" } else sleep 5 end } Am, I right? Of course, I'm wrong since I tested it; tlist size is steady at LIMIT. When ...

threads within a thread
I have a program that uses threads to quickly check class B networks (65,536) hosts for public web servers. It works great. I'd like to check for other servers as well. Basically, I've got the hosts threaded. Now, I'd like to thread the ports so that while the hosts are being probed concurrently that ports on the hosts could be probed concurrently as well. It might look like this: A Thread host A Thread port 80 port 443 port 25 host A Thread port 445 port 139 host A Thread ... I can demonstrate actually code (with only threaded ho...

Threads: does Thread.start() atomically set Thread.__started ?
Can some kind person please further my education on Threads? When I create a thread called "t" and I do a "t.start()" am I guaranteed that "t.isAlive()" will return True as long as the thread hasn't already completed? Put another way, does "t.start()" ever return before t.__started is set to True? consider this example: import time import threading class MyThread(threading.Thread): def __init__(self): self.completed = False threading.Thread.__init__(self) def run(self): #do something time.sleep(1) se...

how to start thread in thread
Hello everyone, I'm a perl beginner. I created a piece of code which starts a first thread. Thie first thread in turn creates a second thread (It's only testing but I will need such feature soon). Unfortunately the creation of the second thread hangs the first one (please, see the test example attached below). Is there anybody who knows what's going on ?. I decided to create fully OO software and I wouldn't like to go back into a fork-based solution (It would be a really painful operation). Env: Active State Perl 5.8.3/w2k (unfortunately I had no time to test it under Red Hat...

Thread within a thread
Is it possible to call a thread (pthread) from within a thread invocation ? If I correctly pthread_join() the new thread from the calling thread, does it make good design sense ? <raseelbhagat@gmail.com> wrote in message news:de3c9fd5-5d89-4a0c-a2ac-14a1f7da0784@i28g2000prd.googlegroups.com... > Is it possible to call a thread (pthread) from within a thread > invocation ? Yes. > If I correctly pthread_join() the new thread from the calling thread, > does it make good design sense ? Well, perhaps it makes 100% complete sense for your particular application; I need mo...

Canceling Threads from other Threads
Hey, I am using Posix Threads, and I am trying to cancel one thread from another. My Main spawns 2 tasks. Task 1 after a few iterations calls: if(pthread_cancel(ptrThread2)==ESRCH) printf("Thread Canceling Failed \n"); where thread2's creation looks like: TaskStart(ptrThread2,ptrThreadAttr2, taskHandler2); and in the thread of ptrThread2 I have the code: int old_cancel_state, old_cancel_type; pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancel_state); pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_cancel_type); I declare pthread_t ptrThread2; u...

Stopping all threads from other thread
Hello, it is possible to stop all threads (application) from thread of application: App |-MainThread |-WebServer |-CmdListener # From this I want to stop App The sys.exit isn't working... Amadeusz Jasak (Poland) amadeusz.jasak@gmail.com wrote: > Hello, > it is possible to stop all threads (application) from thread of > application: > App > |-MainThread > |-WebServer > |-CmdListener # From this I want to stop App > > The sys.exit isn't working... You can start all threads other than CmdListener as daemon threads, using threading.Thread.setDaemon(...

threads creating threads ..
Hi, scenario: I create a thread X using pthread_create(). thread X then creates thread Y using pthread_create(). thread X then completes it's operations and returns thus terminating the thread X. question: What happens to thread Y? I need to have thread Y continue to run after thread X has terminated. I haven't coded this yet, just need to know in advance if there is anything i need to do. My application is already threaded using pthreads, this is a modification needed to handle some extra functionaity. I'm using codwarrior 8.3 on mac osX (10.3) and pth...

Thread within a thread
Is it possible to call a thread (pthread) from within a thread invocation ? If I correctly pthread_join() the new thread from the calling thread, does it make good design sense ? On May 14, 10:33=A0pm, Raseel <raseelbha...@gmail.com> wrote: > Is it possible to call a thread (pthread) from within a thread > invocation ? There is no other way to do it. If code is running, it's a thread that's running it. > If I correctly pthread_join() the new thread from the calling thread, > does it make good design sense ? You can call 'pthread_join' fro...

Sub thread and Thread ?
Hi, all: Can I build 2 sub threads in another thread? which means: main call thread A ; Thread A call thread A1; call Thread A2; ; Thanks for any comments. bin YE yezi wrote: > Hi, all: > > Can I build 2 sub threads in another thread? which means: If in the pseudo code below 'call' means 'create' then yes, most thread APIs allow threads to create threads. > main > > call thread A > > ; > > Thread A > call thread A1; > call Thread A2; > > ; > > Tha...

Bug in threading.Thread.join() ?
I'm still trying to understand the behaviour that I'm seeing but I'm already pretty sure that it's either a bug, or something that would be considered a bug if it didn't perhaps avoid even worse behaviour. Inside the join() method of threading.Thread objects, a Condition named self.__block is acquired, and then the wait logic is executed. After the wait() finishes, self.__block is released and the method returns. If you hit Ctrl-C while the join's wait() is occurring, you'll raise a KeyboardInterrupt and bypass the release() call. (I'm observing this on Win ...

Is this thread the event dispatching thread?
I believe having read in a newsgroup recently a special call to find out if it is executed within the event dispatching thread. I would know how to get and inspect the name of the current thread, but I believe this was a special purpose call for to check whether it is executed within the event dispatching thread. Does anyone know of such a method? SwingUtilities.isEventDispatchThread() Best regards Thomas "Stefan Ram" <ram@zedat.fu-berlin.de> schrieb im Newsbeitrag news:EDT-20050926140135@ram.dialup.fu-berlin.de... > I believe having read in a newsgroup r...