System.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Util/System.h>
  6. #include <AnKi/Util/Logger.h>
  7. #include <cstdio>
  8. #if ANKI_POSIX
  9. # include <unistd.h>
  10. # include <signal.h>
  11. #elif ANKI_OS_WINDOWS
  12. # include <AnKi/Util/Win32Minimal.h>
  13. #else
  14. # error "Unimplemented"
  15. #endif
  16. // For print backtrace
  17. #if ANKI_POSIX && !ANKI_OS_ANDROID
  18. # include <execinfo.h>
  19. # include <cstdlib>
  20. #endif
  21. namespace anki
  22. {
  23. U32 getCpuCoresCount()
  24. {
  25. #if ANKI_POSIX
  26. return U32(sysconf(_SC_NPROCESSORS_ONLN));
  27. #elif ANKI_OS_WINDOWS
  28. SYSTEM_INFO sysinfo;
  29. GetSystemInfo(&sysinfo);
  30. return sysinfo.dwNumberOfProcessors;
  31. #else
  32. # error "Unimplemented"
  33. #endif
  34. }
  35. void getBacktrace(BackTraceWalker& walker)
  36. {
  37. #if ANKI_POSIX && !ANKI_OS_ANDROID
  38. // Get addresses's for all entries on the stack
  39. const U32 maxStackSize = 64;
  40. void** array = static_cast<void**>(malloc(maxStackSize * sizeof(void*)));
  41. if(array)
  42. {
  43. const I32 size = backtrace(array, I32(maxStackSize));
  44. // Get symbols
  45. char** strings = backtrace_symbols(array, size);
  46. if(strings)
  47. {
  48. for(I32 i = 0; i < size; ++i)
  49. {
  50. walker(strings[i]);
  51. }
  52. free(strings);
  53. }
  54. free(array);
  55. }
  56. #else
  57. walker("getBacktrace() not supported in " ANKI_OS_STR);
  58. #endif
  59. }
  60. Bool runningFromATerminal()
  61. {
  62. #if ANKI_POSIX
  63. return isatty(fileno(stdin));
  64. #else
  65. return false;
  66. #endif
  67. }
  68. } // end namespace anki