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