1 /* 2 * Copyright (c) 2023-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 FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_STORAGE_H 17 #define FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_STORAGE_H 18 19 #include <memory> 20 #include <string> 21 22 #include "rdb_store.h" 23 24 namespace OHOS::HiviewDFX { 25 struct TraceFlowRecord { 26 std::string systemTime; 27 std::string callerName; 28 int64_t usedSize = 0; 29 int64_t dynamicDecrease = 0; 30 }; 31 32 class TraceStorage { 33 public: 34 TraceStorage(std::shared_ptr<NativeRdb::RdbStore> dbStore, const std::string& caller, 35 const std::string& configPath); 36 ~TraceStorage() = default; 37 38 int64_t GetRemainingTraceSize(); 39 bool IsOverLimit(); 40 void DecreaseDynamicThreshold(); 41 void StoreDb(int64_t traceSize); 42 #ifdef TRACE_MANAGER_UNITTEST SetTestDate(const std::string & testDate)43 void SetTestDate(const std::string& testDate) 44 { 45 testDate_ = testDate; 46 } 47 #endif 48 49 private: 50 void InitTableRecord(); 51 void Store(const TraceFlowRecord& traceFlowRecord); 52 void Query(TraceFlowRecord& traceFlowRecord); 53 void InsertTable(const TraceFlowRecord& traceFlowRecord); 54 void QueryTable(TraceFlowRecord& traceFlowRecord); 55 void UpdateTable(const TraceFlowRecord& traceFlowRecord); 56 int64_t GetTraceQuota(const std::string& key); 57 bool IsDateChange(); 58 std::string GetDate(); 59 60 private: 61 TraceFlowRecord traceFlowRecord_; 62 std::string caller_; 63 std::string traceQuotaConfig_; 64 int64_t quota_ = 0; 65 int64_t decreaseUnit_ = 0; 66 std::shared_ptr<NativeRdb::RdbStore> dbStore_; 67 #ifdef TRACE_MANAGER_UNITTEST 68 std::string testDate_; 69 #endif 70 }; // TraceStorage 71 } // namespace OHOS::HiviewDFX 72 73 #endif // FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_STORAGE_H 74