1 /*
2 * Copyright (c) 2021 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 SRC_TRACE_BASE_TS_COMMON_H
17 #define SRC_TRACE_BASE_TS_COMMON_H
18
19 #include <atomic>
20 #include <cstdint>
21 #include <limits>
22 #include <map>
23 #include <string>
24 using ClockId = uint32_t;
25 constexpr size_t G_CHUNK_SIZE = 1024 * 1024;
26 constexpr size_t FLUSH_CHUNK_THRESHOLD = G_CHUNK_SIZE - 10000;
27 const std::string INVALID_STRING = "INVALID_STRING";
28 const uint64_t INVALID_ITID = std::numeric_limits<uint32_t>::max();
29 const uint64_t INVALID_IPID = std::numeric_limits<uint32_t>::max();
30 const uint64_t INVALID_UINT64 = std::numeric_limits<uint64_t>::max();
31 const uint64_t MAX_UINT32 = std::numeric_limits<uint32_t>::max();
32 const uint64_t MAX_UINT64 = std::numeric_limits<uint64_t>::max();
33 const uint32_t INVALID_UINT8 = std::numeric_limits<uint8_t>::max();
34 const uint32_t INVALID_UINT16 = std::numeric_limits<uint16_t>::max();
35 const uint32_t INVALID_UINT32 = std::numeric_limits<uint32_t>::max();
36 const uint32_t INVALID_INT32 = std::numeric_limits<int32_t>::max();
37 const int64_t INVALID_INT64 = std::numeric_limits<int64_t>::max();
38 const uint64_t INVALID_DATAINDEX = std::numeric_limits<uint64_t>::max();
39 const uint64_t INVALID_CALL_CHAIN_ID = std::numeric_limits<uint64_t>::max();
40 const size_t MAX_SIZE_T = std::numeric_limits<size_t>::max();
41 const uint32_t INVALID_ID = std::numeric_limits<uint32_t>::max();
42 const uint64_t SEC_TO_NS = 1000 * 1000 * 1000;
43 const int32_t STR_DEFAULT_LEN = -1;
44 const auto INVALID_CPU = INVALID_UINT32;
45 const auto INVALID_TIME = INVALID_UINT64;
46 const std::string HEX_PREFIX = "0x";
47 const std::string MEM_QUERY =
48 "select max(value) as maxNum, min(value) as minNum, avg(value) as avgNum, filter.name as name, p.name as "
49 "processName from process_measure left join process_measure_filter as filter on filter.id= filter_id left join "
50 "process as p on p.id = filter.ipid where filter_id > 0 and filter.name = 'mem.rss.anon' group by filter_id order "
51 "by avgNum desc;";
52 const std::string MEM_TOP_QUERY =
53 "select max(value) as maxNum, min(value) as minNum, avg(value) as avgNum, f.name as name, p.name as processName "
54 "from process_measure left join process_measure_filter as f on f.id= filter_id left join process as p on p.id = "
55 "f.ipid where filter_id > 0 and f.name = 'mem.rss.anon' group by filter_id order by avgNum desc limit 10;";
56 const std::string CPU_SQL_QUERY =
57 "SELECT itid AS tid, ipid AS pid, group_concat(cpu, ',') AS cpu, group_concat(dur, ',') AS dur, "
58 "group_concat(min_freq, ',') AS min_freq, group_concat(max_freq, ',') AS max_freq, group_concat(avg_frequency, "
59 "',') AS avg_frequency FROM (SELECT itid, ipid, cpu, CAST (SUM(dur) AS INT) AS dur, CAST (MIN(freq) AS INT) AS "
60 "min_freq, CAST (MAX(freq) AS INT) AS max_freq, CAST ( (SUM(dur * freq) / SUM(dur) ) AS INT) AS avg_frequency from "
61 "result group by itid, cpu)GROUP BY ipid, itid ORDER BY ipid;";
62 const std::string CPU_TOP_TEN_SQL_QUERY =
63 "SELECT itid AS tid, ipid AS pid, group_concat(cpu, ',') AS cpu,group_concat(dur, ',') AS dur, "
64 "group_concat(min_freq, ',') AS min_freq, group_concat(max_freq, ',') AS max_freq, group_concat(avg_frequency, "
65 "',') AS avg_frequency, sum(dur * avg_frequency) AS sumNum FROM (SELECT itid, ipid, cpu, CAST (SUM(dur) AS INT) AS "
66 "dur,CAST (MIN(freq) AS INT) AS min_freq, CAST (MAX(freq) AS INT) AS max_freq,CAST ( (SUM(dur * freq) / SUM(dur) ) "
67 "AS INT) AS avg_frequency from result group by itid, cpu) GROUP BY ipid, itid ORDER BY sumNum DESC LIMIT 10";
68 const std::string DISTRIBUTED_TERM_QUERY =
69 "select group_concat(thread.id,',') as threadId, group_concat(thread.name,',') as threadName, "
70 "group_concat(process.id,',') as processId, group_concat(process.name,',') as processName, "
71 "group_concat(callstack.name,',') as funName, group_concat(callstack.dur,',') as dur, "
72 "group_concat(callstack.ts,',') as ts, cast(callstack.chainId as varchar) as chainId, callstack.spanId as spanId, "
73 "callstack.parentSpanId as parentSpanId, group_concat(callstack.flag,',') as flag, (select value from meta where "
74 "name='source_name') as trace_name from callstack inner join thread on callstack.callid = thread.id inner join "
75 "process on process.id = thread.ipid where (callstack.flag='S' or callstack.flag='C') group by "
76 "callstack.chainId,callstack.spanId,callstack.parentSpanId;";
77 const std::string MEM_UNAGG_QUERY =
78 "select p.name as processName, group_concat(filter.name) as name, cast(group_concat(value) as varchar) as value, "
79 "cast(group_concat(ts) as varchar) as ts from process_measure m left join process_measure_filter as filter on "
80 "filter.id= m.filter_id left join process as p on p.id = filter.ipid where filter.name = 'mem.rss.anon' or "
81 "filter.name = 'mem.rss.file' or filter.name = 'mem.swap' or filter.name = 'oom_score_adj' group by "
82 "p.name,filter.ipid order by filter.ipid;";
83 const std::string META_DATA_QUERY =
84 "select cast(name as varchar) as name, cast(value as varchar) as valueText from meta UNION select "
85 "'start_ts',cast(start_ts as varchar) from trace_range UNION select 'end_ts',cast(end_ts as varchar) from "
86 "trace_range;";
87 const std::string SYS_CALLS_TOP_QUERY =
88 "SELECT cpu.tid AS tid, cpu.pid AS pid, callstack.name AS funName, count(callstack.name) AS frequency, "
89 "min(callstack.dur) AS minDur, max(callstack.dur) AS maxDur, round(avg(callstack.dur)) AS avgDur FROM callstack "
90 "INNER JOIN (SELECT itid AS tid, ipid AS pid, group_concat(cpu, ',') AS cpu, group_concat(dur, ',') AS dur, "
91 "group_concat(min_freq, ',') AS min_freq, group_concat(max_freq, ',') AS max_freq, group_concat(avg_frequency, "
92 "',') AS avg_frequency, sum(dur * avg_frequency) AS sumNum FROM (SELECT itid, ipid, cpu, CAST (SUM(dur) AS INT) AS "
93 "dur, CAST (MIN(freq) AS INT) AS min_freq, CAST (MAX(freq) AS INT) AS max_freq, CAST ( (SUM(dur * freq) / SUM(dur) "
94 ") AS INT) AS avg_frequency FROM result GROUP BY itid, cpu) GROUP BY ipid, itid ORDER BY sumNum DESC LIMIT 10) AS "
95 "cpu ON callstack.callid = cpu.tid GROUP BY callstack.name ORDER BY frequency DESC LIMIT 10;";
96 const std::string SYS_CALL_QUERY =
97 "select count(*) as frequency, min(dur) as minDur, max(dur) as maxDur, avg(dur) as avgDur, name as funName from "
98 "callstack group by name order by frequency desc limit 100;";
99 const std::string TRACE_STATE_QUERY = "select event_name,stat_type,count,source,serverity from stat;";
100 const std::string TRACE_TASK_NAME =
101 "select P.id as id, P.pid as pid, P.name as process_name, group_concat(T.name,',') as thread_name from process as "
102 "P left join thread as T where P.id = T.ipid group by pid;";
103 enum BuiltinClocks {
104 TS_CLOCK_UNKNOW = 0,
105 TS_CLOCK_BOOTTIME = 1,
106 TS_CLOCK_REALTIME = 2,
107 TS_CLOCK_REALTIME_COARSE = 3,
108 TS_MONOTONIC = 4,
109 TS_MONOTONIC_COARSE = 5,
110 TS_MONOTONIC_RAW = 6,
111 };
112 extern BuiltinClocks g_primaryClockId;
113 enum RefType {
114 K_REF_NO_REF = 0,
115 K_REF_ITID = 1,
116 K_REF_CPUID = 2,
117 K_REF_IRQ = 3,
118 K_REF_SOFT_IRQ = 4,
119 K_REF_IPID = 5,
120 K_REF_ITID_LOOKUP_IPID = 6,
121 K_REF_MAX
122 };
123
124 enum TraceFileType {
125 TRACE_FILETYPE_BY_TRACE,
126 TRACE_FILETYPE_H_TRACE,
127 TRACE_FILETYPE_RAW_TRACE,
128 TRACE_FILETYPE_HI_SYSEVENT,
129 TRACE_FILETYPE_PERF,
130 TRACE_FILETYPE_HILOG,
131 TRACE_FILETYPE_UN_KNOW
132 };
133
134 enum EndState {
135 // (R) ready state or running state, the process is ready to run, but not necessarily occupying the CPU
136 TASK_RUNNABLE = 0,
137 // (S) Indicates that the process is in light sleep, waiting for the resource state, and can respond to the signal.
138 // Generally, the process actively sleeps into 'S' state.
139 TASK_INTERRUPTIBLE = 1,
140 // (D) Indicates that the process is in deep sleep, waiting for resources, and does not respond to signals.
141 // Typical scenario: process acquisition semaphore blocking.
142 TASK_UNINTERRUPTIBLE = 2,
143 // (D-IO)
144 TASK_UNINTERRUPTIBLE_IO = 21,
145 // (D-NIO)
146 TASK_UNINTERRUPTIBLE_NIO = 22,
147 // (Running) Indicates that the thread is running
148 TASK_RUNNING = 3,
149 // (T) Thread in interrupt state
150 TASK_STOPPED = 4,
151 // (t) Task being traced
152 TASK_TRACED = 8,
153 // (X) Exit status, the process is about to be destroyed.
154 TASK_EXIT_DEAD = 16,
155 // (Z) Zombie state
156 TASK_ZOMBIE = 32,
157 // (P)
158 TASK_PARKED = 64,
159 // (I) Process killed
160 TASK_DEAD = 128,
161 // (DK)
162 TASK_DK = 130,
163 // (DK-IO)
164 TASK_DK_IO = 131,
165 // (DK-NIO)
166 TASK_DK_NIO = 132,
167 // (tK)the process is being debug now
168 TASK_TRACED_KILL = 136,
169 // (R+) The process is in a deep sleep state and will be killed directly after waking up
170 TASK_WAKEKILL = 256,
171 // TASK_WAKING = 512 (R) waking dont use for Runable state
172 // TASK_NOLOAD = 1024
173 // (R+) Process groups in the foreground
174 TASK_NEW = 2048,
175 TASK_RUNNABLE_BINDER = 2049,
176 TASK_MAX = 4096,
177 TASK_INVALID = 0x8000
178 };
179 enum TSLogLevel {
180 TS_DEBUG = 68, // Debug
181 TS_ERROR = 69, // Error
182 TS_INFO = 73, // Info
183 TS_VERBOSE = 86, // Verbose
184 TS_WARN = 87 // Warn
185 };
186 enum SchedWakeType {
187 SCHED_WAKING = 0, // sched_waking
188 SCHED_WAKEUP = 1, // sched_wakeup
189 };
190 enum DataSourceType {
191 DATA_SOURCE_TYPE_TRACE,
192 DATA_SOURCE_TYPE_MEM,
193 DATA_SOURCE_TYPE_HILOG,
194 DATA_SOURCE_TYPE_NATIVEHOOK,
195 DATA_SOURCE_TYPE_NATIVEHOOK_CONFIG,
196 DATA_SOURCE_TYPE_FPS,
197 DATA_SOURCE_TYPE_NETWORK,
198 DATA_SOURCE_TYPE_DISKIO,
199 DATA_SOURCE_TYPE_CPU,
200 DATA_SOURCE_TYPE_PROCESS,
201 DATA_SOURCE_TYPE_HISYSEVENT,
202 DATA_SOURCE_TYPE_HISYSEVENT_CONFIG,
203 DATA_SOURCE_TYPE_JSMEMORY,
204 DATA_SOURCE_TYPE_JSMEMORY_CONFIG,
205 DATA_SOURCE_TYPE_MEM_CONFIG
206 };
207 enum class HookMemoryType { MALLOC = 0, MMAP = 1, FILE_PAGE_MSG = 2, MEMORY_USING_MSG = 3 };
208 using DataIndex = uint64_t;
209 using TableRowId = int32_t;
210 using InternalPid = uint32_t;
211 using InternalTid = uint32_t;
212 using InternalTime = uint64_t;
213 using FilterId = uint32_t;
214 using InternalCpu = uint32_t; // how many cpus? could change to int8_t?
215
216 enum BaseDataType { BASE_DATA_TYPE_INT, BASE_DATA_TYPE_STRING, BASE_DATA_TYPE_DOUBLE, BASE_DATA_TYPE_BOOLEAN };
217 namespace SysTuning {
218 namespace TraceStreamer {
219 struct ArgsData {
220 BaseDataType type;
221 int64_t value;
222 };
223 struct TraceTimeSnap {
224 uint64_t startTime;
225 uint64_t endTime;
226 };
227 class SpinLock {
228 public:
lock()229 void lock()
230 {
231 while (valueCAS_.test_and_set(std::memory_order_acquire)) {
232 ;
233 }
234 }
unlock()235 void unlock()
236 {
237 valueCAS_.clear(std::memory_order_release);
238 }
239
240 private:
241 std::atomic_flag valueCAS_{0};
242 };
243 template <typename T>
Unused(const T & expr)244 void Unused(const T& expr)
245 {
246 static_cast<void>(expr);
247 }
248 } // namespace TraceStreamer
249 } // namespace SysTuning
250 #endif
251