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 "process_helper.h"
17 
18 #include <cstdint>
19 #include <fstream>
20 #include <sstream>
21 #include <sys/resource.h>
22 #include <sys/time.h>
23 #include <sys/sysinfo.h>
24 #include <uv.h>
25 
26 namespace Commonlibrary::Platform {
27 constexpr int NUM_OF_DATA = 15; // 15:Data size
28 const std::string PROCESS_START_TIME_DATA = "PROCESS_START_TIME";
29 
ProcessExit(int signal)30 void ProcessExit(int signal)
31 {
32     quick_exit(signal);
33 }
34 
GetSysConfig(int number)35 int GetSysConfig(int number)
36 {
37     auto configinfo = static_cast<int32_t>(sysconf(number));
38     return configinfo;
39 }
40 
GetSysTimer()41 double GetSysTimer()
42 {
43     struct sysinfo information = {0};
44     time_t systimer = 0;
45     if (sysinfo(&information)) {
46         HILOG_ERROR("Failed to get sysinfo");
47         return SYS_INFO_FAILED;
48     }
49     systimer = information.uptime;
50     return static_cast<double>(systimer);
51 }
52 
GetThreadId()53 int GetThreadId()
54 {
55     auto proTid = static_cast<int64_t>(gettid());
56     return proTid;
57 }
58 
GetThreadPRY(int tid)59 int GetThreadPRY(int tid)
60 {
61     int32_t pri = getpriority(PRIO_PROCESS, tid);
62     return pri;
63 }
64 
GetProcessStartRealtime()65 double GetProcessStartRealtime()
66 {
67     double startRealtime = 0;
68     char buf[NUM_OF_DATA] = { 0 };
69     size_t length = sizeof(buf);
70     auto envNum = uv_os_getenv(PROCESS_START_TIME_DATA.c_str(), buf, &length);
71     if (envNum == UV_ENOENT || strlen(buf) == 0) {
72         HILOG_ERROR("process:: Failed to get process start env");
73         startRealtime = 0;
74     } else {
75         startRealtime = std::strtod(buf, nullptr);
76     }
77     return startRealtime;
78 }
79 } // namespace Commonlibrary::Platform