Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
SystemConfiguration.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Common_SystemConfiguration_h_
5#define _Stroika_Foundation_Common_SystemConfiguration_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
10#include "Stroika/Foundation/Common/Common.h"
11#include "Stroika/Foundation/Containers/Sequence.h"
13
14/**
15 * \file
16 *
17 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
18 *
19 * TODO:
20 * @todo Review API provide, and document relationship with sysconf/etc (idea is simpler, and a bit more portable,
21 * but just subset).
22 *
23 * @todo BootInformation: consider adding info like 'last shutdown at' and last shutdown reason.
24 *
25 * @todo BootInformation: simplistic impl - may not handle 'hibernate' properly... - must verify/test, and timezone
26 * change etc... just uses seconds since boot on most platforms??
27 *
28 */
29
31
32 using Characters::String;
33
34 /**
35 */
36 struct SystemConfiguration {
37
38 /**
39 */
40 struct BootInformation {
41 optional<Time::DateTime> fBootedAt;
42
43 /**
44 * @see Characters::ToString ();
45 */
46 nonvirtual String ToString () const;
47 };
48
49 /**
50 */
51 struct CPU {
52 public:
53 /**
54 * Number of Physical Sockets/CPU chips. Also this is the number of distinct 'socket ids' from the fCores.
55 */
56 nonvirtual unsigned int GetNumberOfSockets () const;
57
58 public:
59 /**
60 * Number of Logical Cores (aka max concurrent logical thread count). This will generally be
61 * divisible by fNumberOfSockets.
62 *
63 * @see Common::GetNumberOfLogicalCPUCores
64 */
65 nonvirtual unsigned int GetNumberOfLogicalCores () const;
66
67 public:
68 /**
69 * Each socket will typically have the identical model name. This returns the value from the first.
70 * check each fCore to see if they differ.
71 *
72 * If the fCores is empty, this is safe, and returns an empty string.
73 */
74 nonvirtual String GetCPUModelPrintName () const;
75
76 public:
77 /**
78 * Details we track per CPU (socket/chip). There is much more info in /proc/cpuinfo, like
79 * MHz, and cache size, and particular numerical model numbers. Possibly also add 'bogomips'?
80 */
81 struct CoreDetails {
82 /**
83 * /proc/cpuinfo 'physical id' - use to tell number of sockets. Each distinct socketID is a different socket
84 */
85 unsigned int fSocketID{};
86
87 /**
88 * /proc/cpuinfo 'model name' field - a semi-standardized representation of what you want to know about a CPU chip
89 */
91
92 /**
93 */
94 CoreDetails (unsigned int socketID = {}, const String& modelName = String{});
95
96 /**
97 * @see Characters::ToString ();
98 */
99 nonvirtual String ToString () const;
100 };
101
102 public:
103 /**
104 * A computer may have multiple CPUCores, and in principle they can differ.
105 * The number of filled 'cpu sockets' is fCPUs.length ().
106 *
107 * \note These are 'logical cores' and may not be physical cores.
108 * \note We have no way to capture physical cores (per socket). Not sure that is helpful/needed.
109 */
111
112 public:
113 /**
114 * @see Characters::ToString ();
115 */
116 nonvirtual String ToString () const;
117 };
118
119 /**
120 */
121 struct Memory {
122 /**
123 * Size in bytes
124 */
125 size_t fPageSize{};
126
127 /**
128 * Size in bytes
129 */
130 uint64_t fTotalPhysicalRAM{};
131
132 /**
133 * Size in bytes
134 */
135 uint64_t fTotalVirtualRAM{};
136
137 /**
138 * @see Characters::ToString ();
139 */
140 nonvirtual String ToString () const;
141 };
142
143 /**
144 */
145 struct OperatingSystem {
146 /**
147 * e.g. Linux/MacOS/Windows/Unix (on POSIX systems - value of uname)
148 */
149 String fTokenName;
150
151 /**
152 * e.g. Linux/Ubuntu,RedHat, Windows XP, Windows 2000
153 */
154 String fShortPrettyName;
155
156 /**
157 * e.g. RedHat 3.5, Ubuntu 11, Windows XP, Windows 8, Windows 8.1, Windows 10
158 */
159 String fPrettyNameWithMajorVersion;
160
161 /**
162 * Similar level of detail to what is printed by 'winver' application.
163 *
164 * e.g. Windows 10 Version 1809 (OS Build 17763.379)
165 */
166 String fPrettyNameWithVersionDetails;
167
168 /**
169 * e.g. 1.0, 3.5, etc. Note - this refers to the overall os (distribution -
170 * like for ubuntu 11.04, this would be 11.04, not the kernel version)
171 */
172 String fMajorMinorVersionString;
173
174 /**
175 * http://tools.ietf.org/html/rfc1945#section-10.15
176 * http://tools.ietf.org/html/rfc1945#section-3.7
177 *
178 * e.g. MyProduct/1.0, Mozilla/3.5, etc
179 */
180 String fRFC1945CompatProductTokenWithVersion;
181
182 /**
183 * Number of bits the OS targets. Often a 64-bit OS will support 32-bits, and concievably other
184 * combinations are possible. But this value returns the principle / primary number of bits supported
185 * by the OS (bits of addressing).
186 */
187 unsigned int fBits{32};
188
189 /**
190 *
191 * \note Common::DefaultNames<> supported
192 */
193 enum class InstallerTechnology {
194 eRPM,
195 eMSI,
196 eDPKG,
197
198 Stroika_Define_Enum_Bounds (eRPM, eDPKG)
199 };
200
201 /**
202 * Some UNIX systems use rpm (redhat, and many others), and others use dpkg (Debian based).
203 * Windows uses MSI.
204 * But there are a wide variety of other choices (portage, ports, etc).
205 */
206 optional<InstallerTechnology> fPreferredInstallerTechnology;
207
208 /**
209 * @see Characters::ToString ();
210 */
211 nonvirtual String ToString () const;
212 };
213
214 /**
215 * This is very frequently NOT useful, not unique, but frequently desired/used,
216 * so at least you can get to it uniformly, cross-platform.
217 */
219 /**
220 * Returns the best OS dependent guess at a computer name we have.
221 *
222 * On windows, this amounts to the NETBIOS name, and on UNIX, this amounts
223 * to the result of 'gethostname' (man 2 hostname).
224 */
226
227 /**
228 * @see Characters::ToString ();
229 */
230 nonvirtual String ToString () const;
231 };
232
233 BootInformation fBootInformation;
234 CPU fCPU;
235 Memory fMemory;
236
237 /**
238 * Info about the actual operating system this software is running on (if possible to tell, it could be well hidden)
239 *
240 * Often virtualization (things like compatibility mode in a manifest, or perhaps docker, or WSL) makes the apparent
241 * operating system different than the one you are actually running on).
242 *
243 * \note Actual Operating System is typically what you would report to a user (GUI).
244 */
245 OperatingSystem fActualOperatingSystem;
246
247 /**
248 * Return info about the apparent operating system this software is running on.
249 *
250 * Often virtualization (things like compatibility mode in a manifest, or perhaps docker, or WSL) makes the apparent
251 * operating system different than the one you are actually running on).
252 */
253 OperatingSystem fApparentOperatingSystem;
254 ComputerNames fComputerNames;
255
256 /**
257 */
258 SystemConfiguration (const BootInformation& bi, const CPU& ci, const Memory& mi, const OperatingSystem& oi, const ComputerNames& cn);
259 SystemConfiguration (const BootInformation& bi, const CPU& ci, const Memory& mi, const OperatingSystem& actualOS,
260 const OperatingSystem& apparentOS, const ComputerNames& cn);
261
262 /**
263 * @see Characters::ToString ();
264 */
265 nonvirtual String ToString () const;
266 };
267
268 /**
269 * @brief Get the System Configuration object - note not a system global - because the configuration can change while the app is running
270 *
271 * @return SystemConfiguration
272 */
273 SystemConfiguration GetSystemConfiguration ();
274
275 /**
276 */
277 SystemConfiguration::BootInformation GetSystemConfiguration_BootInformation ();
278
279 /**
280 */
281 SystemConfiguration::CPU GetSystemConfiguration_CPU ();
282
283 /**
284 */
285 SystemConfiguration::Memory GetSystemConfiguration_Memory ();
286
287 /**
288 * Return info about the actual operating system this software is running on (if possible to tell, it could be well hidden)
289 *
290 * Often virtualization (things like compatability mode in a manifest, or perhaps docker, or WSL) makes the apprarent
291 * operating system different than the one you are actually running on).
292 */
293 SystemConfiguration::OperatingSystem GetSystemConfiguration_ActualOperatingSystem ();
294
295 /**
296 * Return info about the apparent operating system this software is running on.
297 *
298 * Often virtualization (things like compatability mode in a manifest, or perhaps docker, or WSL) makes the apprarent
299 * operating system different than the one you are actually running on).
300 */
301 SystemConfiguration::OperatingSystem GetSystemConfiguration_ApparentOperatingSystem ();
302
303 /**
304 */
305 SystemConfiguration::ComputerNames GetSystemConfiguration_ComputerNames ();
306
307 /**
308 * \brief return the number of currently available CPU cores on this (virtual) machine
309 *
310 * This is very roughly GetSystemConfiguration_CPU ().GetNumberOfLogicalCores ()
311 * BUT - this CAN CHANGE over time, and this routine tries to provide a QUICK (cheap so not 100% guaranteed right) answer
312 * but generally will be quicker than GetSystemConfiguration_CPU ().GetNumberOfLogicalCores (), and provides
313 * stronger guaranteeds about accurance than std::thread::hardware_concurrency (https://en.cppreference.com/w/cpp/thread/thread/hardware_concurrency)
314 *
315 * @see GetSystemConfiguration_CPU
316 * @see SystemConfiguration::CPU::GetNumberOfLogicalCores
317 */
318 unsigned int GetNumberOfLogicalCPUCores (const chrono::duration<double>& allowedStaleness = 1min);
319
320}
321
322/*
323 ********************************************************************************
324 ***************************** Implementation Details ***************************
325 ********************************************************************************
326 */
327#include "SystemConfiguration.inl"
328
329#endif /*_Stroika_Foundation_Common_SystemConfiguration_h_*/
#define Stroika_Define_Enum_Bounds(FIRST_ITEM, LAST_ITEM)
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
A generalization of a vector: a container whose elements are keyed by the natural numbers.
Definition Sequence.h:187
STRING_TYPE ToString(FLOAT_TYPE f, const ToStringOptions &options={})
SystemConfiguration::OperatingSystem GetSystemConfiguration_ActualOperatingSystem()
SystemConfiguration GetSystemConfiguration()
Get the System Configuration object - note not a system global - because the configuration can change...
SystemConfiguration::OperatingSystem GetSystemConfiguration_ApparentOperatingSystem()
unsigned int GetNumberOfLogicalCPUCores(const chrono::duration< double > &allowedStaleness=1min)
return the number of currently available CPU cores on this (virtual) machine