The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
9.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. String SystemStats::getJUCEVersion()
  20. {
  21. // Some basic tests, to keep an eye on things and make sure these types work ok
  22. // on all platforms. Let me know if any of these assertions fail on your system!
  23. static_assert (sizeof (pointer_sized_int) == sizeof (void*), "Basic sanity test failed: please report!");
  24. static_assert (sizeof (int8) == 1, "Basic sanity test failed: please report!");
  25. static_assert (sizeof (uint8) == 1, "Basic sanity test failed: please report!");
  26. static_assert (sizeof (int16) == 2, "Basic sanity test failed: please report!");
  27. static_assert (sizeof (uint16) == 2, "Basic sanity test failed: please report!");
  28. static_assert (sizeof (int32) == 4, "Basic sanity test failed: please report!");
  29. static_assert (sizeof (uint32) == 4, "Basic sanity test failed: please report!");
  30. static_assert (sizeof (int64) == 8, "Basic sanity test failed: please report!");
  31. static_assert (sizeof (uint64) == 8, "Basic sanity test failed: please report!");
  32. return "JUCE v" JUCE_STRINGIFY(JUCE_MAJOR_VERSION)
  33. "." JUCE_STRINGIFY(JUCE_MINOR_VERSION)
  34. "." JUCE_STRINGIFY(JUCE_BUILDNUMBER);
  35. }
  36. #if JUCE_ANDROID && ! defined (JUCE_DISABLE_JUCE_VERSION_PRINTING)
  37. #define JUCE_DISABLE_JUCE_VERSION_PRINTING 1
  38. #endif
  39. #if JUCE_DEBUG && ! JUCE_DISABLE_JUCE_VERSION_PRINTING
  40. struct JuceVersionPrinter
  41. {
  42. JuceVersionPrinter()
  43. {
  44. DBG (SystemStats::getJUCEVersion());
  45. }
  46. };
  47. static JuceVersionPrinter juceVersionPrinter;
  48. #endif
  49. StringArray SystemStats::getDeviceIdentifiers()
  50. {
  51. StringArray ids;
  52. #if JUCE_WINDOWS
  53. File f (File::getSpecialLocation (File::windowsSystemDirectory));
  54. #else
  55. File f ("~");
  56. #endif
  57. if (auto num = f.getFileIdentifier())
  58. {
  59. ids.add (String::toHexString ((int64) num));
  60. }
  61. else
  62. {
  63. for (auto& address : MACAddress::getAllAddresses())
  64. ids.add (address.toString());
  65. }
  66. jassert (! ids.isEmpty()); // Failed to create any IDs!
  67. return ids;
  68. }
  69. //==============================================================================
  70. struct CPUInformation
  71. {
  72. CPUInformation() noexcept { initialise(); }
  73. void initialise() noexcept;
  74. int numLogicalCPUs = 0, numPhysicalCPUs = 0;
  75. bool hasMMX = false, hasSSE = false, hasSSE2 = false, hasSSE3 = false,
  76. has3DNow = false, hasFMA3 = false, hasFMA4 = false, hasSSSE3 = false,
  77. hasSSE41 = false, hasSSE42 = false, hasAVX = false, hasAVX2 = false,
  78. hasAVX512F = false, hasAVX512BW = false, hasAVX512CD = false,
  79. hasAVX512DQ = false, hasAVX512ER = false, hasAVX512IFMA = false,
  80. hasAVX512PF = false, hasAVX512VBMI = false, hasAVX512VL = false,
  81. hasAVX512VPOPCNTDQ = false,
  82. hasNeon = false;
  83. };
  84. static const CPUInformation& getCPUInformation() noexcept
  85. {
  86. static CPUInformation info;
  87. return info;
  88. }
  89. int SystemStats::getNumCpus() noexcept { return getCPUInformation().numLogicalCPUs; }
  90. int SystemStats::getNumPhysicalCpus() noexcept { return getCPUInformation().numPhysicalCPUs; }
  91. bool SystemStats::hasMMX() noexcept { return getCPUInformation().hasMMX; }
  92. bool SystemStats::has3DNow() noexcept { return getCPUInformation().has3DNow; }
  93. bool SystemStats::hasFMA3() noexcept { return getCPUInformation().hasFMA3; }
  94. bool SystemStats::hasFMA4() noexcept { return getCPUInformation().hasFMA4; }
  95. bool SystemStats::hasSSE() noexcept { return getCPUInformation().hasSSE; }
  96. bool SystemStats::hasSSE2() noexcept { return getCPUInformation().hasSSE2; }
  97. bool SystemStats::hasSSE3() noexcept { return getCPUInformation().hasSSE3; }
  98. bool SystemStats::hasSSSE3() noexcept { return getCPUInformation().hasSSSE3; }
  99. bool SystemStats::hasSSE41() noexcept { return getCPUInformation().hasSSE41; }
  100. bool SystemStats::hasSSE42() noexcept { return getCPUInformation().hasSSE42; }
  101. bool SystemStats::hasAVX() noexcept { return getCPUInformation().hasAVX; }
  102. bool SystemStats::hasAVX2() noexcept { return getCPUInformation().hasAVX2; }
  103. bool SystemStats::hasAVX512F() noexcept { return getCPUInformation().hasAVX512F; }
  104. bool SystemStats::hasAVX512BW() noexcept { return getCPUInformation().hasAVX512BW; }
  105. bool SystemStats::hasAVX512CD() noexcept { return getCPUInformation().hasAVX512CD; }
  106. bool SystemStats::hasAVX512DQ() noexcept { return getCPUInformation().hasAVX512DQ; }
  107. bool SystemStats::hasAVX512ER() noexcept { return getCPUInformation().hasAVX512ER; }
  108. bool SystemStats::hasAVX512IFMA() noexcept { return getCPUInformation().hasAVX512IFMA; }
  109. bool SystemStats::hasAVX512PF() noexcept { return getCPUInformation().hasAVX512PF; }
  110. bool SystemStats::hasAVX512VBMI() noexcept { return getCPUInformation().hasAVX512VBMI; }
  111. bool SystemStats::hasAVX512VL() noexcept { return getCPUInformation().hasAVX512VL; }
  112. bool SystemStats::hasAVX512VPOPCNTDQ() noexcept { return getCPUInformation().hasAVX512VPOPCNTDQ; }
  113. bool SystemStats::hasNeon() noexcept { return getCPUInformation().hasNeon; }
  114. //==============================================================================
  115. String SystemStats::getStackBacktrace()
  116. {
  117. String result;
  118. #if JUCE_ANDROID || JUCE_MINGW || JUCE_WASM
  119. jassertfalse; // sorry, not implemented yet!
  120. #elif JUCE_WINDOWS
  121. HANDLE process = GetCurrentProcess();
  122. SymInitialize (process, nullptr, TRUE);
  123. void* stack[128];
  124. int frames = (int) CaptureStackBackTrace (0, numElementsInArray (stack), stack, nullptr);
  125. HeapBlock<SYMBOL_INFO> symbol;
  126. symbol.calloc (sizeof (SYMBOL_INFO) + 256, 1);
  127. symbol->MaxNameLen = 255;
  128. symbol->SizeOfStruct = sizeof (SYMBOL_INFO);
  129. for (int i = 0; i < frames; ++i)
  130. {
  131. DWORD64 displacement = 0;
  132. if (SymFromAddr (process, (DWORD64) stack[i], &displacement, symbol))
  133. {
  134. result << i << ": ";
  135. IMAGEHLP_MODULE64 moduleInfo;
  136. zerostruct (moduleInfo);
  137. moduleInfo.SizeOfStruct = sizeof (moduleInfo);
  138. if (::SymGetModuleInfo64 (process, symbol->ModBase, &moduleInfo))
  139. result << moduleInfo.ModuleName << ": ";
  140. result << symbol->Name << " + 0x" << String::toHexString ((int64) displacement) << newLine;
  141. }
  142. }
  143. #else
  144. void* stack[128];
  145. auto frames = backtrace (stack, numElementsInArray (stack));
  146. char** frameStrings = backtrace_symbols (stack, frames);
  147. for (int i = 0; i < frames; ++i)
  148. result << frameStrings[i] << newLine;
  149. ::free (frameStrings);
  150. #endif
  151. return result;
  152. }
  153. //==============================================================================
  154. #if ! JUCE_WASM
  155. static SystemStats::CrashHandlerFunction globalCrashHandler = nullptr;
  156. #if JUCE_WINDOWS
  157. static LONG WINAPI handleCrash (LPEXCEPTION_POINTERS ep)
  158. {
  159. globalCrashHandler (ep);
  160. return EXCEPTION_EXECUTE_HANDLER;
  161. }
  162. #else
  163. static void handleCrash (int signum)
  164. {
  165. globalCrashHandler ((void*) (pointer_sized_int) signum);
  166. ::kill (getpid(), SIGKILL);
  167. }
  168. int juce_siginterrupt (int sig, int flag);
  169. #endif
  170. void SystemStats::setApplicationCrashHandler (CrashHandlerFunction handler)
  171. {
  172. jassert (handler != nullptr); // This must be a valid function.
  173. globalCrashHandler = handler;
  174. #if JUCE_WINDOWS
  175. SetUnhandledExceptionFilter (handleCrash);
  176. #else
  177. const int signals[] = { SIGFPE, SIGILL, SIGSEGV, SIGBUS, SIGABRT, SIGSYS };
  178. for (int i = 0; i < numElementsInArray (signals); ++i)
  179. {
  180. ::signal (signals[i], handleCrash);
  181. juce_siginterrupt (signals[i], 1);
  182. }
  183. #endif
  184. }
  185. #endif
  186. bool SystemStats::isRunningInAppExtensionSandbox() noexcept
  187. {
  188. #if JUCE_MAC || JUCE_IOS
  189. static bool firstQuery = true;
  190. static bool isRunningInAppSandbox = false;
  191. if (firstQuery)
  192. {
  193. firstQuery = false;
  194. File bundle = File::getSpecialLocation (File::invokedExecutableFile).getParentDirectory();
  195. #if JUCE_MAC
  196. bundle = bundle.getParentDirectory().getParentDirectory();
  197. #endif
  198. if (bundle.isDirectory())
  199. isRunningInAppSandbox = (bundle.getFileExtension() == ".appex");
  200. }
  201. return isRunningInAppSandbox;
  202. #else
  203. return false;
  204. #endif
  205. }
  206. } // namespace juce