Cleanup Solaris support
[ACE_TAO.git] / ACE / ace / Monitor_Control / CPU_Load_Monitor.cpp
blobaf3bfe97163094cd7dc0c54d7c0af71c9aaded5d
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"
7 #endif
9 #include "ace/Log_Msg.h"
10 #include "ace/OS_NS_string.h"
12 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
14 namespace ACE
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"))
25 #endif
26 #if defined (ACE_LINUX)
27 , user_ (0)
28 , wait_ (0)
29 , kernel_ (0)
30 , idle_ (0)
31 , prev_idle_ (0)
32 , prev_total_ (0.0)
33 #endif
34 #if defined (ACE_LINUX)
35 , file_ptr_ (0)
36 #endif
38 this->init ();
41 void
42 CPU_Load_Monitor::update ()
44 #if defined (ACE_HAS_WIN32_PDH)
45 this->update_i ();
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_);
51 #endif
53 #if defined (ACE_LINUX)
54 double delta_idle = this->idle_ - this->prev_idle_;
55 double total =
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.
63 return;
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;
73 #endif
76 const char*
77 CPU_Load_Monitor::default_name ()
79 return CPU_Load_Monitor::default_name_;
82 void
83 CPU_Load_Monitor::clear_i ()
85 #if defined (ACE_HAS_WIN32_PDH)
86 this->clear_impl ();
87 #endif
89 this->init ();
90 this->Monitor_Base::clear_i ();
93 void
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_);
102 this->prev_total_ =
103 this->user_ + this->wait_ + this->kernel_ + this->prev_idle_;
104 #endif
107 #if defined (ACE_LINUX)
108 void
109 CPU_Load_Monitor::access_proc_stat (unsigned long *which_idle)
111 this->file_ptr_ = ACE_OS::fopen (ACE_TEXT ("/proc/stat"),
112 ACE_TEXT ("r"));
114 if (this->file_ptr_ == 0)
116 ACELIB_ERROR ((LM_ERROR,
117 ACE_TEXT ("CPU load - opening /proc/stat failed\n")));
118 return;
121 char *item = 0;
122 char *arg = 0;
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)
131 continue;
134 if (ACE_OS::strcmp (item, "cpu") == 0)
136 sscanf (arg,
137 "%lu %lu %lu %lu",
138 &this->user_,
139 &this->wait_,
140 &this->kernel_,
141 which_idle);
142 break;
146 ACE_OS::fclose (this->file_ptr_);
148 #endif
152 ACE_END_VERSIONED_NAMESPACE_DECL
154 #endif /* ACE_HAS_MONITOR_FRAMEWORK==1 */