JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_SystemStats_linux.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23#if JUCE_BELA
24extern "C" int cobalt_thread_mode();
25#endif
26
27namespace juce
28{
29
30#if ! JUCE_BSD
31static String getCpuInfo (const char* key)
32{
33 return readPosixConfigFileValue ("/proc/cpuinfo", key);
34}
35
36static String getLocaleValue (nl_item key)
37{
38 auto oldLocale = ::setlocale (LC_ALL, "");
39 auto result = String::fromUTF8 (nl_langinfo (key));
40 ::setlocale (LC_ALL, oldLocale);
41 return result;
42}
43#endif
44
45//==============================================================================
47{
48 std::cerr << text << std::endl;
49}
50
51//==============================================================================
56
58{
59 return "Linux";
60}
61
63{
64 #if JUCE_64BIT
65 return true;
66 #else
67 //xxx not sure how to find this out?..
68 return false;
69 #endif
70}
71
72//==============================================================================
74{
75 #if JUCE_BSD
76 int mib[] = {
77 CTL_HW,
79 };
80 size_t machineDescriptionLength = 0;
81 auto result = sysctl (mib, numElementsInArray (mib), nullptr, &machineDescriptionLength, nullptr, 0);
82
83 if (result != 0 || machineDescriptionLength == 0)
84 return {};
85
87 result = sysctl (mib, numElementsInArray (mib), machineDescription.getData(), &machineDescriptionLength, nullptr, 0);
88 return String::fromUTF8 (result == 0 ? (char*) machineDescription.getData() : "");
89 #else
90 return getCpuInfo ("Hardware");
91 #endif
92}
93
98
100{
101 #if JUCE_BSD
102 return {};
103 #else
104 auto v = getCpuInfo ("vendor_id");
105
106 if (v.isEmpty())
107 v = getCpuInfo ("model name");
108
109 return v;
110 #endif
111}
112
114{
115 #if JUCE_BSD
116 int mib[] = {
117 CTL_HW,
119 };
120 size_t modelLength = 0;
121 auto result = sysctl (mib, numElementsInArray (mib), nullptr, &modelLength, nullptr, 0);
122
123 if (result != 0 || modelLength == 0)
124 return {};
125
126 MemoryBlock model { modelLength };
127 result = sysctl (mib, numElementsInArray (mib), model.getData(), &modelLength, nullptr, 0);
128 return String::fromUTF8 (result == 0 ? (char*) model.getData() : "");
129 #else
130 return getCpuInfo ("model name");
131 #endif
132}
133
135{
136 #if JUCE_BSD
137 int32 clockRate = 0;
138 auto clockRateSize = sizeof (clockRate);
139 auto result = sysctlbyname ("hw.clockrate", &clockRate, &clockRateSize, nullptr, 0);
140 return result == 0 ? clockRate : 0;
141 #else
142 return roundToInt (getCpuInfo ("cpu MHz").getFloatValue());
143 #endif
144}
145
147{
148 #if JUCE_BSD
149 int mib[] = {
150 CTL_HW,
152 };
153 int64 memory = 0;
154 auto memorySize = sizeof (memory);
155 auto result = sysctl (mib, numElementsInArray (mib), &memory, &memorySize, nullptr, 0);
156 return result == 0 ? (int) (memory / (int64) 1e6) : 0;
157 #else
158 struct sysinfo sysi;
159
160 if (sysinfo (&sysi) == 0)
161 return (int) (sysi.totalram * sysi.mem_unit / (1024 * 1024));
162
163 return 0;
164 #endif
165}
166
168{
169 return (int) sysconf (_SC_PAGESIZE);
170}
171
172//==============================================================================
174{
175 if (auto user = getenv ("USER"))
176 return String::fromUTF8 (user);
177
178 if (auto pw = getpwuid (getuid()))
179 return String::fromUTF8 (pw->pw_name);
180
181 return {};
182}
183
188
190{
191 char name[256] = {};
192
193 if (gethostname (name, sizeof (name) - 1) == 0)
194 return name;
195
196 return {};
197}
198
200{
201 #if JUCE_BSD
202 if (auto langEnv = getenv ("LANG"))
203 return String::fromUTF8 (langEnv).upToLastOccurrenceOf (".UTF-8", false, true);
204
205 return {};
206 #else
207 return getLocaleValue (_NL_ADDRESS_LANG_AB);
208 #endif
209}
210
212{
213 #if JUCE_BSD
214 return {};
215 #else
216 return getLocaleValue (_NL_ADDRESS_COUNTRY_AB2);
217 #endif
218}
219
221{
222 auto result = getUserLanguage();
223 auto region = getUserRegion();
224
225 if (region.isNotEmpty())
226 result << "-" << region;
227
228 return result;
229}
230
231//==============================================================================
232void CPUInformation::initialise() noexcept
233{
234 #if JUCE_BSD
235 #if JUCE_INTEL && ! JUCE_NO_INLINE_ASM
236 SystemStatsHelpers::getCPUInfo (hasMMX,
237 hasSSE,
238 hasSSE2,
239 has3DNow,
240 hasSSE3,
241 hasSSSE3,
242 hasFMA3,
243 hasSSE41,
244 hasSSE42,
245 hasAVX,
246 hasFMA4,
247 hasAVX2,
248 hasAVX512F,
249 hasAVX512DQ,
250 hasAVX512IFMA,
251 hasAVX512PF,
252 hasAVX512ER,
253 hasAVX512CD,
254 hasAVX512BW,
255 hasAVX512VL,
256 hasAVX512VBMI,
257 hasAVX512VPOPCNTDQ);
258 #endif
259
260 numLogicalCPUs = numPhysicalCPUs = []
261 {
262 int mib[] = {
263 CTL_HW,
264 HW_NCPU
265 };
266 int32 numCPUs = 1;
267 auto numCPUsSize = sizeof (numCPUs);
268 auto result = sysctl (mib, numElementsInArray (mib), &numCPUs, &numCPUsSize, nullptr, 0);
269 return result == 0 ? numCPUs : 1;
270 }();
271 #else
272 auto flags = getCpuInfo ("flags");
273
274 hasMMX = flags.contains ("mmx");
275 hasFMA3 = flags.contains ("fma");
276 hasFMA4 = flags.contains ("fma4");
277 hasSSE = flags.contains ("sse");
278 hasSSE2 = flags.contains ("sse2");
279 hasSSE3 = flags.contains ("sse3");
280 has3DNow = flags.contains ("3dnow");
281 hasSSSE3 = flags.contains ("ssse3");
282 hasSSE41 = flags.contains ("sse4_1");
283 hasSSE42 = flags.contains ("sse4_2");
284 hasAVX = flags.contains ("avx");
285 hasAVX2 = flags.contains ("avx2");
286 hasAVX512F = flags.contains ("avx512f");
287 hasAVX512BW = flags.contains ("avx512bw");
288 hasAVX512CD = flags.contains ("avx512cd");
289 hasAVX512DQ = flags.contains ("avx512dq");
290 hasAVX512ER = flags.contains ("avx512er");
291 hasAVX512IFMA = flags.contains ("avx512ifma");
292 hasAVX512PF = flags.contains ("avx512pf");
293 hasAVX512VBMI = flags.contains ("avx512vbmi");
294 hasAVX512VL = flags.contains ("avx512vl");
295 hasAVX512VPOPCNTDQ = flags.contains ("avx512_vpopcntdq");
296
297 numLogicalCPUs = getCpuInfo ("processor").getIntValue() + 1;
298
299 // Assume CPUs in all sockets have the same number of cores
300 numPhysicalCPUs = getCpuInfo ("cpu cores").getIntValue() * (getCpuInfo ("physical id").getIntValue() + 1);
301
302 if (numPhysicalCPUs <= 0)
303 numPhysicalCPUs = numLogicalCPUs;
304 #endif
305}
306
308{
309 static const auto deviceId = []()
310 {
311 const auto call = [] (auto command) -> String
312 {
314
315 if (proc.start (command, ChildProcess::wantStdOut))
316 return proc.readAllProcessOutput();
317
318 return {};
319 };
320
321 auto data = call ("cat /sys/class/dmi/id/board_serial");
322
323 // 'board_serial' is enough on its own, fallback to bios stuff if we can't find it.
324 if (data.isEmpty())
325 {
326 data = call ("cat /sys/class/dmi/id/bios_date")
327 + call ("cat /sys/class/dmi/id/bios_release")
328 + call ("cat /sys/class/dmi/id/bios_vendor")
329 + call ("cat /sys/class/dmi/id/bios_version");
330 }
331
332 auto cpuData = call ("lscpu");
333
334 if (cpuData.isNotEmpty())
335 {
336 auto getCpuInfo = [&cpuData] (auto key) -> String
337 {
338 auto index = cpuData.indexOf (key);
339
340 if (index >= 0)
341 {
342 auto start = cpuData.indexOf (index, ":");
343 auto end = cpuData.indexOf (start, "\n");
344
345 return cpuData.substring (start + 1, end).trim();
346 }
347
348 return {};
349 };
350
351 data += getCpuInfo ("CPU family:");
352 data += getCpuInfo ("Model:");
353 data += getCpuInfo ("Model name:");
354 data += getCpuInfo ("Vendor ID:");
355 }
356
357 return String ((uint64_t) data.hashCode64());
358 }();
359
360 // Please tell someone at JUCE if this occurs
361 jassert (deviceId.isNotEmpty());
362 return deviceId;
363}
364
365//==============================================================================
366uint32 juce_millisecondsSinceStartup() noexcept
367{
368 return (uint32) (Time::getHighResolutionTicks() / 1000);
369}
370
372{
373 timespec t;
374
375 #if JUCE_BELA
376 if (cobalt_thread_mode() == 0x200 /*XNRELAX*/)
378 else
380 #else
382 #endif
383
384 return (t.tv_sec * (int64) 1000000) + (t.tv_nsec / 1000);
385}
386
388{
389 return 1000000; // (microseconds)
390}
391
393{
394 return (double) getHighResolutionTicks() * 0.001;
395}
396
398{
399 timeval t;
400 t.tv_sec = decltype (timeval::tv_sec) (millisSinceEpoch / 1000);
401 t.tv_usec = decltype (timeval::tv_usec) ((millisSinceEpoch - t.tv_sec * 1000) * 1000);
402
403 return settimeofday (&t, nullptr) == 0;
404}
405
407{
408 #if JUCE_BSD
409 int mib[] =
410 {
411 CTL_KERN,
412 KERN_PROC,
414 ::getpid()
415 };
416 struct kinfo_proc info;
417 auto infoSize = sizeof (info);
418 auto result = sysctl (mib, numElementsInArray (mib), &info, &infoSize, nullptr, 0);
419 return result == 0 ? ((info.ki_flag & P_TRACED) != 0) : false;
420 #else
421 return readPosixConfigFileValue ("/proc/self/status", "TracerPid").getIntValue() > 0;
422 #endif
423}
424
425} // namespace juce
Launches and monitors a child process.
String readAllProcessOutput()
Blocks until the process has finished, and then returns its complete output as a string.
static void JUCE_CALLTYPE outputDebugString(const String &text)
Writes a message to the standard error stream.
A class to hold a resizable block of raw data.
The JUCE String class!
Definition juce_String.h:53
int indexOf(StringRef textToLookFor) const noexcept
Searches for a substring within this string.
String upToLastOccurrenceOf(StringRef substringToFind, bool includeSubStringInResult, bool ignoreCase) const
Returns the start of this string, up to the last occurrence of a substring.
static String fromUTF8(const char *utf8buffer, int bufferSizeBytes=-1)
Creates a String from a UTF-8 encoded buffer.
int getIntValue() const noexcept
Reads the value of the string as a decimal number (up to 32 bits in size).
static String getUniqueDeviceID()
This method returns a machine unique ID unaffected by storage or peripheral changes.
static int getPageSize()
Returns the system page-size.
static String getOperatingSystemName()
Returns the name of the type of operating system we're running on.
static String getUserRegion()
Returns the region of the user's locale.
static String getDeviceDescription()
This will attempt to return some kind of string describing the device.
static String getLogonName()
Returns the current user's name, if available.
static String getUserLanguage()
Returns the language of the user's locale.
static String getDeviceManufacturer()
This will attempt to return the manufacturer of the device.
static bool isOperatingSystem64Bit()
Returns true if the OS is 64-bit, or false for a 32-bit OS.
OperatingSystemType
The set of possible results of the getOperatingSystemType() method.
static String getComputerName()
Returns the host-name of the computer.
static OperatingSystemType getOperatingSystemType()
Returns the type of operating system we're running on.
static String getCpuModel()
Attempts to return a string describing the CPU model.
static int getCpuSpeedInMegahertz()
Returns the approximate CPU speed.
static String getCpuVendor()
Returns a string to indicate the CPU vendor.
static String getFullUserName()
Returns the current user's full name, if available.
static int getMemorySizeInMegabytes()
Finds out how much RAM is in the machine.
static String getDisplayLanguage()
Returns the user's display language.
static double getMillisecondCounterHiRes() noexcept
Returns the number of millisecs since a fixed event (usually system startup).
static int64 getHighResolutionTicksPerSecond() noexcept
Returns the resolution of the high-resolution counter in ticks per second.
bool setSystemTimeToThisTime() const
Tries to set the computer's clock.
static int64 getHighResolutionTicks() noexcept
Returns the current high-resolution counter's tick-count.
clock_gettime
T endl(T... args)
getenv
gethostname
getpwuid
getuid
#define jassert(expression)
Platform-independent assertion macro.
#define JUCE_CALLTYPE
This macro defines the C calling convention used as the standard for JUCE calls.
typedef int
JUCE Namespace.
bool juce_isRunningUnderDebugger() noexcept
< This macro is added to all JUCE public class declarations.
RangedDirectoryIterator end(const RangedDirectoryIterator &)
Returns a default-constructed sentinel value.
signed int int32
A platform-independent 32-bit signed integer type.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
unsigned int uint32
A platform-independent 32-bit unsigned integer type.
int roundToInt(const FloatType value) noexcept
Fast floating-point-to-integer conversion.
constexpr int numElementsInArray(Type(&)[N]) noexcept
Handy function for getting the number of elements in a simple const C array.
long long int64
A platform-independent 64-bit integer type.
typedef uint64_t
sysconf