1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
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 "ffrt_profiler_common.h"
17
18 #include "logging.h"
19
20 namespace OHOS::Developtools::Profiler {
SplitString(const std::string & str,const std::string & sep,std::vector<std::string> & ret)21 void SplitString(const std::string& str, const std::string &sep, std::vector<std::string>& ret)
22 {
23 if (str.empty()) {
24 PROFILER_LOG_ERROR(LOG_CORE, "The string splited is empty!");
25 return;
26 }
27 std::string::size_type beginPos = str.find_first_not_of(sep);
28 std::string::size_type findPos = 0;
29 while (beginPos != std::string::npos) {
30 findPos = str.find(sep, beginPos);
31 std::string tmp;
32 if (findPos != std::string::npos) {
33 tmp = str.substr(beginPos, findPos - beginPos);
34 beginPos = findPos + sep.length();
35 } else {
36 tmp = str.substr(beginPos);
37 beginPos = findPos;
38 }
39 if (!tmp.empty()) {
40 ret.push_back(tmp);
41 tmp.clear();
42 }
43 }
44 }
45
GetProcessName(int32_t pid)46 std::string GetProcessName(int32_t pid)
47 {
48 std::string path = "/proc/" + std::to_string(pid) + "/cmdline";
49 std::ifstream cmdlineFile(path);
50 if (!cmdlineFile) {
51 return "";
52 }
53
54 std::string processName;
55 std::getline(cmdlineFile, processName, '\0');
56
57 static constexpr size_t headSize = 2;
58 if (processName.substr(0, headSize) == "./") {
59 processName = processName.substr(headSize);
60 }
61 size_t found = processName.rfind("/");
62 std::string procName;
63 if (found != std::string::npos) {
64 procName = processName.substr(found + 1);
65 } else {
66 procName = processName;
67 }
68 return procName;
69 }
70 }