-
Notifications
You must be signed in to change notification settings - Fork 6
/
ftrace.cpp
121 lines (103 loc) · 2.98 KB
/
ftrace.cpp
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
/*
KWin - the KDE window manager
This file is part of the KDE project.
SPDX-FileCopyrightText: 2021 David Edmundson <davidedmundson@kde.org>
SPDX-FileCopyrightText: 2020 Roman Gilg <subdiff@gmail.com>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "ftrace.h"
#include <QDBusConnection>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QScopeGuard>
#include <QTextStream>
namespace KWin
{
KWIN_SINGLETON_FACTORY(KWin::FTraceLogger)
FTraceLogger::FTraceLogger(QObject *parent)
: QObject(parent)
{
if (qEnvironmentVariableIsSet("KWIN_PERF_FTRACE")) {
setEnabled(true);
} else {
QDBusConnection::sessionBus().registerObject(QStringLiteral("/FTrace"), this, QDBusConnection::ExportScriptableContents);
}
}
bool FTraceLogger::isEnabled() const
{
return m_file.isOpen();
}
void FTraceLogger::setEnabled(bool enabled)
{
QMutexLocker lock(&m_mutex);
if (enabled == isEnabled()) {
return;
}
if (enabled) {
open();
} else {
m_file.close();
}
emit enabledChanged();
}
bool FTraceLogger::open()
{
const QString path = filePath();
if (path.isEmpty()) {
return false;
}
m_file.setFileName(path);
if (!m_file.open(QIODevice::WriteOnly)) {
qWarning() << "No access to trace marker file at:" << path;
}
return true;
}
QString FTraceLogger::filePath()
{
if (qEnvironmentVariableIsSet("KWIN_PERF_FTRACE_FILE")) {
return qgetenv("KWIN_PERF_FTRACE_FILE");
}
QFile mountsFile("/proc/mounts");
if (!mountsFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << "No access to mounts file. Can not determine trace marker file location.";
return QString();
}
auto lineInfo = [](const QString &line) {
const int start = line.indexOf(' ') + 1;
const int end = line.indexOf(' ', start);
const QString dirPath(line.mid(start, end - start));
if (dirPath.isEmpty() || !QFileInfo(dirPath).exists()) {
return QFileInfo();
}
return QFileInfo(QDir(dirPath), QStringLiteral("trace_marker"));
};
QFileInfo markerFileInfo;
QTextStream mountsIn(&mountsFile);
QString mountsLine = mountsIn.readLine();
while (!mountsLine.isNull()) {
if (mountsLine.startsWith("tracefs")) {
const auto info = lineInfo(mountsLine);
if (info.exists()) {
markerFileInfo = info;
break;
}
}
if (mountsLine.startsWith("debugfs")) {
markerFileInfo = lineInfo(mountsLine);
}
mountsLine = mountsIn.readLine();
}
mountsFile.close();
if (!markerFileInfo.exists()) {
qWarning() << "Could not determine trace marker file location from mounts.";
return QString();
}
return markerFileInfo.absoluteFilePath();
}
FTraceDuration::~FTraceDuration()
{
FTraceLogger::self()->trace(m_message, " end_ctx=", m_context);
}
}