1 /* 2 * Copyright (c) 2025 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 OHOS_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_PERFSTAT_H 17 #define OHOS_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_PERFSTAT_H 18 #include <atomic> 19 #include <chrono> 20 #include <shared_mutex> 21 #include <memory> 22 #include <list> 23 24 #include "rdb_types.h" 25 #include "rdb_visibility.h" 26 27 namespace OHOS { 28 template<typename _Key, typename _Tp> 29 class ConcurrentMap; 30 namespace DistributedRdb { 31 class PerfStat { 32 public: 33 struct ThreadParam { 34 int32_t suspenders_ = 0; 35 size_t size_ = 0; 36 }; 37 enum Step : int32_t { 38 STEP_TOTAL, 39 STEP_TOTAL_REF, 40 STEP_TOTAL_RES, 41 STEP_WAIT, 42 STEP_PREPARE, 43 STEP_EXECUTE, 44 STEP_TRANS_START, 45 STEP_TRANS, 46 STEP_TRANS_END, 47 STEP_BUTT, 48 }; 49 API_EXPORT static int Subscribe(const std::string &storeId, std::shared_ptr<SqlObserver> observer); 50 API_EXPORT static int Unsubscribe(const std::string &storeId, std::shared_ptr<SqlObserver> observer); 51 static uint32_t GenerateId(); 52 static void Pause(uint32_t seqId = 0); 53 static void Resume(uint32_t seqId = 0); 54 55 ~PerfStat(); 56 PerfStat(const std::string &storeId, const std::string &sql, int32_t step, uint32_t seqId = 0, size_t size = 0); 57 58 private: 59 using SqlExecInfo = SqlObserver::SqlExecutionInfo; 60 using Release = std::function<void(SqlExecInfo *)>; 61 void FormatSql(const std::string& sql); 62 static Release GetRelease(int32_t step, uint32_t seqId, const std::string &storeId); 63 static void Merge(uint32_t seqId, SqlExecInfo *execInfo); 64 static void Notify(SqlExecInfo *execInfo, const std::string &storeId); 65 static bool IsPaused(); 66 static size_t GetSize(); 67 static void SetSize(size_t size); 68 static ConcurrentMap<std::string, std::set<std::shared_ptr<SqlObserver>>> observers_; 69 static ConcurrentMap<uint64_t, std::shared_ptr<SqlExecInfo>> execInfos_; 70 static bool enabled_; 71 static std::atomic_uint32_t seqId_; 72 static std::shared_mutex mutex_; 73 static std::map<uint64_t, ThreadParam> threadParams_; 74 75 int32_t step_ = 0; 76 uint64_t key_ = 0; 77 std::chrono::steady_clock::time_point time_; 78 std::shared_ptr<SqlExecInfo> execInfo_; 79 }; 80 } // namespace DistributedRdb 81 } // namespace OHOS 82 #endif // OHOS_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_PERFSTAT_H