summaryrefslogtreecommitdiffstats
path: root/src/shared-main-lib/startuptimer.h
blob: 58e38f695534bb83e676d3ddc9c39459e43ed5f0 (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
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2019 Luxoft Sweden AB
// Copyright (C) 2018 Pelagicore AG
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef STARTUPTIMER_H
#define STARTUPTIMER_H

#include <cstdio>
#include <QtCore/QObject>
#include <QtCore/QVector>
#include <QtCore/QPair>
#include <QtCore/QByteArray>
#include <QtCore/QElapsedTimer>
#include <QtAppManCommon/global.h>

QT_BEGIN_NAMESPACE_AM

class StartupTimer : public QObject
{
    Q_OBJECT
    Q_PROPERTY(quint64 timeToFirstFrame READ timeToFirstFrame NOTIFY timeToFirstFrameChanged FINAL)
    Q_PROPERTY(quint64 systemUpTime READ systemUpTime NOTIFY systemUpTimeChanged FINAL)
    Q_PROPERTY(bool automaticReporting READ automaticReporting WRITE setAutomaticReporting NOTIFY automaticReportingChanged FINAL)

public:
    static StartupTimer *instance();
    ~StartupTimer() override;

    Q_INVOKABLE void checkpoint(const QString &name);
    Q_INVOKABLE void createReport(const QString &title = QString());

    quint64 timeToFirstFrame() const;
    quint64 systemUpTime() const;
    bool automaticReporting() const;

    void checkpoint(const char *name);
    void createAutomaticReport(const QString &title);
    void checkFirstFrame();
    void reset();

    void setAutomaticReporting(bool enableAutomaticReporting);

Q_SIGNALS:
    void timeToFirstFrameChanged(quint64 timeToFirstFrame);
    void systemUpTimeChanged(quint64 systemUpTime);
    void automaticReportingChanged(bool setAutomaticReporting);

private:
    StartupTimer();
    static StartupTimer *s_instance;

    static QByteArray formatMicroSecs(quint64 micros);

    FILE *m_output = nullptr;
    bool m_initialized = false;
    bool m_automaticReporting = true;
    quint64 m_processCreation = 0;
    quint64 m_timeToFirstFrame = 0;
    quint64 m_systemUpTime = 0;
    QElapsedTimer m_timer;
    QVector<std::pair<quint64, QByteArray>> m_checkpoints;

    Q_DISABLE_COPY_MOVE(StartupTimer)
};

QT_END_NAMESPACE_AM

#endif // STARTUPTIMER_H