LINUX:ITIMER_PROF的分辨率/粒度是多少?

时间:2014-04-03 20:52:44

标签: linux ubuntu timer system-calls

问题:

LINUX配置文件计时器的分辨率是多少?显然,这是系统特定的,因此我将在下面提供更多详细信息。

背景

我正在尝试使用Google GPerfTools套件,而且我发现无论我怎么努力,我都无法获得超过200个样本/秒的CPU时间(不是挂钟时间,甚至更低) ),即使GPerfTools中允许的最大值为4000 /秒。

Please see this (unloved) question: /questions/22799490

在查看GPerfTools代码时,我还发现它正在使用ITIMER_PROF(毫不奇怪),并使用setitimer(2)系统调用时使用正确的timestruct值(duh!):

void ProfileHandler::StartTimer() {
  if (!allowed_) {
    return;
  }
  struct itimerval timer;
  timer.it_interval.tv_sec = 0;
  timer.it_interval.tv_usec = 1000000 / frequency_;
  timer.it_value = timer.it_interval;
  setitimer(timer_type_, &timer, 0);
}

因此,对采样率的限制要么是系统ITIMER_PROF限制,要么是GPerfTools与我正在分析的程序交互的副产品(当我们在I / O上阻塞时,计时器可能会关闭) ?当发生这种情况时...也许这不算作样本?:))

系统细节:

我在运行Ubuntu Linux的标准x86_64机器上运行。这是我的uname结果: Linux <hostname> 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 17:37:58 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

程序和GPerfTools库以32位模式编译。实际上,GPerfTools构建中的configure步骤是在32位chroot'd jail setarch'd到i386中完成的。

1 个答案:

答案 0 :(得分:0)

在不同的Ubuntu x86 LINUX盒(包括x86 VM)上重复测试都会确认250 /秒,或实际使用CPU时间隔为4毫秒。在这个时代,这实际上是否正确?测试程序包括在下面

示例结果:

./itimer 200 3
freq = 200
timeout = 3
When over, you should see 599 firings
PERF timer fired 599 times
VIRTUAL timer fired 599 times

./itimer 500 3
freq = 500
timeout = 3
When over, you should see 1499 firings
PERF timer fired 749 times
VIRTUAL timer fired 749 times

测试计划

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>

struct itimerval mrVal;
struct itimerval timeout;

int ncalls = 0;
int nvcalls = 0;
void killer(int signum)
{
    // Off by a few because we don't stop the timers,
    // but good enough for testing purposes
    printf("PERF timer fired %d times\n", ncalls);
    printf("VIRTUAL timer fired %d times\n", nvcalls);
    exit(0);
}

void interval_handler(int signum)
{
    ncalls += 1;
}

void virtual_handler(int signum)
{
    nvcalls += 1;
}

static double keeper = 12.345;

int main(int argc, char **argv)
{
    if (argc < 3) {
        printf("Usage: itimer freq seconds-to-run\n");
        exit(1);
    }
    int freq = atoi(argv[1]);
    int sleepsex = atoi(argv[2]);
    printf("freq = %d\n", freq);
    printf("timeout = %d\n", sleepsex);
    printf("When over, you should see %d firings\n",
                    (freq * sleepsex) - 1);

    timeout.it_interval.tv_sec = sleepsex;
    timeout.it_interval.tv_usec = 0;
    timeout.it_value = timeout.it_interval;

    mrVal.it_interval.tv_sec = 0;
    mrVal.it_interval.tv_usec = 1000000 / freq;
    mrVal.it_value = mrVal.it_interval;
    if(signal(SIGPROF, interval_handler) == SIG_ERR) {
        printf("HOSER!\n");
    }
    if(signal(SIGVTALRM, virtual_handler) == SIG_ERR) {
        printf("HOSER!\n");
    }
    if(signal(SIGALRM, killer) == SIG_ERR) {
        printf("HOSER!\n");
    }
    if(signal(SIGINT, killer) == SIG_ERR) {
        printf("HOSER!\n");
    }
    setitimer(ITIMER_PROF, &mrVal, 0);
    setitimer(ITIMER_VIRTUAL, &mrVal, 0);
#if 1
    // Use CPU. Otherwise, the PERF timer doesn't go off
    // Trivial floating point stuff that counts good enough
    // as work
    setitimer(ITIMER_REAL, &timeout, 0);
    while(1) {
        double d = keeper;
        d/=1.3773;
        d*=1.3777;
        keeper = d;
    }
#else
    sleep(sleepsex);
    killer(0);
#endif
}