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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/****************************************************************************
**
** Copyright (C) 2016 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Pelagicore Application Manager.
**
** $QT_BEGIN_LICENSE:LGPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company. For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: LGPL-3.0
**
****************************************************************************/
#include "startuptimer.h"
#include "utilities.h"
#if defined(Q_OS_WIN)
# include <windows.h>
# include <io.h>
# define isatty _isatty
#elif defined(Q_OS_LINUX)
# include <time.h>
# include <qplatformdefs.h>
# include <sys/syscall.h>
#elif defined(Q_OS_OSX)
# include <unistd.h>
# include <sys/sysctl.h>
#endif
StartupTimer::StartupTimer()
{
QByteArray useTimer = qgetenv("AM_STARTUP_TIMER");
if (useTimer.isNull())
return;
else if (useTimer.isEmpty() || useTimer == "1")
m_output = stderr;
else
m_output = fopen(useTimer, "w");
#if defined(Q_OS_WIN)
// Windows reports FILETIMEs in 100nsec steps: divide by 10 to get usec
auto winFileTimeToUSec = [](const FILETIME &filetime) {
return ((quint64(filetime.dwHighDateTime) << 32) | quint64(filetime.dwLowDateTime)) / 10;
};
FILETIME creationTime, dummy, now;
if (GetProcessTimes(GetCurrentProcess(), &creationTime, &dummy, &dummy, &dummy)) {
GetSystemTimeAsFileTime(&now);
m_processCreation = (winFileTimeToUSec(now) - winFileTimeToUSec(creationTime));
m_initialized = true;
} else {
qWarning("StartupTimer: could not get process creation time");
}
#elif defined(Q_OS_LINUX)
// Linux is stupid: there's only one way to get your own process' start time with a high
// resolution: using the async netlink protocol to get a 'taskstat', but this is highly complex
// and requires root privileges.
// And then there's /proc/self/task/<gettid>/stat which gives you the "jiffies" at creation
// time, but no reference to compare it to (plus its resolution is very coarse).
// The following implements the idea from the Mozilla team to get a stable reference for this
// jiffie value: https://blog.mozilla.org/tglek/2011/01/14/builtin-startup-measurement/
// This will give you roughly a 10ms resolution for the time from process creation to entering
// main(), depending on your kernel's HZ value.
// really bool (*)(quint32 *result), but casting the lambda does not work
auto readJiffiesFromProc = [](void *resultPtr) -> void * {
void *result = 0;
QByteArray file = "/proc/self/task/" + QByteArray::number((int) syscall(SYS_gettid)) + "/stat";
int fd = QT_OPEN(file, O_RDONLY);
if (fd >= 0) {
char buffer[1024];
ssize_t bytesRead = QT_READ(fd, buffer, sizeof(buffer) - 1);
if (bytesRead > 0) {
buffer[bytesRead] = 0;
for (int field = 0, pos = 0; pos < bytesRead; ) {
if (buffer[pos++] == ' ') {
if (++field == 21) {
*reinterpret_cast<quint32 *>(resultPtr) = strtoul(buffer + pos, 0, 10);
result = reinterpret_cast<void *>(1);
break;
}
}
}
}
QT_CLOSE(fd);
}
return result;
};
quint32 threadJiffies;
pthread_t pt;
void *threadJiffiesOk = 0;
// using clone() with CLONE_VFORK would be more efficient, but it messes up the NPTL internal state
if ((pthread_create(&pt, 0, readJiffiesFromProc, &threadJiffies) == 0)
&& (pthread_join(pt, &threadJiffiesOk) == 0)
&& threadJiffiesOk) {
quint32 processJiffies = 0;
if (readJiffiesFromProc(&processJiffies)) {
int clkTck = sysconf(_SC_CLK_TCK);
if (clkTck > 0) {
m_processCreation = (threadJiffies - processJiffies) * 1000*1000 / clkTck;
m_initialized = true;
} else {
qWarning("StartupTimer: could not get _SC_CLK_TCK");
}
} else {
qWarning("StartupTimer: could not read process creation jiffies");
}
} else {
qWarning("StartupTimer: could not read thread creation jiffies");
}
#elif defined(Q_OS_OSX)
int mibNames[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
size_t procInfoSize;
if (sysctl(mibNames, sizeof(mibNames) / sizeof(mibNames[0]), 0, &procInfoSize, 0, 0) == 0) {
kinfo_proc *procInfo = (kinfo_proc *) malloc(procInfoSize);
if (sysctl(mibNames, sizeof(mibNames) / sizeof(mibNames[0]), procInfo, &procInfoSize, 0, 0) == 0) {
struct timeval now;
if (gettimeofday(&now, 0) == 0) {
m_processCreation = (quint64(now.tv_sec) * 1000000 + now.tv_usec)
- (procInfo->kp_proc.p_un.__p_starttime.tv_sec * 1000000 + procInfo->kp_proc.p_un.__p_starttime.tv_usec);
m_initialized = true;
}
} else {
qWarning("StartupTimer: could not get kinfo_proc from kernel");
}
free(procInfo);
} else {
qWarning("StartupTimer: could not get size of kinfo_proc buffer");
}
#else
qWarning("StartupTimer: not implemented on this platform");
m_initialized = false;
#endif
if (m_initialized) {
m_timer.start();
checkpoint("entered main");
}
}
StartupTimer::~StartupTimer()
{
if (m_output && m_output != stderr)
fclose(m_output);
}
void StartupTimer::checkpoint(const char *name)
{
if (Q_UNLIKELY(m_initialized)) {
qint64 delta = m_timer.nsecsElapsed();
m_checkpoints << qMakePair(delta / 1000 + m_processCreation, name);
}
}
void StartupTimer::createReport() const
{
if (m_output) {
bool colorSupport = canOutputAnsiColors(fileno(m_output));
if (colorSupport) {
fprintf(m_output, "\n\033[33m== STARTUP TIMING REPORT ==\033[0m\n");
} else {
fprintf(m_output, "\n== STARTUP TIMING REPORT ==\n");
}
static const int cols = 120;
static const int barCols = 60;
int delta = m_checkpoints.last().first;
qreal usecPerCell = delta / barCols;
int secondsLength = QByteArray::number(delta / 1000000).length();
for (int i = 0; i < m_checkpoints.size(); ++i) {
quint64 usec = m_checkpoints.at(i).first;
const QByteArray text = m_checkpoints.at(i).second;
int sec = 0;
int cells = usec / usecPerCell;
QByteArray bar(cells, colorSupport ? ' ' : '#');
QByteArray spacing(cols - cells - 2 - secondsLength - 8 - text.length(), ' ');
if (usec > 1000*1000) {
sec = usec / (1000*1000);
usec %= (1000*1000);
}
int msec = usec / 1000;
usec %= 1000;
if (colorSupport) {
fprintf(m_output, "\033[32m%d'%03d.%03d\033[0m %s %s\033[44m %s\033[0m\n", sec, msec, int(usec), text.constData(), spacing.constData(), bar.constData());
} else {
fprintf(m_output, "%d'%03d.%03d %s %s#%s\n", sec, msec, int(usec), text.constData(), spacing.constData(), bar.constData());
}
}
fprintf(m_output, "\n");
fflush(m_output);
}
}
|