summaryrefslogblamecommitdiffstats
path: root/external/optick/optick_core.linux.h
blob: e0f4b49afced0a1a59f6ca0fa86d525d1bf76caa (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410

























































































































































































































































































































































































































                                                                                                                                                                                          
#pragma once
#if defined(__linux__)

#include "optick.config.h"
#if USE_OPTICK

#include "optick_core.platform.h"

#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <pthread.h>
#include <unistd.h>

namespace Optick
{
	const char* Platform::GetName() 
	{
		return "Linux";
	}

	ThreadID Platform::GetThreadID()
	{
		return syscall(SYS_gettid);
	}

	ProcessID Platform::GetProcessID()
	{
		return (ProcessID)getpid();
	}

	int64 Platform::GetFrequency()
	{
		return 1000000000;
	}

	int64 Platform::GetTime()
	{
		struct timespec ts;
		clock_gettime(CLOCK_MONOTONIC, &ts);
		return ts.tv_sec * 1000000000LL + ts.tv_nsec;
	}
}

#if OPTICK_ENABLE_TRACING

#include "optick_memory.h"

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace ft
{
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	struct base_event
	{
		int64_t timestamp;
		short common_type;
		uint8_t cpu_id;
		base_event(short type) : timestamp(-1), common_type(type), cpu_id(uint8_t(-1)) {}
};
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	template<short TYPE>
	struct event : public base_event
	{
		static const short type = TYPE;
		event() : base_event(TYPE) {}
	};
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	struct process_state
	{
		enum type
		{
			Unknown,
			//D	Uninterruptible sleep(usually IO)
			UninterruptibleSleep,
			//R	Running or runnable(on run queue)
			Running,
			//S	Interruptible sleep(waiting for an event to complete)
			InterruptibleSleep,
			//T	Stopped, either by a job control signal or because it is being traced.
			Stopped,
			//X	dead(should never be seen)
			Dead,
			//Z	Defunct(“zombie”) process, terminated but not reaped by its parent.
			Zombie,
		};
	};
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	struct sched_switch : public event<305>
	{
		char prev_comm[16];
		pid_t prev_pid;
		int prev_prio;
		process_state::type prev_state;
		char next_comm[16];
		pid_t next_pid;
		int next_prio;
	};
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} // namespace ft
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Optick
{
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static const char* KERNEL_TRACING_PATH = "/sys/kernel/debug/tracing";
static const char* FTRACE_TRACE = "trace";
static const char* FTRACE_TRACING_ON = "tracing_on";
static const char* FTRACE_TRACE_CLOCK = "trace_clock";
static const char* FTRACE_OPTIONS_IRQ_INFO = "options/irq-info";
static const char* FTRACE_SCHED_SWITCH = "events/sched/sched_switch/enable";
static const uint8_t PROCESS_STATE_REASON_START = 38;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class FTrace : public Trace
{
	bool isActive;
	string password;

	bool Parse(const char* line);
	bool ProcessEvent(const ft::base_event& ev);

	void Set(const char* name, bool value);
	void Set(const char* name, const char* value);
	void Exec(const char* cmd);
public:

	FTrace();
	~FTrace();

	virtual void SetPassword(const char* pwd) override { password = pwd; }
	virtual CaptureStatus::Type Start(Mode::Type mode, int frequency, const ThreadList& threads) override;
	virtual bool Stop() override;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FTrace g_FTrace;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct Parser
{
	const char* cursor;
	const char* finish;
	size_t length;

	Parser(const char* b) : cursor(b), finish(b + strlen(b)) {}

	bool Skip(size_t count)
	{
		if ((size_t)(finish - cursor) > count)
		{
			cursor += count;
			return true;
		}
		return false;
	}

	bool Skip(const char* text, char* output = nullptr, size_t size = 0)
	{
		if (const char* ptr = strstr(cursor, text))
		{
			if (output != nullptr)
			{
				size_t count = std::min(size - 1, (size_t)(ptr - cursor));
				strncpy(output, cursor, count);
				output[count] = '\0';
			}
			cursor = ptr + strlen(text);
			return true;
		}
		return false;
	}

	void SkipSpaces()
	{
		while (cursor != finish && (*cursor == ' ' || *cursor == '\t' || *cursor == '\n'))
			++cursor;
	}

	bool Starts(const char* text) const
	{
		return strncmp(cursor, text, strlen(text)) == 0;
	}

	int GetInt() const
	{
		return atoi(cursor);
	}

	char GetChar() const
	{
		return *cursor;
	}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CaptureStatus::Type FTrace::Start(Mode::Type mode, int /*frequency*/, const ThreadList& /*threads*/)
{
	if (!isActive)
	{
		// Disable tracing
		Set(FTRACE_TRACING_ON, false);
		// Cleanup old data
		Set(FTRACE_TRACE, "");
		// Set clock type
		Set(FTRACE_TRACE_CLOCK, "mono");
		// Disable irq info
		Set(FTRACE_OPTIONS_IRQ_INFO, false);
		// Enable switch events
		Set(FTRACE_SCHED_SWITCH, (mode & Mode::SWITCH_CONTEXT) != 0);

		// Enable tracing
		Set(FTRACE_TRACING_ON, true);

		isActive = true;
	}

	return CaptureStatus::OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FTrace::Stop()
{
	if (!isActive)
	{
		return false;
	}

	// Reset variables
	Set(FTRACE_TRACING_ON, false);
	Set(FTRACE_SCHED_SWITCH, false);

	// Parsing the output
	char buffer[256] = { 0 };
	sprintf_s(buffer, "echo \'%s\' | sudo -S sh -c \'cat %s/%s\'", password.c_str(), KERNEL_TRACING_PATH, FTRACE_TRACE);
	if (FILE* pipe = popen(buffer, "r"))
	{
		char* line = NULL;
		size_t len = 0;
		while ((getline(&line, &len, pipe)) != -1)
			Parse(line);
		fclose(pipe);
	}

	// Cleanup data
	Set(FTRACE_TRACE, "");

	isActive = false;

	return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FTrace::Parse(const char * line)
{
	// sched_switch:      
	//	   ConsoleApp-8687  [000]  181944.352057: sched_switch: prev_comm=ConsoleApp prev_pid=8687 prev_prio=120 prev_state=S ==> next_comm=ConsoleApp next_pid=8686 next_prio=120

	Parser p(line);
	if (p.Starts("#"))
		return true;

	if (!p.Skip(16))
		return false;

	if (!p.Skip("["))
		return false;

	int cpu = p.GetInt();
	if (!p.Skip("]"))
		return false;

	int64 timestampInt = p.GetInt();
	if (!p.Skip("."))
		return false;

	int64 timestampFraq = p.GetInt();
	if (!p.Skip(": "))
		return false;

	int64 timestamp = ((timestampInt * 1000000) + timestampFraq) * 1000;

	if (p.Starts("sched_switch:"))
	{
		ft::sched_switch ev;
		ev.cpu_id = cpu;
		ev.timestamp = timestamp;

		if (!p.Skip("prev_comm="))
			return false;

		if (!p.Skip(" prev_pid=", ev.prev_comm, OPTICK_ARRAY_SIZE(ev.prev_comm)))
			return false;

		ev.prev_pid = p.GetInt();

		if (!p.Skip(" prev_prio="))
			return false;

		ev.prev_prio = p.GetInt();

		if (!p.Skip(" prev_state="))
			return false;

		switch (p.GetChar())
		{
		case 'D':
			ev.prev_state = ft::process_state::UninterruptibleSleep;
			break;

		case 'R':
			ev.prev_state = ft::process_state::Running;
			break;

		case 'S':
			ev.prev_state = ft::process_state::InterruptibleSleep;
			break;

		case 'T':
			ev.prev_state = ft::process_state::Stopped;
			break;

		case 'X':
			ev.prev_state = ft::process_state::Dead;
			break;

		case 'Z':
			ev.prev_state = ft::process_state::Zombie;
			break;

		default:
			ev.prev_state = ft::process_state::Unknown;
			break;
		}

		if (!p.Skip("==> next_comm="))
			return false;

		if (!p.Skip(" next_pid=", ev.next_comm, OPTICK_ARRAY_SIZE(ev.prev_comm)))
			return false;

		ev.next_pid = p.GetInt();

		if (!p.Skip(" next_prio="))
			return false;

		ev.next_prio = p.GetInt();

		return ProcessEvent(ev);
	}
	return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FTrace::ProcessEvent(const ft::base_event& ev)
{
	switch (ev.common_type)
	{
	case ft::sched_switch::type:
	{
		const ft::sched_switch& switchEv = (const ft::sched_switch&)ev;
		SwitchContextDesc desc;
		desc.reason = switchEv.prev_state + PROCESS_STATE_REASON_START;
		desc.cpuId = switchEv.cpu_id;
		desc.oldThreadId = (uint64)switchEv.prev_pid;
		desc.newThreadId = (uint64)switchEv.next_pid;
		desc.timestamp = switchEv.timestamp;
		Core::Get().ReportSwitchContext(desc);
		return true;
	}
	break;
	}

	return false;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FTrace::Set(const char * name, bool value)
{
	Set(name, value ? "1" : "0");
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FTrace::Set(const char* name, const char* value)
{
	char buffer[256] = { 0 };
	sprintf_s(buffer, "echo %s > %s/%s", value, KERNEL_TRACING_PATH, name);
	Exec(buffer);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FTrace::Exec(const char* cmd)
{
	char buffer[256] = { 0 };
	sprintf_s(buffer, "echo \'%s\' | sudo -S sh -c \'%s\'", password.c_str(), cmd);
	std::system(buffer);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FTrace::FTrace() : isActive(false)
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FTrace::~FTrace()
{
	Stop();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Trace* Platform::GetTrace()
{
	return &g_FTrace;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SymbolEngine* Platform::GetSymbolEngine()
{
	return nullptr;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
#endif //OPTICK_ENABLE_TRACING
#endif //USE_OPTICK
#endif //__linux__