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