Newer
Older
/**
* @file llprocessor.cpp
* @brief Code to figure out the processor. Originally by Benjamin Jurke.
*
* $LicenseInfo:firstyear=2002&license=viewergpl$
*
* Copyright (c) 2002-2009, Linden Research, Inc.
*
* Second Life Viewer Source Code
* The source code in this file ("Source Code") is provided by Linden Lab
* to you under the terms of the GNU General Public License, version 2.0
* ("GPL"), unless you have obtained a separate licensing agreement
* ("Other License"), formally executed by you and Linden Lab. Terms of
* the GPL can be found in doc/GPL-license.txt in this distribution, or
* online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
*
* There are special exceptions to the terms and conditions of the GPL as
* it is applied to this Source Code. View the full text of the exception
* in the file doc/FLOSS-exception.txt in this software distribution, or
* online at
* http://secondlifegrid.net/programs/open_source/licensing/flossexception
*
* By copying, modifying or distributing this software, you acknowledge
* that you have read and understood your obligations described above,
* and agree to abide by those obligations.
*
* ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
* WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
* COMPLETENESS OR PERFORMANCE.
* $/LicenseInfo$
Mark Palange (Mani)
committed
#include "linden_common.h"
#include "llprocessor.h"
#include "llerror.h"
Mark Palange (Mani)
committed
//#include <memory>
#if LL_WINDOWS
# define WIN32_LEAN_AND_MEAN
# include <winsock2.h>
# include <windows.h>
Mark Palange (Mani)
committed
#endif
Mark Palange (Mani)
committed
class LLProcessorInfoImpl; // foward declaration for the mImpl;
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
enum cpu_info
{
eBrandName = 0,
eFrequency,
eVendor,
eStepping,
eFamily,
eExtendedFamily,
eModel,
eExtendedModel,
eType,
eBrandID,
eFamilyName
};
const char* cpu_info_names[] =
{
"Processor Name",
"Frequency",
"Vendor",
"Stepping",
"Family",
"Extended Family",
"Model",
"Extended Model",
"Type",
"Brand ID",
"Family Name"
};
enum cpu_config
{
eMaxID,
eMaxExtID,
eCLFLUSHCacheLineSize,
eAPICPhysicalID,
eCacheLineSize,
eL2Associativity,
eCacheSizeK,
eFeatureBits,
eExtFeatureBits
};
const char* cpu_config_names[] =
{
"Max Supported CPUID level",
"Max Supported Ext. CPUID level",
"CLFLUSH cache line size",
"APIC Physical ID",
"Cache Line Size",
"L2 Associativity",
"Cache Size",
"Feature Bits",
"Ext. Feature Bits"
};
// *NOTE:Mani - this contains the elements we reference directly and extensions beyond the first 32.
// The rest of the names are referenced by bit maks returned from cpuid.
enum cpu_features
{
eSSE_Ext=25,
eSSE2_Ext=26,
eSSE3_Features=32,
eMONTIOR_MWAIT=33,
eCPLDebugStore=34,
eThermalMonitor2=35,
eAltivec=36
};
const char* cpu_feature_names[] =
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
{
"x87 FPU On Chip",
"Virtual-8086 Mode Enhancement",
"Debugging Extensions",
"Page Size Extensions",
"Time Stamp Counter",
"RDMSR and WRMSR Support",
"Physical Address Extensions",
"Machine Check Exception",
"CMPXCHG8B Instruction",
"APIC On Chip",
"Unknown1",
"SYSENTER and SYSEXIT",
"Memory Type Range Registers",
"PTE Global Bit",
"Machine Check Architecture",
"Conditional Move/Compare Instruction",
"Page Attribute Table",
"Page Size Extension",
"Processor Serial Number",
"CFLUSH Extension",
"Unknown2",
"Debug Store",
"Thermal Monitor and Clock Ctrl",
"MMX Technology",
"FXSAVE/FXRSTOR",
"SSE Extensions",
"SSE2 Extensions",
"Self Snoop",
"Hyper-threading Technology",
"Thermal Monitor",
"Unknown4",
"Pend. Brk. EN.", // 31 End of FeatureInfo bits
"SSE3 New Instructions", // 32
"MONITOR/MWAIT",
"CPL Qualified Debug Store",
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
std::string compute_CPUFamilyName(const char* cpu_vendor, int family, int ext_family)
{
const char* intel_string = "GenuineIntel";
const char* amd_string = "AuthenticAMD";
if(!strncmp(cpu_vendor, intel_string, strlen(intel_string)))
{
U32 composed_family = family + ext_family;
switch(composed_family)
{
case 3: return "Intel i386";
case 4: return "Intel i486";
case 5: return "Intel Pentium";
case 6: return "Intel Pentium Pro/2/3, Core";
case 7: return "Intel Itanium (IA-64)";
case 0xF: return "Intel Pentium 4";
case 0x10: return "Intel Itanium 2 (IA-64)";
default: return "Unknown";
}
}
else if(!strncmp(cpu_vendor, amd_string, strlen(amd_string)))
{
U32 composed_family = (family == 0xF)
? family + ext_family
: family;
switch(composed_family)
{
case 4: return "AMD 80486/5x86";
case 5: return "AMD K5/K6";
case 6: return "AMD K7";
case 0xF: return "AMD K8";
case 0x10: return "AMD K8L";
default: return "Unknown";
}
}
return "Unknown";
}
} // end unnamed namespace
// The base class for implementations.
Mark Palange (Mani)
committed
// Each platform should override this class.
class LLProcessorInfoImpl
{
public:
LLProcessorInfoImpl()
{
mProcessorInfo["info"] = LLSD::emptyMap();
mProcessorInfo["config"] = LLSD::emptyMap();
mProcessorInfo["extension"] = LLSD::emptyMap();
}
Mark Palange (Mani)
committed
virtual ~LLProcessorInfoImpl() {}
F64 getCPUFrequency() const
{
return getInfo(eFrequency, 0).asReal();
}
bool hasSSE() const
{
return hasExtension(cpu_feature_names[eSSE_Ext]);
}
bool hasSSE2() const
{
return hasExtension(cpu_feature_names[eSSE2_Ext]);
}
bool hasAltivec() const
{
return hasExtension("Altivec");
}
std::string getCPUFamilyName() const { return getInfo(eFamilyName, "Unknown").asString(); }
std::string getCPUBrandName() const { return getInfo(eBrandName, "Unknown").asString(); }
std::string getCPUFeatureDescription() const
{
std::ostringstream out;
out << std::endl << std::endl;
out << "// CPU General Information" << std::endl;
out << "//////////////////////////" << std::endl;
out << "Processor Name: " << getCPUBrandName() << std::endl;
out << "Frequency: " << getCPUFrequency() / (F64)1000000 << " MHz" << std::endl;
out << "Vendor: " << getInfo(eVendor, "Unknown").asString() << std::endl;
out << "Family: " << getCPUFamilyName() << " (" << getInfo(eFamily, 0) << ")" << std::endl;
out << "Extended family: " << getInfo(eExtendedFamily, 0) << std::endl;
out << "Model: " << getInfo(eModel, 0) << std::endl;
out << "Extended model: " << getInfo(eExtendedModel, 0) << std::endl;
out << "Type: " << getInfo(eType, 0) << std::endl;
out << "Brand ID: " << getInfo(eBrandID, 0) << std::endl;
out << std::endl;
out << "// CPU Configuration" << std::endl;
out << "//////////////////////////" << std::endl;
// Iterate through the dictionary of configuration options.
LLSD configs = mProcessorInfo["config"];
for(LLSD::map_const_iterator cfgItr = configs.beginMap(); cfgItr != configs.endMap(); ++cfgItr)
{
out << cfgItr->first << " = " << cfgItr->second << std::endl;
}
out << "// CPU Extensions" << std::endl;
out << "//////////////////////////" << std::endl;
for(LLSD::map_const_iterator itr = mProcessorInfo["extension"].beginMap(); itr != mProcessorInfo["extension"].endMap(); ++itr)
{
out << " " << itr->first << std::endl;
}
return out.str();
}
protected:
void setInfo(cpu_info info_type, const LLSD& value)
{
setInfo(cpu_info_names[info_type], value);
}
LLSD getInfo(cpu_info info_type, const LLSD& defaultVal) const
{
return getInfo(cpu_info_names[info_type], defaultVal);
}
void setConfig(cpu_config config_type, const LLSD& value)
{
setConfig(cpu_config_names[config_type], value);
}
LLSD getConfig(cpu_config config_type, const LLSD& defaultVal) const
{
return getConfig(cpu_config_names[config_type], defaultVal);
}
void setExtension(const std::string& name) { mProcessorInfo["extension"][name] = "true"; }
bool hasExtension(const std::string& name) const
{
return mProcessorInfo["extension"].has(name);
}
private:
void setInfo(const std::string& name, const LLSD& value) { mProcessorInfo["info"][name]=value; }
LLSD getInfo(const std::string& name, const LLSD& defaultVal) const
{
LLSD r = mProcessorInfo["info"].get(name);
return r.isDefined() ? r : defaultVal;
}
void setConfig(const std::string& name, const LLSD& value) { mProcessorInfo["config"][name]=value; }
LLSD getConfig(const std::string& name, const LLSD& defaultVal) const
{
LLSD r = mProcessorInfo["config"].get(name);
return r.isDefined() ? r : defaultVal;
}
private:
Mark Palange (Mani)
committed
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
};
#ifdef LL_MSVC
// LL_MSVC and not LLWINDOWS because some of the following code
// uses the MSVC compiler intrinsics __cpuid() and __rdtsc().
// Delays for the specified amount of milliseconds
static void _Delay(unsigned int ms)
{
LARGE_INTEGER freq, c1, c2;
__int64 x;
// Get High-Res Timer frequency
if (!QueryPerformanceFrequency(&freq))
return;
// Convert ms to High-Res Timer value
x = freq.QuadPart/1000*ms;
// Get first snapshot of High-Res Timer value
QueryPerformanceCounter(&c1);
do
{
// Get second snapshot
QueryPerformanceCounter(&c2);
}while(c2.QuadPart-c1.QuadPart < x);
// Loop while (second-first < x)
}
static F64 calculate_cpu_frequency(U32 measure_msecs)
{
if(measure_msecs == 0)
{
return 0;
}
// After that we declare some vars and check the frequency of the high
// resolution timer for the measure process.
// If there"s no high-res timer, we exit.
Mark Palange (Mani)
committed
unsigned __int64 starttime, endtime, timedif, freq, start, end, dif;
if (!QueryPerformanceFrequency((LARGE_INTEGER *) &freq))
{
return 0;
}
// Now we can init the measure process. We set the process and thread priority
// to the highest available level (Realtime priority). Also we focus the
// first processor in the multiprocessor system.
HANDLE hProcess = GetCurrentProcess();
HANDLE hThread = GetCurrentThread();
unsigned long dwCurPriorityClass = GetPriorityClass(hProcess);
int iCurThreadPriority = GetThreadPriority(hThread);
unsigned long dwProcessMask, dwSystemMask, dwNewMask = 1;
GetProcessAffinityMask(hProcess, &dwProcessMask, &dwSystemMask);
SetPriorityClass(hProcess, REALTIME_PRIORITY_CLASS);
SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
SetProcessAffinityMask(hProcess, dwNewMask);
//// Now we call a CPUID to ensure, that all other prior called functions are
//// completed now (serialization)
//__asm cpuid
int cpu_info[4] = {-1};
__cpuid(cpu_info, 0);
Mark Palange (Mani)
committed
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
// We ask the high-res timer for the start time
QueryPerformanceCounter((LARGE_INTEGER *) &starttime);
// Then we get the current cpu clock and store it
start = __rdtsc();
// Now we wart for some msecs
_Delay(measure_msecs);
// Sleep(uiMeasureMSecs);
// We ask for the end time
QueryPerformanceCounter((LARGE_INTEGER *) &endtime);
// And also for the end cpu clock
end = __rdtsc();
// Now we can restore the default process and thread priorities
SetProcessAffinityMask(hProcess, dwProcessMask);
SetThreadPriority(hThread, iCurThreadPriority);
SetPriorityClass(hProcess, dwCurPriorityClass);
// Then we calculate the time and clock differences
dif = end - start;
timedif = endtime - starttime;
// And finally the frequency is the clock difference divided by the time
// difference.
F64 frequency = (F64)dif / (((F64)timedif) / freq);
// At last we just return the frequency that is also stored in the call
// member var uqwFrequency
return frequency;
}
// Windows implementation
class LLProcessorInfoWindowsImpl : public LLProcessorInfoImpl
{
public:
LLProcessorInfoWindowsImpl() :
{
getCPUIDInfo();
AddInfoItem("Frequency", calculate_cpu_frequency(50));
Mark Palange (Mani)
committed
}
private:
void getCPUIDInfo()
Mark Palange (Mani)
committed
{
// http://msdn.microsoft.com/en-us/library/hskdteyh(VS.80).aspx
// __cpuid with an InfoType argument of 0 returns the number of
// valid Ids in cpu_info[0] and the CPU identification string in
// the other three array elements. The CPU identification string is
// not in linear order. The code below arranges the information
// in a human readable form.
int cpu_info[4] = {-1};
__cpuid(cpu_info, 0);
unsigned int ids = (unsigned int)cpu_info[0];
char cpu_vendor[0x20];
memset(cpu_vendor, 0, sizeof(cpu_vendor));
*((int*)cpu_vendor) = cpu_info[1];
*((int*)(cpu_vendor+4)) = cpu_info[3];
*((int*)(cpu_vendor+8)) = cpu_info[2];
setInfo(eVendor, cpu_vendor);
// Get the information associated with each valid Id
for(unsigned int i=0; i<=ids; ++i)
Mark Palange (Mani)
committed
// Interpret CPU feature information.
if (i == 1)
{
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
setInfo(eStepping, cpu_info[0] & 0xf);
setInfo(eModel, (cpu_info[0] >> 4) & 0xf);
int family = (cpu_info[0] >> 8) & 0xf;
setInfo(eFamily, family);
setInfo(eType, (cpu_info[0] >> 12) & 0x3);
setInfo(eExtendedModel, (cpu_info[0] >> 16) & 0xf);
int ext_family = (cpu_info[0] >> 20) & 0xff;
setInfo(eExtendedFamily, ext_family);
setInfo(eBrandIndex, cpu_info[1] & 0xff);
setInfo(eFamilyName, compute_CPUFamilyName(family, ext_family));
setConfig(eCLFLUSHCacheLineSize, ((cpu_info[1] >> 8) & 0xff) * 8);
setConfig(eAPICPhysicalID, (cpu_info[1] >> 24) & 0xff);
if(cpu_info[2] & 0x1)
{
setExtension(cpu_feature_names[eSSE3_Features]);
}
if(cpu_info[2] & 0x8)
{
setExtension(cpu_feature_names[eMONTIOR_MWAIT]);
}
if(cpu_info[2] & 0x10) || false;
{
setExtension(cpu_feature_names[eCPLDebugStore]);
}
if(cpu_info[2] & 0x100)
{
setExtension(cpu_feature_names[eThermalMonitor2]);
}
unsigned int feature_info = (unsigned int) cpu_info[3];
for(unsigned int index = 0, bit = 1; index < eSSE3_Features; ++index, bit <<= 1)
{
if(feature_info & bit)
{
setExtension(cpu_feature_names[index]);
}
}
}
}
// Calling __cpuid with 0x80000000 as the InfoType argument
// gets the number of valid extended IDs.
__cpuid(cpu_info, 0x80000000);
unsigned int ext_ids = cpu_info[0];
setConfig(eMaxExtID, 0);
char cpu_brand_string[0x40];
memset(cpu_brand_string, 0, sizeof(cpu_brand_string));
// Get the information associated with each extended ID.
for(unsigned int i=0x80000000; i<=mExtIds; ++i)
{
__cpuid(cpu_info, i);
// Interpret CPU brand string and cache information.
if (i == 0x80000002)
memcpy(cpu_brand_string, cpu_info, sizeof(cpu_info));
memcpy(cpu_brand_string + 16, cpu_info, sizeof(cpu_info));
Mark Palange (Mani)
committed
{
memcpy(cpu_brand_string + 32, cpu_info, sizeof(cpu_info));
setInfo(eBrandName, cpu_brand_string);
Mark Palange (Mani)
committed
}
else if (i == 0x80000006)
Mark Palange (Mani)
committed
{
setConfig(eCacheLineSize, cpu_info[2] & 0xff);
setConfig(eL2Associativity, (cpu_info[2] >> 12) & 0xf);
setConfig(eCacheSizeK, (cpu_info[2] >> 16) & 0xffff);
Mark Palange (Mani)
committed
}
}
}
Mark Palange (Mani)
committed
Mark Palange (Mani)
committed
#include <mach/machine.h>
#include <sys/sysctl.h>
class LLProcessorInfoDarwinImpl : public LLProcessorInfoImpl
{
public:
LLProcessorInfoDarwinImpl()
Mark Palange (Mani)
committed
{
uint64_t frequency = getSysctlInt64("hw.cpufrequency");
setInfo(eFrequency, (F64)frequency);
Mark Palange (Mani)
committed
}
virtual ~LLProcessorInfoDarwinImpl() {}
virtual F64 getCPUFrequency() const { return 0; }
virtual bool hasSSE() const { return false; }
virtual bool hasSSE2() const { return false; }
virtual bool hasAltivec() const { return false; }
virtual std::string getCPUFamilyName() const { return "Unknown"; }
virtual std::string getCPUBrandName() const { return "Unknown"; }
virtual std::string getCPUFeatureDescription() const { return "Unknown"; }
Mark Palange (Mani)
committed
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
private:
int getSysctlInt(const char* name)
{
int result = 0;
size_t len = sizeof(int);
int error = sysctlbyname(name, (void*)&result, &len, NULL, 0);
return error == -1 ? 0 : result;
}
uint64_t getSysctlInt64(const char* name)
{
uint64_t value = 0;
size_t size = sizeof(value);
int result = sysctlbyname(name, (void*)&value, &size, NULL, 0);
if ( result == 0 )
{
if ( size == sizeof( uint64_t ) )
;
else if ( size == sizeof( uint32_t ) )
value = (uint64_t)(( uint32_t *)&value);
else if ( size == sizeof( uint16_t ) )
value = (uint64_t)(( uint16_t *)&value);
else if ( size == sizeof( uint8_t ) )
value = (uint64_t)(( uint8_t *)&value);
else
{
LL_ERRS("Unknown type returned from sysctl!") << LL_ENDL;
}
}
return result == -1 ? 0 : value;
}
void getCPUIDInfo()
{
size_t len = 0;
char cpu_brand_string[0x40];
len = sizeof(cpu_brand_string);
memset(cpu_brand_string, 0, len);
sysctlbyname("machdep.cpu.brand_string", (void*)cpu_brand_string, &len, NULL, 0);
cpu_brand_string[0x3f] = 0;
setInfo(eBrandName, cpu_brand_string);
char cpu_vendor[0x20];
len = sizeof(cpu_vendor);
memset(cpu_vendor, 0, len);
sysctlbyname("machdep.cpu.vendor", (void*)cpu_vendor, &len, NULL, 0);
cpu_vendor[0x1f] = 0;
setInfo(eVendor, cpu_vendor);
setInfo(eStepping, getSysctlInt("machdep.cpu.stepping"));
setInfo(eModel, getSysctlInt("machdep.cpu.model"));
int family = getSysctlInt("machdep.cpu.family");
int ext_family = getSysctlInt("machdep.cpu.extfamily");
setInfo(eFamily, family);
setInfo(eExtendedFamily, ext_family);
setInfo(eFamilyName, compute_CPUFamilyName(cpu_vendor, family, ext_family));
setInfo(eExtendedModel, getSysctlInt("machdep.cpu.extmodel"));
setInfo(eBrandID, getSysctlInt("machdep.cpu.brand"));
setInfo(eType, 0); // ? where to find this?
//setConfig(eCLFLUSHCacheLineSize, ((cpu_info[1] >> 8) & 0xff) * 8);
//setConfig(eAPICPhysicalID, (cpu_info[1] >> 24) & 0xff);
setConfig(eCacheLineSize, getSysctlInt("machdep.cpu.cache.linesize"));
setConfig(eL2Associativity, getSysctlInt("machdep.cpu.cache.L2_associativity"));
setConfig(eCacheSizeK, getSysctlInt("machdep.cpu.cache.size"));
uint64_t feature_info = getSysctlInt64("machdep.cpu.feature_bits");
S32 *feature_infos = (S32*)(&feature_info);
setConfig(eFeatureBits, feature_infos[0]);
for(unsigned int index = 0, bit = 1; index < eSSE3_Features; ++index, bit <<= 1)
{
if(feature_info & bit)
{
setExtension(cpu_feature_names[index]);
}
}
// *NOTE:Mani - I didn't find any docs that assure me that machdep.cpu.feature_bits will always be
// The feature bits I think it is. Here's a test:
#ifndef LL_RELEASE_FOR_DOWNLOAD
#if defined(__i386__) && defined(__PIC__)
/* %ebx may be the PIC register. */
#define __cpuid(level, a, b, c, d) \
__asm__ ("xchgl\t%%ebx, %1\n\t" \
"cpuid\n\t" \
"xchgl\t%%ebx, %1\n\t" \
: "=a" (a), "=r" (b), "=c" (c), "=d" (d) \
: "0" (level))
#else
#define __cpuid(level, a, b, c, d) \
__asm__ ("cpuid\n\t" \
: "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
: "0" (level))
#endif
unsigned int eax, ebx, ecx, edx;
__cpuid(0x1, eax, ebx, ecx, edx);
if(feature_infos[0] != (S32)edx)
{
llerrs << "machdep.cpu.feature_bits doesn't match expected cpuid result!" << llendl;
}
#endif // LL_RELEASE_FOR_DOWNLOAD
Mark Palange (Mani)
committed
uint64_t ext_feature_info = getSysctlInt64("machdep.cpu.extfeature_bits");
S32 *ext_feature_infos = (S32*)(&ext_feature_info);
setConfig(eExtFeatureBits, ext_feature_infos[0]);
}
};
Mark Palange (Mani)
committed
Mark Palange (Mani)
committed
//////////////////////////////////////////////////////
// Interface definition
LLProcessorInfo::LLProcessorInfo() : mImpl(NULL)
Mark Palange (Mani)
committed
{
// *NOTE:Mani - not thread safe.
Mark Palange (Mani)
committed
{
#ifdef LL_MSVC
static LLProcessorInfoWindowsImpl the_impl;
mImpl = &the_impl;
static LLProcessorInfoDarwinImpl the_impl;
mImpl = &the_impl;
Mark Palange (Mani)
committed
#error "Unimplemented"
#endif // LL_MSVC
}
}
LLProcessorInfo::~LLProcessorInfo() {}
F64 LLProcessorInfo::getCPUFrequency() const { return mImpl->getCPUFrequency(); }
bool LLProcessorInfo::hasSSE() const { return mImpl->hasSSE(); }
bool LLProcessorInfo::hasSSE2() const { return mImpl->hasSSE2(); }
bool LLProcessorInfo::hasAltivec() const { return mImpl->hasAltivec(); }
std::string LLProcessorInfo::getCPUFamilyName() const { return mImpl->getCPUFamilyName(); }
std::string LLProcessorInfo::getCPUBrandName() const { return mImpl->getCPUBrandName(); }
std::string LLProcessorInfo::getCPUFeatureDescription() const { return mImpl->getCPUFeatureDescription(); }
Mark Palange (Mani)
committed
#if 0
// Filename: Processor.cpp
// =======================
// Author: Benjamin Jurke
// File history: 27.02.2002 - File created. Support for Intel and AMD processors
// 05.03.2002 - Fixed the CPUID bug: On Pre-Pentium CPUs the CPUID
// command is not available
// - The CProcessor::WriteInfoTextFile function do not
// longer use Win32 file functions (-> os independend)
// - Optional include of the windows.h header which is
// still need for CProcessor::GetCPUFrequency.
// 06.03.2002 - My birthday (18th :-))
// - Replaced the "\r\n" line endings in function
// CProcessor::cpu_infoToText by "\n"
// - Replaced unsigned __int64 by signed __int64 for
// solving some compiler conversion problems
// - Fixed a bug at family=6, model=6 (Celeron -> P2)
//////////////////////////////////////////////////////////////////////////////////
#include "processor.h"
Aaron Brashears
committed
#include <memory>
#if LL_WINDOWS
# define WIN32_LEAN_AND_MEAN
# include <winsock2.h>
# include <windows.h>
#endif
Don Kjer
committed
#if !LL_DARWIN && !LL_SOLARIS
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
#ifdef PROCESSOR_FREQUENCY_MEASURE_AVAILABLE
// We need the QueryPerformanceCounter and Sleep functions
#define FORCEINLINE __forceinline
#else
#define FORCEINLINE
#endif
// Some macros we often need
////////////////////////////
#define CheckBit(var, bit) ((var & (1 << bit)) ? true : false)
#ifdef PROCESSOR_FREQUENCY_MEASURE_AVAILABLE
// Delays for the specified amount of milliseconds
static void _Delay(unsigned int ms)
{
LARGE_INTEGER freq, c1, c2;
__int64 x;
// Get High-Res Timer frequency
if (!QueryPerformanceFrequency(&freq))
return;
// Convert ms to High-Res Timer value
x = freq.QuadPart/1000*ms;
// Get first snapshot of High-Res Timer value
QueryPerformanceCounter(&c1);
do
{
// Get second snapshot
QueryPerformanceCounter(&c2);
}while(c2.QuadPart-c1.QuadPart < x);
// Loop while (second-first < x)
}
#endif
// CProcessor::CProcessor
// ======================
// Class constructor:
/////////////////////////
CProcessor::CProcessor()
{
uqwFrequency = 0;
Josh Bell
committed
strCPUName[0] = 0;
Mark Palange (Mani)
committed
memset(&cpu_info, 0, sizeof(cpu_info));
}
// unsigned __int64 CProcessor::GetCPUFrequency(unsigned int uiMeasureMSecs)
// =========================================================================
// Function to measure the current CPU frequency
////////////////////////////////////////////////////////////////////////////
F64 CProcessor::GetCPUFrequency(unsigned int uiMeasureMSecs)
{
#ifndef PROCESSOR_FREQUENCY_MEASURE_AVAILABLE
return 0;
#else
// If there are invalid measure time parameters, zero msecs for example,
// we"ve to exit the function
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
if (uiMeasureMSecs < 1)
{
// If theres already a measured frequency available, we return it
if (uqwFrequency > 0)
return uqwFrequency;
else
return 0;
}
// Now we check if the CPUID command is available
if (!CheckCPUIDPresence())
return 0;
// First we get the CPUID standard level 0x00000001
unsigned long reg;
__asm
{
mov eax, 1
cpuid
mov reg, edx
}
// Then we check, if the RDTSC (Real Date Time Stamp Counter) is available.
// This function is necessary for our measure process.
if (!(reg & (1 << 4)))
return 0;
// After that we declare some vars and check the frequency of the high
// resolution timer for the measure process.
// If there"s no high-res timer, we exit.
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
__int64 starttime, endtime, timedif, freq, start, end, dif;
if (!QueryPerformanceFrequency((LARGE_INTEGER *) &freq))
return 0;
// Now we can init the measure process. We set the process and thread priority
// to the highest available level (Realtime priority). Also we focus the
// first processor in the multiprocessor system.
HANDLE hProcess = GetCurrentProcess();
HANDLE hThread = GetCurrentThread();
unsigned long dwCurPriorityClass = GetPriorityClass(hProcess);
int iCurThreadPriority = GetThreadPriority(hThread);
unsigned long dwProcessMask, dwSystemMask, dwNewMask = 1;
GetProcessAffinityMask(hProcess, &dwProcessMask, &dwSystemMask);
SetPriorityClass(hProcess, REALTIME_PRIORITY_CLASS);
SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
SetProcessAffinityMask(hProcess, dwNewMask);
// Now we call a CPUID to ensure, that all other prior called functions are
// completed now (serialization)
__asm cpuid
// We ask the high-res timer for the start time
QueryPerformanceCounter((LARGE_INTEGER *) &starttime);
// Then we get the current cpu clock and store it
__asm
{
rdtsc
mov dword ptr [start+4], edx
mov dword ptr [start], eax
}
// Now we wart for some msecs
_Delay(uiMeasureMSecs);
// Sleep(uiMeasureMSecs);
// We ask for the end time
QueryPerformanceCounter((LARGE_INTEGER *) &endtime);
// And also for the end cpu clock
__asm
{
rdtsc
mov dword ptr [end+4], edx
mov dword ptr [end], eax
}
// Now we can restore the default process and thread priorities
SetProcessAffinityMask(hProcess, dwProcessMask);
SetThreadPriority(hThread, iCurThreadPriority);
SetPriorityClass(hProcess, dwCurPriorityClass);
// Then we calculate the time and clock differences
dif = end - start;
timedif = endtime - starttime;
// And finally the frequency is the clock difference divided by the time
// difference.
uqwFrequency = (F64)dif / (((F64)timedif) / freq);
// At last we just return the frequency that is also stored in the call
// member var uqwFrequency
return uqwFrequency;
#endif
}
// bool CProcessor::AnalyzeIntelProcessor()
// ========================================
// Private class function for analyzing an Intel processor
//////////////////////////////////////////////////////////
bool CProcessor::AnalyzeIntelProcessor()
{
Mark Palange (Mani)
committed
// *NOTE:Mani - http://www.intel.com/Assets/PDF/appnote/241618.pdf
// According to the above doc, a lot of this what follows is wrong.
#if LL_WINDOWS
unsigned long eaxreg, ebxreg, edxreg;
// First we check if the CPUID command is available
if (!CheckCPUIDPresence())
return false;
// Now we get the CPUID standard level 0x00000001
__asm
{
mov eax, 1
cpuid
mov eaxreg, eax
mov ebxreg, ebx
mov edxreg, edx
}
// Then get the cpu model, family, type, stepping and brand id by masking
// the eax and ebx register
Mark Palange (Mani)
committed
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
cpu_info.uiStepping = eaxreg & 0xF;
cpu_info.uiModel = (eaxreg >> 4) & 0xF;
cpu_info.uiFamily = (eaxreg >> 8) & 0xF;
cpu_info.uiType = (eaxreg >> 12) & 0x3;
cpu_info.uiBrandID = ebxreg & 0xF;
// *NOTE:Mani - see http://www.intel.com/assets/pdf/appnote/241618.pdf
// These values are composed according to section 2.1.2.2 of the above doc.
cpu_info.uiExtendedFamily = ((eaxreg >> 20) & 0xFF) + cpu_info.uiFamily;
cpu_info.uiExtendedModel = (((eaxreg >> 16) & 0xFF) << 4) + cpu_info.uiModel;
// Getting the Brand ID string if supported.
if (cpu_info.MaxSupportedExtendedLevel >= 0x80000004)
{
// If it supports the extended CPUID level 0x80000004 we read the data
char tmp[52]; /* Flawfinder: ignore */
memset(tmp, 0, sizeof(tmp));
__asm
{
mov eax, 0x80000002
cpuid
mov dword ptr [tmp], eax
mov dword ptr [tmp+4], ebx
mov dword ptr [tmp+8], ecx
mov dword ptr [tmp+12], edx
mov eax, 0x80000003
cpuid
mov dword ptr [tmp+16], eax
mov dword ptr [tmp+20], ebx
mov dword ptr [tmp+24], ecx
mov dword ptr [tmp+28], edx
mov eax, 0x80000004
cpuid
mov dword ptr [tmp+32], eax
mov dword ptr [tmp+36], ebx
mov dword ptr [tmp+40], ecx
mov dword ptr [tmp+44], edx
}
// And copy it to the brand id string
strncpy(cpu_info.strBrandID, tmp,sizeof(cpu_info.strBrandID)-1);
cpu_info.strBrandID[sizeof(cpu_info.strBrandID)-1]="\0";
Mark Palange (Mani)
committed
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
}
else
{
static const char* INTEL_BRAND[] =
{
/* 0x00 */ "",
/* 0x01 */ "0.18 micron Intel Celeron",
/* 0x02 */ "0.18 micron Intel Pentium III",
/* 0x03 */ "0.13 micron Intel Celeron",
/* 0x04 */ "0.13 micron Intel Pentium III",
/* 0x05 */ "",
/* 0x06 */ "0.13 micron Intel Pentium III Mobile",
/* 0x07 */ "0.13 micron Intel Celeron Mobile",
/* 0x08 */ "0.18 micron Intel Pentium 4",
/* 0x09 */ "0.13 micron Intel Pentium 4",
/* 0x0A */ "0.13 micron Intel Celeron",
/* 0x0B */ "0.13 micron Intel Pentium 4 Xeon",
/* 0x0C */ "Intel Xeon MP",
/* 0x0D */ "",
/* 0x0E */ "0.18 micron Intel Pentium 4 Xeon",
/* 0x0F */ "Mobile Intel Celeron",
/* 0x10 */ "",
/* 0x11 */ "Mobile Genuine Intel",
/* 0x12 */ "Intel Celeron M",
/* 0x13 */ "Mobile Intel Celeron",
/* 0x14 */ "Intel Celeron",
/* 0x15 */ "Mobile Genuine Intel",
/* 0x16 */ "Intel Pentium M",
/* 0x17 */ "Mobile Intel Celeron",
};
// Only override the brand if we have it in the lookup table. We should
// already have a string here from Getcpu_info(). JC