~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/NCPU.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-02 03:28:11 UTC
  • Revision ID: neil.patel@canonical.com-20100902032811-i2m18tfb6pkasnvt
Remove Win EOL chars

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 */
21
21
 
22
22
 
23
 
#include "NKernel.h"
24
 
 
25
 
#ifdef _WIN32
26
 
#include <intrin.h>
27
 
 
28
 
NAMESPACE_BEGIN
29
 
 
30
 
INL_IMPLEMENT_GLOBAL_OBJECT(NCPU);
31
 
 
32
 
//============================================================================
33
 
// Calculate and log the amount of RAM in the machine
34
 
//============================================================================
35
 
static t_u64 GetTotalPhysRAM_MB()
36
 
{
37
 
    // Get memory status
38
 
    MEMORYSTATUSEX theStatus;
39
 
    ZeroMemory(&theStatus,sizeof(theStatus));
40
 
    theStatus.dwLength = sizeof(theStatus);
41
 
    GlobalMemoryStatusEx(&theStatus);
42
 
 
43
 
    return theStatus.ullTotalPhys/(1024*1024);
44
 
}
45
 
 
46
 
//============================================================================
47
 
// Read the CPU speed from the registry
48
 
//============================================================================
49
 
static DWORD GetCPUSpeedFromRegistry(DWORD dwCPU)
50
 
{
51
 
    HKEY hKey;
52
 
    DWORD dwSpeed;
53
 
 
54
 
    // Get the key name
55
 
    TCHAR szKey[256];
56
 
    _sntprintf_s(szKey, 256, sizeof(szKey)/sizeof(wchar_t), TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\%d\\"), dwCPU);
57
 
 
58
 
    // Open the key
59
 
    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKey, 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
60
 
    {
61
 
        return 0;
62
 
    }
63
 
 
64
 
    // Read the value
65
 
    DWORD dwLen = 4;
66
 
    if(RegQueryValueEx(hKey, TEXT("~MHz"), NULL, NULL, (LPBYTE)&dwSpeed, &dwLen) != ERROR_SUCCESS)
67
 
    {
68
 
        RegCloseKey(hKey);
69
 
        return 0;
70
 
    }
71
 
 
72
 
    // Cleanup and return
73
 
    RegCloseKey(hKey);
74
 
    return dwSpeed;
75
 
}
76
 
 
77
 
void NCPU::Constructor()
78
 
{
79
 
    unsigned nHighestFeature;
80
 
    unsigned nHighestFeatureEx;
81
 
    int nBuff[4];
82
 
    char szMan[13];
83
 
 
84
 
    Memset(&m_CPUInfo, 0, sizeof(m_CPUInfo));
85
 
    Memset(&m_CPUFeatures, 0, sizeof(m_CPUFeatures));
86
 
 
87
 
    // Get CPU manufacturer and highest CPUID
88
 
    __cpuid(nBuff, 0);
89
 
 
90
 
    nHighestFeature = (unsigned)nBuff[0];
91
 
    *(int*)&szMan[0] = nBuff[1];
92
 
    *(int*)&szMan[4] = nBuff[3];
93
 
    *(int*)&szMan[8] = nBuff[2];
94
 
    szMan[12] = 0;
95
 
 
96
 
    if(strcmp(szMan, "AuthenticAMD") == 0)
97
 
    {
98
 
        m_CPUInfo.m_CPUType = CPU_AMD;
99
 
    }
100
 
    else if(strcmp(szMan, "GenuineIntel") == 0)
101
 
    {
102
 
        m_CPUInfo.m_CPUType = CPU_INTEL;
103
 
    }
104
 
    else
105
 
    {
106
 
        m_CPUInfo.m_CPUType = CPU_UNKNOWN;
107
 
    }
108
 
 
109
 
    m_CPUString = szMan;
110
 
 
111
 
    // Get highest extended feature
112
 
    __cpuid(nBuff, 0x80000000);
113
 
    nHighestFeatureEx = (unsigned)nBuff[0];
114
 
 
115
 
    // Get processor brand name
116
 
    if(nHighestFeatureEx >= 0x80000004)
117
 
    {
118
 
        char szCPUName[49];
119
 
        szCPUName[0] = 0;
120
 
        __cpuid((int*)&szCPUName[0], 0x80000002);
121
 
        __cpuid((int*)&szCPUName[16], 0x80000003);
122
 
        __cpuid((int*)&szCPUName[32], 0x80000004);
123
 
        szCPUName[48] = 0;
124
 
        
125
 
        m_CPUBrandString = szCPUName;
126
 
//         for(int i=(int)strlen(szCPUName)-1; i>=0; --i)
127
 
//         {
128
 
//             if(szCPUName[i] == ' ')
129
 
//                 szCPUName[i] = '\0';
130
 
//             else
131
 
//                 break;
132
 
//         }
133
 
    }
134
 
 
135
 
    // Get CPU features
136
 
    if(nHighestFeature >= 1)
137
 
    {
138
 
        __cpuid(nBuff, 1);
139
 
 
140
 
 
141
 
        m_CPUInfo.m_CPUSteppingID = nBuff[0] & 0xf;
142
 
        m_CPUInfo.m_CPUModel = (nBuff[0] >> 4) & 0xf;
143
 
        m_CPUInfo.m_CPUFamily = (nBuff[0] >> 8) & 0xf;
144
 
        //m_CPUInfo.nProcessorType = (nBuff[0] >> 12) & 0x3;
145
 
        m_CPUInfo.m_CPUExtModel = (nBuff[0] >> 16) & 0xf;
146
 
        m_CPUInfo.m_CPUExtFamily = (nBuff[0] >> 20) & 0xff;
147
 
        //nBrandIndex = nBuff[1] & 0xff;
148
 
        m_CPUInfo.m_CPUNumLogicalProcessors = ((nBuff[1] >> 16) & 0xff);
149
 
 
150
 
 
151
 
 
152
 
 
153
 
        if(nBuff[3] & 1<<0)
154
 
            m_CPUFeatures.m_HasFPU = true;
155
 
        if(nBuff[3] & 1<<23)
156
 
            m_CPUFeatures.m_HasMMX = true;
157
 
        if(nBuff[3] & 1<<25)
158
 
            m_CPUFeatures.m_HasSSE = true;
159
 
        if(nBuff[3] & 1<<26)
160
 
            m_CPUFeatures.m_HasSSE2 = true;
161
 
        if(nBuff[2] & 1<<0)
162
 
            m_CPUFeatures.m_HasSSE3 = true;
163
 
        if(nBuff[2] & 1<<19)
164
 
            m_CPUFeatures.m_HasSSE41 = true;
165
 
        if(nBuff[2] & 1<<20)
166
 
            m_CPUFeatures.m_HasSSE42 = true;
167
 
 
168
 
        // Intel specific:
169
 
        if(m_CPUInfo.m_CPUType == CPU_INTEL)
170
 
        {
171
 
            if(nBuff[2] & 1<<9)
172
 
                m_CPUFeatures.m_HasSSSE3 = true;
173
 
            if(nBuff[2] & 1<<7)
174
 
                m_CPUFeatures.m_HasEST = true;
175
 
        }
176
 
 
177
 
        if(nBuff[3] & 1<<28)
178
 
            m_CPUFeatures.m_HasHTT = true;
179
 
    }
180
 
 
181
 
    // AMD specific:
182
 
    if(m_CPUInfo.m_CPUType == CPU_AMD)
183
 
    {
184
 
        // Get extended features
185
 
        __cpuid(nBuff, 0x80000000);
186
 
        if(nHighestFeatureEx >= 0x80000001)
187
 
        {
188
 
            __cpuid(nBuff, 0x80000001);
189
 
            if(nBuff[3] & 1<<31)
190
 
                m_CPUFeatures.m_Has3DNow = true;
191
 
            if(nBuff[3] & 1<<30)
192
 
                m_CPUFeatures.m_Has3DNowExt = true; 
193
 
            if(nBuff[3] & 1<<22)
194
 
                m_CPUFeatures.m_HasAMDMMX = true;
195
 
        }
196
 
 
197
 
//         // Get level 1 cache size
198
 
//         if(nHighestFeatureEx >= 0x80000005)
199
 
//         {
200
 
//             __cpuid(nBuff, 0x80000005);
201
 
//             ELog::Get().SystemFormat(L"PERF    : L1 cache size: %dK\n", ((unsigned)nBuff[2])>>24);
202
 
//         }
203
 
    }
204
 
 
205
 
    if(nHighestFeatureEx >= 0x80000001)
206
 
    {
207
 
        __cpuid(nBuff, 0x80000001);
208
 
        if(nBuff[3] & 1 << 29)
209
 
            m_CPUFeatures.m_Hasx64 = true;
210
 
 
211
 
    }
212
 
 
213
 
    m_CPUInfo.m_CPUSpeed = GetCPUSpeedFromRegistry(0);
214
 
    m_MemInfo.m_PhysicalMemorySize = GetTotalPhysRAM_MB();
215
 
    GetOS();
216
 
 
217
 
    unsigned int dw = 0x00000001;
218
 
    m_IsLittleEndian = (*( (char*)&dw ) ? true : false);
219
 
 
220
 
    //m_MemInfo
221
 
//     // Get cache size
222
 
//     if(nHighestFeatureEx >= 0x80000006)
223
 
//     {
224
 
//         __cpuid(nBuff, 0x80000006);
225
 
//         ELog::Get().SystemFormat(L"PERF    : L2 cache size: %dK\n", ((unsigned)nBuff[2])>>16);
226
 
//     }
227
 
// 
228
 
//     // Log features
229
 
//     ELog::Get().SystemFormat(L"PERF    : CPU Features: %S\n", szFeatures);
230
 
// 
231
 
//     // Get misc system info
232
 
//     SYSTEM_INFO theInfo;
233
 
//     GetSystemInfo(&theInfo);
234
 
// 
235
 
//     // Log number of CPUs and speeds
236
 
//     ELog::Get().SystemFormat(L"PERF    : Number of CPUs: %d\n", theInfo.dwNumberOfProcessors);
237
 
//     for(DWORD i=0; i<theInfo.dwNumberOfProcessors; ++i)
238
 
//     {
239
 
//         DWORD dwCPUSpeed = ReadCPUSpeedFromRegistry(i);
240
 
//         ELog::Get().SystemFormat(L"PERF    : * CPU %d speed: ~%dMHz\n", i, dwCPUSpeed);
241
 
//     }
242
 
}
243
 
 
244
 
 
245
 
void NCPU::Destructor()
246
 
{
247
 
 
248
 
}
249
 
 
250
 
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
251
 
LPFN_ISWOW64PROCESS fnIsWow64Process;
252
 
 
253
 
 
254
 
//============================================================================
255
 
// Determine OS type
256
 
//============================================================================
257
 
void NCPU::GetOS()
258
 
{
259
 
    void* ptr = 0;
260
 
    int PointerSize = sizeof(ptr);
261
 
    OSVERSIONINFOEX osInfo;
262
 
    SYSTEM_INFO sysInfo;
263
 
 
264
 
    // Get system info
265
 
    ZeroMemory(&sysInfo, sizeof(sysInfo));
266
 
    GetSystemInfo(&sysInfo);
267
 
 
268
 
    // Get OS version
269
 
    ZeroMemory(&osInfo, sizeof(osInfo));
270
 
    osInfo.dwOSVersionInfoSize = sizeof(osInfo);
271
 
    if(!GetVersionEx((OSVERSIONINFO*)&osInfo))
272
 
    {
273
 
        ZeroMemory(&osInfo, sizeof(osInfo));
274
 
        osInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
275
 
        GetVersionEx((OSVERSIONINFO*)&osInfo);
276
 
    }
277
 
 
278
 
    // Win 9x
279
 
    if(osInfo.dwPlatformId == 1)
280
 
    {
281
 
        if((osInfo.dwMajorVersion == 4) && (osInfo.dwMinorVersion == 0))
282
 
            m_OSInfo.m_OSType = OS_WIN95;
283
 
        else if((osInfo.dwMajorVersion == 4) && (osInfo.dwMinorVersion == 10))
284
 
            m_OSInfo.m_OSType = OS_WIN98;
285
 
        else if((osInfo.dwMajorVersion == 4) && (osInfo.dwMinorVersion == 90))
286
 
            m_OSInfo.m_OSType = OS_WINME;
287
 
        else
288
 
            m_OSInfo.m_OSType = OS_UNKNOWN;
289
 
    }
290
 
 
291
 
    // Win NT
292
 
    else if(osInfo.dwPlatformId == 2)
293
 
    {
294
 
        if((osInfo.dwMajorVersion == 4) && (osInfo.dwMinorVersion == 0))
295
 
        {
296
 
            m_OSInfo.m_OSType = OS_WINNT40;
297
 
        }
298
 
        else if((osInfo.dwMajorVersion == 5) && (osInfo.dwMinorVersion == 0))
299
 
        {
300
 
            m_OSInfo.m_OSType = OS_WIN2000;
301
 
        }
302
 
        else if((osInfo.dwMajorVersion == 5) && (osInfo.dwMinorVersion == 1))
303
 
        {
304
 
            m_OSInfo.m_OSType = OS_WINXP;
305
 
        }
306
 
        else if((osInfo.dwMajorVersion == 5) && (osInfo.dwMinorVersion == 2))
307
 
        {
308
 
            if(GetSystemMetrics(89))    // SM_SERVERR2 == 89
309
 
            {
310
 
                m_OSInfo.m_OSType = OS_WINSERVER2003R2;
311
 
            }
312
 
            else if((osInfo.wProductType == VER_NT_WORKSTATION) &&
313
 
                (sysInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64))
314
 
            {
315
 
                m_OSInfo.m_OSType = OS_WINXP64;
316
 
            }
317
 
            else
318
 
            {
319
 
                m_OSInfo.m_OSType = OS_WINSERVER2003;
320
 
            }
321
 
        }
322
 
        else if((osInfo.dwMajorVersion == 6) && (osInfo.dwMinorVersion == 0))
323
 
        {
324
 
            m_OSInfo.m_OSType = OS_WINVISTA;
325
 
            BOOL bIsWow64 = FALSE;
326
 
            fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
327
 
            if(fnIsWow64Process != NULL)
328
 
            {
329
 
                if(!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
330
 
                {
331
 
                    // handle error
332
 
                    inlWin32MessageBox(NULL, TEXT("Error"), MBTYPE_Ok, MBICON_Error, MBMODAL_ApplicationModal, 
333
 
                        TEXT("Failed to getOS info.\nThe program will exit."));
334
 
                    exit(-1);
335
 
                }
336
 
            }
337
 
 
338
 
            if((PointerSize == 4) && bIsWow64)
339
 
            {
340
 
                // This is Vista 64 bits
341
 
                m_OSInfo.m_OSType = OS_WINVISTA64;
342
 
            }
343
 
 
344
 
            if(PointerSize == 8)
345
 
            {
346
 
                // This is Vista 64 bits
347
 
                m_OSInfo.m_OSType = OS_WINVISTA64;
348
 
            }
349
 
        }
350
 
        else
351
 
        {
352
 
            m_OSInfo.m_OSType = OS_UNKNOWN;
353
 
        }
354
 
    }
355
 
    else
356
 
    {
357
 
        m_OSInfo.m_OSType = OS_UNKNOWN;
358
 
    }
359
 
}
360
 
 
361
 
bool NCPU::IsBigEndian()
362
 
{
363
 
    return !m_IsLittleEndian;
364
 
}
365
 
 
366
 
bool NCPU::IsLittleEndian()
367
 
{
368
 
    return m_IsLittleEndian;
369
 
}
370
 
 
371
 
NAMESPACE_END
372
 
 
373
 
#endif // _WIN32
374
 
 
 
23
#include "NKernel.h"
 
24
 
 
25
#ifdef _WIN32
 
26
#include <intrin.h>
 
27
 
 
28
NAMESPACE_BEGIN
 
29
 
 
30
INL_IMPLEMENT_GLOBAL_OBJECT(NCPU);
 
31
 
 
32
//============================================================================
 
33
// Calculate and log the amount of RAM in the machine
 
34
//============================================================================
 
35
static t_u64 GetTotalPhysRAM_MB()
 
36
{
 
37
    // Get memory status
 
38
    MEMORYSTATUSEX theStatus;
 
39
    ZeroMemory(&theStatus,sizeof(theStatus));
 
40
    theStatus.dwLength = sizeof(theStatus);
 
41
    GlobalMemoryStatusEx(&theStatus);
 
42
 
 
43
    return theStatus.ullTotalPhys/(1024*1024);
 
44
}
 
45
 
 
46
//============================================================================
 
47
// Read the CPU speed from the registry
 
48
//============================================================================
 
49
static DWORD GetCPUSpeedFromRegistry(DWORD dwCPU)
 
50
{
 
51
    HKEY hKey;
 
52
    DWORD dwSpeed;
 
53
 
 
54
    // Get the key name
 
55
    TCHAR szKey[256];
 
56
    _sntprintf_s(szKey, 256, sizeof(szKey)/sizeof(wchar_t), TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\%d\\"), dwCPU);
 
57
 
 
58
    // Open the key
 
59
    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKey, 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
 
60
    {
 
61
        return 0;
 
62
    }
 
63
 
 
64
    // Read the value
 
65
    DWORD dwLen = 4;
 
66
    if(RegQueryValueEx(hKey, TEXT("~MHz"), NULL, NULL, (LPBYTE)&dwSpeed, &dwLen) != ERROR_SUCCESS)
 
67
    {
 
68
        RegCloseKey(hKey);
 
69
        return 0;
 
70
    }
 
71
 
 
72
    // Cleanup and return
 
73
    RegCloseKey(hKey);
 
74
    return dwSpeed;
 
75
}
 
76
 
 
77
void NCPU::Constructor()
 
78
{
 
79
    unsigned nHighestFeature;
 
80
    unsigned nHighestFeatureEx;
 
81
    int nBuff[4];
 
82
    char szMan[13];
 
83
 
 
84
    Memset(&m_CPUInfo, 0, sizeof(m_CPUInfo));
 
85
    Memset(&m_CPUFeatures, 0, sizeof(m_CPUFeatures));
 
86
 
 
87
    // Get CPU manufacturer and highest CPUID
 
88
    __cpuid(nBuff, 0);
 
89
 
 
90
    nHighestFeature = (unsigned)nBuff[0];
 
91
    *(int*)&szMan[0] = nBuff[1];
 
92
    *(int*)&szMan[4] = nBuff[3];
 
93
    *(int*)&szMan[8] = nBuff[2];
 
94
    szMan[12] = 0;
 
95
 
 
96
    if(strcmp(szMan, "AuthenticAMD") == 0)
 
97
    {
 
98
        m_CPUInfo.m_CPUType = CPU_AMD;
 
99
    }
 
100
    else if(strcmp(szMan, "GenuineIntel") == 0)
 
101
    {
 
102
        m_CPUInfo.m_CPUType = CPU_INTEL;
 
103
    }
 
104
    else
 
105
    {
 
106
        m_CPUInfo.m_CPUType = CPU_UNKNOWN;
 
107
    }
 
108
 
 
109
    m_CPUString = szMan;
 
110
 
 
111
    // Get highest extended feature
 
112
    __cpuid(nBuff, 0x80000000);
 
113
    nHighestFeatureEx = (unsigned)nBuff[0];
 
114
 
 
115
    // Get processor brand name
 
116
    if(nHighestFeatureEx >= 0x80000004)
 
117
    {
 
118
        char szCPUName[49];
 
119
        szCPUName[0] = 0;
 
120
        __cpuid((int*)&szCPUName[0], 0x80000002);
 
121
        __cpuid((int*)&szCPUName[16], 0x80000003);
 
122
        __cpuid((int*)&szCPUName[32], 0x80000004);
 
123
        szCPUName[48] = 0;
 
124
        
 
125
        m_CPUBrandString = szCPUName;
 
126
//         for(int i=(int)strlen(szCPUName)-1; i>=0; --i)
 
127
//         {
 
128
//             if(szCPUName[i] == ' ')
 
129
//                 szCPUName[i] = '\0';
 
130
//             else
 
131
//                 break;
 
132
//         }
 
133
    }
 
134
 
 
135
    // Get CPU features
 
136
    if(nHighestFeature >= 1)
 
137
    {
 
138
        __cpuid(nBuff, 1);
 
139
 
 
140
 
 
141
        m_CPUInfo.m_CPUSteppingID = nBuff[0] & 0xf;
 
142
        m_CPUInfo.m_CPUModel = (nBuff[0] >> 4) & 0xf;
 
143
        m_CPUInfo.m_CPUFamily = (nBuff[0] >> 8) & 0xf;
 
144
        //m_CPUInfo.nProcessorType = (nBuff[0] >> 12) & 0x3;
 
145
        m_CPUInfo.m_CPUExtModel = (nBuff[0] >> 16) & 0xf;
 
146
        m_CPUInfo.m_CPUExtFamily = (nBuff[0] >> 20) & 0xff;
 
147
        //nBrandIndex = nBuff[1] & 0xff;
 
148
        m_CPUInfo.m_CPUNumLogicalProcessors = ((nBuff[1] >> 16) & 0xff);
 
149
 
 
150
 
 
151
 
 
152
 
 
153
        if(nBuff[3] & 1<<0)
 
154
            m_CPUFeatures.m_HasFPU = true;
 
155
        if(nBuff[3] & 1<<23)
 
156
            m_CPUFeatures.m_HasMMX = true;
 
157
        if(nBuff[3] & 1<<25)
 
158
            m_CPUFeatures.m_HasSSE = true;
 
159
        if(nBuff[3] & 1<<26)
 
160
            m_CPUFeatures.m_HasSSE2 = true;
 
161
        if(nBuff[2] & 1<<0)
 
162
            m_CPUFeatures.m_HasSSE3 = true;
 
163
        if(nBuff[2] & 1<<19)
 
164
            m_CPUFeatures.m_HasSSE41 = true;
 
165
        if(nBuff[2] & 1<<20)
 
166
            m_CPUFeatures.m_HasSSE42 = true;
 
167
 
 
168
        // Intel specific:
 
169
        if(m_CPUInfo.m_CPUType == CPU_INTEL)
 
170
        {
 
171
            if(nBuff[2] & 1<<9)
 
172
                m_CPUFeatures.m_HasSSSE3 = true;
 
173
            if(nBuff[2] & 1<<7)
 
174
                m_CPUFeatures.m_HasEST = true;
 
175
        }
 
176
 
 
177
        if(nBuff[3] & 1<<28)
 
178
            m_CPUFeatures.m_HasHTT = true;
 
179
    }
 
180
 
 
181
    // AMD specific:
 
182
    if(m_CPUInfo.m_CPUType == CPU_AMD)
 
183
    {
 
184
        // Get extended features
 
185
        __cpuid(nBuff, 0x80000000);
 
186
        if(nHighestFeatureEx >= 0x80000001)
 
187
        {
 
188
            __cpuid(nBuff, 0x80000001);
 
189
            if(nBuff[3] & 1<<31)
 
190
                m_CPUFeatures.m_Has3DNow = true;
 
191
            if(nBuff[3] & 1<<30)
 
192
                m_CPUFeatures.m_Has3DNowExt = true; 
 
193
            if(nBuff[3] & 1<<22)
 
194
                m_CPUFeatures.m_HasAMDMMX = true;
 
195
        }
 
196
 
 
197
//         // Get level 1 cache size
 
198
//         if(nHighestFeatureEx >= 0x80000005)
 
199
//         {
 
200
//             __cpuid(nBuff, 0x80000005);
 
201
//             ELog::Get().SystemFormat(L"PERF    : L1 cache size: %dK\n", ((unsigned)nBuff[2])>>24);
 
202
//         }
 
203
    }
 
204
 
 
205
    if(nHighestFeatureEx >= 0x80000001)
 
206
    {
 
207
        __cpuid(nBuff, 0x80000001);
 
208
        if(nBuff[3] & 1 << 29)
 
209
            m_CPUFeatures.m_Hasx64 = true;
 
210
 
 
211
    }
 
212
 
 
213
    m_CPUInfo.m_CPUSpeed = GetCPUSpeedFromRegistry(0);
 
214
    m_MemInfo.m_PhysicalMemorySize = GetTotalPhysRAM_MB();
 
215
    GetOS();
 
216
 
 
217
    unsigned int dw = 0x00000001;
 
218
    m_IsLittleEndian = (*( (char*)&dw ) ? true : false);
 
219
 
 
220
    //m_MemInfo
 
221
//     // Get cache size
 
222
//     if(nHighestFeatureEx >= 0x80000006)
 
223
//     {
 
224
//         __cpuid(nBuff, 0x80000006);
 
225
//         ELog::Get().SystemFormat(L"PERF    : L2 cache size: %dK\n", ((unsigned)nBuff[2])>>16);
 
226
//     }
 
227
// 
 
228
//     // Log features
 
229
//     ELog::Get().SystemFormat(L"PERF    : CPU Features: %S\n", szFeatures);
 
230
// 
 
231
//     // Get misc system info
 
232
//     SYSTEM_INFO theInfo;
 
233
//     GetSystemInfo(&theInfo);
 
234
// 
 
235
//     // Log number of CPUs and speeds
 
236
//     ELog::Get().SystemFormat(L"PERF    : Number of CPUs: %d\n", theInfo.dwNumberOfProcessors);
 
237
//     for(DWORD i=0; i<theInfo.dwNumberOfProcessors; ++i)
 
238
//     {
 
239
//         DWORD dwCPUSpeed = ReadCPUSpeedFromRegistry(i);
 
240
//         ELog::Get().SystemFormat(L"PERF    : * CPU %d speed: ~%dMHz\n", i, dwCPUSpeed);
 
241
//     }
 
242
}
 
243
 
 
244
 
 
245
void NCPU::Destructor()
 
246
{
 
247
 
 
248
}
 
249
 
 
250
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
 
251
LPFN_ISWOW64PROCESS fnIsWow64Process;
 
252
 
 
253
 
 
254
//============================================================================
 
255
// Determine OS type
 
256
//============================================================================
 
257
void NCPU::GetOS()
 
258
{
 
259
    void* ptr = 0;
 
260
    int PointerSize = sizeof(ptr);
 
261
    OSVERSIONINFOEX osInfo;
 
262
    SYSTEM_INFO sysInfo;
 
263
 
 
264
    // Get system info
 
265
    ZeroMemory(&sysInfo, sizeof(sysInfo));
 
266
    GetSystemInfo(&sysInfo);
 
267
 
 
268
    // Get OS version
 
269
    ZeroMemory(&osInfo, sizeof(osInfo));
 
270
    osInfo.dwOSVersionInfoSize = sizeof(osInfo);
 
271
    if(!GetVersionEx((OSVERSIONINFO*)&osInfo))
 
272
    {
 
273
        ZeroMemory(&osInfo, sizeof(osInfo));
 
274
        osInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
 
275
        GetVersionEx((OSVERSIONINFO*)&osInfo);
 
276
    }
 
277
 
 
278
    // Win 9x
 
279
    if(osInfo.dwPlatformId == 1)
 
280
    {
 
281
        if((osInfo.dwMajorVersion == 4) && (osInfo.dwMinorVersion == 0))
 
282
            m_OSInfo.m_OSType = OS_WIN95;
 
283
        else if((osInfo.dwMajorVersion == 4) && (osInfo.dwMinorVersion == 10))
 
284
            m_OSInfo.m_OSType = OS_WIN98;
 
285
        else if((osInfo.dwMajorVersion == 4) && (osInfo.dwMinorVersion == 90))
 
286
            m_OSInfo.m_OSType = OS_WINME;
 
287
        else
 
288
            m_OSInfo.m_OSType = OS_UNKNOWN;
 
289
    }
 
290
 
 
291
    // Win NT
 
292
    else if(osInfo.dwPlatformId == 2)
 
293
    {
 
294
        if((osInfo.dwMajorVersion == 4) && (osInfo.dwMinorVersion == 0))
 
295
        {
 
296
            m_OSInfo.m_OSType = OS_WINNT40;
 
297
        }
 
298
        else if((osInfo.dwMajorVersion == 5) && (osInfo.dwMinorVersion == 0))
 
299
        {
 
300
            m_OSInfo.m_OSType = OS_WIN2000;
 
301
        }
 
302
        else if((osInfo.dwMajorVersion == 5) && (osInfo.dwMinorVersion == 1))
 
303
        {
 
304
            m_OSInfo.m_OSType = OS_WINXP;
 
305
        }
 
306
        else if((osInfo.dwMajorVersion == 5) && (osInfo.dwMinorVersion == 2))
 
307
        {
 
308
            if(GetSystemMetrics(89))    // SM_SERVERR2 == 89
 
309
            {
 
310
                m_OSInfo.m_OSType = OS_WINSERVER2003R2;
 
311
            }
 
312
            else if((osInfo.wProductType == VER_NT_WORKSTATION) &&
 
313
                (sysInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64))
 
314
            {
 
315
                m_OSInfo.m_OSType = OS_WINXP64;
 
316
            }
 
317
            else
 
318
            {
 
319
                m_OSInfo.m_OSType = OS_WINSERVER2003;
 
320
            }
 
321
        }
 
322
        else if((osInfo.dwMajorVersion == 6) && (osInfo.dwMinorVersion == 0))
 
323
        {
 
324
            m_OSInfo.m_OSType = OS_WINVISTA;
 
325
            BOOL bIsWow64 = FALSE;
 
326
            fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
 
327
            if(fnIsWow64Process != NULL)
 
328
            {
 
329
                if(!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
 
330
                {
 
331
                    // handle error
 
332
                    inlWin32MessageBox(NULL, TEXT("Error"), MBTYPE_Ok, MBICON_Error, MBMODAL_ApplicationModal, 
 
333
                        TEXT("Failed to getOS info.\nThe program will exit."));
 
334
                    exit(-1);
 
335
                }
 
336
            }
 
337
 
 
338
            if((PointerSize == 4) && bIsWow64)
 
339
            {
 
340
                // This is Vista 64 bits
 
341
                m_OSInfo.m_OSType = OS_WINVISTA64;
 
342
            }
 
343
 
 
344
            if(PointerSize == 8)
 
345
            {
 
346
                // This is Vista 64 bits
 
347
                m_OSInfo.m_OSType = OS_WINVISTA64;
 
348
            }
 
349
        }
 
350
        else
 
351
        {
 
352
            m_OSInfo.m_OSType = OS_UNKNOWN;
 
353
        }
 
354
    }
 
355
    else
 
356
    {
 
357
        m_OSInfo.m_OSType = OS_UNKNOWN;
 
358
    }
 
359
}
 
360
 
 
361
bool NCPU::IsBigEndian()
 
362
{
 
363
    return !m_IsLittleEndian;
 
364
}
 
365
 
 
366
bool NCPU::IsLittleEndian()
 
367
{
 
368
    return m_IsLittleEndian;
 
369
}
 
370
 
 
371
NAMESPACE_END
 
372
 
 
373
#endif // _WIN32
 
374