Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
os_cpu.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2010 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * OS-specific support functions relating to CPU and memory
25  */
26 
27 #include "precompiled.h"
28 #include "lib/sysdep/os_cpu.h"
29 
30 #include "lib/alignment.h"
31 #include "lib/sysdep/smbios.h"
32 
33 #if OS_WIN
34 # include "lib/sysdep/os/win/wcpu.h"
35 #endif
36 
38  { ERR::OS_CPU_RESTRICTED_AFFINITY, L"Cannot set desired CPU affinity" }
39 };
40 STATUS_ADD_DEFINITIONS(osCpuStatusDefinitions);
41 
42 
44 {
45  static double clockFrequency;
46  if(clockFrequency != 0.0) // already initialized
47  return clockFrequency;
48 
49 #if OS_WIN
50  u32 freqMhz;
52  return clockFrequency = freqMhz * 1e6;
53 #endif
54 
56  if(structures->Processor_)
57  return clockFrequency = structures->Processor_->maxFrequency * 1e6;
58 
59  return clockFrequency = -1.0; // unknown
60 }
61 
62 
64 {
65  static size_t memorySize;
66  if(memorySize != 0) // already initialized
67  return memorySize;
68 
69  memorySize = os_cpu_QueryMemorySize();
70 
71  // replace with the sum of all memory devices reported by SMBIOS if
72  // that's within 10% of what the OS reported
73  {
75  u64 memorySizeBytes = 0;
76  for(const SMBIOS::MemoryDevice* p = structures->MemoryDevice_; p; p = p->next)
77  memorySizeBytes += p->size;
78  const size_t memorySize2 = memorySizeBytes/MiB;
79  if(9*memorySize/10 <= memorySize2 && memorySize2 <= 11*memorySize/10)
80  memorySize = memorySize2;
81  }
82 
83  return memorySize;
84 }
double os_cpu_ClockFrequency()
Definition: os_cpu.cpp:43
size_t os_cpu_QueryMemorySize()
Definition: bcpu.cpp:86
static const StatusDefinition osCpuStatusDefinitions[]
Definition: os_cpu.cpp:37
const Status OK
Definition: status.h:386
const Status OS_CPU_RESTRICTED_AFFINITY
Definition: os_cpu.h:32
const Structures * GetStructures()
Definition: smbios.cpp:689
size_t os_cpu_MemorySize()
Definition: os_cpu.cpp:63
static Structures structures
Definition: smbios.cpp:121
static const size_t MiB
Definition: alignment.h:72
#define STATUS_ADD_DEFINITIONS(definitions)
add a module&#39;s array of StatusDefinition to the list.
Definition: status.h:216
#define u64
Definition: types.h:42
#define u32
Definition: types.h:41
Status wcpu_ReadFrequencyFromRegistry(u32 &freqMhz)
Definition: wcpu.cpp:77