kwin/src/ftrace.cpp
Laurent Montel b823747c3b Add explicit moc includes to sources for moc-covered headers
* speeds up incremental builds as changes to a header will not always
  need the full mocs_compilation.cpp for all the target's headers rebuild,
  while having a moc file sourced into a source file only adds minor
  extra costs, due to small own code and the used headers usually
  already covered by the source file, being for the same class/struct
* seems to not slow down clean builds, due to empty mocs_compilation.cpp
  resulting in those quickly processed, while the minor extra cost of the
  sourced moc files does not outweigh that in summary.
  Measured times actually improved by some percent points.
  (ideally CMake would just skip empty mocs_compilation.cpp & its object
  file one day)
* enables compiler to see all methods of a class in same compilation unit
  to do some sanity checks
* potentially more inlining in general, due to more in the compilation unit
* allows to keep using more forward declarations in the header, as with the
  moc code being sourced into the cpp file there definitions can be ensured
  and often are already for the needs of the normal class methods
2023-07-15 08:40:49 +00:00

123 lines
3 KiB
C++

/*
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();
}
Q_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::exists(dirPath)) {
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);
}
}
#include "moc_ftrace.cpp"