aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/clangcodemodel/tasktimers.cpp
blob: f0b1c550f597fe33b16304a956c3f81c511a2bbe (plain)
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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "tasktimers.h"

#include <utils/qtcassert.h>

#include <QDateTime>

namespace ClangCodeModel::Internal {

Q_LOGGING_CATEGORY(clangdLogTiming, "qtc.clangcodemodel.clangd.timing", QtWarningMsg);

void ClangCodeModel::Internal::TaskTimer::stopTask()
{
    // This can happen due to the RAII mechanism employed with SubtaskTimer.
    // The subtask timers will expire immediately after, so this does not distort
    // the timing data.
    if (m_subtasks > 0) {
        QTC_CHECK(m_timer.isValid());
        m_elapsedMs += m_timer.elapsed();
        m_timer.invalidate();
        m_subtasks = 0;
    }
    m_started = false;
    qCDebug(clangdLogTiming).noquote().nospace() << m_task << ": took " << m_elapsedMs
                                                 << " ms in UI thread";
    m_elapsedMs = 0;
}

void TaskTimer::startSubtask()
{
    // We have some callbacks that are either synchronous or asynchronous, depending on
    // dynamic conditions. In the sync case, we will have nested subtasks, in which case
    // the inner ones must not collect timing data, as their code blocks are already covered.
    if (++m_subtasks > 1)
        return;
    if (!m_started) {
        QTC_ASSERT(m_elapsedMs == 0, m_elapsedMs = 0);
        m_started = true;
        m_finalized = false;
        qCDebug(clangdLogTiming).noquote().nospace() << m_task << ": starting";

        // Used by ThreadedSubtaskTimer to mark the end of the whole highlighting operation
        m_startTimer.restart();
    }
    qCDebug(clangdLogTiming).noquote().nospace() << m_task << ": subtask started at "
            << QDateTime::currentDateTime().time().toString("hh:mm:ss.zzz");
    QTC_CHECK(!m_timer.isValid());
    m_timer.start();
}

void TaskTimer::stopSubtask(bool isFinalizing)
{
    if (m_subtasks == 0) // See stopTask().
        return;
    if (isFinalizing)
        m_finalized = true;
    if (--m_subtasks > 0) // See startSubtask().
        return;
    qCDebug(clangdLogTiming).noquote().nospace() << m_task << ": subtask stopped at "
            << QDateTime::currentDateTime().time().toString("hh:mm:ss.zzz");
    QTC_CHECK(m_timer.isValid());
    m_elapsedMs += m_timer.elapsed();
    m_timer.invalidate();
    if (m_finalized)
        stopTask();
}

ThreadedSubtaskTimer::ThreadedSubtaskTimer(const QString &task, const TaskTimer &taskTimer)
    : m_task(task), m_taskTimer(taskTimer)
{
    qCDebug(clangdLogTiming).noquote().nospace() << m_task << ": starting thread";
    m_timer.start();
}

ThreadedSubtaskTimer::~ThreadedSubtaskTimer()
{
    qCDebug(clangdLogTiming).noquote().nospace() << m_task << ": took " << m_timer.elapsed()
                                                 << " ms in dedicated thread";

    qCDebug(clangdLogTiming).noquote().nospace() << m_task << ": Start to end: "
                                                 << m_taskTimer.startTimer().elapsed() << " ms";
}

} // namespace ClangCodeModel::Internal