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 82 void SetOSPlatformKey(const std::unordered_map<int, std::pair<std::string, std::vector<int>>> &ffrt_tid_map); 83 void FindFfrtProcClassifyLogs(LogInfo logInfo, WakeLogs &traceMap, PidMap &pidMap, 84 FfrtTidMap &ffrtTidMap, FfrtWakeLogs &ffrtWakeLogs); 85 void ClassifyLogsForFfrtWorker(FfrtPids &ffrt_pids, FfrtWakeLogs &ffrt_wake_logs); 86 87 void ConvertFrrtThreadToFfrtTaskOhos(FfrtPids &ffrtPids, FfrtWakeLogs& ffrtWakeLogs); 88 void ConvertFrrtThreadToFfrtTaskNohos(FfrtPids &ffrtPids, FfrtWakeLogs &ffrtWakeLogs); 89 90 // trace content 91 std::vector<std::string> context_ = {}; 92 std::string tracingMarkerKey_; 93 std::string osPlatformKet_ = "ohos"; 94 bool isOldVersionTrace_ = false; 95 96 void FindQueueTaskInfo(FfrtPids &ffrtPids, QueueTaskInfo &queueTaskInfo); 97 98 void HandleFfrtQueueTasks(FfrtQueueTasks &ffrtQueueTasks, FfrtWakeLogs& ffrtWakeLogs); 99 100 void HandleMarks(ConStr &log, int lineno, int pid); 101 102 bool HandleFfrtTaskCo(ConStr &log, int lineno, bool &switchInFakeLog, bool &switchOutFakeLog); 103 104 bool HandleFfrtTaskExecute(FakeLogArgs &fakLogArg, WakeLogs &wakeLogs, 105 TaskLabels &taskLabels, std::string &label); 106 107 void GenTaskLabelsOhos(FfrtPids &ffrtPids, FfrtWakeLogs& ffrtWakeLogs, TaskLabels &taskLabels); 108 109 bool HandlePreLineno(FakeLogArgs &fakArg, WakeLogs &wakeLogs, 110 TaskLabels &taskLabels, ConStr traceBeginMark, ConStr traceEndMark); 111 112 void SetTracingMarkerKey(LogInfo logInfo); 113 114 void ExceQueTaskInfoPreLog(std::vector<int> &linenos, int pid, QueueTaskInfo &queueTaskInfo); 115 116 void ExceTaskGroups(std::vector<tidInfo> &group, WakeLogs &wakeLogs, int firstGid); 117 118 void HandleTaskGroups(std::vector<std::vector<tidInfo>> &taskGroups, WakeLogs &wakeLogs); 119 120 void ExceTaskLabelOhos(TaskLabels &taskLabels, FfrtWakeLogs &ffrtWakeLogs, std::pair<int, FfrtTidMap> pidItem, 121 std::string traceBeginMark, std::string traceEndMark); 122 123 bool HandleHFfrtTaskExecute(FakeLogArgs &fakeArgs, WakeLogs &wakeLogs, TaskLabels &taskLabels, 124 std::string label, std::unordered_map<int, int> &schedWakeFlag); 125 126 bool HandlePreLinenoNohos(FakeLogArgs &fakArg, WakeLogs &wakeLogs, 127 TaskLabels &taskLabels, std::unordered_map<int, int> &schedWakeFlag); 128 129 void ExceTaskLabelNohos(TaskLabels &taskLabels, FfrtWakeLogs &ffrtWakeLogs, 130 std::pair<int, FfrtTidMap> pidItem, std::unordered_map<int, int> &schedWakeFlag); 131 }; 132 } // namespace TraceStreamer 133 } // namespace SysTuning 134 #endif // FFRT_CONVERTER_H