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