1 /* 2 * Copyright (c) 2021 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 #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 <variant> 28 #include <vector> 29 #include "log.h" 30 #include "ts_common.h" 31 namespace SysTuning { 32 namespace TraceStreamer { 33 using namespace std; 34 constexpr int32_t WAKE_EVENT_DEFAULT_VALUE = -1; 35 constexpr int32_t STR_LEGH = 2; 36 constexpr int32_t STR_LEN = 8; 37 constexpr int32_t MAX_LEN = 256; 38 struct ffrtContent { 39 std::string name; 40 std::vector<int> line; 41 }; 42 struct WakeEvent { 43 std::string state = "none"; 44 int prevWakLine = WAKE_EVENT_DEFAULT_VALUE; 45 std::string prevWakeLog; 46 }; 47 class FfrtConverter { 48 public: 49 FfrtConverter() = default; 50 ~FfrtConverter() = default; 51 bool RecoverTraceAndGenerateNewFile(const std::string& ffrtFileName, std::ofstream& outFile); 52 53 private: 54 using TypeFfrtPid = std::unordered_map<int, std::unordered_map<int, ffrtContent>>; 55 int ExtractProcessId(const std::string& log); 56 std::string ExtractTimeStr(const std::string& log); 57 std::string ExtractCpuId(const std::string& log); 58 TypeFfrtPid ClassifyLogsForFfrtWorker(vector<std::string>& results); 59 void FindFfrtProcessAndClassifyLogs(std::string& log, 60 size_t line, 61 std::unordered_map<int, std::vector<int>>& traceMap, 62 TypeFfrtPid& ffrtPidsMap); 63 void ClassifySchedSwitchLogs(std::string& log, 64 size_t line, 65 std::unordered_map<int, std::vector<int>>& traceMap, 66 FfrtConverter::TypeFfrtPid& ffrtPidsMap); 67 int FindTid(string& log); 68 void ConvertFfrtThreadToFfrtTaskByLine(int pid, 69 int tid, 70 int& prio, 71 std::vector<std::string>& results, 72 ffrtContent& content, 73 std::unordered_map<int, std::unordered_map<int, std::string>>& taskLabels); 74 void ConvertFfrtThreadToFfrtTask(vector<std::string>& results, TypeFfrtPid& ffrtPidsMap); 75 std::string MakeBeginFakeLog(const std::string& mark, 76 const int pid, 77 const std::string& label, 78 const long long gid, 79 const int tid, 80 const std::string& threadName, 81 const int prio); 82 std::string MakeEndFakeLog(const std::string& mark, 83 const int pid, 84 const std::string& label, 85 const long long gid, 86 const int tid, 87 const std::string& threadName, 88 const int prio); 89 std::string ReplaceSchedSwitchLog(std::string& fakeLog, 90 const std::string& mark, 91 const int pid, 92 const std::string& label, 93 const long long gid, 94 const int tid); 95 std::string ReplaceSchedWakeLog(std::string& fakeLog, const std::string& label, const int pid, const long long gid); 96 std::string ReplaceSchedBlockLog(std::string& fakeLog, const int pid, const long long gid); 97 std::string ReplaceTracingMarkLog(std::string& fakeLog, 98 const std::string& label, 99 const int pid, 100 const long long gid); 101 std::string ConvertWorkerLogToTask(const std::string& mark, 102 const int pid, 103 const std::string& label, 104 const long long gid, 105 const int tid); 106 std::string GetTaskId(int pid, long long gid); 107 bool IsDigit(const std::string& str); 108 void CheckTraceMarker(vector<std::string>& lines); 109 void UpdatePrio(int& prio, const std::string& mark, const int tid); 110 std::string GetLabel(const std::string& mark); 111 void DeleteRedundance(const std::string& mark, 112 std::string& log, 113 bool switchInFakeLog, 114 bool switchOutFakeLog, 115 const int pid, 116 const std::string& label, 117 long long gid, 118 const int tid, 119 const std::string& threadName, 120 const int prio); 121 std::string getNewMissLog(std::string& missLog, 122 const std::string& mark, 123 const int pid, 124 const int tid, 125 std::string threadName); 126 127 private: 128 const std::regex indexPattern_ = std::regex(R"(\(.+\)\s+\[\d)"); 129 const std::regex matchPattern_ = std::regex(R"( \(.+\)\s+\[\d)"); 130 const int scaleFactor_ = 10; 131 std::string tracingMarkerKey_ = "tracing_mark_write: "; 132 }; 133 } // namespace TraceStreamer 134 } // namespace SysTuning 135 #endif // FFRT_CONVERTER_H 136