1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
// **********************************************************************
//
// Copyright (c) 2003-2006 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <Ice/Communicator.h>
#include <Ice/Properties.h>
#include <Ice/LocalException.h>
#include <Ice/LoggerUtil.h>
#include <Ice/ProtocolPluginFacade.h> // Just to get the hostname
#include <IceGrid/PlatformInfo.h>
#include <IceGrid/TraceLevels.h>
#include <IcePatch2/Util.h>
#if defined(_WIN32)
# include <direct.h> // For _getcwd
# include <pdhmsg.h> // For PDH_MORE_DATA
#else
# include <sys/utsname.h>
# if defined(__APPLE__)
# include <sys/sysctl.h>
# elif defined(__sun)
# include <sys/loadavg.h>
# elif defined(__hpux)
# include <sys/pstat.h>
# elif defined(_AIX)
# include <nlist.h>
# include <fcntl.h>
# endif
#endif
using namespace std;
using namespace IceGrid;
#ifdef _WIN32
namespace IceGrid
{
string
getLocalizedPerfName(const map<string, string>& perfNames, const string& name)
{
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)
{
size += 256;
localized.resize(size);
}
return string(&localized[0]);
}
};
#endif
PlatformInfo::PlatformInfo(const string& prefix,
const Ice::CommunicatorPtr& communicator,
const TraceLevelsPtr& traceLevels) :
_traceLevels(traceLevels)
{
//
// Initialization of the necessary data structures to get the load average.
//
#if defined(_WIN32)
//
// The performance counter query is lazy initialized. We can't
// initialize it in the constructor because it might be called
// when IceGrid is started on boot as a Windows service with the
// Windows service control manager (SCM) locked. The query
// initialization would fail (hang) because it requires to start
// the "WMI Windows Adapter" service (which can't be started
// because the SCM is locked...).
//
_query = NULL;
_counter = NULL;
_usages1.insert(_usages1.end(), 1 * 60 / 5, 0); // 1 sample every 5 seconds during 1 minutes.
_usages5.insert(_usages5.end(), 5 * 60 / 5, 0); // 1 sample every 5 seconds during 5 minutes.
_usages15.insert(_usages15.end(), 15 * 60 / 5, 0); // 1 sample every 5 seconds during 15 minutes.
_last1Total = 0;
_last5Total = 0;
_last15Total = 0;
#elif defined(_AIX)
struct nlist nl;
nl.n_name = "avenrun";
nl.n_value = 0;
if(knlist(&nl, 1, sizeof(nl)) == 0)
{
_kmem = open("/dev/kmem", O_RDONLY);
//
// Give up root permissions to minimize security risks, it's
// only needed to access /dev/kmem.
//
setuid(getuid());
setgid(getgid());
}
else
{
_kmem = -1;
}
#endif
//
// Get the number of processors.
//
#if defined(_WIN32)
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);
_nProcessors = sysInfo.dwNumberOfProcessors;
#elif defined(__APPLE__)
static int ncpu[2] = { CTL_HW, HW_NCPU };
size_t sz = sizeof(_nProcessors);
if(sysctl(ncpu, 2, &_nProcessors, &sz, 0, 0) == -1)
{
Ice::SyscallException ex(__FILE__, __LINE__);
ex.error = getSystemErrno();
throw ex;
}
#elif defined(__hpux)
struct pst_dynamic dynInfo;
if(pstat_getdynamic(&dynInfo, sizeof(dynInfo), 1, 0) >= 0)
{
_nProcessors = dynInfo.psd_proc_cnt;
}
else
{
_nProcessors = 1;
}
#else
_nProcessors = static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
#endif
//
// Get the rest of the node information.
//
#ifdef _WIN32
_os = "Windows";
char hostname[MAX_COMPUTERNAME_LENGTH + 1];
unsigned long size = sizeof(hostname);
if(GetComputerName(hostname, &size))
{
_hostname = hostname;
}
OSVERSIONINFO osInfo;
osInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osInfo);
ostringstream os;
os << osInfo.dwMajorVersion << "." << osInfo.dwMinorVersion;
_release = os.str();
_version = osInfo.szCSDVersion;
_machine = "x86"; // TODO?
#else
struct utsname utsinfo;
uname(&utsinfo);
_os = utsinfo.sysname;
_hostname = utsinfo.nodename;
_release = utsinfo.release;
_version = utsinfo.version;
_machine = utsinfo.machine;
#endif
//
// DEPRECATED PROPERTIES: Remove extra code in future release.
//
Ice::PropertiesPtr properties = communicator->getProperties();
string endpointsPrefix;
string oldEndpointsPrefix;
if(prefix == "IceGrid.Registry")
{
_name = properties->getPropertyWithDefault("IceGrid.Registry.ReplicaName", "Master");
endpointsPrefix = "Ice.OA." + prefix + ".Client";
oldEndpointsPrefix = prefix + ".Client";
}
else
{
_name = properties->getProperty(prefix + ".Name");
endpointsPrefix = prefix;
oldEndpointsPrefix = prefix;
}
Ice::PropertyDict props = properties->getPropertiesForPrefix(endpointsPrefix);
Ice::PropertyDict::const_iterator p = props.find(endpointsPrefix + ".PublishedEndpoints");
if(p != props.end())
{
_endpoints = p->second;
}
else
{
Ice::PropertyDict oldProps = properties->getPropertiesForPrefix(oldEndpointsPrefix);
p = props.find(oldEndpointsPrefix + ".PublishedEndpoints");
if(p != props.end())
{
_endpoints = p->second;
}
else
{
_endpoints = properties->getPropertyWithDefault(
endpointsPrefix + ".Endpoints", properties->getProperty(oldEndpointsPrefix + ".Endpoints"));
}
}
_dataDir = properties->getProperty(prefix + ".Data");
if(!IcePatch2::isAbsolute(_dataDir))
{
#ifdef _WIN32
char cwd[_MAX_PATH];
if(_getcwd(cwd, _MAX_PATH) == NULL)
#else
char cwd[PATH_MAX];
if(getcwd(cwd, PATH_MAX) == NULL)
#endif
{
throw "cannot get the current directory:\n" + IcePatch2::lastError();
}
_dataDir = string(cwd) + '/' + _dataDir;
}
if(_dataDir[_dataDir.length() - 1] == '/')
{
_dataDir = _dataDir.substr(0, _dataDir.length() - 1);
}
}
PlatformInfo::~PlatformInfo()
{
#ifdef _WIN32
if(_query != NULL)
{
PdhCloseQuery(_query);
}
#elif defined(_AIX)
if(_kmem > 0)
{
close(_kmem);
}
#endif
}
NodeInfo
PlatformInfo::getNodeInfo() const
{
NodeInfo info;
info.name = _name;
info.os = _os;
info.hostname = _hostname;
info.release = _release;
info.version = _version;
info.machine = _machine;
info.nProcessors = _nProcessors;
info.dataDir = _dataDir;
return info;
}
RegistryInfo
PlatformInfo::getRegistryInfo() const
{
RegistryInfo info;
info.name = _name;
info.hostname = _hostname;
info.endpoints = _endpoints;
return info;
}
LoadInfo
PlatformInfo::getLoadInfo()
{
LoadInfo info;
info.avg1 = -1.0f;
info.avg5 = -1.0f;
info.avg15 = -1.0f;
#if defined(_WIN32)
int usage = 100;
if(_query == NULL)
{
initQuery();
}
if(_query != NULL && _counter != NULL && PdhCollectQueryData(_query) == ERROR_SUCCESS)
{
DWORD type;
PDH_FMT_COUNTERVALUE value;
PdhGetFormattedCounterValue(_counter, PDH_FMT_LONG, &type, &value);
usage = static_cast<int>(value.longValue);
}
_last1Total += usage - _usages1.back();
_last5Total += usage - _usages5.back();
_last15Total += usage - _usages15.back();
_usages1.pop_back();
_usages5.pop_back();
_usages15.pop_back();
_usages1.push_front(usage);
_usages5.push_front(usage);
_usages15.push_front(usage);
info.avg1 = static_cast<float>(_last1Total) / _usages1.size() / 100.0f;
info.avg5 = static_cast<float>(_last5Total) / _usages5.size() / 100.0f;
info.avg15 = static_cast<float>(_last15Total) / _usages15.size() / 100.0f;
#elif defined(__sun) || defined(__linux) || defined(__APPLE__)
//
// We use the load average divided by the number of
// processors to figure out if the machine is busy or
// not. The result is capped at 1.0f.
//
double loadAvg[3];
if(getloadavg(loadAvg, 3) != -1)
{
info.avg1 = static_cast<float>(loadAvg[0]);
info.avg5 = static_cast<float>(loadAvg[1]);
info.avg15 = static_cast<float>(loadAvg[2]);
}
#elif defined(__hpux)
struct pst_dynamic dynInfo;
if(pstat_getdynamic(&dynInfo, sizeof(dynInfo), 1, 0) >= 0)
{
info.avg1 = dynInfo.psd_avg_1_min;
info.avg5 = dynInfo.psd_avg_5_min;
info.avg15 = dynInfo.psd_avg_15_min;
}
#elif defined(_AIX)
if(_kmem > 1)
{
long long avenrun[3];
struct nlist nl;
nl.n_name = "avenrun";
nl.n_value = 0;
if(knlist(&nl, 1, sizeof(nl)) == 0)
{
if(pread(_kmem, avenrun, sizeof(avenrun), nl.n_value) >= sizeof(avenrun))
{
info.avg1 = avenrun[0] / 65536.0f;
info.avg5 = avenrun[1] / 65536.0f;
info.avg15 = avenrun[2] / 65536.0f;
}
}
}
#endif
return info;
}
std::string
PlatformInfo::getHostname() const
{
return _hostname;
}
std::string
PlatformInfo::getDataDir() const
{
return _dataDir;
}
#ifdef _WIN32
void
PlatformInfo::initQuery()
{
//
// Open the query
//
PDH_STATUS err = PdhOpenQuery(0, 0, &_query);
if(err != ERROR_SUCCESS)
{
Ice::SyscallException ex(__FILE__, __LINE__);
ex.error = err;
Ice::Warning out(_traceLevels->logger);
out << "can't open performance data query:\n" << ex;
return;
}
//
// Load the english perf name table.
//
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));
}
//
// Get the localized version of "Processor" and "%Processor Time"
//
string proc = getLocalizedPerfName(perfNames, "Processor");
string proctime = getLocalizedPerfName(perfNames, "% Processor Time");
//
// Add the counter
//
const string name = "\\" + proc + "(_Total)\\" + proctime;
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;
}
}
#endif
|