System.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (C) 2009-2020, 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 BackTraceWalker::exec()
  36. {
  37. #if ANKI_POSIX && !ANKI_OS_ANDROID
  38. // Get addresses's for all entries on the stack
  39. void** array = static_cast<void**>(malloc(m_stackSize * sizeof(void*)));
  40. if(array)
  41. {
  42. I32 size = backtrace(array, I32(m_stackSize));
  43. // Get symbols
  44. char** strings = backtrace_symbols(array, size);
  45. if(strings)
  46. {
  47. for(I32 i = 0; i < size; ++i)
  48. {
  49. operator()(strings[i]);
  50. }
  51. free(strings);
  52. }
  53. free(array);
  54. }
  55. #else
  56. ANKI_UTIL_LOGW("BackTraceWalker::exec() Not supported in this platform");
  57. #endif
  58. }
  59. Bool runningFromATerminal()
  60. {
  61. #if ANKI_POSIX
  62. return isatty(fileno(stdin));
  63. #else
  64. return false;
  65. #endif
  66. }
  67. } // end namespace anki