summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/LoadBalancing
diff options
context:
space:
mode:
authorjai <jai@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>2004-04-21 00:08:15 +0000
committerjai <jai@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>2004-04-21 00:08:15 +0000
commit1885f0e7a225e3d4e07b63a589743d5f26758795 (patch)
treef1a5f4df93aa9642e98164420826ad36c873185e /TAO/orbsvcs/orbsvcs/LoadBalancing
parent4b2b51be3db8281aff35eb5ca235e1df9df70796 (diff)
downloadATCD-1885f0e7a225e3d4e07b63a589743d5f26758795.tar.gz
Tue Apr 20 19:05:02 2004 Jaiganesh B <jai@dre.vanderbilt.edu>
Diffstat (limited to 'TAO/orbsvcs/orbsvcs/LoadBalancing')
-rw-r--r--TAO/orbsvcs/orbsvcs/LoadBalancing/LB_CPU_Utilization_Monitor.cpp171
-rw-r--r--TAO/orbsvcs/orbsvcs/LoadBalancing/LB_CPU_Utilization_Monitor.h98
2 files changed, 269 insertions, 0 deletions
diff --git a/TAO/orbsvcs/orbsvcs/LoadBalancing/LB_CPU_Utilization_Monitor.cpp b/TAO/orbsvcs/orbsvcs/LoadBalancing/LB_CPU_Utilization_Monitor.cpp
new file mode 100644
index 00000000000..7440ac3f258
--- /dev/null
+++ b/TAO/orbsvcs/orbsvcs/LoadBalancing/LB_CPU_Utilization_Monitor.cpp
@@ -0,0 +1,171 @@
+#include "LB_CPU_Utilization_Monitor.h"
+
+#include "tao/ORB_Constants.h"
+#include <unistd.h>
+#include <stdio.h>
+#include <iostream>
+#include "ace/OS_NS_time.h"
+#include "ace/OS_NS_stdio.h"
+#include "ace/os_include/os_netdb.h"
+
+#if defined (ACE_HAS_SYS_LOADAVG_H)
+# include <sys/loadavg.h>
+#endif /* ACE_HAS_SYS_LOADAVG_H */
+
+ACE_RCSID (LoadBalancing,
+ LB_CPU_Utilization_Monitor,
+ "$Id$")
+
+double calc_cpu_loading (void)
+{
+ static char buf[1024];
+ static unsigned long prev_idle = 0;
+ static double prev_total = 0.0;
+
+ FILE *file_ptr = NULL;
+ char *item = NULL;
+ char *arg = NULL;
+ unsigned long delta_idle = 0;
+ unsigned long user = 0;
+ unsigned long nice = 0;
+ unsigned long idle = 0;
+ unsigned long sys = 0;
+
+ double percent_cpu_load = 0.0;
+
+ if ((file_ptr = fopen("/proc/stat", "r")) == NULL)
+ return percent_cpu_load;
+
+ while ((fgets (buf, sizeof (buf), file_ptr)) != NULL)
+ {
+ item = strtok (buf, " \t\n");
+ arg = strtok (NULL, "\n");
+
+ if (item == NULL || arg == NULL)
+ continue;
+ if (item[0] == 'c' && strlen (item) == 3)
+ {
+ sscanf (arg, "%lu %lu %lu %lu", &user, &nice, &sys, &idle);
+ break;
+ }
+
+
+ }
+
+ fclose (file_ptr);
+
+ delta_idle = idle - prev_idle;
+ double total;
+ double time_passed;
+ total = (double) (user + nice + sys + idle);
+ time_passed = total - prev_total;
+
+ percent_cpu_load = 100.0 - (delta_idle / time_passed * 100.0);
+
+ prev_idle = idle;
+ prev_total = total;
+
+ return percent_cpu_load;
+
+}
+
+
+TAO_LB_CPU_Utilization_Monitor::TAO_LB_CPU_Utilization_Monitor (const char * location_id,
+ const char * location_kind)
+ : location_ (1)
+{
+ this->location_.length (1);
+
+ if (location_id == 0)
+ {
+ char host[MAXHOSTNAMELEN + 1];
+ if (ACE_OS::hostname (host, sizeof (host)) != 0)
+ {
+ // Couldn't determine hostname. Use the current time
+ // instead.
+ CORBA::ULong t = ACE_static_cast (CORBA::ULong, ACE_OS::time ());
+
+ // A 64 byte buffer is more than enough to contain the
+ // string representation of a 32 bit unsigned integer.
+ char buf[64] = { '\0' };
+ ACE_OS::sprintf (buf, "%u", t);
+
+ this->location_[0].id = CORBA::string_dup (buf);
+ this->location_[0].kind = CORBA::string_dup ("Creation Time");
+ }
+ else
+ {
+ this->location_[0].id = CORBA::string_dup (host);
+ this->location_[0].kind = CORBA::string_dup ("Hostname");
+ }
+ }
+ else
+ {
+ this->location_[0].id = CORBA::string_dup (location_id);
+
+ if (location_kind != 0)
+ this->location_[0].kind = CORBA::string_dup (location_kind);
+ }
+}
+
+TAO_LB_CPU_Utilization_Monitor::~TAO_LB_CPU_Utilization_Monitor (void)
+{
+}
+
+CosLoadBalancing::Location *
+TAO_LB_CPU_Utilization_Monitor::the_location (ACE_ENV_SINGLE_ARG_DECL)
+ ACE_THROW_SPEC ((CORBA::SystemException))
+{
+ CosLoadBalancing::Location * location;
+ ACE_NEW_THROW_EX (location,
+ CosLoadBalancing::Location (this->location_),
+ CORBA::NO_MEMORY (
+ CORBA::SystemException::_tao_minor_code (
+ TAO_DEFAULT_MINOR_CODE,
+ ENOMEM),
+ CORBA::COMPLETED_NO));
+ ACE_CHECK_RETURN (0);
+
+ return location;
+}
+
+CosLoadBalancing::LoadList *
+TAO_LB_CPU_Utilization_Monitor::loads (ACE_ENV_SINGLE_ARG_DECL)
+ ACE_THROW_SPEC ((CORBA::SystemException))
+{
+ CORBA::Float load = 0;
+
+#if defined (linux) || defined (sun) || defined (hpux)
+
+ double load_double = calc_cpu_loading ();
+ load = load_double;
+
+ CosLoadBalancing::LoadList * tmp;
+ ACE_NEW_THROW_EX (tmp,
+ CosLoadBalancing::LoadList (1),
+ CORBA::NO_MEMORY (
+ CORBA::SystemException::_tao_minor_code (
+ TAO_DEFAULT_MINOR_CODE,
+ ENOMEM),
+ CORBA::COMPLETED_NO));
+ ACE_CHECK_RETURN (0);
+
+ CosLoadBalancing::LoadList_var load_list = tmp;
+
+ load_list->length (1);
+
+ load_list[0].id = CosLoadBalancing::CPU;
+ load_list[0].value = load;
+
+ ACE_DEBUG ((LM_DEBUG, "%2f\n", load_list[0].value));
+
+ return load_list._retn ();
+
+#else
+
+ ACE_UNUSED_ARG (load);
+ ACE_THROW_RETURN (CORBA::NO_IMPLEMENT (), 0);
+
+#endif /* WINDOWS || linux || sun || hpux */
+
+}
diff --git a/TAO/orbsvcs/orbsvcs/LoadBalancing/LB_CPU_Utilization_Monitor.h b/TAO/orbsvcs/orbsvcs/LoadBalancing/LB_CPU_Utilization_Monitor.h
new file mode 100644
index 00000000000..fab93d1af5f
--- /dev/null
+++ b/TAO/orbsvcs/orbsvcs/LoadBalancing/LB_CPU_Utilization_Monitor.h
@@ -0,0 +1,98 @@
+// -*- C++ -*-
+
+//=============================================================================
+/**
+ * @file LB_CPU_Utilization_Monitor.h
+ *
+ * $Id$
+ *
+ * @author Jaiganesh Balasubramanian <jai@dre.vanderbilt.edu>
+ * Ossama Othman <ossama@uci.edu>
+ */
+//=============================================================================
+
+
+#ifndef TAO_LB_CPU_UTILIZATION_MONITOR_H
+#define TAO_LB_CPU_UTILIZATION_MONITOR_H
+
+#include /**/ "ace/pre.h"
+
+#include "orbsvcs/orbsvcs/LoadBalancing/LoadBalancing_export.h"
+
+# if !defined (ACE_LACKS_PRAGMA_ONCE)
+# pragma once
+# endif /* ACE_LACKS_PRAGMA_ONCE */
+
+#include "orbsvcs/orbsvcs/CosLoadBalancingS.h"
+
+
+/**
+ * @class TAO_LB_CPU_Utilization_Monitor
+ *
+ * @brief LoadMonitor implementation that monitors the overall CPU
+ * load on a given host.
+ *
+ * Loads returned from this load monitor is the utilization of
+ * CPU in the "form" of percentage.
+ */
+class TAO_LoadBalancing_Export TAO_LB_CPU_Utilization_Monitor
+ : public virtual POA_CosLoadBalancing::LoadMonitor,
+ public virtual PortableServer::RefCountServantBase
+{
+public:
+
+ /// Constructor
+ /**
+ * If no location is supplied the hostname or IP address is used by
+ * default.
+ */
+ TAO_LB_CPU_Utilization_Monitor (const char * location_id = 0,
+ const char * location_kind = 0);
+
+ /**
+ * @name CosLoadBalancing::LoadMonitor Methods
+ *
+ * Methods required by the CosLoadBalancing::LoadMonitor interface.
+ */
+ //@{
+
+ /// Return the location at which the LoadMonitor resides.
+ /**
+ * The returned "Location" is a sequence of length 1.
+ */
+ virtual CosLoadBalancing::Location * the_location (
+ ACE_ENV_SINGLE_ARG_DECL_WITH_DEFAULTS)
+ ACE_THROW_SPEC ((CORBA::SystemException));
+
+ /// Return the average CPU load at the location which this
+ /// LoadMonitor resides.
+ /**
+ * @return A "Load" sequence of length 1 that contains a LoadId
+ * equal to CosLoadBalancing::CPU, and the average CPU
+ * load.
+ */
+ virtual CosLoadBalancing::LoadList * loads (
+ ACE_ENV_SINGLE_ARG_DECL_WITH_DEFAULTS)
+ ACE_THROW_SPEC ((CORBA::SystemException));
+
+ //@}
+
+protected:
+
+ /// Destructor
+ /**
+ * Protected destructor to enforce proper memory management through
+ * reference counting.
+ */
+ ~TAO_LB_CPU_Utilization_Monitor (void);
+
+private:
+
+ /// The name of the location at which this LoadMonitor resides.
+ CosLoadBalancing::Location location_;
+
+};
+
+#include /**/ "ace/post.h"
+
+#endif /* TAO_LB_CPU_UTILIZATION_MONITOR_H */