42 #include <sys/types.h>
62 unsigned long long last_user_time;
63 unsigned long long last_system_time;
64 unsigned long long last_idle_time;
66 unsigned long long last_nice_time;
67 unsigned long long last_irq_time;
68 unsigned long long last_soft_irq_time;
69 unsigned long long last_io_wait_time;
70 unsigned long long last_steal_time;
75 #elif defined (WIN32) || defined (WIN64)
76 __int64 i64LastUserTime;
77 __int64 i64LastKernelTime;
78 __int64 i64LastIdleTime;
86 unsigned long long *user,
87 unsigned long long *nice,
88 unsigned long long *system,
89 unsigned long long *idle,
90 unsigned long long *iowait,
91 unsigned long long *irq,
92 unsigned long long *softirq,
93 unsigned long long *steal)
98 #define CPU_ELEMENTS_1 7 // change this if you change the format string
99 #define CPU_INFO_FORMAT_1 "cpu %llu %llu %llu %llu %llu %llu %llu"
101 #define CPU_ELEMENTS_2 8 // change this if you change the format string
102 #define CPU_INFO_FORMAT_2 "cpu %llu %llu %llu %llu %llu %llu %llu %llu"
104 #define CPU_ELEMENTS_3 9 // change this if you change the format string
105 #define CPU_INFO_FORMAT_3 "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu"
106 static const char procfile[] =
"/proc/stat";
110 const char *cpustr = NULL;
112 unsigned long long guest = 0;
115 p->procfd = open(procfile, O_RDONLY, 0);
116 if(p->procfd == -1) {
122 lseek(p->procfd, 0L, SEEK_SET);
125 rc = read(p->procfd, statbuff,
sizeof(statbuff) - 1);
134 cpustr = strstr(statbuff,
"cpu ");
141 elements = sscanf(cpustr, CPU_INFO_FORMAT_3, user, nice, system, idle, iowait, irq, softirq, steal, &guest);
142 if (elements == CPU_ELEMENTS_3) {
147 elements = sscanf(cpustr, CPU_INFO_FORMAT_2, user, nice, system, idle, iowait, irq, softirq, steal);
148 if (elements == CPU_ELEMENTS_2) {
152 elements = sscanf(cpustr, CPU_INFO_FORMAT_1, user, nice, system, idle, iowait, irq, softirq);
153 if (elements == CPU_ELEMENTS_1) {
164 unsigned long long user, nice, system, idle, iowait, irq, softirq, steal;
165 unsigned long long usertime, kerneltime, idletime, totaltime, halftime;
168 *idle_percentage = 100.0;
173 if (read_cpu_stats(p, &user, &nice, &system, &idle, &iowait, &irq, &softirq, &steal)) {
179 if (!p->valid_last_times) {
181 p->valid_last_times = 1;
182 p->last_user_time = user;
183 p->last_nice_time = nice;
184 p->last_system_time = system;
185 p->last_irq_time = irq;
186 p->last_soft_irq_time = softirq;
187 p->last_io_wait_time = iowait;
188 p->last_steal_time = steal;
189 p->last_idle_time = idle;
190 p->last_percentage_of_idle_time = 100.0;
191 *idle_percentage = p->last_percentage_of_idle_time;
195 usertime = (user - p->last_user_time) + (nice - p->last_nice_time);
196 kerneltime = (system - p->last_system_time) + (irq - p->last_irq_time) + (softirq - p->last_soft_irq_time);
197 kerneltime += (iowait - p->last_io_wait_time);
198 kerneltime += (steal - p->last_steal_time);
199 idletime = (idle - p->last_idle_time);
201 totaltime = usertime + kerneltime + idletime;
203 if (totaltime <= 0) {
208 *idle_percentage = p->last_percentage_of_idle_time;
212 halftime = totaltime / 2UL;
214 p->last_idle_time_index += 1;
215 if ( p->last_idle_time_index >= p->cpu_idle_smoothing_depth ) {
216 p->last_idle_time_index = 0;
218 p->percentage_of_idle_time_ring[p->last_idle_time_index] = ((100 * idletime + halftime) / totaltime);
220 p->last_percentage_of_idle_time = 0;
221 for ( x = 0; x < p->cpu_idle_smoothing_depth; x++ ) {
223 p->last_percentage_of_idle_time += p->percentage_of_idle_time_ring[x];
225 p->last_percentage_of_idle_time /= p->cpu_idle_smoothing_depth;
227 *idle_percentage = p->last_percentage_of_idle_time;
230 p->last_user_time = user;
231 p->last_nice_time = nice;
232 p->last_system_time = system;
233 p->last_irq_time = irq;
234 p->last_soft_irq_time = softirq;
235 p->last_io_wait_time = iowait;
236 p->last_steal_time = steal;
237 p->last_idle_time = idle;
242 #elif defined (WIN32) || defined (WIN64)
249 __int64 i64UserTime, i64KernelTime, i64IdleTime;
251 if (!GetSystemTimes(&idleTime, &kernelTime, &userTime)) {
255 i64UserTime = (__int64)userTime.dwLowDateTime | ((__int64)userTime.dwHighDateTime << 32);
257 i64KernelTime = (__int64)kernelTime.dwLowDateTime | ((__int64)kernelTime.dwHighDateTime << 32);
259 i64IdleTime = (__int64)idleTime.dwLowDateTime | ((__int64)idleTime.dwHighDateTime << 32);
261 if (p->valid_last_times) {
262 __int64 i64User = i64UserTime - p->i64LastUserTime;
263 __int64 i64Kernel = i64KernelTime - p->i64LastKernelTime;
264 __int64 i64Idle = i64IdleTime - p->i64LastIdleTime;
265 __int64 i64System = i64User + i64Kernel;
268 p->last_idle_time_index += 1;
269 if ( p->last_idle_time_index >= p->cpu_idle_smoothing_depth ) {
270 p->last_idle_time_index = 0;
272 p->percentage_of_idle_time_ring[p->last_idle_time_index] = 100.0 * i64Idle / i64System;
274 *idle_percentage = 0;
275 for (x = 0; x < p->cpu_idle_smoothing_depth; x++ ) {
276 *idle_percentage += p->percentage_of_idle_time_ring[x];
278 *idle_percentage /= p->cpu_idle_smoothing_depth;
280 *idle_percentage = 100.0;
281 p->valid_last_times = 1;
285 p->i64LastUserTime = i64UserTime;
286 p->i64LastKernelTime = i64KernelTime;
287 p->i64LastIdleTime = i64IdleTime;
298 *idle_percentage = 100.0;
331 free((*p)->percentage_of_idle_time_ring);
#define SWITCH_CHANNEL_LOG
struct switch_runtime runtime
switch_profile_timer_t * switch_new_profile_timer(void)
create a new profile timer
void switch_delete_profile_timer(switch_profile_timer_t **p)
Deletes profile timer.
unsigned int cpu_idle_smoothing_depth
double * percentage_of_idle_time_ring
double last_percentage_of_idle_time
unsigned int last_idle_time_index
switch_bool_t switch_get_system_idle_time(switch_profile_timer_t *p, double *idle_percentage)
provides the percentage of idle system time
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
uint32_t cpu_idle_smoothing_depth