-
Notifications
You must be signed in to change notification settings - Fork 17
/
getcpu.c
133 lines (103 loc) · 3.55 KB
/
getcpu.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* ------- file: -------------------------- gettime.c ---------------
Version: rh2.0
Author: Han Uitenbroek ([email protected])
Last modified: Thu Nov 1 12:51:38 2007 --
-------------------------- ----------RH-- */
/* --- Prints elapsed time in seconds: action == TIME_POLL.
Clock is started if called with: action == TIME_START.
Borrowed heavily from MULTI's ctime().
Works on: SUN sparc (SunOs 4.x & 5.x)
SGI mips (IRIX 5.2)
DEC alpha (OSF1 3.2 & 4.0)
i386 (Linux 1.x)
i586 (Linux 2.0)
-- -------------- */
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/times.h>
#include <unistd.h>
#if defined(SunOS5)
#include <sys/time.h>
#if defined(_POSIX_C_SOURCE)
hrtime_t gethrtime(void);
#endif
#define NANOSECOND 1.0E-9
#endif
#include "rh.h"
#include "statistics.h"
#include "error.h"
#include "inputs.h"
#define TAB_POSITION 24
#define DOTS "...................................."
#define SPACES " "
#define TIME_DOT_OUT "time.out"
/* --- Function prototypes -- -------------- */
/* --- Global variables -- -------------- */
extern ProgramStats stats;
extern CommandLine commandline;
extern char messageStr[];
/* ------- begin -------------------------- getTime.c --------------- */
void getCPU(int level, enum CPUaction action, char *label)
{
register int n;
int Nblanck, Nspace, Ndot;
#if defined(SunOS5)
static hrtime_t CPU[N_TIME_LEVELS];
static double scale = NANOSECOND;
hrtime_t CPUtime;
#else
static clock_t CPU[N_TIME_LEVELS];
static double scale = 1.0 / CLOCKS_PER_SEC;
clock_t CPUtime;
#endif
if (level == 0 && action == TIME_START && !commandline.showkeywords) {
stats.fp_CPU = fopen(TIME_DOT_OUT, "w");
setvbuf(stats.fp_CPU, NULL, _IOLBF, BUFSIZ);
}
if (!stats.printCPU || !stats.fp_CPU)
return;
else {
#if defined(SunOS5)
/* --- Use high-resolution time on SUN Solaris machines -- ------ */
CPUtime = gethrtime();
#else
if ((CPUtime = clock()) == ((clock_t) -1)) {
sprintf(messageStr, "Cannot poll resources");
Error(ERROR_LEVEL_1, "getCPU", messageStr);
}
#endif
switch (action) {
case TIME_START:
CPU[level] = CPUtime;
return;
case TIME_POLL:
CPUtime = CPUtime - CPU[level];
break;
case TIME_ADD:
break;
}
}
/* --- Format output -- -------------- */
Nspace = (level < 4) ? level + 1 : 4;
Ndot = TAB_POSITION - Nspace - strlen(label);
Nblanck = (level > 2) ? 0 : 3 - level;
fprintf(stats.fp_CPU, "%.*s%.*s%s: %7.3f [sec]\n", Nspace, SPACES,
Ndot, DOTS, label, CPUtime * scale);
for (n = 0; n < Nblanck; n++) fprintf(stats.fp_CPU, "\n");
}
/* ------- end ---------------------------- getTime.c --------------- */
/* ------- begin -------------------------- printTotalCPU ----------- */
void printTotalCPU()
{
double user, system, total;
struct tms tms;
times(&tms);
user = ((double) tms.tms_utime) / sysconf(_SC_CLK_TCK);
system = ((double) tms.tms_stime) / sysconf(_SC_CLK_TCK);
total = user + system;
fprintf(stats.fp_CPU, "\nTotal_time____: %8.3f [sec] ", total);
fprintf(stats.fp_CPU, " (user: %4.1f%%, system: %4.1f%%)\n\n",
100*user/total, 100*system/total);
}
/* ------- end ---------------------------- printTotalCPU ----------- */