首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我在linux中的系统调用时间几乎为0?

为什么我在linux中的系统调用时间几乎为0?
EN

Stack Overflow用户
提问于 2019-05-29 19:19:42
回答 1查看 74关注 0票数 0

现在,我正在学习一门系统内核实践课程。然而,当我将系统调用与用户调用进行比较时,奇怪的是系统调用返回的时间计数为0us(有时返回1)。但是我通过了count=1e8,这是一个相当大的数字。

我怀疑计算没有发生是因为结果没有被使用。然后我更改add as result = result +1并打印最终结果。然而,结果是正确的,时间计数从0或1变为2-6。

代码语言:javascript
复制
long yanpan_oper(int* result,int num1,int num2,char* op)
{
    if(op)
    {
        if(*op == '+')
        {
            *result = num1 + num2;
        }
        else if(*op == '-')
        {
            *result = num1 - num2;
        }
        else if(*op == '*')
        {
            *result = num1*num2;
        }
        else if(*op == '\\')
        {
            if(num2!=0)
                *result = num1/num2;
            else
                printk("divided number can't be zero!\n");
        }else
            printk("unrecongized operator %c\n", *op);
    }else
    {
        printk("operation is empty.\n");
    }
    return 0;
}
SYSCALL_DEFINE1(yanpan_func, int, count)
{
    printk("The count is %d.\n", count);
    struct timeval tstart, tend;
    do_gettimeofday(&tstart);
    int i;
    for(i=0;i<count;i++) // +
    {
        int result;
        char op_add = '+';
        yanpan_oper(&result, i, 10, &op_add);
    }
    for(i=0;i<count;i++) // -
    {
        int result;
        char op_sub = '-';
        yanpan_oper(&result, i, 10, &op_sub);
    }
    for(i=0;i<count;i++) // *
    {
        int result;
        char op_mul = '*';
        yanpan_oper(&result, i, 2, &op_mul);
    }
    for(i=0;i<count;i++) // '//'
    {
        int result;
        char op_div = '\\';
        yanpan_oper(&result, i, 10, &op_div);
    }
    do_gettimeofday(&tend);
    long delta_time = 1000000*(tend.tv_sec - tstart.tv_sec) + (tend.tv_usec - tstart.tv_usec);
    printk("The start time is %ld.\n", tstart.tv_sec*1000000+tstart.tv_usec);
    printk("The end time is %ld.\n", tend.tv_sec*1000000+tend.tv_usec);
    printk("Syscall time use:%ld usec", delta_time);
    return delta_time;
}

我试了很多次,但结果没有改变。相同数量的用户调用计算大约需要1300ms,在内核中的计算能像这样快吗?

EN

回答 1

Stack Overflow用户

发布于 2019-05-29 19:30:46

让我们看一个循环:

代码语言:javascript
复制
for(i=0;i<count;i++) // +
{
    int result;
    char op_add = '+';
    yanpan_oper(&result, i, 10, &op_add);
}

这将调用函数yanpan_oper count次。但每次它都会覆盖存储在result中的先前结果,而不使用该值进行计算。很可能编译器只是优化了整个循环,只用一次对yanpan_oper的调用来替换它,因为for循环实际上相当于只执行一次循环体。

此外,循环体只影响循环体内的变量,因此编译器不仅可以决定只保留最后一次迭代。它基本上可以跳过整个代码,所以你实际执行的是:

代码语言:javascript
复制
SYSCALL_DEFINE1(yanpan_func, int, count)
{
    printk("The count is %d.\n", count);
    struct timeval tstart, tend;
    do_gettimeofday(&tstart);
    do_gettimeofday(&tend);
    long delta_time = 1000000*(tend.tv_sec - tstart.tv_sec) + (tend.tv_usec - tstart.tv_usec);
    printk("The start time is %ld.\n", tstart.tv_sec*1000000+tstart.tv_usec);
    printk("The end time is %ld.\n", tend.tv_sec*1000000+tend.tv_usec);
    printk("Syscall time use:%ld usec", delta_time);
    return delta_time;
}

这里有一些关于如何欺骗优化器的提示:

代码语言:javascript
复制
// Create input that cannot be calculated at compile time
int input1[count];
int input2[count];
srand(time(NULL));
for(int i=0; i<count; i++) { 
    input1[i] = rand()%1000;
    input2[i] = rand()%1000;
}

// Store the output, so that the optimizer cannot take away the loop
int output[count];

// Start timer
for(i=0;i<count;i++) // +
{
    char op_add = '+';
    yanpan_oper(&output[i], input1[i], input2[i], &op_add);
}
// End timer

// Use the output to that the optimizer cannot remove the array, and thus
// also the loop
for(int i=0; i<count; i++) 
    printf("%d ", output[i]);

请注意,这些数组对于堆栈来说可能太大了。如果是这样的话,使用下面的代码:

代码语言:javascript
复制
int *input1 = malloc(count * sizeof(*input1));
int *input2 = malloc(count * sizeof(*input2));
srand(time(NULL));
for(int i=0; i<count; i++) { 
    input1[i] = rand()%1000;
    input2[i] = rand()%1000;
}

int *output = malloc(count * sizeof(*output));

(记得检查malloc是否成功,然后释放内存)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56359402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档