1 #include "ace/Monitor_Control/CPU_Load_Monitor.h"
3 #if defined (ACE_HAS_MONITOR_FRAMEWORK) && (ACE_HAS_MONITOR_FRAMEWORK == 1)
5 #if defined (ACE_LINUX)
6 #include "ace/OS_NS_stdio.h"
9 #include "ace/Log_Msg.h"
10 #include "ace/OS_NS_string.h"
12 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
16 namespace Monitor_Control
18 const char* CPU_Load_Monitor::default_name_
=
19 "OS/Processor/CPULoad";
21 CPU_Load_Monitor::CPU_Load_Monitor (const char* name
)
22 : Monitor_Base (name
, Monitor_Control_Types::MC_NUMBER
)
23 #if defined (ACE_HAS_WIN32_PDH)
24 , Windows_Monitor (ACE_TEXT("\\Processor(_Total)\\% Processor Time"))
26 #if defined (ACE_LINUX)
34 #if defined (ACE_LINUX)
42 CPU_Load_Monitor::update ()
44 #if defined (ACE_HAS_WIN32_PDH)
46 this->receive (this->value_
);
47 #elif defined (ACE_LINUX)
48 this->access_proc_stat (&this->idle_
);
49 #elif defined (ACE_HAS_KSTAT)
50 this->access_kstats (&this->idle_
);
53 #if defined (ACE_LINUX)
54 double delta_idle
= this->idle_
- this->prev_idle_
;
56 this->user_
+ this->wait_
+ this->kernel_
+ this->idle_
;
57 double delta_total
= total
- this->prev_total_
;
59 if (ACE::is_equal (delta_total
, 0.0))
61 /// The system hasn't updated /proc/stat since the last call
62 /// to update(), we must avoid dividing by 0.
66 double percent_cpu_load
= 100.0 - (delta_idle
/ delta_total
* 100.0);
68 /// Stores value and timestamp with thread-safety.
69 this->receive (percent_cpu_load
);
71 this->prev_idle_
= this->idle_
;
72 this->prev_total_
= total
;
77 CPU_Load_Monitor::default_name ()
79 return CPU_Load_Monitor::default_name_
;
83 CPU_Load_Monitor::clear_i ()
85 #if defined (ACE_HAS_WIN32_PDH)
90 this->Monitor_Base::clear_i ();
94 CPU_Load_Monitor::init ()
96 #if defined (ACE_LINUX)
97 /// All data in this file are stored as running 'jiffy' totals, so we
98 /// get values here in the constructor to subtract for the difference
99 /// in subsequent calls.
100 this->access_proc_stat (&this->prev_idle_
);
103 this->user_
+ this->wait_
+ this->kernel_
+ this->prev_idle_
;
107 #if defined (ACE_LINUX)
109 CPU_Load_Monitor::access_proc_stat (unsigned long *which_idle
)
111 this->file_ptr_
= ACE_OS::fopen (ACE_TEXT ("/proc/stat"),
114 if (this->file_ptr_
== 0)
116 ACELIB_ERROR ((LM_ERROR
,
117 ACE_TEXT ("CPU load - opening /proc/stat failed\n")));
124 while ((ACE_OS::fgets (buf_
, sizeof (buf_
), file_ptr_
)) != 0)
126 item
= ACE_OS::strtok (this->buf_
, " \t\n");
127 arg
= ACE_OS::strtok (0, "\n");
129 if (item
== 0 || arg
== 0)
134 if (ACE_OS::strcmp (item
, "cpu") == 0)
146 ACE_OS::fclose (this->file_ptr_
);
152 ACE_END_VERSIONED_NAMESPACE_DECL
154 #endif /* ACE_HAS_MONITOR_FRAMEWORK==1 */