-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproc_time.c
92 lines (64 loc) · 1.86 KB
/
proc_time.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// Created by liushan on 18-11-18.
//
#include "proc_time.h"
#include "pr_exit.h"
#include <stdlib.h>
#include <stdio.h>
#include <sys/times.h>
#include <zconf.h>
static void pr_times(clock_t, struct tms *, struct tms *);
static void do_cmd(char *);
void run_proc_time(void)
{
int index;
char * cmd_all[] = {
"sleep 4",
"date",
"man tar > /dev/null"
};
setbuf(stdout, NULL);
for (index = 0; index < (sizeof(cmd_all) / sizeof(char *) ); index++)
{
do_cmd(cmd_all[index]);
}
}
static void do_cmd(char * cmd)
{
struct tms tms_start, tms_end;
clock_t start, end;
int status;
printf("command: %s\n", cmd);
if ( (start = times(&tms_start) ) < 0)
{
printf("times exec error\n");
exit(EXIT_FAILURE);
}
if ( (status = system(cmd) ) < 0)
{
printf("system exec cmd error cmd: %s\n", cmd);
exit(EXIT_FAILURE);
}
if ( (end = times(&tms_end) ) < 0)
{
printf("times exec error\n");
exit(EXIT_FAILURE);
}
pr_exit(status);
pr_times(end - start, &tms_start, &tms_end);
}
static void pr_times(clock_t real, struct tms * tms_start, struct tms * tms_end)
{
static long tick_sec = 0;
if (tick_sec == 0)
if ( (tick_sec = sysconf(_SC_CLK_TCK) ) < 0)
{
printf("sysconf get _SC_CLK_TCK error\n");
exit(EXIT_FAILURE);
}
printf("real: %.2f\n", real / (double) tick_sec);
printf("user: %.2f\n", (tms_end->tms_utime - tms_start->tms_utime) / (double) tick_sec);
printf("sys: %.2f\n", (tms_end->tms_stime - tms_start->tms_stime) / (double) tick_sec);
printf("child user: %.2f\n", (tms_end->tms_cutime - tms_start->tms_cutime) / (double) tick_sec);
printf("child sys: %.2f\n", (tms_end->tms_cstime - tms_start->tms_cstime) / (double) tick_sec);
}