Plasma Engine  2.0
Loading...
Searching...
No Matches
SystemInformation_Posix.h
1#include <Foundation/FoundationInternal.h>
2PL_FOUNDATION_INTERNAL_HEADER
3
4#include <Foundation/System/SystemInformation.h>
5
6#include <Foundation/IO/OSFile.h>
7#include <unistd.h>
8
10{
11 plOSFile status;
12 if (status.Open("/proc/self/status", plFileOpenMode::Read).Failed())
13 {
14 return false;
15 }
16
17
18 char buffer[2048];
19 plUInt64 numBytesRead = status.Read(buffer, PL_ARRAY_SIZE(buffer));
20 status.Close();
21
22
23 plStringView contents(buffer, numBytesRead);
24 const char* tracerPid = contents.FindSubString("TracerPid:");
25 if (tracerPid == nullptr)
26 {
27 return false;
28 }
29
30 tracerPid += 10; // Skip TracerPid:
31
32 while (*tracerPid == ' ' || *tracerPid == '\t')
33 {
34 tracerPid++;
35 }
36
37 return *tracerPid == '0' ? false : true;
38}
39
40void plSystemInformation::Initialize()
41{
42 if (s_SystemInformation.m_bIsInitialized)
43 return;
44
45 s_SystemInformation.m_CpuFeatures.Detect();
46
47 // Get system information via various APIs
48 s_SystemInformation.m_uiCPUCoreCount = sysconf(_SC_NPROCESSORS_ONLN);
49
50 plUInt64 uiPageCount = sysconf(_SC_PHYS_PAGES);
51 plUInt64 uiPageSize = sysconf(_SC_PAGE_SIZE);
52
53 s_SystemInformation.m_uiInstalledMainMemory = uiPageCount * uiPageSize;
54 s_SystemInformation.m_uiMemoryPageSize = uiPageSize;
55
56 // Not correct for 32 bit process on 64 bit system
57#if PL_ENABLED(PL_PLATFORM_64BIT)
58 s_SystemInformation.m_bB64BitOS = true;
59#else
60 s_SystemInformation.m_bB64BitOS = false;
61# if PL_ENABLED(PL_PLATFORM_OSX)
62# error "32 Bit builds are not supported on OSX"
63# endif
64#endif
65
66#if defined BUILDSYSTEM_BUILDTYPE
67 s_SystemInformation.m_szBuildConfiguration = BUILDSYSTEM_BUILDTYPE;
68#else
69 s_SystemInformation.m_szBuildConfiguration = "undefined";
70#endif
71
72 // Each posix system should have its correct name so they can be distinguished.
73#if PL_ENABLED(PL_PLATFORM_LINUX)
74 s_SystemInformation.m_szPlatformName = "Linux";
75#elif PL_ENABLED(PL_PLATFORM_ANDROID)
76 s_SystemInformation.m_szPlatformName = "Android";
77#else
78# error "Platform name not defined on current posix platform"
79#endif
80
81 // Get host name
82 if (gethostname(s_SystemInformation.m_sHostName, sizeof(s_SystemInformation.m_sHostName)) == -1)
83 {
84 strcpy(s_SystemInformation.m_sHostName, "");
85 }
86
87 s_SystemInformation.m_bIsInitialized = true;
88}
89
91{
92 return static_cast<plUInt64>(sysconf(_SC_AVPHYS_PAGES)) * static_cast<plUInt64>(sysconf(_SC_PAGESIZE));
93}
94
96{
97 PL_ASSERT_NOT_IMPLEMENTED;
98 return 0.0f;
99}
100
101#if PL_ENABLED(PL_PLATFORM_ARCH_X86)
102namespace cpu_x86
103{
104# include <cpuid.h>
105
106 void cpuid(int32_t out[4], int32_t eax, int32_t ecx)
107 {
108 __cpuid_count(eax, ecx, out[0], out[1], out[2], out[3]);
109 }
110
111 uint64_t xgetbv(unsigned int index)
112 {
113 uint32_t eax, edx;
114 __asm__ __volatile__("xgetbv"
115 : "=a"(eax), "=d"(edx)
116 : "c"(index));
117 return ((uint64_t)edx << 32) | eax;
118 }
119
120 bool detect_OS_x64()
121 {
122 // We only support x64 on Linux / Mac / etc.
123 return true;
124 }
125
126} // namespace cpu_x86
127#endif
This is an abstraction for the most important file operations.
Definition OSFile.h:170
void Close()
Closes the file, if it is currently opened.
Definition OSFile.cpp:93
plUInt64 Read(void *pBuffer, plUInt64 uiBytes)
Reads up to the given number of bytes from the file. Returns the actual number of bytes that was read...
Definition OSFile.cpp:143
plResult Open(plStringView sFile, plFileOpenMode::Enum openMode, plFileShareMode::Enum fileShareMode=plFileShareMode::Default)
Opens a file for reading or writing. Returns PL_SUCCESS if the file could be opened successfully.
Definition OSFile.cpp:33
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
const char * FindSubString(plStringView sStringToFind, const char *szStartSearchAt=nullptr) const
Definition StringView.cpp:60
plUInt64 GetAvailableMainMemory() const
Returns the currently available physical memory.
Definition SystemInformation_Posix.h:90
static bool IsDebuggerAttached()
Returns whether a debugger is currently attached to this process.
Definition SystemInformation_Posix.h:9
float GetCPUUtilization() const
Returns the total utilization of the CPU core in percent.
Definition SystemInformation_Posix.h:95
void Detect()
CPU feature detection code copied from https://github.com/Mysticial/FeatureDetector.
Definition SystemInformation.cpp:168
@ Read
Open file for reading.
Definition OSFile.h:26