12498b56bSopenharmony_ci/* 22498b56bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 32498b56bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 42498b56bSopenharmony_ci * you may not use this file except in compliance with the License. 52498b56bSopenharmony_ci * You may obtain a copy of the License at 62498b56bSopenharmony_ci * 72498b56bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 82498b56bSopenharmony_ci * 92498b56bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 102498b56bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 112498b56bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 122498b56bSopenharmony_ci * See the License for the specific language governing permissions and 132498b56bSopenharmony_ci * limitations under the License. 142498b56bSopenharmony_ci */ 152498b56bSopenharmony_ci 162498b56bSopenharmony_ci#ifndef LOG_QUERIER_H 172498b56bSopenharmony_ci#define LOG_QUERIER_H 182498b56bSopenharmony_ci 192498b56bSopenharmony_ci#include <array> 202498b56bSopenharmony_ci#include <vector> 212498b56bSopenharmony_ci#include <atomic> 222498b56bSopenharmony_ci#include <condition_variable> 232498b56bSopenharmony_ci#include <future> 242498b56bSopenharmony_ci#include <memory> 252498b56bSopenharmony_ci#include <mutex> 262498b56bSopenharmony_ci#include <socket.h> 272498b56bSopenharmony_ci 282498b56bSopenharmony_ci#include <hilog_common.h> 292498b56bSopenharmony_ci#include <hilog_cmd.h> 302498b56bSopenharmony_ci 312498b56bSopenharmony_ci#include "log_persister.h" 322498b56bSopenharmony_ci#include "log_stats.h" 332498b56bSopenharmony_ci#include "log_buffer.h" 342498b56bSopenharmony_ci#include "log_collector.h" 352498b56bSopenharmony_ci 362498b56bSopenharmony_cinamespace OHOS { 372498b56bSopenharmony_cinamespace HiviewDFX { 382498b56bSopenharmony_ciusing CmdList = std::vector<IoctlCmd>; 392498b56bSopenharmony_ci 402498b56bSopenharmony_ciclass ServiceController { 412498b56bSopenharmony_cipublic: 422498b56bSopenharmony_ci static constexpr int MAX_DATA_LEN = 2048; 432498b56bSopenharmony_ci using PacketBuf = std::array<char, MAX_DATA_LEN>; 442498b56bSopenharmony_ci 452498b56bSopenharmony_ci ServiceController(std::unique_ptr<Socket> communicationSocket, LogCollector& collector, HilogBuffer& hilogBuffer, 462498b56bSopenharmony_ci HilogBuffer& kmsgBuffer); 472498b56bSopenharmony_ci ~ServiceController(); 482498b56bSopenharmony_ci 492498b56bSopenharmony_ci void CommunicationLoop(std::atomic<bool>& stopLoop, const CmdList& list); 502498b56bSopenharmony_ci 512498b56bSopenharmony_ciprivate: 522498b56bSopenharmony_ci int GetMsgHeader(MsgHeader& hdr); 532498b56bSopenharmony_ci int GetRqst(const MsgHeader& hdr, char* rqst, int expectedLen); 542498b56bSopenharmony_ci void WriteErrorRsp(int code); 552498b56bSopenharmony_ci void WriteRspHeader(IoctlCmd cmd, size_t len); 562498b56bSopenharmony_ci template<typename T> 572498b56bSopenharmony_ci void RequestHandler(const MsgHeader& hdr, std::function<void(const T& rqst)> handle); 582498b56bSopenharmony_ci 592498b56bSopenharmony_ci // util 602498b56bSopenharmony_ci int CheckOutputRqst(const OutputRqst& rqst); 612498b56bSopenharmony_ci void LogFilterFromOutputRqst(const OutputRqst& rqst, LogFilter& filter); 622498b56bSopenharmony_ci int CheckPersistStartRqst(const PersistStartRqst &rqst); 632498b56bSopenharmony_ci void PersistStartRqst2Msg(const PersistStartRqst &rqst, LogPersistStartMsg &msg); 642498b56bSopenharmony_ci // log query 652498b56bSopenharmony_ci int WriteQueryResponse(OptCRef<HilogData> pData); 662498b56bSopenharmony_ci // statistics 672498b56bSopenharmony_ci void SendOverallStats(const LogStats& stats); 682498b56bSopenharmony_ci void SendLogTypeDomainStats(const LogStats& stats); 692498b56bSopenharmony_ci void SendDomainStats(const LogStats& stats); 702498b56bSopenharmony_ci void SendDomainTagStats(const LogStats& stats); 712498b56bSopenharmony_ci void SendProcStats(const LogStats& stats); 722498b56bSopenharmony_ci void SendProcLogTypeStats(const LogStats& stats); 732498b56bSopenharmony_ci void SendProcTagStats(const LogStats& stats); 742498b56bSopenharmony_ci void SendTagStats(const TagTable &tagTable); 752498b56bSopenharmony_ci // cmd handlers 762498b56bSopenharmony_ci void HandleOutputRqst(const OutputRqst &rqst); 772498b56bSopenharmony_ci void HandlePersistStartRqst(const PersistStartRqst &rqst); 782498b56bSopenharmony_ci void HandlePersistStopRqst(const PersistStopRqst &rqst); 792498b56bSopenharmony_ci void HandlePersistQueryRqst(const PersistQueryRqst& rqst); 802498b56bSopenharmony_ci void HandlePersistRefreshRqst(const PersistRefreshRqst& rqst); 812498b56bSopenharmony_ci void HandlePersistClearRqst(); 822498b56bSopenharmony_ci void HandleBufferSizeGetRqst(const BufferSizeGetRqst& rqst); 832498b56bSopenharmony_ci void HandleBufferSizeSetRqst(const BufferSizeSetRqst& rqst); 842498b56bSopenharmony_ci void HandleStatsQueryRqst(const StatsQueryRqst& rqst); 852498b56bSopenharmony_ci void HandleStatsClearRqst(const StatsClearRqst& rqst); 862498b56bSopenharmony_ci void HandleDomainFlowCtrlRqst(const DomainFlowCtrlRqst& rqst); 872498b56bSopenharmony_ci void HandleLogRemoveRqst(const LogRemoveRqst& rqst); 882498b56bSopenharmony_ci void HandleLogKmsgEnableRqst(const KmsgEnableRqst& rqst); 892498b56bSopenharmony_ci 902498b56bSopenharmony_ci void NotifyForNewData(); 912498b56bSopenharmony_ci bool IsValidCmd(const CmdList& list, IoctlCmd cmd); 922498b56bSopenharmony_ci 932498b56bSopenharmony_ci std::unique_ptr<Socket> m_communicationSocket; 942498b56bSopenharmony_ci LogCollector& m_logCollector; 952498b56bSopenharmony_ci HilogBuffer& m_hilogBuffer; 962498b56bSopenharmony_ci HilogBuffer& m_kmsgBuffer; 972498b56bSopenharmony_ci HilogBuffer::ReaderId m_hilogBufferReader; 982498b56bSopenharmony_ci HilogBuffer::ReaderId m_kmsgBufferReader; 992498b56bSopenharmony_ci std::condition_variable m_notifyNewDataCv; 1002498b56bSopenharmony_ci std::mutex m_notifyNewDataMtx; 1012498b56bSopenharmony_ci}; 1022498b56bSopenharmony_ci 1032498b56bSopenharmony_citemplate<typename T> 1042498b56bSopenharmony_civoid ServiceController::RequestHandler(const MsgHeader& hdr, std::function<void(const T& rqst)> handle) 1052498b56bSopenharmony_ci{ 1062498b56bSopenharmony_ci std::vector<char> buffer(hdr.len, 0); 1072498b56bSopenharmony_ci char *data = buffer.data(); 1082498b56bSopenharmony_ci int ret = GetRqst(hdr, data, sizeof(T)); 1092498b56bSopenharmony_ci if (ret != RET_SUCCESS) { 1102498b56bSopenharmony_ci std::cout << "Error GetRqst" << std::endl; 1112498b56bSopenharmony_ci return; 1122498b56bSopenharmony_ci } 1132498b56bSopenharmony_ci T *rqst = reinterpret_cast<T *>(data); 1142498b56bSopenharmony_ci handle(*rqst); 1152498b56bSopenharmony_ci} 1162498b56bSopenharmony_ci 1172498b56bSopenharmony_ciint RestorePersistJobs(HilogBuffer& hilogBuffer, HilogBuffer& kmsgBuffer); 1182498b56bSopenharmony_ci} // namespace HiviewDFX 1192498b56bSopenharmony_ci} // namespace OHOS 1202498b56bSopenharmony_ci#endif 121