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 "xcollie_utils.h"
17
18 #include <algorithm>
19 namespace OHOS {
20 namespace HiviewDFX {
GetCurrentTickMillseconds()21 uint64_t GetCurrentTickMillseconds()
22 {
23 return std::chrono::duration_cast<std::chrono::milliseconds>(
24 std::chrono::steady_clock::now().time_since_epoch()).count();
25 }
26
IsFileNameFormat(char c)27 bool IsFileNameFormat(char c)
28 {
29 if (c >= '0' && c <= '9') {
30 return false;
31 }
32
33 if (c >= 'a' && c <= 'z') {
34 return false;
35 }
36
37 if (c >= 'A' && c <= 'Z') {
38 return false;
39 }
40
41 if (c == '.' || c == '-' || c == '_') {
42 return false;
43 }
44
45 return true;
46 }
47
GetSelfProcName()48 std::string GetSelfProcName()
49 {
50 constexpr uint16_t READ_SIZE = 128;
51 std::ifstream fin;
52 fin.open("/proc/self/comm", std::ifstream::in);
53 if (!fin.is_open()) {
54 XCOLLIE_LOGE("fin.is_open() false");
55 return "";
56 }
57 char readStr[READ_SIZE] = {'\0'};
58 fin.getline(readStr, READ_SIZE - 1);
59 fin.close();
60
61 std::string ret = std::string(readStr);
62 ret.erase(std::remove_if(ret.begin(), ret.end(), IsFileNameFormat), ret.end());
63 return ret;
64 }
65 } // end of HiviewDFX
66 } // end of OHOS