-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathftrace.h
105 lines (88 loc) · 2.96 KB
/
ftrace.h
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
/*
KWin - the KDE window manager
This file is part of the KDE project.
SPDX-FileCopyrightText: 2021 David Edmundson <[email protected]>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#pragma once
#include <kwinglobals.h>
#include <QFile>
#include <QMutex>
#include <QMutexLocker>
#include <QObject>
#include <QTextStream>
namespace KWin
{
/**
* FTraceLogger is a singleton utility for writing log messages using ftrace
*
* Usage: Either:
* Set the KWIN_PERF_FTRACE environment variable before starting the application
* Calling on DBus /FTrace org.kde.kwin.FTrace.setEnabled true
* After having created the ftrace mount
*/
class KWIN_EXPORT FTraceLogger : public QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.kde.kwin.FTrace");
Q_PROPERTY(bool isEnabled READ isEnabled NOTIFY enabledChanged)
public:
/**
* Enabled through DBus and logging has started
*/
bool isEnabled() const;
/**
* Main log function
* Takes any number of arguments that can be written into QTextStream
*/
template<typename... Args> void trace(Args... args)
{
Q_ASSERT(isEnabled());
QMutexLocker lock(&m_mutex);
if (!m_file.isOpen()) {
return;
}
QTextStream stream(&m_file);
(stream << ... << args) << Qt::endl;
}
Q_SIGNALS:
void enabledChanged();
public Q_SLOTS:
Q_SCRIPTABLE void setEnabled(bool enabled);
private:
static QString filePath();
bool open();
QFile m_file;
QMutex m_mutex;
KWIN_SINGLETON(FTraceLogger)
};
class KWIN_EXPORT FTraceDuration
{
public:
template<typename... Args> FTraceDuration(Args... args)
{
static QAtomicInteger<qulonglong> s_context = 0;
QTextStream stream(&m_message);
(stream << ... << args);
stream.flush();
m_context = ++s_context;
FTraceLogger::self()->trace(m_message, " begin_ctx=", m_context);
}
~FTraceDuration();
private:
QByteArray m_message;
qulonglong m_context;
};
} // namespace KWin
/**
* Optimised macro, arguments are only copied if tracing is enabled
*/
#define fTrace(...) \
if (KWin::FTraceLogger::self()->isEnabled()) \
KWin::FTraceLogger::self()->trace(__VA_ARGS__);
/**
* Will insert two markers into the log. Once when called, and the second at the end of the relevant block
* In GPUVis this will appear as a timed block with begin_ctx and end_ctx markers
*/
#define fTraceDuration(...) \
QScopedPointer<KWin::FTraceDuration> _duration(KWin::FTraceLogger::self()->isEnabled() ? new KWin::FTraceDuration(__VA_ARGS__) : nullptr);