TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
System.h
Go to the documentation of this file.
1 
14 #ifndef G3D_System_h
15 #define G3D_System_h
16 
17 #include "G3D/platform.h"
18 #include "G3D/g3dmath.h"
19 #include "G3D/G3DGameUnits.h"
20 #include "G3D/BinaryFormat.h"
21 #include "G3D/FileNotFound.h"
22 #include <string>
23 
24 #ifdef G3D_OSX
25 #define Zone OSX_Zone
26 # include <CoreServices/CoreServices.h>
27 #endif
28 
29 namespace G3D {
30 
38 std::string license();
39 
48 enum G3DEndian {
51 };
52 
71 class System {
72 public:
83  typedef bool (*OutOfMemoryCallback)(size_t size, bool recoverable);
84 
85 private:
86 
89  bool m_hasCPUID;
90  bool m_hasRDTSC;
91  bool m_hasMMX;
92  bool m_hasSSE;
93  bool m_hasSSE2;
94  bool m_hasSSE3;
95  bool m_has3DNOW;
98  std::string m_cpuVendor;
100 
103  std::string m_appDataDir;
104 
106  std::string m_cpuArch;
107  std::string m_operatingSystem;
108 
109 # ifdef G3D_WINDOWS
110 
111  LARGE_INTEGER m_start;
112  LARGE_INTEGER m_counterFrequency;
113 #else
114  struct timeval m_start;
115 #endif
116 
117  std::string m_version;
119 
120 #ifdef G3D_OSX
121 
122  SInt32 m_OSXCPUSpeed;
123  double m_secondsPerNS;
124 #endif
125 
128 
130 
132  System();
133 
140  static System& instance();
141 
143  CPUID_VENDOR_ID = 0x00000000,
145  CPUID_NUM_CORES = 0x00000004,
147  CPUID_EXTENDED_FEATURES = 0x80000001};
148 
156  static void cpuid(CPUIDFunction func, uint32& areg, uint32& breg, uint32& creg, uint32& dreg);
157 
158 
161 
163  void initTime();
164 
165  void init();
166 
167 public:
168 
170  static void cleanup();
171 
174  inline static int cpuSpeedMHz() {
175  return instance().m_cpuSpeed;
176  }
177 
180  inline static int numCores() {
181  return instance().m_numCores;
182  }
183 
184  inline static bool hasCPUID() {
185  return instance().m_hasCPUID;
186  }
187 
188  inline static bool hasRDTSC() {
189  return instance().m_hasRDTSC;
190  }
191 
192  inline static bool hasSSE() {
193  return instance().m_hasSSE;
194  }
195 
196  inline static bool hasSSE2() {
197  return instance().m_hasSSE2;
198  }
199 
200  inline static bool hasSSE3() {
201  return instance().m_hasSSE3;
202  }
203 
204  inline static bool hasMMX() {
205  return instance().m_hasMMX;
206  }
207 
208  inline static bool has3DNow() {
209  return instance().m_has3DNOW;
210  }
211 
212  inline static const std::string& cpuVendor() {
213  return instance().m_cpuVendor;
214  }
215 
219  inline static G3DEndian machineEndian() {
220  return instance().m_machineEndian;
221  }
222 
224  inline static const std::string& operatingSystem() {
225  return instance().m_operatingSystem;
226  }
227 
229  inline static const std::string& cpuArchitecture() {
230  return instance().m_cpuArch;
231  }
232 
236  static std::string currentDateString();
237 
239  static std::string currentTimeString();
240 
252  static void* malloc(size_t bytes);
253 
254  static void* calloc(size_t n, size_t x);
255 
259  static void* realloc(void* block, size_t bytes);
260 
264  static std::string mallocPerformance();
265  static void resetMallocPerformanceCounters();
266 
271  static std::string mallocStatus();
272 
278  static void free(void* p);
279 
284  static void* alignedMalloc(size_t bytes, size_t alignment);
285 
289  static void alignedFree(void* ptr);
290 
294  static void memcpy(void* dst, const void* src, size_t numBytes);
295 
299  static void memset(void* dst, uint8 value, size_t numBytes);
300 
310  static std::string currentProgramFilename();
311 
314  static std::string& appName();
315 
317  inline static const std::string& version() {
318  return instance().m_version;
319  }
320 
327  static const std::string& build();
328 
335  static void sleep(RealTime t);
336 
341  static void consoleClearScreen();
342 
347  static bool consoleKeyPressed();
348 
354  static int consoleReadKey();
355 
363  static RealTime time();
364 
378  static void beginCycleCount(uint64& cycleCount);
379  static void endCycleCount(uint64& cycleCount);
380 
381  static uint64 getCycleCount();
382 
385  }
386 
400  }
401 
403  static void setEnv(const std::string& name, const std::string& value);
404 
406  static const char* getEnv(const std::string& name);
407 
412  static void describeSystem
413  (class TextOutput& t);
414 
415  static void describeSystem
416  (std::string& s);
417 
452  static std::string findDataFile(const std::string& full, bool exceptionIfNotFound = true, bool caseSensitive =
453 #ifdef G3D_WINDOWS
454  false
455 #else
456  true
457 #endif
458  );
459 
465  static void setAppDataDir(const std::string& path);
466 
467 };
468 
469 
470 #ifdef _MSC_VER
471 # ifdef _M_IX86
472  // 32-bit
473  inline uint64 System::getCycleCount() {
474  uint32 timehi, timelo;
475 
476  // Use the assembly instruction rdtsc, which gets the current
477  // cycle count (since the process started) and puts it in edx:eax.
478  __asm
479  {
480  rdtsc;
481  mov timehi, edx;
482  mov timelo, eax;
483  }
484 
485  return ((uint64)timehi << 32) + (uint64)timelo;
486  }
487 # else
488  // 64-bit
489  inline uint64 System::getCycleCount() {
490  LARGE_INTEGER now;
491  QueryPerformanceCounter(&now);
492  return now.QuadPart;
493  }
494 
495 # endif
496 
497 #elif defined(G3D_LINUX)
498 
499  inline uint64 System::getCycleCount() {
500  uint32 timehi, timelo;
501 
502  __asm__ __volatile__ (
503  "rdtsc "
504  : "=a" (timelo),
505  "=d" (timehi)
506  : );
507 
508  return ((uint64)timehi << 32) + (uint64)timelo;
509  }
510 
511 #elif defined(G3D_OSX)
512 
513  inline uint64 System::getCycleCount() {
514  //Note: To put off extra processing until the end, this does not
515  //return the actual clock cycle count. It is a bus cycle count.
516  //When endCycleCount() is called, it converts the two into a difference
517  //of clock cycles
518 
519  return (uint64) UnsignedWideToUInt64(UpTime());
520  //return (uint64) mach_absolute_time();
521  }
522 
523 #endif
524 
525 inline void System::beginCycleCount(uint64& cycleCount) {
526  cycleCount = getCycleCount();
527 }
528 
529 
530 inline void System::endCycleCount(uint64& cycleCount) {
531 #ifndef G3D_OSX
532  cycleCount = getCycleCount() - cycleCount;
533 #else
534  AbsoluteTime end = UpTime();
535  Nanoseconds diffNS =
536  AbsoluteDeltaToNanoseconds(end, UInt64ToUnsignedWide(cycleCount));
537  cycleCount =
538  (uint64) ((double) (instance().m_OSXCPUSpeed) *
539  (double) UnsignedWideToUInt64(diffNS) * instance().m_secondsPerNS);
540 #endif
541 }
542 
543 
544 } // namespace
545 
546 
547 #ifdef G3D_OSX
548 #undef Zone
549 #endif
550 
551 #endif
static void cpuid(CPUIDFunction func, uint32 &areg, uint32 &breg, uint32 &creg, uint32 &dreg)
Definition: System.cpp:1704
static bool consoleKeyPressed()
Definition: System.cpp:846
OutOfMemoryCallback m_outOfMemoryCallback
Definition: System.h:118
static void setEnv(const std::string &name, const std::string &value)
Definition: System.cpp:1566
void getStandardProcessorExtensions()
Definition: System.cpp:555
static void memset(void *dst, uint8 value, size_t numBytes)
Definition: System.cpp:695
bool(* OutOfMemoryCallback)(size_t size, bool recoverable)
Definition: System.h:83
RealTime m_realWorldGetTickTime0
Definition: System.h:127
static void beginCycleCount(uint64 &cycleCount)
Definition: System.h:525
static std::string currentDateString()
Definition: System.cpp:1673
static int cpuSpeedMHz()
Definition: System.h:174
CPUIDFunction
Definition: System.h:142
Definition: System.h:145
static void memcpy(void *dst, const void *src, size_t numBytes)
Definition: System.cpp:643
static void setOutOfMemoryCallback(OutOfMemoryCallback c)
Definition: System.h:383
static bool has3DNow()
Definition: System.h:208
void init()
Definition: System.cpp:128
static const std::string & cpuVendor()
Definition: System.h:212
std::string m_operatingSystem
Definition: System.h:107
uint32 m_highestCPUIDFunction
Definition: System.h:129
static System & instance()
The singleton instance.
Definition: System.cpp:96
Definition: AABox.h:25
System()
Used for the singleton instance only.
Definition: System.cpp:102
static std::string & appName()
Definition: System.cpp:740
static const std::string & build()
The optimization status of the G3D library (not the program compiled against it)
Definition: System.cpp:524
uint64_t uint64
Definition: g3dmath.h:170
static uint64 getCycleCount()
bool m_has3DNOW
Definition: System.h:95
static void * realloc(void *block, size_t bytes)
Definition: System.cpp:1463
static void describeSystem(class TextOutput &t)
Definition: System.cpp:1610
static void alignedFree(void *ptr)
Definition: System.cpp:1546
static void sleep(RealTime t)
Definition: SystemImpl.cpp:14
static std::string currentTimeString()
Definition: System.cpp:1680
bool m_hasSSE
Definition: System.h:92
double RealTime
Definition: G3DGameUnits.h:27
#define bool
Definition: CascPort.h:16
static std::string currentProgramFilename()
Definition: System.cpp:746
int m_numCores
Definition: System.h:99
static void * malloc(size_t bytes)
Definition: System.cpp:1441
Definition: System.h:147
static bool hasRDTSC()
Definition: System.h:188
static bool hasMMX()
Definition: System.h:204
static bool hasSSE3()
Definition: System.h:200
static int consoleReadKey()
Definition: System.cpp:888
std::string m_version
Definition: System.h:117
static std::string findDataFile(const std::string &full, bool exceptionIfNotFound=true, bool caseSensitive=true)
Definition: System.cpp:365
G3DEndian m_machineEndian
Definition: System.h:105
static int numCores()
Definition: System.h:180
bool m_hasMMX
Definition: System.h:91
Definition: System.h:49
bool m_hasRDTSC
Definition: System.h:90
uint32_t uint32
Definition: Define.h:150
uint64_t uint64
Definition: Define.h:149
bool m_hasAMDMMX
Definition: System.h:97
static bool hasSSE2()
Definition: System.h:196
bool m_hasCPUID
Definition: System.h:89
G3DEndian
The order in which the bytes of an integer are stored on a machine.
Definition: System.h:48
Definition: TextOutput.h:60
std::string license()
Definition: license.cpp:15
static void * alignedMalloc(size_t bytes, size_t alignment)
Definition: System.cpp:1482
static void cleanup()
Definition: System.cpp:518
static void * calloc(size_t n, size_t x)
Definition: System.cpp:1450
struct timeval m_start
Definition: System.h:114
static G3DEndian machineEndian()
Definition: System.h:219
static void free(void *p)
Definition: System.cpp:1473
static const std::string & version()
Definition: System.h:317
uint8_t uint8
Definition: g3dmath.h:164
Definition: System.h:143
bool m_hasSSE3
Definition: System.h:94
static RealTime time()
Definition: System.cpp:934
static void resetMallocPerformanceCounters()
Definition: System.cpp:1418
Definition: System.h:50
bool m_has3DNOW2
Definition: System.h:96
void initTime()
Definition: System.cpp:899
static void consoleClearScreen()
Definition: System.cpp:837
static const std::string & cpuArchitecture()
Definition: System.h:229
static OutOfMemoryCallback outOfMemoryCallback()
Definition: System.h:398
bool m_hasSSE2
Definition: System.h:93
bool m_initialized
Definition: System.h:87
std::string m_cpuArch
Definition: System.h:106
std::string m_cpuVendor
Definition: System.h:98
std::string m_appDataDir
Definition: System.h:103
const FieldDescriptor value
Definition: descriptor.h:1522
uint32_t uint32
Definition: g3dmath.h:168
static std::string mallocPerformance()
Definition: System.cpp:1401
G3D::int16 x
Definition: Vector2int16.h:37
int m_cpuSpeed
Definition: System.h:88
static std::string mallocStatus()
Definition: System.cpp:1409
static bool hasSSE()
Definition: System.h:192
static void endCycleCount(uint64 &cycleCount)
Definition: System.h:530
static bool hasCPUID()
Definition: System.h:184
OS and processor abstraction.
Definition: System.h:71
static const std::string & operatingSystem()
Definition: System.h:224
static const char * getEnv(const std::string &name)
Definition: System.cpp:1577
static void setAppDataDir(const std::string &path)
Definition: System.cpp:509