Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Monitor / CPU_Load / cpu_load.cpp
blob4fe244f651db5e45210e5031172d10086f029231
1 #include "ace/OS_NS_unistd.h"
3 #include "ace/Monitor_Control/Monitor_Control.h"
5 #include "examples/Monitor/MC_Test_Utilities.h"
7 #if defined (ACE_HAS_MONITOR_FRAMEWORK) && (ACE_HAS_MONITOR_FRAMEWORK == 1)
9 /// Subclass of ACE_Task_Base, meaning that the override of
10 /// the svc() method below will run in a new thread when
11 /// activate() is called on a class instance.
12 class Monitor_Checker : public ACE_Task_Base
14 public:
15 int svc ()
17 /// Get an instance of the MC service singleton.
18 MC_ADMINMANAGER* mgr =
19 ACE_Dynamic_Service<MC_ADMINMANAGER>::instance ("MC_ADMINMANAGER");
21 /// Call on the administrator class to look up the desired monitors.
22 ACE::Monitor_Control::Monitor_Base *cpu_monitor =
23 mgr->admin ().monitor_point ("OS/Processor/CPULoad");
25 if (cpu_monitor != 0)
27 /// Query each monitor for its data every 2 seconds, and call the
28 /// appropriate display function.
29 for (int i = 0; i < 10; ++i)
31 ACE_OS::sleep (2);
33 Monitor_Control_Types::Data data (cpu_monitor->type ());
34 cpu_monitor->retrieve (data);
35 MC_Test_Utilities::display_cpu_load (data);
38 cpu_monitor->remove_ref ();
41 return 0;
45 #endif /* ACE_HAS_MONITOR_FRAMEWORK==1 */
47 int
48 ACE_TMAIN (int /* argc */, ACE_TCHAR * /* argv */ [])
50 #if defined (ACE_HAS_MONITOR_FRAMEWORK) && (ACE_HAS_MONITOR_FRAMEWORK == 1)
52 /// The Admin class will own the reactor and destroy it. We are
53 /// passing a vanilla reactor to show how it works, but in real
54 /// life it could be some specialized reactor.
55 ACE_Reactor new_reactor;
56 MC_ADMINMANAGER* mgr =
57 ACE_Dynamic_Service<MC_ADMINMANAGER>::instance ("MC_ADMINMANAGER");
58 mgr->admin ().reactor (&new_reactor);
60 /// Set the timer for CPU load check at 2 sec.
61 Monitor_Base *cpu_monitor =
62 create_os_monitor<CPU_LOAD_MONITOR> (0, ACE_Time_Value (2));
64 /// Runs the reactor's event loop in a separate thread so the timer(s)
65 /// can run concurrently with the application.
66 START_PERIODIC_MONITORS;
68 /// Run the monitor checker in a separate thread.
69 Monitor_Checker monitor_checker;
70 monitor_checker.activate ();
72 /// Make sure the monitor checker is spawned before doing anything.
73 ACE_OS::sleep (1);
75 for (int i = 0; i < 10; ++i)
77 /// Alternate between letting the CPU sleep and keeping it
78 /// busy.
79 if (i % 2 == 0)
81 ACE_OS::sleep (1);
83 else
85 for (unsigned long j = 0; j < 5050505; j++)
87 (void) ACE::gcd (2419233733UL, 567715713UL);
92 /// End the reactor's event loop, stopping the timer(s).
93 STOP_PERIODIC_MONITORS;
95 cpu_monitor->remove_ref ();
97 #endif /* ACE_HAS_MONITOR_FRAMEWORK==1 */
99 return 0;