简体   繁体   中英

Multitasking and measuring time difference

I understand that a preemptive multitasking OS can interrupt a process at any "code position".

Given the following code:

int main() {
  while( true ) {
    doSthImportant(); // needs to be executed at least each 20 msec
    // start of critical section
    int start_usec = getTime_usec();
    doSthElse();
    int timeDiff_usec = getTime_usec() - start_usec;
    // end of critical section
    evalUsedTime( timeDiff_usec );
    sleep_msec( 10 );
  }
}

I would expect this code to usually produce proper results for timeDiff_usec , especially in case that doSthElse() and getTime_usec() don't take much time so they get interrupted rarely by the OS scheduler.

But the program would get interrupted from time to time somewhere in the "critical section". The context switch will do what it is supposed to do, and still in such a case the program would produce wrong results for the timeDiff_usec .

This is the only example I have in mind right now but I'm sure there would be other scenarios where multitasking might get a program(mer) into trouble (as time is not the only state that might be changed at re-entry).

  • Is there a way to ensure that measuring the time for a certain action works fine?
  • Which other common issues are critical with multitasking and need to be considered? (I'm not thinking of thread safety - but there might be common issues).

Edit: I changed the sample code to make it more precise. I want to check the time being spent to make sure that doSthElse() doesn't take like 50 msec or so, and if it does I would look for a better solution.

  • Is there a way to ensure that measuring the time for a certain action works fine?

That depends on your operating system and your privilege level. On some systems, for some privilege levels, you can set a process or thread to have a priority that prevents it from being preempted by anything at lower priority. For example, on Linux, you might use sched_setscheduler to give a thread real-time priority. (If you're really serious, you can also set the thread affinity and SMP affinities to prevent any interrupts from being handled on the CPU that's running your thread.)

Your system may also provide time tracking that accounts for time spent preempted. For example, POSIX defines the getrusage function, which returns a struct containing ru_utime (the amount of time spent in “user mode” by the process) and ru_stime (the amount of time spent in “kernel mode” by the process). These should sum to the total time the CPU spent on the process, excluding intervals during which the process was suspended. Note that if the kernel needs to, for example, spend time paging on behalf of your process, it's not defined how much (if any) of that time is charged to your process.

Anyway, the common way to measure time spent on some critical action is to time it (essentially the way your question presents) repeatedly, on an otherwise idle system, throw out outlier measurements, and take the mean (after eliminating outliers), or take the median or 95th percentile of the measurements, depending on why you need the measurement.

Which other common issues are critical with multitasking and need to be considered? (I'm not thinking of thread safety - but there might be common issues).

Too broad. There are whole books written about this subject.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM