• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024-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 TRACE_FILE_UTILS_H
17 #define TRACE_FILE_UTILS_H
18 
19 #include <map>
20 #include <set>
21 #include <string>
22 #include <vector>
23 
24 #include "hitrace_define.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 namespace Hitrace {
29 const std::string CACHE_FILE_PREFIX = "cache_";
30 
31 struct TraceFileInfo {
32     std::string filename = "";
33     uint64_t traceStartTime = 0; // in ms
34     uint64_t traceEndTime = 0; // in ms
35     int64_t fileSize = 0;
36     time_t ctime = 0;
37     bool isNewFile = true;  // only used in record mode
38 
39     TraceFileInfo() = default;
40     explicit TraceFileInfo(const std::string& name);
41     TraceFileInfo(const std::string& name, time_t time, int64_t sizekB, bool newFile);
42 };
43 
44 void GetTraceFilesInDir(std::vector<TraceFileInfo>& fileList, TraceDumpType traceType);
45 void GetTraceFileNamesInDir(std::set<std::string>& fileSet, TraceDumpType traceType);
46 bool RemoveFile(const std::string& fileName);
47 std::string GenerateTraceFileName(TraceDumpType traceType);
48 std::string GenerateTraceFileNameByTraceTime(TraceDumpType traceType,
49     const uint64_t& firstPageTraceTime, const uint64_t& lastPageTraceTime);
50 void DelSavedEventsFormat();
51 void ClearCacheTraceFileByDuration(std::vector<TraceFileInfo>& cacheFileVec);
52 void ClearCacheTraceFileBySize(std::vector<TraceFileInfo>& cacheFileVec, const uint64_t& fileSizeLimit);
53 off_t GetFileSize(const std::string& filePath);
54 uint64_t GetCurUnixTimeMs();
55 void RefreshTraceVec(std::vector<TraceFileInfo>& traceVec, const TraceDumpType traceType);
56 std::string RenameCacheFile(const std::string& cacheFile);
57 bool SetFileInfo(const bool isFileExist, const std::string outPath, const uint64_t& firstPageTimestamp,
58     const uint64_t& lastPageTimestamp, TraceFileInfo& traceFileInfo);
59 } // namespace Hitrace
60 } // namespace HiviewDFX
61 } // namespace OHOS
62 #endif // TRACE_FILE_UTILS_H