views:

71

answers:

2

I am writing small debugging program for multithread apps. My idea is to run the target, that is being debuged for for example 100 nanosecs, then pause and examine its memory. However this ( just pseudocode )

nanosleep(100);             //sleep debuger for 100 nanosec and let a program run
kill(target_app_pid, SIGSTOP); //stop target app

wouldn't work, because process-switch could happen right after nanosleep and target would run longer as required. Is there any way in to give a process "defined" time slice and then suspend it? Only solution that I can imagine is add system calls and fix the scheduler to achieve what I require but this assumes great effort and many bugs. Perhaps setting "real-time" priority for debuger process can help? Would it be clean solution?

Assume too, that I can insrument source code of target app. Is it possible to set up some kind of timer and sleep the whole process after some (very small and precise) period of time?

+1  A: 

Do you really think you can nanosleep for 100 nanoseconds? What's the HZ value in your kernel? The closest you get to this order of sleep is udelay(), but that's in the kernel where you can disable preemption, not in user-land. Check out linux/include/delay.h and init/calibrate.c in the Linux kernel source.

Nikolai N Fetissov
nanosleep(100) was just example. In general I need something like time_execute(target_pid, time_to_execute)
iddqd
The amount of time is a very important consideration here, though. Can you give us a real time value to consider?
jheddings
The problem with your approach is that you want 'no longer' delay, while Linux will only allow you 'no shorter then X' delays with some lower bound on X (typically 10 microseconds.) The amount of time by how much you overrun the delay is up to the scheduler. You can look into "real-time" POSIX signals/timers - there you get a clear indication how many timer pulses you have missed.
Nikolai N Fetissov
Due to the fact that I can instrument target source code or binary to make it "slow" a real time value can be of the order of microseconds
iddqd
+1  A: 

Since linux is at best a "soft real time" OS (especially working in user space), it will be difficult to get the type of precision you are after.

If you are working on an embedded processor, the nanosleep and microsleep calls are often implemented as a spin loop. During boot, the processor is characterized to "calibrate" this spin loop. Still, however, getting this type of precision requires a very detailed understanding of the processor architecture and what is being executed (at the instruction level).

Newer versions of the linux kernel do have some high-speed timer implementations, but you are still looking at microsecond resolution.

If you are running on a desktop system, I have no idea why you need this level of precision.

jheddings