1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/platform/os.h"
17
18 #include <malloc.h>
19 #include <sys/prctl.h>
20 #include <sys/ptrace.h>
21 #include <sys/sysinfo.h>
22 #include <unistd.h>
23
24 #ifndef PR_SET_VMA
25 #define PR_SET_VMA 0x53564d41
26 #endif
27
28 #ifndef PR_SET_VMA_ANON_NAME
29 #define PR_SET_VMA_ANON_NAME 0
30 #endif
31
32 namespace panda::ecmascript {
MallocUsableSize(void * p)33 size_t MallocUsableSize(void *p)
34 {
35 return malloc_usable_size(p);
36 }
37
NumberOfCpuCore()38 uint32_t NumberOfCpuCore()
39 {
40 return static_cast<uint32_t>(sysconf(_SC_NPROCESSORS_ONLN));
41 }
42
PhysicalSize()43 size_t PhysicalSize()
44 {
45 auto pages = sysconf(_SC_PHYS_PAGES);
46 auto pageSize = sysconf(_SC_PAGE_SIZE);
47 return pages * pageSize;
48 }
49
PrctlSetVMA(const void * p,const size_t size,const char * tag)50 int PrctlSetVMA(const void *p, const size_t size, const char *tag)
51 {
52 return prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, p, size, tag);
53 }
54
PtracePeektext(int pid,uintptr_t addr)55 long PtracePeektext(int pid, uintptr_t addr)
56 {
57 return ptrace(PTRACE_PEEKTEXT, pid, addr, NULL);
58 }
59 } // namespace panda::ecmascript
60