• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 #include "test_utils.h"
17 
18 #include <dirent.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 
22 #include "common_define.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace Hitrace {
27 namespace {
28 const std::string TRACE_SNAPSHOT_PREFIX = "trace_";
29 const std::string TRACE_RECORD_PREFIX = "record_";
30 
GetTraceFileByPrefix(const std::string & prefix)31 std::vector<std::string> GetTraceFileByPrefix(const std::string& prefix)
32 {
33     if (access(TRACE_FILE_DEFAULT_DIR.c_str(), F_OK) != 0) {
34         return {};
35     }
36     DIR* dirPtr = opendir(TRACE_FILE_DEFAULT_DIR.c_str());
37     if (dirPtr == nullptr) {
38         return {};
39     }
40 
41     std::vector<std::string> filelist = {};
42     struct dirent* ptr = nullptr;
43     while ((ptr = readdir(dirPtr)) != nullptr) {
44         if (ptr->d_type == DT_REG) {
45             std::string name = std::string(ptr->d_name);
46             if (name.compare(0, prefix.size(), prefix) == 0) {
47                 filelist.push_back(name);
48             }
49         }
50     }
51     closedir(dirPtr);
52     return filelist;
53 }
54 }
55 
CountSnapShotTraceFile()56 int CountSnapShotTraceFile()
57 {
58     return GetTraceFileByPrefix(TRACE_SNAPSHOT_PREFIX).size();
59 }
60 
CountRecordingTraceFile()61 int CountRecordingTraceFile()
62 {
63     return GetTraceFileByPrefix(TRACE_RECORD_PREFIX).size();
64 }
65 
GetSnapShotTraceFileList(std::vector<std::string> & fileList)66 void GetSnapShotTraceFileList(std::vector<std::string>& fileList)
67 {
68     fileList = GetTraceFileByPrefix(TRACE_SNAPSHOT_PREFIX);
69 }
70 
GetRecordingTraceFileList(std::vector<std::string> & fileList)71 void GetRecordingTraceFileList(std::vector<std::string>& fileList)
72 {
73     fileList = GetTraceFileByPrefix(TRACE_RECORD_PREFIX);
74 }
75 } // namespace Hitrace
76 } // namespace HiviewDFX
77 } // namespace OHOS