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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
|
/**
* @file llsys.cpp
* @brief Impelementation of the basic system query functions.
*
* Copyright (c) 2002-$CurrentYear$, Linden Research, Inc.
* $License$
*/
#include "linden_common.h"
#include "llsys.h"
#include <iostream>
#ifdef LL_STANDALONE
# include <zlib.h>
#else
# include "zlib/zlib.h"
#endif
#include "processor.h"
#if LL_WINDOWS
# define WIN32_LEAN_AND_MEAN
# include <winsock2.h>
# include <windows.h>
#elif LL_DARWIN
# include <sys/sysctl.h>
# include <sys/utsname.h>
#elif LL_LINUX
# include <sys/utsname.h>
const char MEMINFO_FILE[] = "/proc/meminfo";
const char CPUINFO_FILE[] = "/proc/cpuinfo";
#endif
static const S32 CPUINFO_BUFFER_SIZE = 16383;
LLCPUInfo gSysCPU;
LLOSInfo::LLOSInfo() :
mMajorVer(0), mMinorVer(0), mBuild(0),
mOSString("")
{
#if LL_WINDOWS
OSVERSIONINFOEX osvi;
BOOL bOsVersionInfoEx;
// Try calling GetVersionEx using the OSVERSIONINFOEX structure.
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if(!(bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO *) &osvi)))
{
// If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO.
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if(!GetVersionEx( (OSVERSIONINFO *) &osvi))
return;
}
mMajorVer = osvi.dwMajorVersion;
mMinorVer = osvi.dwMinorVersion;
mBuild = osvi.dwBuildNumber;
switch(osvi.dwPlatformId)
{
case VER_PLATFORM_WIN32_NT:
{
// Test for the product.
if(osvi.dwMajorVersion <= 4)
{
mOSString = "Microsoft Windows NT ";
}
else if(osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
{
mOSString = "Microsoft Windows 2000 ";
}
else if(osvi.dwMajorVersion ==5 && osvi.dwMinorVersion == 1)
{
mOSString = "Microsoft Windows XP ";
}
else if(osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
{
if(osvi.wProductType == VER_NT_WORKSTATION)
mOSString = "Microsoft Windows XP x64 Edition ";
else mOSString = "Microsoft Windows Server 2003 ";
}
else if(osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0)
{
if(osvi.wProductType == VER_NT_WORKSTATION)
mOSString = "Microsoft Windows Vista ";
else mOSString = "Microsoft Windows Vista Server ";
}
else // Use the registry on early versions of Windows NT.
{
HKEY hKey;
WCHAR szProductType[80];
DWORD dwBufLen;
RegOpenKeyEx( HKEY_LOCAL_MACHINE,
L"SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
0, KEY_QUERY_VALUE, &hKey );
RegQueryValueEx( hKey, L"ProductType", NULL, NULL,
(LPBYTE) szProductType, &dwBufLen);
RegCloseKey( hKey );
if ( lstrcmpi( L"WINNT", szProductType) == 0 )
{
mOSString += "Professional ";
}
else if ( lstrcmpi( L"LANMANNT", szProductType) == 0 )
{
mOSString += "Server ";
}
else if ( lstrcmpi( L"SERVERNT", szProductType) == 0 )
{
mOSString += "Advanced Server ";
}
}
std::string csdversion = utf16str_to_utf8str(osvi.szCSDVersion);
// Display version, service pack (if any), and build number.
char tmp[MAX_STRING]; /* Flawfinder: ignore */
if(osvi.dwMajorVersion <= 4)
{
snprintf( /* Flawfinder: ignore */
tmp,
sizeof(tmp),
"version %d.%d %s (Build %d)",
osvi.dwMajorVersion,
osvi.dwMinorVersion,
csdversion.c_str(),
(osvi.dwBuildNumber & 0xffff));
}
else
{
snprintf( /* Flawfinder: ignore */
tmp,
sizeof(tmp),
"%s (Build %d)",
csdversion.c_str(),
(osvi.dwBuildNumber & 0xffff));
}
mOSString += tmp;
}
break;
case VER_PLATFORM_WIN32_WINDOWS:
// Test for the Windows 95 product family.
if(osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
{
mOSString = "Microsoft Windows 95 ";
if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' )
{
mOSString += "OSR2 ";
}
}
if(osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
{
mOSString = "Microsoft Windows 98 ";
if ( osvi.szCSDVersion[1] == 'A' )
{
mOSString += "SE ";
}
}
if(osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
{
mOSString = "Microsoft Windows Millennium Edition ";
}
break;
}
#else
struct utsname un;
if(uname(&un) != -1)
{
mOSString.append(un.sysname);
mOSString.append(" ");
mOSString.append(un.release);
mOSString.append(" ");
mOSString.append(un.version);
mOSString.append(" ");
mOSString.append(un.machine);
}
else
{
mOSString.append("Unable to collect OS info");
}
#endif
}
#ifndef LL_WINDOWS
// static
S32 LLOSInfo::getMaxOpenFiles()
{
const S32 OPEN_MAX_GUESS = 256;
#ifdef OPEN_MAX
static S32 open_max = OPEN_MAX;
#else
static S32 open_max = 0;
#endif
if (0 == open_max)
{
// First time through.
errno = 0;
if ( (open_max = sysconf(_SC_OPEN_MAX)) < 0)
{
if (0 == errno)
{
// Indeterminate.
open_max = OPEN_MAX_GUESS;
}
else
{
llerrs << "LLOSInfo::getMaxOpenFiles: sysconf error for _SC_OPEN_MAX" << llendl;
}
}
}
return open_max;
}
#endif
void LLOSInfo::stream(std::ostream& s) const
{
s << mOSString;
}
const std::string& LLOSInfo::getOSString() const
{
return mOSString;
}
const S32 STATUS_SIZE = 8192;
//static
U32 LLOSInfo::getProcessVirtualSizeKB()
{
U32 virtual_size = 0;
#if LL_WINDOWS
#endif
#if LL_LINUX
FILE* status_filep = LLFile::fopen("/proc/self/status", "r"); /* Flawfinder: ignore */
S32 numRead = 0;
char buff[STATUS_SIZE]; /* Flawfinder: ignore */
size_t nbytes = fread(buff, 1, STATUS_SIZE-1, status_filep);
buff[nbytes] = '\0';
// All these guys return numbers in KB
char *memp = strstr(buff, "VmSize:");
if (memp)
{
numRead += sscanf(memp, "%*s %u", &virtual_size);
}
fclose(status_filep);
#elif LL_SOLARIS
char proc_ps[LL_MAX_PATH];
sprintf(proc_ps, "/proc/%d/psinfo", (int)getpid());
int proc_fd = -1;
if((proc_fd = open(proc_ps, O_RDONLY)) == -1){
llwarns << "unable to open " << proc_ps << llendl;
return 0;
}
psinfo_t proc_psinfo;
if(read(proc_fd, &proc_psinfo, sizeof(psinfo_t)) != sizeof(psinfo_t)){
llwarns << "Unable to read " << proc_ps << llendl;
close(proc_fd);
return 0;
}
close(proc_fd);
virtual_size = proc_psinfo.pr_size;
#endif
return virtual_size;
}
//static
U32 LLOSInfo::getProcessResidentSizeKB()
{
U32 resident_size = 0;
#if LL_WINDOWS
#endif
#if LL_LINUX
FILE* status_filep = LLFile::fopen("/proc/self/status", "r"); /* Flawfinder: ignore */
if (status_filep != NULL)
{
S32 numRead = 0;
char buff[STATUS_SIZE]; /* Flawfinder: ignore */
size_t nbytes = fread(buff, 1, STATUS_SIZE-1, status_filep);
buff[nbytes] = '\0';
// All these guys return numbers in KB
char *memp = strstr(buff, "VmRSS:");
if (memp)
{
numRead += sscanf(memp, "%*s %u", &resident_size);
}
fclose(status_filep);
}
#elif LL_SOLARIS
char proc_ps[LL_MAX_PATH];
sprintf(proc_ps, "/proc/%d/psinfo", (int)getpid());
int proc_fd = -1;
if((proc_fd = open(proc_ps, O_RDONLY)) == -1){
llwarns << "unable to open " << proc_ps << llendl;
return 0;
}
psinfo_t proc_psinfo;
if(read(proc_fd, &proc_psinfo, sizeof(psinfo_t)) != sizeof(psinfo_t)){
llwarns << "Unable to read " << proc_ps << llendl;
close(proc_fd);
return 0;
}
close(proc_fd);
resident_size = proc_psinfo.pr_rssize;
#endif
return resident_size;
}
LLCPUInfo::LLCPUInfo()
{
CProcessor proc;
const ProcessorInfo* info = proc.GetCPUInfo();
// proc.WriteInfoTextFile("procInfo.txt");
mHasSSE = info->_Ext.SSE_StreamingSIMD_Extensions;
mHasSSE2 = info->_Ext.SSE2_StreamingSIMD2_Extensions;
mHasAltivec = info->_Ext.Altivec_Extensions;
mCPUMhz = (S32)(proc.GetCPUFrequency(50)/1000000.0);
mFamily.assign( info->strFamily );
}
bool LLCPUInfo::hasAltivec() const
{
return mHasAltivec;
}
bool LLCPUInfo::hasSSE() const
{
return mHasSSE;
}
bool LLCPUInfo::hasSSE2() const
{
return mHasSSE2;
}
S32 LLCPUInfo::getMhz() const
{
return mCPUMhz;
}
std::string LLCPUInfo::getCPUString() const
{
#if LL_WINDOWS || LL_DARWIN || LL_SOLARIS
std::ostringstream out;
CProcessor proc;
(void) proc.GetCPUInfo();
out << proc.strCPUName << " ";
F32 freq = (F32)(proc.GetCPUFrequency(50) / 1000000.0);
// cpu speed is often way wrong, do a sanity check
if (200.f < freq && freq < 10000.f)
{
out << "(" << (S32)(freq) << " MHz)";
}
return out.str();
#else
return "Can't get terse CPU information";
#endif
}
void LLCPUInfo::stream(std::ostream& s) const
{
#if LL_WINDOWS || LL_DARWIN || LL_SOLARIS
// gather machine information.
char proc_buf[CPUINFO_BUFFER_SIZE]; /* Flawfinder: ignore */
CProcessor proc;
if(proc.CPUInfoToText(proc_buf, CPUINFO_BUFFER_SIZE))
{
s << proc_buf;
}
else
{
s << "Unable to collect processor info";
}
#else
// *NOTE: This works on linux. What will it do on other systems?
FILE* cpuinfo = LLFile::fopen(CPUINFO_FILE, "r"); /* Flawfinder: ignore */
if(cpuinfo)
{
char line[MAX_STRING]; /* Flawfinder: ignore */
memset(line, 0, MAX_STRING);
while(fgets(line, MAX_STRING, cpuinfo))
{
line[strlen(line)-1] = ' '; /*Flawfinder: ignore*/
s << line;
}
fclose(cpuinfo);
}
else
{
s << "Unable to collect memory information";
}
#endif
}
LLMemoryInfo::LLMemoryInfo()
{
}
#if LL_LINUX
#include <unistd.h>
#include <sys/sysinfo.h>
#endif
U32 LLMemoryInfo::getPhysicalMemory() const
{
#if LL_WINDOWS
MEMORYSTATUS state;
state.dwLength = sizeof(state);
GlobalMemoryStatus(&state);
return (U32)state.dwTotalPhys;
#elif LL_DARWIN
// This might work on Linux as well. Someone check...
unsigned int phys = 0;
int mib[2] = { CTL_HW, HW_PHYSMEM };
size_t len = sizeof(phys);
sysctl(mib, 2, &phys, &len, NULL, 0);
return phys;
#elif LL_LINUX
return getpagesize() * get_phys_pages();
#elif LL_SOLARIS
return getpagesize() * sysconf(_SC_PHYS_PAGES);
#else
return 0;
#endif
}
void LLMemoryInfo::stream(std::ostream& s) const
{
#if LL_WINDOWS
MEMORYSTATUS state;
state.dwLength = sizeof(state);
GlobalMemoryStatus(&state);
s << "Percent Memory use: " << (U32)state.dwMemoryLoad << '%' << std::endl;
s << "Total Physical Kb: " << (U32)state.dwTotalPhys/1024 << std::endl;
s << "Avail Physical Kb: " << (U32)state.dwAvailPhys/1024 << std::endl;
s << "Total page Kb: " << (U32)state.dwTotalPageFile/1024 << std::endl;
s << "Avail page Kb: " << (U32)state.dwAvailPageFile/1024 << std::endl;
s << "Total Virtual Kb: " << (U32)state.dwTotalVirtual/1024 << std::endl;
s << "Avail Virtual Kb: " << (U32)state.dwAvailVirtual/1024 << std::endl;
#elif LL_DARWIN
U64 phys = 0;
size_t len = sizeof(phys);
if(sysctlbyname("hw.memsize", &phys, &len, NULL, 0) == 0)
{
s << "Total Physical Kb: " << phys/1024 << std::endl;
}
else
{
s << "Unable to collect memory information";
}
#elif LL_SOLARIS
U64 phys = 0;
phys = (U64)(sysconf(_SC_PHYS_PAGES)) * (U64)(sysconf(_SC_PAGESIZE)/1024);
s << "Total Physical Kb: " << phys << std::endl;
#else
// *NOTE: This works on linux. What will it do on other systems?
FILE* meminfo = LLFile::fopen(MEMINFO_FILE,"r"); /* Flawfinder: ignore */
if(meminfo)
{
char line[MAX_STRING]; /* Flawfinder: ignore */
memset(line, 0, MAX_STRING);
while(fgets(line, MAX_STRING, meminfo))
{
line[strlen(line)-1] = ' '; /*Flawfinder: ignore*/
s << line;
}
fclose(meminfo);
}
else
{
s << "Unable to collect memory information";
}
#endif
}
std::ostream& operator<<(std::ostream& s, const LLOSInfo& info)
{
info.stream(s);
return s;
}
std::ostream& operator<<(std::ostream& s, const LLCPUInfo& info)
{
info.stream(s);
return s;
}
std::ostream& operator<<(std::ostream& s, const LLMemoryInfo& info)
{
info.stream(s);
return s;
}
BOOL gunzip_file(const char *srcfile, const char *dstfile)
{
char tmpfile[LL_MAX_PATH]; /* Flawfinder: ignore */
const S32 UNCOMPRESS_BUFFER_SIZE = 32768;
BOOL retval = FALSE;
gzFile src = NULL;
U8 buffer[UNCOMPRESS_BUFFER_SIZE];
FILE *dst = NULL;
S32 bytes = 0;
(void *) strcpy(tmpfile, dstfile); /* Flawfinder: ignore */
(void *) strncat(tmpfile, ".t", sizeof(tmpfile) - strlen(tmpfile) -1); /* Flawfinder: ignore */
src = gzopen(srcfile, "rb");
if (! src) goto err;
dst = LLFile::fopen(tmpfile, "wb"); /* Flawfinder: ignore */
if (! dst) goto err;
do
{
bytes = gzread(src, buffer, UNCOMPRESS_BUFFER_SIZE);
size_t nwrit = fwrite(buffer, sizeof(U8), bytes, dst);
if (nwrit < (size_t) bytes)
{
llerrs << "Short write on " << tmpfile << llendl;
}
} while(gzeof(src) == 0);
fclose(dst);
dst = NULL;
if (LLFile::rename(tmpfile, dstfile) == -1) goto err; /* Flawfinder: ignore */
retval = TRUE;
err:
if (src != NULL) gzclose(src);
if (dst != NULL) fclose(dst);
return retval;
}
BOOL gzip_file(const char *srcfile, const char *dstfile)
{
const S32 COMPRESS_BUFFER_SIZE = 32768;
char tmpfile[LL_MAX_PATH]; /* Flawfinder: ignore */
BOOL retval = FALSE;
U8 buffer[COMPRESS_BUFFER_SIZE];
gzFile dst = NULL;
FILE *src = NULL;
S32 bytes = 0;
(void *) strcpy(tmpfile, dstfile); /* Flawfinder: ignore */
(void *) strncat(tmpfile, ".t", sizeof(tmpfile) - strlen(tmpfile) -1); /* Flawfinder: ignore */
dst = gzopen(tmpfile, "wb"); /* Flawfinder: ignore */
if (! dst) goto err;
src = LLFile::fopen(srcfile, "rb"); /* Flawfinder: ignore */
if (! src) goto err;
do
{
bytes = (S32)fread(buffer, sizeof(U8), COMPRESS_BUFFER_SIZE,src);
gzwrite(dst, buffer, bytes);
} while(feof(src) == 0);
gzclose(dst);
dst = NULL;
#if LL_WINDOWS
// Rename in windows needs the dstfile to not exist.
LLFile::remove(dstfile);
#endif
if (LLFile::rename(tmpfile, dstfile) == -1) goto err; /* Flawfinder: ignore */
retval = TRUE;
err:
if (src != NULL) fclose(src);
if (dst != NULL) gzclose(dst);
return retval;
}
|