1 /*
2 * Copyright (c) 2023 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 "napi/native_api.h"
17 #include <netdb.h>
18 #include <sys/sysinfo.h>
19 #define SUCCESS 1
20 #define FAIL (-1)
21 #define NOERROR (-1)
22
GetPhysPages(napi_env env, napi_callback_info info)23 napi_value GetPhysPages(napi_env env, napi_callback_info info)
24 {
25
26 long phys_pages = get_phys_pages();
27 int ret = FAIL;
28 if (phys_pages >= NOERROR) {
29 ret = SUCCESS;
30 }
31 napi_value result = nullptr;
32 napi_create_int32(env, ret, &result);
33 return result;
34 }
35
GetNprocsConf(napi_env env, napi_callback_info info)36 napi_value GetNprocsConf(napi_env env, napi_callback_info info)
37 {
38
39 int nprocs_conf = get_nprocs_conf();
40 int ret = FAIL;
41 if (nprocs_conf > NOERROR) {
42 ret = SUCCESS;
43 }
44 napi_value result = nullptr;
45 napi_create_int32(env, ret, &result);
46 return result;
47 }
48
GetNprocs(napi_env env, napi_callback_info info)49 napi_value GetNprocs(napi_env env, napi_callback_info info)
50 {
51
52 int nprocs = get_nprocs();
53 int ret = FAIL;
54 if (nprocs > NOERROR) {
55 ret = SUCCESS;
56 }
57 napi_value result = nullptr;
58 napi_create_int32(env, ret, &result);
59 return result;
60 }
61
GetAvphysPages(napi_env env, napi_callback_info info)62 napi_value GetAvphysPages(napi_env env, napi_callback_info info)
63 {
64
65 long avail_phys_pages = get_avphys_pages();
66 int ret = FAIL;
67 if (avail_phys_pages > NOERROR) {
68 ret = SUCCESS;
69 }
70 napi_value result = nullptr;
71 napi_create_int32(env, ret, &result);
72 return result;
73 }
74
75 EXTERN_C_START
Init(napi_env env, napi_value exports)76 static napi_value Init(napi_env env, napi_value exports)
77 {
78 napi_property_descriptor desc[] = {
79 {"getPhysPages", nullptr, GetPhysPages, nullptr, nullptr, nullptr, napi_default, nullptr},
80 {"getNprocsConf", nullptr, GetNprocsConf, nullptr, nullptr, nullptr, napi_default, nullptr},
81 {"getNprocs", nullptr, GetNprocs, nullptr, nullptr, nullptr, napi_default, nullptr},
82 {"getAvphysPages", nullptr, GetAvphysPages, nullptr, nullptr, nullptr, napi_default, nullptr},
83 };
84 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
85 return exports;
86 }
87 EXTERN_C_END
88
89 static napi_module demoModule = {
90 .nm_version = 1,
91 .nm_flags = 0,
92 .nm_filename = nullptr,
93 .nm_register_func = Init,
94 .nm_modname = "time",
95 .nm_priv = ((void *)0),
96 .reserved = {0},
97 };
98
RegisterModule(void)99 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }