summaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
authorMatthew Newhook <matthew@zeroc.com>2007-02-07 05:42:19 +0000
committerMatthew Newhook <matthew@zeroc.com>2007-02-07 05:42:19 +0000
commit26c73071d30ec71d6e41e819af797bc83b37a0c3 (patch)
tree26c105be53c5688b31fa46952661205c76e89232 /cpp
parentupdated depend files (diff)
downloadice-26c73071d30ec71d6e41e819af797bc83b37a0c3.tar.bz2
ice-26c73071d30ec71d6e41e819af797bc83b37a0c3.tar.xz
ice-26c73071d30ec71d6e41e819af797bc83b37a0c3.zip
http://bugzilla.zeroc.com/bugzilla/show_bug.cgi?id=1750
Diffstat (limited to 'cpp')
-rw-r--r--cpp/src/IceGrid/PlatformInfo.cpp110
-rw-r--r--cpp/src/IceGrid/PlatformInfo.h2
2 files changed, 58 insertions, 54 deletions
diff --git a/cpp/src/IceGrid/PlatformInfo.cpp b/cpp/src/IceGrid/PlatformInfo.cpp
index ff7ccf65a3c..a407c082d95 100644
--- a/cpp/src/IceGrid/PlatformInfo.cpp
+++ b/cpp/src/IceGrid/PlatformInfo.cpp
@@ -38,35 +38,39 @@
using namespace std;
using namespace IceGrid;
-
-namespace IceGrid
+namespace
{
#ifdef _WIN32
static string
-getLocalizedPerfName(const map<string, string>& perfNames, const string& name)
+getLocalizedPerfName(const Ice::LoggerPtr& logger, int idx)
{
- unsigned long idx;
- map<string, string>::const_iterator p = perfNames.find(name);
- if(p == perfNames.end())
- {
- return "";
- }
- istringstream is(p->second);
- is >> idx;
-
vector<char> localized;
unsigned long size = 256;
localized.resize(size);
- while(PdhLookupPerfNameByIndex(0, idx, &localized[0], &size) == PDH_MORE_DATA)
+ PDH_STATUS err;
+ while((err = PdhLookupPerfNameByIndex(0, idx, &localized[0], &size)) == PDH_MORE_DATA)
{
size += 256;
localized.resize(size);
}
+ if(err != ERROR_SUCCESS)
+ {
+ Ice::SyscallException ex(__FILE__, __LINE__);
+ ex.error = err;
+ Ice::Warning out(logger);
+ out << "pdhLookupPerfNameByIndex: " << ex;
+ return "";
+ }
return string(&localized[0]);
}
#endif
+}
+
+namespace IceGrid
+{
+
RegistryInfo
toRegistryInfo(const InternalReplicaInfoPtr& replica)
{
@@ -111,6 +115,7 @@ PlatformInfo::PlatformInfo(const string& prefix,
// the "WMI Windows Adapter" service (which can't be started
// because the SCM is locked...).
//
+ _initQuery = false;
_query = NULL;
_counter = NULL;
_usages1.insert(_usages1.end(), 1 * 60 / 5, 0); // 1 sample every 5 seconds during 1 minutes.
@@ -342,16 +347,28 @@ PlatformInfo::getLoadInfo()
#if defined(_WIN32)
int usage = 100;
- if(_query == NULL)
+ // If we haven't yet initialized the query system do so.
+ if(!_initQuery)
{
initQuery();
}
- if(_query != NULL && _counter != NULL && PdhCollectQueryData(_query) == ERROR_SUCCESS)
+ if(_query != NULL && _counter != NULL)
{
- DWORD type;
- PDH_FMT_COUNTERVALUE value;
- PdhGetFormattedCounterValue(_counter, PDH_FMT_LONG, &type, &value);
- usage = static_cast<int>(value.longValue);
+ PDH_STATUS err = PdhCollectQueryData(_query);
+ if(err == ERROR_SUCCESS)
+ {
+ DWORD type;
+ PDH_FMT_COUNTERVALUE value;
+ PdhGetFormattedCounterValue(_counter, PDH_FMT_LONG, &type, &value);
+ usage = static_cast<int>(value.longValue);
+ }
+ else
+ {
+ Ice::SyscallException ex(__FILE__, __LINE__);
+ ex.error = err;
+ Ice::Warning out(_traceLevels->logger);
+ out << "PdhCollectQueryData failed: " << ex;
+ }
}
_last1Total += usage - _usages1.back();
@@ -432,8 +449,11 @@ PlatformInfo::getCwd() const
void
PlatformInfo::initQuery()
{
+ // We only want to try to initialize the query subsystem once.
+ _initQuery = true;
+
//
- // Open the query
+ // Open the query.
//
PDH_STATUS err = PdhOpenQuery(0, 0, &_query);
if(err != ERROR_SUCCESS)
@@ -446,50 +466,34 @@ PlatformInfo::initQuery()
}
//
- // Load the english perf name table.
+ // Add the counter for \\Processor(_Total)\\"%Processor Time".
//
- vector<unsigned char> buffer;
- unsigned long size = 32768;
- buffer.resize(size);
- while(RegQueryValueEx(HKEY_PERFORMANCE_DATA, "Counter 09", 0, 0, &buffer[0], &size) == ERROR_MORE_DATA)
- {
- size += 8192;
- buffer.resize(size);
- }
-
- map<string, string> perfNames;
- const char* buf = reinterpret_cast<const char*>(&buffer[0]);
- unsigned int i = 0;
- while(i < buffer.size() && buf[i])
- {
- string index(&buf[i]);
- i += static_cast<int>(index.size()) + 1;
- if(i >= buffer.size())
- {
- break;
- }
- string name(&buf[i]);
- i += static_cast<int>(name.size()) + 1;
- perfNames.insert(make_pair(name, index));
- }
-
+ // We have to look up the localized names for these. "Processor"
+ // is index 238 and "%Processor Time" is index 6.
//
- // Get the localized version of "Processor" and "%Processor Time"
+ // If either lookup fails, close the query system, and we're done.
//
- string proc = getLocalizedPerfName(perfNames, "Processor");
- string proctime = getLocalizedPerfName(perfNames, "% Processor Time");
+ string processor = getLocalizedPerfName(_traceLevels->logger, 238);
+ string percentProcessorTime = getLocalizedPerfName(_traceLevels->logger, 6);
+ if(processor.empty() || percentProcessorTime.empty())
+ {
+ PdhCloseQuery(_query);
+ _query = NULL;
+ return;
+ }
- //
- // Add the counter
- //
- const string name = "\\" + proc + "(_Total)\\" + proctime;
+ const string name = "\\" + processor + "(_Total)\\" + percentProcessorTime;
err = PdhAddCounter(_query, name.c_str(), 0, &_counter);
if(err != ERROR_SUCCESS)
{
Ice::SyscallException ex(__FILE__, __LINE__);
ex.error = err;
+
Ice::Warning out(_traceLevels->logger);
out << "can't add performance counter `" << name << "':\n" << ex;
+ PdhCloseQuery(_query);
+ _query = NULL;
+ return;
}
}
#endif
diff --git a/cpp/src/IceGrid/PlatformInfo.h b/cpp/src/IceGrid/PlatformInfo.h
index 0cf49549de9..1447cdd3cc1 100644
--- a/cpp/src/IceGrid/PlatformInfo.h
+++ b/cpp/src/IceGrid/PlatformInfo.h
@@ -23,7 +23,6 @@ namespace IceGrid
class TraceLevels;
typedef IceUtil::Handle<TraceLevels> TraceLevelsPtr;
-
NodeInfo toNodeInfo(const InternalNodeInfoPtr&);
RegistryInfo toRegistryInfo(const InternalReplicaInfoPtr&);
@@ -64,6 +63,7 @@ private:
std::string _endpoints;
#if defined(_WIN32)
+ bool _initQuery;
HQUERY _query;
HCOUNTER _counter;
std::deque<int> _usages1;