1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#ifndef QQMLJSCOMPILERSTATS_P_H
5#define QQMLJSCOMPILERSTATS_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16
17#include <qtqmlcompilerexports.h>
18
19#include <QHash>
20#include <QJsonDocument>
21
22#include <private/qqmljsdiagnosticmessage_p.h>
23#include <private/qqmljssourcelocation_p.h>
24
25#include <chrono>
26#include <memory>
27
28QT_BEGIN_NAMESPACE
29
30namespace QQmlJS {
31
32enum class CodegenResult : quint8 { Success, Skip, Failure };
33
34struct Q_QMLCOMPILER_EXPORT AotStatsEntry
35{
36 std::chrono::microseconds codegenDuration;
37 QString functionName;
38 QString message;
39 int line = 0;
40 int column = 0;
41 CodegenResult codegenResult = CodegenResult::Success;
42
43 bool operator<(const AotStatsEntry &) const;
44};
45
46class Q_QMLCOMPILER_EXPORT AotStats
47{
48 friend class QQmlJSAotCompilerStats;
49
50public:
51 const QHash<QString, QHash<QString, QList<AotStatsEntry>>> &entries() const
52 {
53 return m_entries;
54 }
55
56 void registerFile(const QString &moduleId, const QString &filepath);
57 void addEntry(const QString &moduleId, const QString &filepath, const AotStatsEntry &entry);
58 void insert(const AotStats &other);
59
60 static std::optional<QStringList> readAllLines(const QString &path);
61 bool saveToDisk(const QString &filepath) const;
62
63 static std::optional<AotStats> parseAotstatsFile(const QString &aotstatsPath);
64 static std::optional<AotStats> aggregateAotstatsList(const QString &aotstatsListPath);
65
66 static std::optional<AotStats> fromJsonDocument(const QJsonDocument &);
67 QJsonDocument toJsonDocument() const;
68
69private:
70 // module Id -> filename -> stats m_entries
71 QHash<QString, QHash<QString, QList<AotStatsEntry>>> m_entries;
72};
73
74class Q_QMLCOMPILER_EXPORT QQmlJSAotCompilerStats
75{
76public:
77 static AotStats *instance() { return s_instance.get(); }
78
79 static bool recordAotStats() { return s_recordAotStats; }
80 static void setRecordAotStats(bool recordAotStats) { s_recordAotStats = recordAotStats; }
81
82 static QString moduleId() { return s_moduleId; }
83 static void setModuleId(const QString &moduleId) { s_moduleId = moduleId; }
84
85 static void registerFile(const QString &filepath);
86 static void addEntry(const QString &filepath, const QQmlJS::AotStatsEntry &entry);
87
88private:
89 static std::unique_ptr<AotStats> s_instance;
90 static QString s_moduleId;
91 static bool s_recordAotStats;
92};
93
94} // namespace QQmlJS
95
96QT_END_NAMESPACE
97
98#endif // QQMLJSCOMPILERSTATS_P_H
99

source code of qtdeclarative/src/qmlcompiler/qqmljscompilerstats_p.h