user3924850
user3924850

Reputation: 77

times in Ubuntu returns 0 everytime / how to

I'm trying to measure sys, usr and real time using times function included from sys/times.h.

But whenever I'm trying to retrieve real time, I'm getting 0.

According to this documentation:

The data returned by times() is valid only if time accounting is enabled. This is the default, but it may be disabled to save time and memory when an operating system image is built using the buildqnx utility. When disabled, the struct tms members will always be zero.

I couldn't find any way to check if it's disabled on my Ubuntu. Any alternative ways of getting time values during some spots in program?

Thanks!

Upvotes: 0

Views: 311

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 753525

You don't identify which version of Ubuntu you are using, but this code (compiled with option -std=c11 or -std=c99) shows that times() works OK on Ubuntu 14.04 LTS when run in a VM on Mac OS X 10.10.2 Yosemite.

File 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;
}

Sample output on 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
$

The error handling in the code is minimal to non-existent; it should not be regarded as good style.

Upvotes: 1

usr242
usr242

Reputation: 61

You'll find what you need by accessing the man page for times, specifically using 3 for the C/API documentation:

man 3 times

Additionally, you'll find example code there.

Upvotes: 1

Related Questions