• 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 
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 
25 namespace Commonlibrary::Platform {
26 static long g_hz = sysconf(_SC_CLK_TCK);
27 
ProcessExit(int signal)28 void ProcessExit(int signal)
29 {
30     quick_exit(signal);
31 }
32 
GetSysConfig(int number)33 int GetSysConfig(int number)
34 {
35     auto configinfo = static_cast<int32_t>(sysconf(number));
36     return configinfo;
37 }
38 
GetSysTimer()39 double GetSysTimer()
40 {
41     struct sysinfo information = {0};
42     time_t systimer = 0;
43     if (sysinfo(&information)) {
44         HILOG_ERROR("Failed to get sysinfo");
45         return SYS_INFO_FAILED;
46     }
47     systimer = information.uptime;
48     return static_cast<double>(systimer);
49 }
50 
GetThreadId()51 int GetThreadId()
52 {
53     auto proTid = static_cast<int64_t>(gettid());
54     return proTid;
55 }
56 
GetThreadPRY(int tid)57 int GetThreadPRY(int tid)
58 {
59     int32_t pri = getpriority(PRIO_PROCESS, tid);
60     return pri;
61 }
62 
GetProcessStartRealtime()63 double GetProcessStartRealtime()
64 {
65     double startRealtime = 0;
66     std::string path = "/proc/" + std::to_string(getpid()) + "/stat";
67     std::ifstream statFile(path);
68     if (!statFile.is_open()) {
69         HILOG_ERROR("process:: Failed to open config file");
70         return startRealtime;
71     }
72     std::string strLine;
73     std::getline(statFile, strLine);
74     if (strLine.empty()) {
75         HILOG_ERROR("process:: No valid content was read");
76         statFile.close();
77         return startRealtime;
78     }
79     std::istringstream iss(strLine);
80     int count = 1;
81     std::string word;
82     while (iss >> word) {
83         if (count == 22) { // 22 : starttime
84             if (g_hz == -1) {
85                 statFile.close();
86                 return startRealtime;
87             }
88             startRealtime = word.empty() ? 0 : std::strtod(word.c_str(), nullptr);
89             startRealtime = (startRealtime * 1000) / static_cast<double>(g_hz); // 1000 : // Calculate milliseconds
90             break;
91         }
92         count++;
93     }
94     statFile.close();
95     return startRealtime;
96 }
97 } // namespace Commonlibrary::Platform