简体   繁体   English

多任务处理和测量时差

[英]Multitasking and measuring time difference

I understand that a preemptive multitasking OS can interrupt a process at any "code position". 我知道抢占式多任务OS可以在任何“代码位置”中断进程。

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. 我希望这段代码通常会为timeDiff_usec产生适当的结果,尤其是在doSthElse()getTime_usec()不需要太多时间的情况下,因此它们很少被OS调度程序打断。

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 . 上下文切换将执行预期的操作,并且在这种情况下,程序仍然会为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). 这是我目前唯一想到的例子,但是我敢肯定,在其他情况下,多任务处理可能会使程序(mer)陷入困境(因为时间不是重新输入时唯一可能改变的状态)。

  • 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. 我想检查一下花费的时间,以确保doSthElse()不会花费大约50毫秒左右的时间,如果可以,那么我会寻找更好的解决方案。

  • 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. 例如,在Linux上,您可以使用sched_setscheduler赋予线程实时优先级。 (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.) (如果您真的很认真,还可以设置线程关联和SMP关联,以防止在运行线程的CPU上处理任何中断。)

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). 例如,POSIX定义了getrusage函数,该函数返回一个结构,其中包含ru_utime (进程在“用户模式”下花费的时间)和ru_stime (进程在“内核模式”下花费的时间)。 These should sum to the total time the CPU spent on the process, excluding intervals during which the process was suspended. 这些应该是CPU在进程上花费的总时间,不包括进程挂起的时间间隔。 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. 无论如何,衡量花费在某个关键动作上的时间的常见方法是在一个闲置的系统上重复计时(基本上是您的问题提出的方式),排除异常值,然后取平均值(消除异常值后),或者采用测量的中位数或第95个百分位数,具体取决于您为什么需要进行测量。

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. 有整本关于这个主题的书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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