• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. 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 #ifndef FFRT_CONVERTER_H
16 #define FFRT_CONVERTER_H
17 #include <cstdio>
18 #include <cstring>
19 #include <fstream>
20 #include <iostream>
21 #include <memory>
22 #include <regex>
23 #include <securec.h>
24 #include <sstream>
25 #include <string>
26 #include <unordered_map>
27 #include <vector>
28 #include "ts_common.h"
29 namespace SysTuning {
30 namespace TraceStreamer {
31 using namespace std;
32 constexpr int32_t WAKE_EVENT_DEFAULT_VALUE = -1;
33 constexpr int32_t STR_LEGH = 2;
34 constexpr int32_t STR_LEN = 8;
35 constexpr int32_t MAX_LEN = 256;
36 struct FfrtContent {
37     std::string name;
38     std::vector<int> indices;
39 };
40 struct WakeEvent {
41     std::string state = "none";
42     int prevWakLine = WAKE_EVENT_DEFAULT_VALUE;
43     std::string prevWakeLog;
44 };
45 struct ThreadInfo {
46     int pid;
47     int tid;
48     std::string name;
49 };
50 class FfrtConverter {
51 public:
52     FfrtConverter() = default;
53     ~FfrtConverter() = default;
54     bool RecoverTraceAndGenerateNewFile(const std::string &ffrtFileName, std::ofstream &outFile);
55 
56 private:
57     void Clear();
58     using TypeFfrtPid = std::unordered_map<int, std::unordered_map<int, FfrtContent>>;
59     int ExtractProcessId(const size_t index);
60     std::string ExtractTimeStr(const std::string &log);
61     std::string ExtractCpuId(const std::string &log);
62     void ClassifyContextForFfrtWorker();
63     void FindFfrtProcessAndClassify(const size_t index, std::unordered_map<int, std::vector<int>> &traceMap);
64     void ClassifySchedSwitchData(const size_t index, std::unordered_map<int, std::vector<int>> &traceMap);
65     int FindIntNumberAfterStr(const size_t index, const string &str);
66     std::string FindSubStrAfterStr(const size_t index, const string &str);
67     std::string GetLabel(const std::string &line);
68     void ConvertFfrtThreadToFfrtTask();
69     void ProcessMarkWithSchedSwitch(const int tid, int &prio, const size_t index);
70     bool ProcessMarkWithFFRT(const int index, int &prio, int32_t &gid, const ThreadInfo &threadInfo);
71     bool DeleteRedundance(bool &switchInFakeLog, bool &switchOutFakeLog, const int index);
72     std::string MakeBeginFakeLog(const std::string &mark,
73                                  const long long gid,
74                                  const int prio,
75                                  const ThreadInfo &threadInfo);
76     std::string MakeEndFakeLog(const std::string &mark,
77                                const long long gid,
78                                const int prio,
79                                const ThreadInfo &threadInfo);
80     std::string ReplaceSchedSwitchLog(std::string &fakeLog,
81                                       const std::string &mark,
82                                       const int pid,
83                                       const long long gid,
84                                       const int tid);
85     std::string ReplaceSchedWakeLog(std::string &fakeLog, const std::string &label, const int pid, const long long gid);
86     std::string ReplaceSchedBlockLog(std::string &fakeLog, const int pid, const long long gid);
87     std::string ReplaceTracingMarkLog(std::string &fakeLog,
88                                       const std::string &label,
89                                       const int pid,
90                                       const long long gid);
91     std::string ConvertWorkerLogToTask(const std::string &mark, const int pid, const long long gid, const int tid);
92     std::string GetTaskId(int pid, long long gid);
93     bool IsDigit(const std::string &str);
94     void InitTracingMarkerKey();
95 
96 private:
97     const std::regex indexPattern_ = std::regex(R"(\(.+\)\s+\[\d)");
98     const std::regex matchPattern_ = std::regex(R"( \(.+\)\s+\[\d)");
99     const int uint32MaxLength_ = 10;
100     std::string tracingMarkerKey_ = "tracing_mark_write: ";
101     std::vector<std::string> context_ = {};
102     TypeFfrtPid ffrtPidMap_ = {};
103     std::unordered_map<int, std::unordered_map<int, std::string>> taskLabels_ = {};
104 };
105 } // namespace TraceStreamer
106 } // namespace SysTuning
107 #endif // FFRT_CONVERTER_H
108