This repository was archived by the owner on Mar 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcputime.c
61 lines (54 loc) · 1.72 KB
/
cputime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* Run the command and measure the user CPU time it consumes.
*
* Outputs to stderr one line with the integer number of milliseconds of user
* CPU time consumed by the program it launches.
*
* Author: Samuel Coleman <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stderr, "Usage: %s command [args]\n", argv[0]);
return EXIT_FAILURE;
}
/* Declare all variables involved in timing ahead of time. Because hey,
* creating some stack variables is totally going to affect time, right? */
int status;
struct rusage start_usage;
struct rusage end_usage;
struct timeval consumed_time;
pid_t child = fork();
if (child < 0)
{
fprintf(stderr, "Failed to fork.");
return EXIT_FAILURE;
}
else if (child == 0)
{
execvp(argv[1], &argv[1]);
fprintf(stderr, "Could not execute %s.\n", argv[1]);
return EXIT_FAILURE;
}
/* Assume that it takes essentially zero time to resume execution in the
* parent process after forking. We don't want to start measuring before the
* fork because that might minisculely penalize the child for the time taken
* to fork. */
getrusage(RUSAGE_CHILDREN, &start_usage);
waitpid(child, &status, 0);
getrusage(RUSAGE_CHILDREN, &end_usage);
timersub(&end_usage.ru_utime, &start_usage.ru_utime, &consumed_time);
fprintf(stderr, "%ld\n",
(consumed_time.tv_sec * 1000000 + consumed_time.tv_usec) / 1000);
if (WIFEXITED(status))
return WEXITSTATUS(status);
else
return EXIT_FAILURE;
}