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 HTRACE_CPU_DATA_PARSER_H 16 #define HTRACE_CPU_DATA_PARSER_H 17 #include <cstdint> 18 #include <map> 19 #include <string> 20 #include "cpu_plugin_result.pbreader.h" 21 #include "event_parser_base.h" 22 #include "hilog_plugin_result.pbreader.h" 23 #include "htrace_plugin_time_parser.h" 24 #include "trace_data/trace_data_cache.h" 25 #include "trace_streamer_config.h" 26 #include "trace_streamer_filters.h" 27 28 namespace SysTuning { 29 namespace TraceStreamer { 30 class HtraceCpuDataParser : public EventParserBase, public HtracePluginTimeParser { 31 public: 32 HtraceCpuDataParser(TraceDataCache* dataCache, const TraceStreamerFilters* ctx); 33 ~HtraceCpuDataParser(); 34 void Parse(ProtoReader::BytesView tracePacket, uint64_t ts); 35 void Finish(); 36 enum TSCpuDataType { TS_CPU_DATA_TYPE_USAGE, TS_CPU_DATA_TYPE_THREAD_INFO, TS_CPU_DATA_TYPE_LOAD }; 37 class TsCpuData { 38 public: TsCpuData()39 TsCpuData() 40 { 41 ts_ = 0; 42 cpuDataType_ = TS_CPU_DATA_TYPE_USAGE; 43 } SetCpuUsage(uint64_t ts)44 void SetCpuUsage(uint64_t ts) 45 { 46 ts_ = ts; 47 cpuDataType_ = TS_CPU_DATA_TYPE_USAGE; 48 } SetThreadInfo(uint64_t ts)49 void SetThreadInfo(uint64_t ts) 50 { 51 ts_ = ts; 52 cpuDataType_ = TS_CPU_DATA_TYPE_THREAD_INFO; 53 } SetExtInfo(double totalLoad,double userLoad,double sysLoad,double processNum)54 void SetExtInfo(double totalLoad, double userLoad, double sysLoad, double processNum) 55 { 56 totalLoad_ = totalLoad; 57 userLoad_ = userLoad; 58 sysLoad_ = sysLoad; 59 processNum_ = processNum; 60 cpuDataType_ = TS_CPU_DATA_TYPE_LOAD; 61 } 62 uint64_t ts_; 63 TSCpuDataType cpuDataType_; 64 double userLoad_ = 0; 65 double sysLoad_ = 0; 66 double processNum_ = 0; 67 double totalLoad_ = 0; 68 }; 69 std::vector<std::unique_ptr<TsCpuData>> cpuData_; 70 71 private: 72 std::string threadStateDesc_[ProtoReader::THREAD_WAITING + 1] = {"undefined", "Running", "Sleep", "Sloped", 73 "Watting"}; 74 }; 75 } // namespace TraceStreamer 76 } // namespace SysTuning 77 78 #endif // HTRACE_CPU_DATA_PARSER_H 79