• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef LOG_QUERIER_H
17 #define LOG_QUERIER_H
18 
19 #include <array>
20 #include <vector>
21 #include <atomic>
22 #include <condition_variable>
23 #include <future>
24 #include <memory>
25 #include <mutex>
26 #include <socket.h>
27 
28 #include <hilog_common.h>
29 #include <hilog_cmd.h>
30 
31 #include "log_persister.h"
32 #include "log_stats.h"
33 #include "log_buffer.h"
34 #include "log_collector.h"
35 
36 namespace OHOS {
37 namespace HiviewDFX {
38 using CmdList = std::vector<IoctlCmd>;
39 
40 class ServiceController  {
41 public:
42     static constexpr int MAX_DATA_LEN = 2048;
43     using PacketBuf = std::array<char, MAX_DATA_LEN>;
44 
45     ServiceController(std::unique_ptr<Socket> communicationSocket, LogCollector& collector, HilogBuffer& buffer);
46     ~ServiceController();
47 
48     void CommunicationLoop(std::atomic<bool>& stopLoop, const CmdList& list);
49 
50 private:
51     int GetMsgHeader(MsgHeader& hdr);
52     int GetRqst(const MsgHeader& hdr, char* rqst, int expectedLen);
53     void WriteErrorRsp(int code);
54     void WriteRspHeader(IoctlCmd cmd, size_t len);
55     template<typename T>
56     void RequestHandler(const MsgHeader& hdr, std::function<void(const T& rqst)> handle);
57 
58     // util
59     int CheckOutputRqst(const OutputRqst& rqst);
60     void LogFilterFromOutputRqst(const OutputRqst& rqst, LogFilter& filter);
61     int CheckPersistStartRqst(const PersistStartRqst &rqst);
62     void PersistStartRqst2Msg(const PersistStartRqst &rqst, LogPersistStartMsg &msg);
63     // log query
64     int WriteQueryResponse(OptCRef<HilogData> pData);
65     // statistics
66     void SendOverallStats(const LogStats& stats);
67     void SendLogTypeDomainStats(const LogStats& stats);
68     void SendDomainStats(const LogStats& stats);
69     void SendDomainTagStats(const LogStats& stats);
70     void SendProcStats(const LogStats& stats);
71     void SendProcLogTypeStats(const LogStats& stats);
72     void SendProcTagStats(const LogStats& stats);
73     void SendTagStats(const TagTable &tagTable);
74     // cmd handlers
75     void HandleOutputRqst(const OutputRqst &rqst);
76     void HandlePersistStartRqst(const PersistStartRqst &rqst);
77     void HandlePersistStopRqst(const PersistStopRqst &rqst);
78     void HandlePersistQueryRqst(const PersistQueryRqst& rqst);
79     void HandleBufferSizeGetRqst(const BufferSizeGetRqst& rqst);
80     void HandleBufferSizeSetRqst(const BufferSizeSetRqst& rqst);
81     void HandleStatsQueryRqst(const StatsQueryRqst& rqst);
82     void HandleStatsClearRqst(const StatsClearRqst& rqst);
83     void HandleDomainFlowCtrlRqst(const DomainFlowCtrlRqst& rqst);
84     void HandleLogRemoveRqst(const LogRemoveRqst& rqst);
85     void HandleLogKmsgEnableRqst(const KmsgEnableRqst& rqst);
86 
87     void NotifyForNewData();
88     bool IsValidCmd(const CmdList& list, IoctlCmd cmd);
89 
90     std::unique_ptr<Socket> m_communicationSocket;
91     LogCollector& m_logCollector;
92     HilogBuffer& m_hilogBuffer;
93     HilogBuffer::ReaderId m_bufReader;
94 
95     std::condition_variable m_notifyNewDataCv;
96     std::mutex m_notifyNewDataMtx;
97 };
98 
99 template<typename T>
RequestHandler(const MsgHeader & hdr,std::function<void (const T & rqst)> handle)100 void ServiceController::RequestHandler(const MsgHeader& hdr, std::function<void(const T& rqst)> handle)
101 {
102     std::vector<char> buffer(hdr.len, 0);
103     char *data = buffer.data();
104     int ret = GetRqst(hdr, data, sizeof(T));
105     if (ret != RET_SUCCESS) {
106         std::cout << "Error GetRqst" << std::endl;
107         return;
108     }
109     T *rqst = reinterpret_cast<T *>(data);
110     handle(*rqst);
111 }
112 
113 int RestorePersistJobs(HilogBuffer& _buffer);
114 } // namespace HiviewDFX
115 } // namespace OHOS
116 #endif
117