Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Monitor / Group / group.cpp
blob30e6c309d25c13fd0abd725a7396cfd7f9bbf1fd
1 #include "ace/OS_NS_unistd.h"
2 #include "ace/streams.h"
3 #include "ace/Monitor_Point_Registry.h"
4 #include "ace/Monitor_Admin_Manager.h"
6 #include "ace/Monitor_Control/Monitor_Control.h"
8 #include "examples/Monitor/MC_Test_Utilities.h"
10 #if defined (ACE_HAS_MONITOR_FRAMEWORK) && (ACE_HAS_MONITOR_FRAMEWORK == 1)
12 using namespace ACE_VERSIONED_NAMESPACE_NAME::ACE::Monitor_Control;
14 /// Subclass of ACE_Task_Base, meaning that the override of
15 /// the svc() method below will run in a new thread when
16 /// activate() is called on a class instance.
17 class Monitor_Checker : public ACE_Task_Base
19 public:
20 int svc ()
22 /// Get an instance of the MC service singleton.
23 MC_ADMINMANAGER* mgr =
24 ACE_Dynamic_Service<Monitor_Admin_Manager>::instance ("MC_ADMINMANAGER");
26 ACE::Monitor_Control::Monitor_Base *cpu_monitor =
27 mgr->admin ().monitor_point ("OS/Processor/CPULoad");
29 ACE::Monitor_Control::Monitor_Base *memory_monitor =
30 mgr->admin ().monitor_point ("OS/Memory/TotalUsage");
32 ACE::Monitor_Control::Monitor_Base *bytes_monitor =
33 mgr->admin ().monitor_point ("OS/Network/BytesSent");
35 /// Query the monitor for its data every 2 seconds, and call the
36 /// appropriate display function.
37 for (int i = 0; i < 10; ++i)
39 ACE_OS::sleep (2);
41 Monitor_Control_Types::Data cpu_data (cpu_monitor->type ());
42 cpu_monitor->retrieve (cpu_data);
43 MC_Test_Utilities::display_cpu_load (cpu_data);
45 Monitor_Control_Types::Data mem_data (memory_monitor->type ());
46 memory_monitor->retrieve (mem_data);
47 MC_Test_Utilities::display_memory_usage (mem_data);
49 Monitor_Control_Types::Data bytes_data (bytes_monitor->type ());
50 bytes_monitor->retrieve (bytes_data);
51 MC_Test_Utilities::display_bytes_sent (bytes_data);
53 cout << endl;
56 cpu_monitor->remove_ref ();
57 memory_monitor->remove_ref ();
58 bytes_monitor->remove_ref ();
60 return 0;
64 #endif /* ACE_HAS_MONITOR_FRAMEWORK==1 */
66 int
67 ACE_TMAIN (int /* argc */, ACE_TCHAR * /* argv */ [])
69 #if defined (ACE_HAS_MONITOR_FRAMEWORK) && (ACE_HAS_MONITOR_FRAMEWORK == 1)
71 /// Creates these future group members without automatic update.
72 Monitor_Base *cpu_load_monitor =
73 create_os_monitor<CPU_LOAD_MONITOR> ();
74 Monitor_Base *bytes_sent_monitor =
75 create_os_monitor<BYTES_SENT_MONITOR> ();
76 Monitor_Base *memory_usage_monitor =
77 create_os_monitor<MEMORY_USAGE_MONITOR> ();
79 Monitor_Group *group = new Monitor_Group ("Test_Group");
81 group->add_member (cpu_load_monitor);
82 group->add_member (bytes_sent_monitor);
83 group->add_member (memory_usage_monitor);
85 /// Register the group as an auto-updated monitor point.
86 MC_ADMINMANAGER* mgr =
87 ACE_Dynamic_Service<MC_ADMINMANAGER>::instance ("MC_ADMINMANAGER");
88 bool good_add = mgr->admin ().monitor_point (group, ACE_Time_Value (2));
90 if (!good_add)
92 ACE_ERROR ((LM_ERROR,
93 "Group monitor \"%s\" registration failed.\n",
94 group->name ()));
97 /// A quick test of the registry's name cache.
98 Monitor_Control_Types::NameList mc_names =
99 Monitor_Point_Registry::instance ()->names ();
101 cout << "stored in monitor point registry:" << endl << endl;
103 for (Monitor_Control_Types::NameList::Iterator i (mc_names);
104 !i.done ();
105 i.advance ())
107 ACE_CString *item = 0;
108 i.next (item);
110 cout << item->c_str () << endl;
113 cout << endl;
115 /// Runs the reactor's event loop in a separate thread so the timer(s)
116 /// can run concurrently with the application.
117 START_PERIODIC_MONITORS;
119 /// Run the monitor checker in a separate thread.
120 Monitor_Checker monitor_checker;
121 monitor_checker.activate ();
123 char * str_array[5] = {0};
125 for (int i = 0; i < 10; ++i)
127 ACE_OS::sleep (1);
129 /// Alternate between letting the CPU sleep and keeping it
130 /// busy.
131 if (i % 2 == 0)
133 ACE_OS::sleep (1);
135 else
137 for (unsigned long j = 0; j < 5050505; j++)
139 (void) ACE::gcd (2419233733UL, 567715713UL);
143 /// Allocate a large string in each of the first 5 loops,
144 /// free them in the last 5 loops.
145 if (i < 5)
147 str_array[i] = new char[1024*1024*10];
149 else
151 delete [] str_array[i - 5];
155 /// End the reactor's event loop, stopping the timer(s).
156 STOP_PERIODIC_MONITORS;
158 cpu_load_monitor->remove_ref ();
159 bytes_sent_monitor->remove_ref ();
160 memory_usage_monitor->remove_ref ();
161 group->remove_ref ();
163 #endif /* ACE_HAS_MONITOR_FRAMEWORK==1 */
165 return 0;