• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 HIVIEW_BASE_EVENT_STORE_INCLUDE_SYS_EVENT_QUERY_WRAPPER_H
17 #define HIVIEW_BASE_EVENT_STORE_INCLUDE_SYS_EVENT_QUERY_WRAPPER_H
18 
19 #include <atomic>
20 #include <ctime>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include <unordered_map>
25 
26 #include "sys_event_query.h"
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 namespace EventStore {
31 using ConcurrentQueries = std::unordered_map<std::string, std::pair<int, int>>;
32 using LastQueries = std::unordered_map<std::string, std::unordered_map<pid_t, time_t>>;
33 
34 class QueryStatusLogUtil {
35 public:
36     static void LogTooManyQueryRules(const std::string sql);
37     static void LogTooManyConcurrentQueries(const int limit, bool innerQuery = true);
38     static void LogQueryOverTime(time_t costTime, const std::string sql, bool innerQuery = true);
39     static void LogQueryCountOverLimit(const int32_t queryCount, const std::string& sql,
40         bool innerQuery = true);
41     static void LogQueryTooFrequently(const std::string& sql, const std::string& processName = std::string(""),
42         bool innerQuery = true);
43 
44 private:
45     static void Logging(const std::string& detail);
46 };
47 
48 class SysEventQueryWrapper : public SysEventQuery {
49 public:
SysEventQueryWrapper(const std::string & dbFile)50     SysEventQueryWrapper(const std::string& dbFile) : SysEventQuery(dbFile) {}
~SysEventQueryWrapper()51     ~SysEventQueryWrapper() {}
52 
53 public:
54     virtual ResultSet Execute(int limit, DbQueryTag tag, QueryProcessInfo callerInfo,
55         DbQueryCallback queryCallback) override;
56 
57 private:
58     bool IsConditionCntValid(const DataQuery& query, const DbQueryTag& tag);
59     bool IsQueryCntLimitValid(const DataQuery& query, const DbQueryTag& tag, const int limit,
60         const DbQueryCallback& callback);
61     bool IsQueryCostTimeValid(const DataQuery& query, const DbQueryTag& tag, const time_t before,
62         const time_t after, const DbQueryCallback& callback);
63     bool IsConcurrentQueryCntValid(const std::string& dbFile, const DbQueryTag& tag,
64         const DbQueryCallback& callback);
65     bool IsQueryFrequenceValid(const DataQuery& query, const DbQueryTag& tag, const std::string& dbFile,
66         const QueryProcessInfo& processInfo, const DbQueryCallback& callback);
67     void IncreaseConcurrentCnt(const std::string& dbFile, const DbQueryTag& tag);
68     void DecreaseConcurrentCnt(const std::string& dbFile, const DbQueryTag& tag);
69 
70 private:
71     static ConcurrentQueries concurrentQueries_;
72     static LastQueries lastQueries_;
73     static std::mutex concurrentQueriesMutex_;
74     static std::mutex lastQueriesMutex_;
75 };
76 } // namespace EventStore
77 } // namespace HiviewDFX
78 } // namespace OHOS
79 
80 #endif // HIVIEW_BASE_EVENT_STORE_INCLUDE_SYS_EVENT_QUERY_WRAPPER_H