0

我正在尝试使用 sys/times.h 中包含的时间函数来测量 sys、usr 和实时。

但是每当我尝试实时检索时,我得到的结果为 0。

根据文档:

times() 返回的数据只有在启用时间计费时才有效。这是默认设置,但在使用 buildqnx 实用程序构建操作系统映像时,可能会禁用它以节省时间和内存。禁用时,struct tms 成员将始终为零。

我找不到任何方法来检查它是否在我的 Ubuntu 上被禁用。在程序中的某些位置获取时间值的任何替代方法?

谢谢!

4

2 回答 2

1

您可以通过多次访问手册页来找到您需要的内容,特别是使用 3 作为 C/API 文档:

man 3 times

此外,您还可以在那里找到示例代码。

于 2015-03-14T23:01:25.920 回答
1

您无法确定您使用的是哪个版本的 Ubuntu,但此代码(使用选项-std=c11或编译-std=c99)表明,times()当在 Mac OS X 10.10.2 Yosemite 上的 VM 中运行时,该代码在 Ubuntu 14.04 LTS 上运行良好。

文件ubuntu.times.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/times.h>
#include <sys/wait.h>
#include <unistd.h>

#define PRI_clock_t "lu"    /* Correct on Mac OS X */

static void report_times(void)
{
    struct tms t;
    clock_t t0 = times(&t);
    printf("%" PRI_clock_t ": %" PRI_clock_t " %" PRI_clock_t "; %" PRI_clock_t " %" PRI_clock_t "\n",
           t0, t.tms_utime, t.tms_stime, t.tms_cutime, t.tms_cstime);
}

static char *cmd0[] = { "sleep", "1", (char *)0 };
static char *cmd1[] = { "dd", "if=/dev/zero", "of=/dev/null", "bs=1024", "count=1000000", (char *)0 };

static void exec_cmd(char **args)
{
    for (int i = 0; i < 10; i++)
    {
        if (fork() == 0)
        {
            execvp(args[0], args);
            exit(1);
        }
        int status;
        int corpse = wait(&status);
        printf("PID %d: 0x%.4X\n", corpse, status);
    }
    report_times();
}

int main(void)
{
    report_times();
    FILE *fp = fopen("/dev/null", "w");
    for (int i = 0; i < 1000000; i++)
    {
        fprintf(fp, "Row %d\n", i);
    }
    report_times();

    exec_cmd(cmd0);
    exec_cmd(cmd1);

    return 0;
}

Ubuntu 上的示例输出:

$ ./ubuntu.times
1718230233: 0 0; 0 0
1718230242: 7 0; 0 0
PID 67448: 0x0000
PID 67449: 0x0000
PID 67450: 0x0000
PID 67451: 0x0000
PID 67452: 0x0000
PID 67453: 0x0000
PID 67455: 0x0000
PID 67456: 0x0000
PID 67457: 0x0000
PID 67458: 0x0000
1718231249: 8 0; 0 1
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.295776 s, 3.5 GB/s
PID 67459: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.291788 s, 3.5 GB/s
PID 67460: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.303734 s, 3.4 GB/s
PID 67461: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.289385 s, 3.5 GB/s
PID 67462: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.292731 s, 3.5 GB/s
PID 67463: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.290734 s, 3.5 GB/s
PID 67464: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.292078 s, 3.5 GB/s
PID 67465: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.289427 s, 3.5 GB/s
PID 67466: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.29415 s, 3.5 GB/s
PID 67467: 0x0000
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB) copied, 0.295052 s, 3.5 GB/s
PID 67468: 0x0000
1718231548: 8 0; 56 234
$

代码中的错误处理很少甚至不存在;它不应该被视为好的风格。

于 2015-03-14T23:01:42.220 回答