• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TEST_TRACE_STATE_MACHINE_H
17 #define FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TEST_TRACE_STATE_MACHINE_H
18 #include "singleton.h"
19 #include "trace_common.h"
20 #include "time_util.h"
21 
22 namespace OHOS::HiviewDFX {
23 const std::string TEST_DB_PATH = "/data/test/trace_db/";
24 const std::string TEST_SRC_PATH = "/data/test/trace_src/test_traces/";
25 const std::string TEST_CONFIG_PATH = "/data/test/trace_config/";
26 const std::string TEST_SHARED_PATH = "/data/test/trace_shared/";
27 const std::string TEST_SHARED_TEMP_PATH = "/data/test/trace_shared/temp";
28 const std::string TEST_SPECIAL_PATH = "/data/test/trace_special/";
29 const std::string TEST_TELEMETRY_PATH = "/data/test/trace_telemetry/";
30 const std::string TRACE_TEST_ID1 = "trace_20170928220220@75724-2015";
31 const std::string TRACE_TEST_ID2 = "trace_20170928220222@75726-992";
32 const std::string TRACE_TEST_ID3 = "trace_20170928223217@77520-2883";
33 const std::string TRACE_TEST_ID4 = "trace_20170928223909@77932-4731";
34 const std::string TRACE_TEST_ID5 = "trace_20170928223913@77937-148363";
35 const uint32_t FILE_SIZE_DEFAULT = 200 * 1024;
36 const std::string TRACE_TEST_SRC1 = TEST_SRC_PATH + TRACE_TEST_ID1 + ".sys";
37 const std::string TRACE_TEST_SRC2 = TEST_SRC_PATH + TRACE_TEST_ID2 + ".sys";
38 const std::string TRACE_TEST_SRC3 = TEST_SRC_PATH + TRACE_TEST_ID3 + ".sys";
39 const std::string TRACE_TEST_SRC4 = TEST_SRC_PATH + TRACE_TEST_ID4 + ".sys";
40 const std::string TRACE_TEST_SRC5 = TEST_SRC_PATH + TRACE_TEST_ID5 + ".sys";
41 const std::string TOTAL = "Total";
42 
IsContainSrcTrace(const std::string & tracePath,const std::string & srcPath)43 inline bool IsContainSrcTrace(const std::string& tracePath, const std::string& srcPath)
44 {
45     std::vector<std::string> files;
46     FileUtil::GetDirFiles(tracePath, files);
47     for (auto& file : files) {
48         if (file.find(srcPath) != std::string::npos) {
49             return true;
50         }
51     }
52     return false;
53 }
54 
GetDirFileCount(const std::string & dirPath)55 inline size_t GetDirFileCount(const std::string& dirPath)
56 {
57     std::vector<std::string> files;
58     FileUtil::GetDirFiles(dirPath, files);
59     return files.size();
60 }
61 
62 const std::map<std::string, int64_t> FLOW_CONTROL_MAP {
63     {CallerName::XPERF, 500}, // telemetry trace threshold
64     {CallerName::XPOWER, 500},
65     {CallerName::RELIABILITY, 500},
66     {TOTAL, 1000} // telemetry total trace threshold
67 };
68 
CreateTraceFile(const std::string & traceName)69 inline void CreateTraceFile(const std::string& traceName)
70 {
71     std::ofstream file(traceName, std::ios::out | std::ios::binary);
72     if (!file) {
73         return;
74     }
75     std::string data(FILE_SIZE_DEFAULT, 'A');
76     file.write(data.data(), FILE_SIZE_DEFAULT);
77     file.close();
78 }
79 
CreateAppCallerEvent(int32_t uid,int32_t pid,uint64_t happendTime)80 inline std::shared_ptr<AppCallerEvent> CreateAppCallerEvent(int32_t uid, int32_t pid, uint64_t happendTime)
81 {
82     std::shared_ptr<AppCallerEvent> appCallerEvent = std::make_shared<AppCallerEvent>("HiViewService");
83     appCallerEvent->messageType_ = Event::MessageType::PLUGIN_MAINTENANCE;
84     appCallerEvent->bundleVersion_ = "2.0.1";
85     appCallerEvent->uid_ = uid;
86     appCallerEvent->pid_ = pid;
87     appCallerEvent->happenTime_ = happendTime;
88     return appCallerEvent;
89 }
90 
91 class MockTraceStateMachine : public DelayedRefSingleton<MockTraceStateMachine> {
92 public:
DumpTraceAsync(const DumpTraceArgs & args,int64_t fileSizeLimit,TraceRetInfo & info,const DumpTraceCallback & callback)93     TraceRet DumpTraceAsync(const DumpTraceArgs &args, int64_t fileSizeLimit,
94         TraceRetInfo &info, const DumpTraceCallback &callback)
95     {
96         info = info_;
97         callback_ = callback;
98         return TraceRet(info.errorCode);
99     }
100 
GetAsyncCallback()101     DumpTraceCallback GetAsyncCallback()
102     {
103         return callback_;
104     }
105 
SetTraceInfo(const TraceRetInfo & info)106     void SetTraceInfo(const TraceRetInfo &info)
107     {
108         info_ = info;
109     }
110 
GetCurrentAppPid()111     int32_t GetCurrentAppPid()
112     {
113         return appid_;
114     }
115 
SetCurrentAppPid(int32_t appid)116     void SetCurrentAppPid(int32_t appid)
117     {
118         appid_ = appid;
119     }
120 
121 private:
122     DumpTraceCallback callback_;
123     TraceRetInfo info_ = {};
124     int32_t appid_ = 0;
125     uint64_t taskBeginTime_ = 0;
126 };
127 }
128 
129 #endif
130