KWin
Loading...
Searching...
No Matches
ftrace.cpp
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2021 David Edmundson <davidedmundson@kde.org>
6 SPDX-FileCopyrightText: 2020 Roman Gilg <subdiff@gmail.com>
7
8 SPDX-License-Identifier: LGPL-2.0-or-later
9*/
10
11#include "ftrace.h"
12
13#include <QDBusConnection>
14#include <QDebug>
15#include <QDir>
16#include <QFile>
17#include <QFileInfo>
18#include <QScopeGuard>
19#include <QTextStream>
20
21namespace KWin
22{
24
25FTraceLogger::FTraceLogger(QObject *parent)
26 : QObject(parent)
27{
28 if (qEnvironmentVariableIsSet("KWIN_PERF_FTRACE")) {
29 setEnabled(true);
30 } else {
31 QDBusConnection::sessionBus().registerObject(QStringLiteral("/FTrace"), this, QDBusConnection::ExportScriptableContents);
32 }
33}
34
35bool FTraceLogger::isEnabled() const
36{
37 return m_file.isOpen();
38}
39
40void FTraceLogger::setEnabled(bool enabled)
41{
42 QMutexLocker lock(&m_mutex);
43 if (enabled == isEnabled()) {
44 return;
45 }
46
47 if (enabled) {
48 open();
49 } else {
50 m_file.close();
51 }
52 Q_EMIT enabledChanged();
53}
54
55bool FTraceLogger::open()
56{
57 const QString path = filePath();
58 if (path.isEmpty()) {
59 return false;
60 }
61
62 m_file.setFileName(path);
63 if (!m_file.open(QIODevice::WriteOnly)) {
64 qWarning() << "No access to trace marker file at:" << path;
65 }
66 return true;
67}
68
69QString FTraceLogger::filePath()
70{
71 if (qEnvironmentVariableIsSet("KWIN_PERF_FTRACE_FILE")) {
72 return qgetenv("KWIN_PERF_FTRACE_FILE");
73 }
74
75 QFile mountsFile("/proc/mounts");
76 if (!mountsFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
77 qWarning() << "No access to mounts file. Can not determine trace marker file location.";
78 return QString();
79 }
80
81 auto lineInfo = [](const QString &line) {
82 const int start = line.indexOf(' ') + 1;
83 const int end = line.indexOf(' ', start);
84 const QString dirPath(line.mid(start, end - start));
85 if (dirPath.isEmpty() || !QFileInfo::exists(dirPath)) {
86 return QFileInfo();
87 }
88 return QFileInfo(QDir(dirPath), QStringLiteral("trace_marker"));
89 };
90 QFileInfo markerFileInfo;
91 QTextStream mountsIn(&mountsFile);
92 QString mountsLine = mountsIn.readLine();
93
94 while (!mountsLine.isNull()) {
95 if (mountsLine.startsWith("tracefs")) {
96 const auto info = lineInfo(mountsLine);
97 if (info.exists()) {
98 markerFileInfo = info;
99 break;
100 }
101 }
102 if (mountsLine.startsWith("debugfs")) {
103 markerFileInfo = lineInfo(mountsLine);
104 }
105 mountsLine = mountsIn.readLine();
106 }
107 mountsFile.close();
108 if (!markerFileInfo.exists()) {
109 qWarning() << "Could not determine trace marker file location from mounts.";
110 return QString();
111 }
112
113 return markerFileInfo.absoluteFilePath();
114}
115
116FTraceDuration::~FTraceDuration()
117{
118 FTraceLogger::self()->trace(m_message, " end_ctx=", m_context);
119}
120
121}
122
123#include "moc_ftrace.cpp"
#define KWIN_SINGLETON_FACTORY(ClassName)
Definition globals.h:335