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 <string> 18 #include <vector> 19 #include <regex> 20 #include <fstream> 21 #include <set> 22 #include <unordered_map> 23 #include "ts_common.h" 24 namespace SysTuning { 25 namespace TraceStreamer { 26 27 using ConStr = const std::string; 28 29 struct tidInfo { 30 std::vector<int> begin; 31 int end; 32 int gid; 33 int qid; 34 }; 35 36 using PidMap = std::unordered_map<int, std::set<int>>; 37 using FfrtTidMap = std::unordered_map<int, std::pair<std::string, std::vector<int>>>; 38 using FfrtPids = std::unordered_map<int, FfrtTidMap>; 39 using WakeLogs = std::unordered_map<int, std::vector<int>>; 40 using FfrtWakeLogs = std::unordered_map<int, WakeLogs>; 41 using QueueTaskInfo = std::unordered_map<int, std::unordered_map<int, tidInfo>>; 42 using FfrtQueueTasks = std::unordered_map<int, std::unordered_map<int, std::vector<tidInfo>>>; 43 using TaskLabels = std::unordered_map<int, std::unordered_map<int, std::string>>; 44 45 struct LogInfo { 46 ConStr &log; 47 int lineno; 48 int pid; 49 int tid; LogInfoLogInfo50 LogInfo(ConStr &log, int lineno, int pid, int tid) : log(log), lineno(lineno), pid(pid), tid(tid) {} 51 }; 52 53 struct FakeLogArgs { 54 int pid; 55 int tid; 56 int &taskRunning; 57 int prio; 58 int lineno; 59 bool &switchInFakeLog; 60 bool &switchOutFakeLog; 61 std::string &log; 62 std::string &tname; 63 std::string &taskLabel; 64 std::string &cpuId; 65 std::string ×tamp; 66 }; 67 68 struct ContextUpdate { 69 size_t position; 70 std::vector<std::string> new_logs; 71 }; 72 73 class FfrtConverter { 74 public: 75 FfrtConverter() = default; 76 ~FfrtConverter() = default; 77 78 bool RecoverTraceAndGenerateNewFile(ConStr &ffrtFileName, std::ofstream &outFile); 79 80 private: 81 void SetOSPlatformKey(const std::unordered_map<int, std::pair<std::string, std::vector<int>>> &ffrt_tid_map); 82 void FindFfrtProcClassifyLogs(LogInfo logInfo, 83 WakeLogs &traceMap, 84 PidMap &pidMap, 85 FfrtTidMap &ffrtTidMap, 86 FfrtWakeLogs &ffrtWakeLogs); 87 void ClassifyLogsForFfrtWorker(FfrtPids &ffrt_pids, FfrtWakeLogs &ffrt_wake_logs); 88 89 void ConvertFrrtThreadToFfrtTaskOhos(FfrtPids &ffrtPids, FfrtWakeLogs &ffrtWakeLogs); 90 void ConvertFrrtThreadToFfrtTaskNohos(FfrtPids &ffrtPids, FfrtWakeLogs &ffrtWakeLogs); 91 92 // trace content 93 std::vector<std::string> context_ = {}; 94 std::string tracingMarkerKey_; 95 std::string osPlatformKet_ = "ohos"; 96 bool isOldVersionTrace_ = false; 97 98 void FindQueueTaskInfo(FfrtPids &ffrtPids, QueueTaskInfo &queueTaskInfo); 99 100 void HandleFfrtQueueTasks(FfrtQueueTasks &ffrtQueueTasks, FfrtWakeLogs &ffrtWakeLogs); 101 102 void HandleMarks(ConStr &log, int lineno, int pid); 103 104 bool HandleFfrtTaskCo(ConStr &log, int lineno, bool &switchInFakeLog, bool &switchOutFakeLog); 105 106 bool HandleFfrtTaskExecute(FakeLogArgs &fakLogArg, WakeLogs &wakeLogs, TaskLabels &taskLabels, std::string &label); 107 108 void GenTaskLabelsOhos(FfrtPids &ffrtPids, FfrtWakeLogs &ffrtWakeLogs, TaskLabels &taskLabels); 109 110 bool HandlePreLineno(FakeLogArgs &fakArg, 111 WakeLogs &wakeLogs, 112 TaskLabels &taskLabels, 113 ConStr traceBeginMark, 114 ConStr traceEndMark); 115 116 void SetTracingMarkerKey(LogInfo logInfo); 117 118 void ExceQueTaskInfoPreLog(std::vector<int> &linenos, int pid, QueueTaskInfo &queueTaskInfo); 119 120 void ExceTaskGroups(std::vector<tidInfo> &group, WakeLogs &wakeLogs, int firstGid); 121 122 void HandleTaskGroups(std::vector<std::vector<tidInfo>> &taskGroups, WakeLogs &wakeLogs); 123 124 void ExceTaskLabelOhos(TaskLabels &taskLabels, 125 FfrtWakeLogs &ffrtWakeLogs, 126 std::pair<int, FfrtTidMap> pidItem, 127 std::string traceBeginMark, 128 std::string traceEndMark); 129 130 bool HandleHFfrtTaskExecute(FakeLogArgs &fakeArgs, 131 WakeLogs &wakeLogs, 132 TaskLabels &taskLabels, 133 std::string label, 134 std::unordered_map<int, int> &schedWakeFlag); 135 136 bool HandlePreLinenoNohos(FakeLogArgs &fakArg, 137 WakeLogs &wakeLogs, 138 TaskLabels &taskLabels, 139 std::unordered_map<int, int> &schedWakeFlag); 140 141 void ExceTaskLabelNohos(TaskLabels &taskLabels, 142 FfrtWakeLogs &ffrtWakeLogs, 143 std::pair<int, FfrtTidMap> pidItem, 144 std::unordered_map<int, int> &schedWakeFlag); 145 }; 146 } // namespace TraceStreamer 147 } // namespace SysTuning 148 #endif // FFRT_CONVERTER_H