alloc.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include "alloc.h"
  4. #include "intrinsics.h"
  5. #include "sysinfo.h"
  6. #include "mutex.h"
  7. ////////////////////////////////////////////////////////////////////////////////
  8. /// All Platforms
  9. ////////////////////////////////////////////////////////////////////////////////
  10. namespace embree
  11. {
  12. size_t total_allocations = 0;
  13. #if defined(EMBREE_SYCL_SUPPORT)
  14. __thread sycl::context* tls_context_tutorial = nullptr;
  15. __thread sycl::device* tls_device_tutorial = nullptr;
  16. __thread sycl::context* tls_context_embree = nullptr;
  17. __thread sycl::device* tls_device_embree = nullptr;
  18. void enableUSMAllocEmbree(sycl::context* context, sycl::device* device)
  19. {
  20. // -- GODOT start --
  21. // if (tls_context_embree != nullptr) throw std::runtime_error("USM allocation already enabled");
  22. // if (tls_device_embree != nullptr) throw std::runtime_error("USM allocation already enabled");
  23. if (tls_context_embree != nullptr) {
  24. abort();
  25. }
  26. if (tls_device_embree != nullptr) {
  27. abort();
  28. }
  29. // -- GODOT end --
  30. tls_context_embree = context;
  31. tls_device_embree = device;
  32. }
  33. void disableUSMAllocEmbree()
  34. {
  35. // -- GODOT start --
  36. // if (tls_context_embree == nullptr) throw std::runtime_error("USM allocation not enabled");
  37. // if (tls_device_embree == nullptr) throw std::runtime_error("USM allocation not enabled");
  38. if (tls_context_embree == nullptr) {
  39. abort();
  40. }
  41. if (tls_device_embree == nullptr) {
  42. abort();
  43. }
  44. // -- GODOT end --
  45. tls_context_embree = nullptr;
  46. tls_device_embree = nullptr;
  47. }
  48. void enableUSMAllocTutorial(sycl::context* context, sycl::device* device)
  49. {
  50. //if (tls_context_tutorial != nullptr) throw std::runtime_error("USM allocation already enabled");
  51. //if (tls_device_tutorial != nullptr) throw std::runtime_error("USM allocation already enabled");
  52. tls_context_tutorial = context;
  53. tls_device_tutorial = device;
  54. }
  55. void disableUSMAllocTutorial()
  56. {
  57. // -- GODOT start --
  58. // if (tls_context_tutorial == nullptr) throw std::runtime_error("USM allocation not enabled");
  59. // if (tls_device_tutorial == nullptr) throw std::runtime_error("USM allocation not enabled");
  60. if (tls_context_tutorial == nullptr) {
  61. abort();
  62. }
  63. if (tls_device_tutorial == nullptr) {
  64. abort();
  65. }
  66. // -- GODOT end --
  67. tls_context_tutorial = nullptr;
  68. tls_device_tutorial = nullptr;
  69. }
  70. #endif
  71. void* alignedMalloc(size_t size, size_t align)
  72. {
  73. if (size == 0)
  74. return nullptr;
  75. assert((align & (align-1)) == 0);
  76. void* ptr = _mm_malloc(size,align);
  77. // -- GODOT start --
  78. // if (size != 0 && ptr == nullptr)
  79. // throw std::bad_alloc();
  80. if (size != 0 && ptr == nullptr) {
  81. abort();
  82. }
  83. // -- GODOT end --
  84. return ptr;
  85. }
  86. void alignedFree(void* ptr)
  87. {
  88. if (ptr)
  89. _mm_free(ptr);
  90. }
  91. #if defined(EMBREE_SYCL_SUPPORT)
  92. void* alignedSYCLMalloc(sycl::context* context, sycl::device* device, size_t size, size_t align, EmbreeUSMMode mode)
  93. {
  94. assert(context);
  95. assert(device);
  96. if (size == 0)
  97. return nullptr;
  98. assert((align & (align-1)) == 0);
  99. total_allocations++;
  100. void* ptr = nullptr;
  101. if (mode == EMBREE_USM_SHARED_DEVICE_READ_ONLY)
  102. ptr = sycl::aligned_alloc_shared(align,size,*device,*context,sycl::ext::oneapi::property::usm::device_read_only());
  103. else
  104. ptr = sycl::aligned_alloc_shared(align,size,*device,*context);
  105. // -- GODOT start --
  106. // if (size != 0 && ptr == nullptr)
  107. // throw std::bad_alloc();
  108. if (size != 0 && ptr == nullptr) {
  109. abort();
  110. }
  111. // -- GODOT end --
  112. return ptr;
  113. }
  114. static MutexSys g_alloc_mutex;
  115. void* alignedSYCLMalloc(size_t size, size_t align, EmbreeUSMMode mode)
  116. {
  117. if (tls_context_tutorial) return alignedSYCLMalloc(tls_context_tutorial, tls_device_tutorial, size, align, mode);
  118. if (tls_context_embree ) return alignedSYCLMalloc(tls_context_embree, tls_device_embree, size, align, mode);
  119. return nullptr;
  120. }
  121. void alignedSYCLFree(sycl::context* context, void* ptr)
  122. {
  123. assert(context);
  124. if (ptr) {
  125. sycl::free(ptr,*context);
  126. }
  127. }
  128. void alignedSYCLFree(void* ptr)
  129. {
  130. if (tls_context_tutorial) return alignedSYCLFree(tls_context_tutorial, ptr);
  131. if (tls_context_embree ) return alignedSYCLFree(tls_context_embree, ptr);
  132. }
  133. #endif
  134. void* alignedUSMMalloc(size_t size, size_t align, EmbreeUSMMode mode)
  135. {
  136. #if defined(EMBREE_SYCL_SUPPORT)
  137. if (tls_context_embree || tls_context_tutorial)
  138. return alignedSYCLMalloc(size,align,mode);
  139. else
  140. #endif
  141. return alignedMalloc(size,align);
  142. }
  143. void alignedUSMFree(void* ptr)
  144. {
  145. #if defined(EMBREE_SYCL_SUPPORT)
  146. if (tls_context_embree || tls_context_tutorial)
  147. return alignedSYCLFree(ptr);
  148. else
  149. #endif
  150. return alignedFree(ptr);
  151. }
  152. static bool huge_pages_enabled = false;
  153. static MutexSys os_init_mutex;
  154. __forceinline bool isHugePageCandidate(const size_t bytes)
  155. {
  156. if (!huge_pages_enabled)
  157. return false;
  158. /* use huge pages only when memory overhead is low */
  159. const size_t hbytes = (bytes+PAGE_SIZE_2M-1) & ~size_t(PAGE_SIZE_2M-1);
  160. return 66*(hbytes-bytes) < bytes; // at most 1.5% overhead
  161. }
  162. }
  163. ////////////////////////////////////////////////////////////////////////////////
  164. /// Windows Platform
  165. ////////////////////////////////////////////////////////////////////////////////
  166. #ifdef _WIN32
  167. #define WIN32_LEAN_AND_MEAN
  168. #include <windows.h>
  169. #include <malloc.h>
  170. namespace embree
  171. {
  172. bool win_enable_selockmemoryprivilege (bool verbose)
  173. {
  174. HANDLE hToken;
  175. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken)) {
  176. if (verbose) std::cout << "WARNING: OpenProcessToken failed while trying to enable SeLockMemoryPrivilege: " << GetLastError() << std::endl;
  177. return false;
  178. }
  179. TOKEN_PRIVILEGES tp;
  180. tp.PrivilegeCount = 1;
  181. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  182. if (!LookupPrivilegeValueW(nullptr, L"SeLockMemoryPrivilege", &tp.Privileges[0].Luid)) {
  183. if (verbose) std::cout << "WARNING: LookupPrivilegeValue failed while trying to enable SeLockMemoryPrivilege: " << GetLastError() << std::endl;
  184. return false;
  185. }
  186. SetLastError(ERROR_SUCCESS);
  187. if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), nullptr, 0)) {
  188. if (verbose) std::cout << "WARNING: AdjustTokenPrivileges failed while trying to enable SeLockMemoryPrivilege" << std::endl;
  189. return false;
  190. }
  191. if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
  192. if (verbose) std::cout << "WARNING: AdjustTokenPrivileges failed to enable SeLockMemoryPrivilege: Add SeLockMemoryPrivilege for current user and run process in elevated mode (Run as administrator)." << std::endl;
  193. return false;
  194. }
  195. return true;
  196. }
  197. bool os_init(bool hugepages, bool verbose)
  198. {
  199. Lock<MutexSys> lock(os_init_mutex);
  200. if (!hugepages) {
  201. huge_pages_enabled = false;
  202. return true;
  203. }
  204. if (GetLargePageMinimum() != PAGE_SIZE_2M) {
  205. huge_pages_enabled = false;
  206. return false;
  207. }
  208. huge_pages_enabled = true;
  209. return true;
  210. }
  211. void* os_malloc(size_t bytes, bool& hugepages)
  212. {
  213. if (bytes == 0) {
  214. hugepages = false;
  215. return nullptr;
  216. }
  217. /* try direct huge page allocation first */
  218. if (isHugePageCandidate(bytes))
  219. {
  220. int flags = MEM_COMMIT | MEM_RESERVE | MEM_LARGE_PAGES;
  221. char* ptr = (char*) VirtualAlloc(nullptr,bytes,flags,PAGE_READWRITE);
  222. if (ptr != nullptr) {
  223. hugepages = true;
  224. return ptr;
  225. }
  226. }
  227. /* fall back to 4k pages */
  228. int flags = MEM_COMMIT | MEM_RESERVE;
  229. char* ptr = (char*) VirtualAlloc(nullptr,bytes,flags,PAGE_READWRITE);
  230. // -- GODOT start --
  231. // if (ptr == nullptr) throw std::bad_alloc();
  232. if (ptr == nullptr) {
  233. abort();
  234. }
  235. // -- GODOT end --
  236. hugepages = false;
  237. return ptr;
  238. }
  239. size_t os_shrink(void* ptr, size_t bytesNew, size_t bytesOld, bool hugepages)
  240. {
  241. if (hugepages) // decommitting huge pages seems not to work under Windows
  242. return bytesOld;
  243. const size_t pageSize = hugepages ? PAGE_SIZE_2M : PAGE_SIZE_4K;
  244. bytesNew = (bytesNew+pageSize-1) & ~(pageSize-1);
  245. bytesOld = (bytesOld+pageSize-1) & ~(pageSize-1);
  246. if (bytesNew >= bytesOld)
  247. return bytesOld;
  248. // -- GODOT start --
  249. // if (!VirtualFree((char*)ptr+bytesNew,bytesOld-bytesNew,MEM_DECOMMIT))
  250. // throw std::bad_alloc();
  251. if (!VirtualFree((char*)ptr+bytesNew,bytesOld-bytesNew,MEM_DECOMMIT)) {
  252. abort();
  253. }
  254. // -- GODOT end --
  255. return bytesNew;
  256. }
  257. void os_free(void* ptr, size_t bytes, bool hugepages)
  258. {
  259. if (bytes == 0)
  260. return;
  261. // -- GODOT start --
  262. // if (!VirtualFree(ptr,0,MEM_RELEASE))
  263. // throw std::bad_alloc();
  264. if (!VirtualFree(ptr,0,MEM_RELEASE)) {
  265. abort();
  266. }
  267. // -- GODOT end --
  268. }
  269. void os_advise(void *ptr, size_t bytes)
  270. {
  271. }
  272. }
  273. #endif
  274. ////////////////////////////////////////////////////////////////////////////////
  275. /// Unix Platform
  276. ////////////////////////////////////////////////////////////////////////////////
  277. #if defined(__UNIX__)
  278. #include <sys/mman.h>
  279. #include <errno.h>
  280. #include <stdlib.h>
  281. #include <string.h>
  282. #include <sstream>
  283. #if defined(__MACOSX__)
  284. #include <mach/vm_statistics.h>
  285. #endif
  286. namespace embree
  287. {
  288. bool os_init(bool hugepages, bool verbose)
  289. {
  290. Lock<MutexSys> lock(os_init_mutex);
  291. if (!hugepages) {
  292. huge_pages_enabled = false;
  293. return true;
  294. }
  295. #if defined(__LINUX__)
  296. int hugepagesize = 0;
  297. std::ifstream file;
  298. file.open("/proc/meminfo",std::ios::in);
  299. if (!file.is_open()) {
  300. if (verbose) std::cout << "WARNING: Could not open /proc/meminfo. Huge page support cannot get enabled!" << std::endl;
  301. huge_pages_enabled = false;
  302. return false;
  303. }
  304. std::string line;
  305. while (getline(file,line))
  306. {
  307. std::stringstream sline(line);
  308. while (!sline.eof() && sline.peek() == ' ') sline.ignore();
  309. std::string tag; getline(sline,tag,' ');
  310. while (!sline.eof() && sline.peek() == ' ') sline.ignore();
  311. std::string val; getline(sline,val,' ');
  312. while (!sline.eof() && sline.peek() == ' ') sline.ignore();
  313. std::string unit; getline(sline,unit,' ');
  314. if (tag == "Hugepagesize:" && unit == "kB") {
  315. hugepagesize = std::stoi(val)*1024;
  316. break;
  317. }
  318. }
  319. if (hugepagesize != PAGE_SIZE_2M)
  320. {
  321. if (verbose) std::cout << "WARNING: Only 2MB huge pages supported. Huge page support cannot get enabled!" << std::endl;
  322. huge_pages_enabled = false;
  323. return false;
  324. }
  325. #endif
  326. huge_pages_enabled = true;
  327. return true;
  328. }
  329. void* os_malloc(size_t bytes, bool& hugepages)
  330. {
  331. if (bytes == 0) {
  332. hugepages = false;
  333. return nullptr;
  334. }
  335. /* try direct huge page allocation first */
  336. if (isHugePageCandidate(bytes))
  337. {
  338. #if defined(__MACOSX__)
  339. void* ptr = mmap(0, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0);
  340. if (ptr != MAP_FAILED) {
  341. hugepages = true;
  342. return ptr;
  343. }
  344. #elif defined(MAP_HUGETLB)
  345. void* ptr = mmap(0, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON | MAP_HUGETLB, -1, 0);
  346. if (ptr != MAP_FAILED) {
  347. hugepages = true;
  348. return ptr;
  349. }
  350. #endif
  351. }
  352. /* fallback to 4k pages */
  353. void* ptr = (char*) mmap(0, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
  354. // -- GODOT start --
  355. // if (ptr == MAP_FAILED) throw std::bad_alloc();
  356. if (ptr == MAP_FAILED) {
  357. abort();
  358. }
  359. // -- GODOT end --
  360. hugepages = false;
  361. /* advise huge page hint for THP */
  362. os_advise(ptr,bytes);
  363. return ptr;
  364. }
  365. size_t os_shrink(void* ptr, size_t bytesNew, size_t bytesOld, bool hugepages)
  366. {
  367. const size_t pageSize = hugepages ? PAGE_SIZE_2M : PAGE_SIZE_4K;
  368. bytesNew = (bytesNew+pageSize-1) & ~(pageSize-1);
  369. bytesOld = (bytesOld+pageSize-1) & ~(pageSize-1);
  370. if (bytesNew >= bytesOld)
  371. return bytesOld;
  372. // -- GODOT start --
  373. // if (munmap((char*)ptr+bytesNew,bytesOld-bytesNew) == -1)
  374. // throw std::bad_alloc();
  375. if (munmap((char*)ptr+bytesNew,bytesOld-bytesNew) == -1) {
  376. abort();
  377. }
  378. // -- GODOT end --
  379. return bytesNew;
  380. }
  381. void os_free(void* ptr, size_t bytes, bool hugepages)
  382. {
  383. if (bytes == 0)
  384. return;
  385. /* for hugepages we need to also align the size */
  386. const size_t pageSize = hugepages ? PAGE_SIZE_2M : PAGE_SIZE_4K;
  387. bytes = (bytes+pageSize-1) & ~(pageSize-1);
  388. // -- GODOT start --
  389. // if (munmap(ptr,bytes) == -1)
  390. // throw std::bad_alloc();
  391. if (munmap(ptr,bytes) == -1) {
  392. abort();
  393. }
  394. // -- GODOT end --
  395. }
  396. /* hint for transparent huge pages (THP) */
  397. void os_advise(void* pptr, size_t bytes)
  398. {
  399. #if defined(MADV_HUGEPAGE)
  400. madvise(pptr,bytes,MADV_HUGEPAGE);
  401. #endif
  402. }
  403. }
  404. #endif