• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 HITRACE_DUMP_H
17 #define HITRACE_DUMP_H
18 
19 #include <string>
20 #include <vector>
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace Hitrace {
25 constexpr uint64_t DEFAULT_TRACE_SLICE_DURATION = 10;
26 constexpr uint64_t DEFAULT_TOTAL_CACHE_FILE_SIZE = 800;
27 
28 enum TraceErrorCode : uint8_t {
29     SUCCESS = 0,
30     TRACE_NOT_SUPPORTED = 1,
31     TRACE_IS_OCCUPIED = 2,
32     TAG_ERROR = 3,
33     FILE_ERROR = 4,
34     WRITE_TRACE_INFO_ERROR = 5,
35     WRONG_TRACE_MODE = 6,
36     OUT_OF_TIME = 7,
37     FORK_ERROR = 8,
38     INVALID_MAX_DURATION = 9,
39     EPOLL_WAIT_ERROR = 10,
40     PIPE_CREATE_ERROR = 11,
41     SYSINFO_READ_FAILURE = 12,
42     UNSET = 255,
43 };
44 
45 enum TraceMode : uint8_t {
46     CLOSE = 0,
47     OPEN = 1 << 0,
48     RECORD = 1 << 1,
49     CACHE = 1 << 2,
50 };
51 
52 struct TraceRetInfo {
53     TraceErrorCode errorCode;
54     std::vector<std::string> outputFiles;
55     int32_t coverRatio = 0;
56     int32_t coverDuration = 0;
57     std::vector<std::string> tags;
58 };
59 
60 #ifdef HITRACE_UNITTEST
61 void SetSysInitParamTags(uint64_t sysInitParamTags);
62 bool SetCheckParam();
63 #endif
64 
65 /**
66  * Get the current trace mode.
67 */
68 uint8_t GetTraceMode();
69 
70 /**
71  * Open trace with customized args.
72 */
73 TraceErrorCode OpenTrace(const std::string& args);
74 
75 /**
76  * Open trace by tag groups using default parameters.
77  * Default parameters: buffersize = 144MB, clockType = boot, overwrite = true.
78 */
79 TraceErrorCode OpenTrace(const std::vector<std::string>& tagGroups);
80 
81 /**
82  * Reading trace data once from ftrace ringbuffer in the kernel.
83  * Using child processes to process trace tasks.
84  * happenTime: the retrospective starting time stamp of target trace.
85  * ----If happenTime = 0, it is not set.
86  * return TraceErrorCode::SUCCESS if any trace is captured between the designated interval
87  * return TraceErrorCode::OUT_OF_TIME otherwise.
88  * maxDuration: the maximum time(s) allowed for the trace task.
89  * ---- If maxDuration is 0, means that is no limit for the trace task.
90  * ---- If maxDuration is less than 0, it is illegal input parameter.
91 */
92 TraceRetInfo DumpTrace(int maxDuration = 0, uint64_t happenTime = 0);
93 
94 /**
95  * Enable sub threads to periodically drop disk trace data.
96  * End the periodic disk drop task until the next call to RecordTraceOff().
97 */
98 TraceErrorCode RecordTraceOn();
99 
100 /**
101  * End the periodic disk drop task.
102 */
103 TraceRetInfo RecordTraceOff();
104 
105 /**
106  * Enable sub threads to periodically dump cache data
107  * End dumping with CacheTraceOff()
108  * CacheTraceOn() function call during the caching phase will return corresponding cache files.
109 */
110 TraceErrorCode CacheTraceOn(uint64_t totalFileSize = DEFAULT_TOTAL_CACHE_FILE_SIZE,
111     uint64_t sliceMaxDuration = DEFAULT_TRACE_SLICE_DURATION);
112 
113 /**
114  * End the periodic cache task.
115 */
116 TraceErrorCode CacheTraceOff();
117 
118 /**
119  * Turn off trace mode.
120 */
121 TraceErrorCode CloseTrace();
122 
123 /**
124  * Get g_traceFilesTable.
125 */
126 std::vector<std::pair<std::string, int>> GetTraceFilesTable();
127 
128 /**
129  * Set g_traceFilesTable.
130 */
131 void SetTraceFilesTable(const std::vector<std::pair<std::string, int>>& traceFilesTable);
132 } // Hitrace
133 
134 }
135 }
136 
137 #endif // HITRACE_DUMP_H