System.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <AnKi/Util/StringList.h>
  8. #include <cstdio>
  9. #if ANKI_POSIX
  10. # include <unistd.h>
  11. # include <signal.h>
  12. #elif ANKI_OS_WINDOWS
  13. # include <AnKi/Util/Win32Minimal.h>
  14. #else
  15. # error "Unimplemented"
  16. #endif
  17. // For print backtrace
  18. #if ANKI_POSIX && !ANKI_OS_ANDROID
  19. # include <execinfo.h>
  20. # include <cstdlib>
  21. #endif
  22. #if ANKI_OS_ANDROID
  23. # include <android_native_app_glue.h>
  24. # include <fcntl.h>
  25. #endif
  26. namespace anki {
  27. U32 getCpuCoresCount()
  28. {
  29. #if ANKI_POSIX
  30. return U32(sysconf(_SC_NPROCESSORS_ONLN));
  31. #elif ANKI_OS_WINDOWS
  32. SYSTEM_INFO sysinfo;
  33. GetSystemInfo(&sysinfo);
  34. return sysinfo.dwNumberOfProcessors;
  35. #else
  36. # error "Unimplemented"
  37. #endif
  38. }
  39. void backtraceInternal(const Function<void(CString)>& lambda)
  40. {
  41. #if ANKI_POSIX && !ANKI_OS_ANDROID
  42. // Get addresses's for all entries on the stack
  43. const U32 maxStackSize = 64;
  44. void** array = static_cast<void**>(malloc(maxStackSize * sizeof(void*)));
  45. if(array)
  46. {
  47. const I32 size = ::backtrace(array, I32(maxStackSize));
  48. // Get symbols
  49. char** strings = backtrace_symbols(array, size);
  50. if(strings)
  51. {
  52. for(I32 i = 0; i < size; ++i)
  53. {
  54. lambda(strings[i]);
  55. }
  56. free(strings);
  57. }
  58. free(array);
  59. }
  60. #else
  61. lambda("backtrace() not supported in " ANKI_OS_STR);
  62. #endif
  63. }
  64. Bool runningFromATerminal()
  65. {
  66. #if ANKI_POSIX
  67. return isatty(fileno(stdin));
  68. #else
  69. return false;
  70. #endif
  71. }
  72. std::tm getLocalTime()
  73. {
  74. std::time_t t = std::time(nullptr);
  75. std::tm tm;
  76. #if ANKI_POSIX
  77. localtime_r(&t, &tm);
  78. #elif ANKI_OS_WINDOWS
  79. localtime_s(&tm, &t);
  80. #else
  81. # error See file
  82. #endif
  83. return tm;
  84. }
  85. #if ANKI_OS_ANDROID
  86. /// Get the name of the apk. Doesn't use File to open files because /proc files are a bit special.
  87. static Error getAndroidApkName(StringAuto& name)
  88. {
  89. const pid_t pid = getpid();
  90. StringAuto path(name.getAllocator());
  91. path.sprintf("/proc/%d/cmdline", pid);
  92. const int fd = open(path.cstr(), O_RDONLY);
  93. if(fd < 0)
  94. {
  95. ANKI_UTIL_LOGE("open() failed for: %s", path.cstr());
  96. return Error::FUNCTION_FAILED;
  97. }
  98. Array<char, 128> tmp;
  99. const ssize_t readBytes = read(fd, &tmp[0], sizeof(tmp));
  100. if(readBytes < 0 || readBytes == 0)
  101. {
  102. close(fd);
  103. ANKI_UTIL_LOGE("read() failed for: %s", path.cstr());
  104. return Error::FUNCTION_FAILED;
  105. }
  106. name.create('?', readBytes);
  107. memcpy(&name[0], &tmp[0], readBytes);
  108. close(fd);
  109. return Error::NONE;
  110. }
  111. void* getAndroidCommandLineArguments(int& argc, char**& argv)
  112. {
  113. argc = 0;
  114. argv = 0;
  115. ANKI_ASSERT(g_androidApp);
  116. JNIEnv* env;
  117. g_androidApp->activity->vm->AttachCurrentThread(&env, NULL);
  118. // Call getIntent().getStringExtra()
  119. jobject me = g_androidApp->activity->clazz;
  120. jclass acl = env->GetObjectClass(me); // class pointer of NativeActivity;
  121. jmethodID giid = env->GetMethodID(acl, "getIntent", "()Landroid/content/Intent;");
  122. jobject intent = env->CallObjectMethod(me, giid); // Got our intent
  123. jclass icl = env->GetObjectClass(intent); // class pointer of Intent
  124. jmethodID gseid = env->GetMethodID(icl, "getStringExtra", "(Ljava/lang/String;)Ljava/lang/String;");
  125. jstring jsParam1 = static_cast<jstring>(env->CallObjectMethod(intent, gseid, env->NewStringUTF("cmd")));
  126. // Parse the command line args
  127. HeapAllocator<U8> alloc(allocAligned, nullptr);
  128. StringListAuto args(alloc);
  129. if(jsParam1)
  130. {
  131. const char* param1 = env->GetStringUTFChars(jsParam1, 0);
  132. args.splitString(param1, ' ');
  133. env->ReleaseStringUTFChars(jsParam1, param1);
  134. }
  135. // Add the apk name
  136. StringAuto apkName(alloc);
  137. if(!getAndroidApkName(apkName))
  138. {
  139. args.pushFront(apkName);
  140. }
  141. else
  142. {
  143. args.pushFront("unknown_apk");
  144. }
  145. // Allocate memory for all
  146. U32 allStringsSize = 0;
  147. for(const String& s : args)
  148. {
  149. allStringsSize += s.getLength() + 1;
  150. ++argc;
  151. }
  152. const PtrSize bufferSize = allStringsSize + sizeof(char*) * argc;
  153. void* buffer = mallocAligned(bufferSize, ANKI_SAFE_ALIGNMENT);
  154. // Set argv
  155. argv = static_cast<char**>(buffer);
  156. char* cbuffer = static_cast<char*>(buffer);
  157. cbuffer += sizeof(char*) * argc;
  158. U32 count = 0;
  159. for(const String& s : args)
  160. {
  161. memcpy(cbuffer, &s[0], s.getLength() + 1);
  162. argv[count++] = &cbuffer[0];
  163. cbuffer += s.getLength() + 1;
  164. }
  165. ANKI_ASSERT(ptrToNumber(cbuffer) == ptrToNumber(buffer) + bufferSize);
  166. return buffer;
  167. }
  168. void cleanupGetAndroidCommandLineArguments(void* ptr)
  169. {
  170. ANKI_ASSERT(ptr);
  171. freeAligned(ptr);
  172. }
  173. #endif
  174. } // end namespace anki