2 * Unix SMB/CIFS implementation.
3 * Performance Counter Daemon
5 * Copyright (C) Marcin Krzysztof Porwit 2005
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 void init_cpudata_desc(PERF_DATA_BLOCK
*data
)
25 init_perf_counter(&(data
->cpuInfo
.cpuObjDesc
),
26 &(data
->cpuInfo
.cpuObjDesc
),
29 "The Processor object consists of counters that describe the behavior of the CPU.",
32 init_perf_counter(&(data
->cpuInfo
.userCPU
),
33 &(data
->cpuInfo
.cpuObjDesc
),
35 "\% User CPU Utilization",
36 "\% User CPU Utilization is the percentage of the CPU used by processes executing user code.",
37 PERF_SIZE_LARGE
| PERF_TYPE_COUNTER
| PERF_COUNTER_RATE
| PERF_TIMER_100NS
| PERF_DELTA_COUNTER
| PERF_DISPLAY_PERCENT
,
39 init_perf_counter(&(data
->cpuInfo
.systemCPU
),
40 &(data
->cpuInfo
.cpuObjDesc
),
42 "\% System CPU Utilization",
43 "\% System CPU Utilization is the percentage of the CPU used by processes doing system calls.",
44 PERF_SIZE_LARGE
| PERF_TYPE_COUNTER
| PERF_COUNTER_RATE
| PERF_TIMER_100NS
| PERF_DELTA_COUNTER
| PERF_DISPLAY_PERCENT
,
46 init_perf_counter(&(data
->cpuInfo
.niceCPU
),
47 &(data
->cpuInfo
.cpuObjDesc
),
49 "\% Nice CPU Utilization",
50 "\% Nice CPU Utilization is the percentage of the CPU used by processes running in nice mode.",
51 PERF_SIZE_LARGE
| PERF_TYPE_COUNTER
| PERF_COUNTER_RATE
| PERF_TIMER_100NS
| PERF_DELTA_COUNTER
| PERF_DISPLAY_NOSHOW
,
53 init_perf_counter(&(data
->cpuInfo
.idleCPU
),
54 &(data
->cpuInfo
.cpuObjDesc
),
57 "\% Idle CPU is the percentage of the CPU not doing any work.",
58 PERF_SIZE_LARGE
| PERF_TYPE_COUNTER
| PERF_COUNTER_RATE
| PERF_TIMER_100NS
| PERF_DELTA_COUNTER
| PERF_DISPLAY_NOSHOW
,
64 void get_cpuinfo(PERF_DATA_BLOCK
*data
)
69 static FILE *fp
= NULL
;
73 if(!(fp
= fopen("/proc/stat", "r")))
75 perror("get_cpuinfo: fopen");
83 /* Read in the first line and discard it -- that has the CPU summary */
84 if(!fgets(buf
, sizeof(buf
), fp
))
86 perror("get_cpuinfo: fgets");
89 for(i
= 0; i
< data
->cpuInfo
.numCPUs
; i
++)
91 if(!fgets(buf
, sizeof(buf
), fp
))
93 perror("get_cpuinfo: fgets");
96 num
= sscanf(buf
, "cpu%u %Lu %Lu %Lu %Lu",
98 &data
->cpuInfo
.data
[i
].user
,
99 &data
->cpuInfo
.data
[i
].nice
,
100 &data
->cpuInfo
.data
[i
].system
,
101 &data
->cpuInfo
.data
[i
].idle
);
104 perror("get_cpuinfo: /proc/stat inconsistent?");
108 Alternate way of doing things:
110 data->PerfTime100nSec = times(&buffer);
112 data
->PerfTime100nSec
+= data
->cpuInfo
.data
[i
].user
+
113 data
->cpuInfo
.data
[i
].nice
+
114 data
->cpuInfo
.data
[i
].system
+
115 data
->cpuInfo
.data
[i
].idle
;
117 data
->PerfTime100nSec
/= data
->cpuInfo
.numCPUs
;
121 void init_cpu_data(PERF_DATA_BLOCK
*data
)
123 data
->cpuInfo
.data
= calloc(data
->cpuInfo
.numCPUs
, sizeof(*data
->cpuInfo
.data
));
124 if(!data
->cpuInfo
.data
)
126 perror("init_cpu_data: out of memory");
130 init_cpudata_desc(data
);
137 void output_cpu_desc(PERF_DATA_BLOCK
*data
, RuntimeSettings rt
)
139 output_perf_desc(data
->cpuInfo
.cpuObjDesc
, rt
);
140 output_perf_desc(data
->cpuInfo
.userCPU
, rt
);
141 output_perf_desc(data
->cpuInfo
.niceCPU
, rt
);
142 output_perf_desc(data
->cpuInfo
.systemCPU
, rt
);
143 output_perf_desc(data
->cpuInfo
.idleCPU
, rt
);
144 if(data
->cpuInfo
.numCPUs
> 1)
145 output_num_instances(data
->cpuInfo
.cpuObjDesc
, data
->cpuInfo
.numCPUs
+ 1, rt
);
150 void output_cpuinfo(PERF_DATA_BLOCK
*data
, RuntimeSettings rt
, int tdb_flags
)
155 output_perf_counter(data
->cpuInfo
.userCPU
,
156 data
->cpuInfo
.data
[0].user
,
158 output_perf_counter(data
->cpuInfo
.systemCPU
,
159 data
->cpuInfo
.data
[0].system
,
161 output_perf_counter(data
->cpuInfo
.niceCPU
,
162 data
->cpuInfo
.data
[0].nice
,
164 output_perf_counter(data
->cpuInfo
.idleCPU
,
165 data
->cpuInfo
.data
[0].idle
,
167 if(data
->cpuInfo
.numCPUs
> 1)
169 for(i
= 0; i
< data
->cpuInfo
.numCPUs
; i
++)
171 memset(buf
, 0, NAME_LEN
);
172 sprintf(buf
, "cpu%d", i
);
173 output_perf_instance(data
->cpuInfo
.cpuObjDesc
.index
,
175 (void *)&(data
->cpuInfo
.data
[i
]),
176 sizeof(data
->cpuInfo
.data
[i
]),
180 memset(buf
, 0, NAME_LEN
);
181 sprintf(buf
, "_Total");
182 output_perf_instance(data
->cpuInfo
.cpuObjDesc
.index
,
184 (void *)&(data
->cpuInfo
.data
[i
]),
185 sizeof(data
->cpuInfo
.data
[i
]),