| 1 |
// Copyright © 2016 Canonical Ltd. |
| 2 |
// Author: Loïc Molinari <[email protected]> |
| 3 |
// |
| 4 |
// This file is part of Lomiri UI Toolkit. |
| 5 |
// |
| 6 |
// Lomiri UI Toolkit is free software: you can redistribute it and/or modify it |
| 7 |
// under the terms of the GNU Lesser General Public License as published by the |
| 8 |
// Free Software Foundation; version 3. |
| 9 |
// |
| 10 |
// Lomiri UI Toolkit is distributed in the hope that it will be useful, but |
| 11 |
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 13 |
// for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU Lesser General Public License |
| 16 |
// along with Lomiri UI Toolkit. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
#include "applicationmonitor_p.h" |
| 19 |
|
| 20 |
#include <QtCore/QTimer> |
| 21 |
#include <QtGui/QGuiApplication> |
| 22 |
#include <QtQuick/QQuickWindow> |
| 23 |
|
| 24 |
// FIXME(loicm) When a monitored window is destroyed and if there's a window |
| 25 |
// that's not monitored because the max count was reached, enable monitoring |
| 26 |
// on it if possible. |
| 27 |
|
| 28 |
const int logQueueSize = 16; |
| 29 |
const int logQueueAlignment = 64; |
| 30 |
|
| 31 |
LoggingThread::LoggingThread() |
| 32 |
: m_loggerCount(0) |
| 33 |
, m_refCount(1) |
| 34 |
, m_queueIndex(0) |
| 35 |
, m_queueSize(0) |
| 36 |
, m_flags(0) |
| 37 |
{ |
| 38 |
m_queue = static_cast<UMEvent*>( |
| 39 |
alignedAlloc(logQueueAlignment, logQueueSize * sizeof(UMEvent))); |
| 40 |
|
| 41 |
#if !defined(QT_NO_DEBUG) |
| 42 |
setObjectName(QStringLiteral("LomiriMetrics logging")); // Thread name. |
| 43 |
#endif |
| 44 |
start(); |
| 45 |
} |
| 46 |
|
| 47 |
LoggingThread::~LoggingThread() |
| 48 |
{ |
| 49 |
m_mutex.lock(); |
| 50 |
m_flags |= JoinRequested; |
| 51 |
if (m_flags & Waiting) { |
| 52 |
m_condition.wakeOne(); |
| 53 |
} |
| 54 |
m_mutex.unlock(); |
| 55 |
wait(); |
| 56 |
|
| 57 |
free(m_queue); |
| 58 |
} |
| 59 |
|
| 60 |
#define BREAK_ON_JOIN_REQUEST() \ |
| 61 |
if (Q_UNLIKELY(m_flags & JoinRequested)) { \ |
| 62 |
m_mutex.unlock(); \ |
| 63 |
break; \ |
| 64 |
} |
| 65 |
|
| 66 |
// Logging thread entry point. |
| 67 |
void LoggingThread::run() |
| 68 |
{ |
| 69 |
DLOG("Entering logging thread."); |
| 70 |
while (true) { |
| 71 |
// Wait for new events in the log queue. |
| 72 |
m_mutex.lock(); |
| 73 |
DASSERT(m_queueSize >= 0); |
| 74 |
if (m_queueSize == 0) { |
| 75 |
BREAK_ON_JOIN_REQUEST(); |
| 76 |
m_flags |= Waiting; |
| 77 |
m_condition.wait(&m_mutex); |
| 78 |
if (m_queueSize == 0) { |
| 79 |
BREAK_ON_JOIN_REQUEST(); |
| 80 |
} |
| 81 |
m_flags &= ~Waiting; |
| 82 |
} |
| 83 |
|
| 84 |
// Unqueue oldest events from the log queue. |
| 85 |
DASSERT(m_queueSize > 0); |
| 86 |
UMEvent event; |
| 87 |
memcpy(&event, &m_queue[m_queueIndex], sizeof(UMEvent)); |
| 88 |
m_queueIndex = (m_queueIndex + 1) % logQueueSize; |
| 89 |
m_queueSize--; |
| 90 |
|
| 91 |
// Log. |
| 92 |
const int loggerCount = m_loggerCount; |
| 93 |
UMLogger* loggers[UMApplicationMonitorPrivate::maxLoggers]; |
| 94 |
memcpy(loggers, m_loggers, loggerCount * sizeof(UMLogger*)); |
| 95 |
m_mutex.unlock(); |
| 96 |
for (int i = 0; i < loggerCount; ++i) { |
| 97 |
loggers[i]->log(event); |
| 98 |
} |
| 99 |
} |
| 100 |
DLOG("Leaving logging thread."); |
| 101 |
} |
| 102 |
|
| 103 |
void LoggingThread::push(const UMEvent* event) |
| 104 |
{ |
| 105 |
// Ensure the log queue is not full. |
| 106 |
m_mutex.lock(); |
| 107 |
DASSERT(m_queueSize <= logQueueSize); |
| 108 |
while (m_queueSize == logQueueSize) { |
| 109 |
m_mutex.unlock(); |
| 110 |
QThread::yieldCurrentThread(); |
| 111 |
m_mutex.lock(); |
| 112 |
} |
| 113 |
|
| 114 |
// Push event to the log queue. |
| 115 |
DASSERT(m_queueSize < logQueueSize); |
| 116 |
memcpy(&m_queue[(m_queueIndex + m_queueSize++) % logQueueSize], event, sizeof(UMEvent)); |
| 117 |
if (m_flags & Waiting) { |
| 118 |
m_condition.wakeOne(); |
| 119 |
} |
| 120 |
m_mutex.unlock(); |
| 121 |
} |
| 122 |
|
| 123 |
void LoggingThread::setLoggers(UMLogger** loggers, int count) |
| 124 |
{ |
| 125 |
DASSERT(count >= 0); |
| 126 |
DASSERT(count <= UMApplicationMonitorPrivate::maxLoggers); |
| 127 |
|
| 128 |
QMutexLocker locker(&m_mutex); |
| 129 |
memcpy(m_loggers, loggers, count * sizeof(UMLogger*)); |
| 130 |
m_loggerCount = count; |
| 131 |
} |
| 132 |
|
| 133 |
LoggingThread* LoggingThread::ref() |
| 134 |
{ |
| 135 |
m_refCount.ref(); |
| 136 |
return this; |
| 137 |
} |
| 138 |
|
| 139 |
void LoggingThread::deref() |
| 140 |
{ |
| 141 |
if (m_refCount.deref() == 0) { |
| 142 |
if (thread() == QThread::currentThread()) { |
| 143 |
delete this; |
| 144 |
} else { |
| 145 |
deleteLater(); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
UMApplicationMonitor* UMApplicationMonitor::self = nullptr; |
| 151 |
|
| 152 |
UMApplicationMonitor::UMApplicationMonitor() |
| 153 |
: d_ptr(new UMApplicationMonitorPrivate(this)) |
| 154 |
{ |
| 155 |
ASSERT_X(!self, "ApplicationMonitor: There should be only one UMApplicationMonitor."); |
| 156 |
self = this; |
| 157 |
} |
| 158 |
|
| 159 |
UMApplicationMonitorPrivate::UMApplicationMonitorPrivate(UMApplicationMonitor* applicationMonitor) |
| 160 |
: q_ptr(applicationMonitor) |
| 161 |
#if !defined(QT_NO_DEBUG) |
| 162 |
, m_monitors{} |
| 163 |
, m_loggers{} |
| 164 |
#endif |
| 165 |
, m_loggingThread(nullptr) |
| 166 |
, m_monitorCount(0) |
| 167 |
, m_loggerCount(0) |
| 168 |
, m_updateInterval{1000, -1, -1} |
| 169 |
, m_flags(UMApplicationMonitor::AllEvents) |
| 170 |
{ |
| 171 |
Q_Q(UMApplicationMonitor); |
| 172 |
|
| 173 |
QGuiApplication* application = qobject_cast<QGuiApplication*>(QCoreApplication::instance()); |
| 174 |
ASSERT_X(application, "ApplicationMonitor: There should be a QGuiApplication instantiated."); |
| 175 |
#if !defined(QT_NO_DEBUG) |
| 176 |
m_application = application; |
| 177 |
#endif |
| 178 |
|
| 179 |
q->setParent(application); |
| 180 |
|
| 181 |
QObject::connect(application, SIGNAL(lastWindowClosed()), q, SLOT(closeDown())); |
| 182 |
QObject::connect(application, SIGNAL(aboutToQuit()), q, SLOT(closeDown())); |
| 183 |
QObject::connect(&m_processTimer, SIGNAL(timeout()), q, SLOT(processTimeout())); |
| 184 |
|
| 185 |
m_processTimer.setInterval(m_updateInterval[UMEvent::Process]); |
| 186 |
} |
| 187 |
|
| 188 |
UMApplicationMonitor::~UMApplicationMonitor() |
| 189 |
{ |
| 190 |
delete d_ptr; |
| 191 |
} |
| 192 |
|
| 193 |
UMApplicationMonitorPrivate::~UMApplicationMonitorPrivate() |
| 194 |
{ |
| 195 |
DASSERT(!(m_flags & Started)); |
| 196 |
|
| 197 |
// Note that there's no need to disconnect from QGuiApplication signals |
| 198 |
// since the application monitor instance is automatically destroyed when |
| 199 |
// the application is destroyed (parenting), the application instance would |
| 200 |
// be NULL. |
| 201 |
} |
| 202 |
|
| 203 |
void UMApplicationMonitor::setOverlay(bool overlay) |
| 204 |
{ |
| 205 |
Q_D(UMApplicationMonitor); |
| 206 |
|
| 207 |
if (!!(d->m_flags & UMApplicationMonitorPrivate::Overlay) != overlay) { |
| 208 |
if (overlay) { |
| 209 |
d->m_flags |= UMApplicationMonitorPrivate::Overlay; |
| 210 |
if (!(d->m_flags & (UMApplicationMonitorPrivate::Started |
| 211 |
| UMApplicationMonitorPrivate::ClosingDown))) { |
| 212 |
d->start(); |
| 213 |
} else { |
| 214 |
d->setMonitoringFlags(d->m_flags); |
| 215 |
} |
| 216 |
} else { |
| 217 |
d->m_flags &= ~UMApplicationMonitorPrivate::Overlay; |
| 218 |
if (!(d->m_flags & UMApplicationMonitorPrivate::Logging)) { |
| 219 |
d->stop(); |
| 220 |
} else { |
| 221 |
d->setMonitoringFlags(d->m_flags); |
| 222 |
} |
| 223 |
} |
| 224 |
Q_EMIT overlayChanged(); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
bool UMApplicationMonitor::overlay() |
| 229 |
{ |
| 230 |
return !!(d_func()->m_flags & UMApplicationMonitorPrivate::Overlay); |
| 231 |
} |
| 232 |
|
| 233 |
void UMApplicationMonitor::setLogging(bool logging) |
| 234 |
{ |
| 235 |
Q_D(UMApplicationMonitor); |
| 236 |
|
| 237 |
if (!!(d->m_flags & UMApplicationMonitorPrivate::Logging) != logging) { |
| 238 |
if (logging) { |
| 239 |
d->m_flags |= UMApplicationMonitorPrivate::Logging; |
| 240 |
if (!(d->m_flags & (UMApplicationMonitorPrivate::Started |
| 241 |
| UMApplicationMonitorPrivate::ClosingDown))) { |
| 242 |
d->start(); |
| 243 |
} else { |
| 244 |
d->setMonitoringFlags(d->m_flags); |
| 245 |
} |
| 246 |
} else { |
| 247 |
d->m_flags &= ~UMApplicationMonitorPrivate::Logging; |
| 248 |
if (!(d->m_flags & UMApplicationMonitorPrivate::Overlay)) { |
| 249 |
d->stop(); |
| 250 |
} else { |
| 251 |
d->setMonitoringFlags(d->m_flags); |
| 252 |
} |
| 253 |
} |
| 254 |
Q_EMIT loggingChanged(); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
bool UMApplicationMonitor::logging() |
| 259 |
{ |
| 260 |
return !!(d_func()->m_flags & UMApplicationMonitorPrivate::Logging); |
| 261 |
} |
| 262 |
|
| 263 |
void UMApplicationMonitorPrivate::startMonitoring(QQuickWindow* window) |
| 264 |
{ |
| 265 |
DASSERT(window); |
| 266 |
DASSERT(m_loggingThread); |
| 267 |
|
| 268 |
if (m_monitorCount < maxMonitors) { |
| 269 |
DASSERT(m_monitors[m_monitorCount] == nullptr); |
| 270 |
static quint32 id = 0; |
| 271 |
m_monitors[m_monitorCount] = |
| 272 |
new WindowMonitor(q_func(), window, m_loggingThread->ref(), m_flags, ++id); |
| 273 |
m_monitors[m_monitorCount]->setProcessEvent(m_processEvent); |
| 274 |
m_monitorCount++; |
| 275 |
} else { |
| 276 |
WARN("ApplicationMonitor: Can't monitor more than %d QQuickWindows.", maxMonitors); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
void UMApplicationMonitorPrivate::start() |
| 281 |
{ |
| 282 |
DASSERT(!(m_flags & Started)); |
| 283 |
DASSERT(!m_loggingThread); |
| 284 |
|
| 285 |
m_loggingThread = new LoggingThread; |
| 286 |
m_loggingThread->setLoggers(m_loggers, m_loggerCount); |
| 287 |
|
| 288 |
QWindowList windows = QGuiApplication::allWindows(); |
| 289 |
const int size = windows.size(); |
| 290 |
m_monitorsMutex.lock(); |
| 291 |
for (int i = 0; i < size; ++i) { |
| 292 |
if (QQuickWindow* window = qobject_cast<QQuickWindow*>(windows[i])) { |
| 293 |
if (window->isVisible()) { |
| 294 |
startMonitoring(window); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
m_monitorsMutex.unlock(); |
| 299 |
|
| 300 |
QGuiApplication::instance()->installEventFilter(q_func()); |
| 301 |
|
| 302 |
// Doing it here so that processTimeout can assert the monitoring started. |
| 303 |
m_flags |= Started; |
| 304 |
|
| 305 |
memset(&m_processEvent, 0, sizeof(UMEvent)); |
| 306 |
processTimeout(); |
| 307 |
if (m_updateInterval[UMEvent::Process] >= 0) { |
| 308 |
m_processTimer.start(); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
bool UMApplicationMonitorPrivate::removeMonitor(WindowMonitor* monitor) |
| 313 |
{ |
| 314 |
DASSERT(monitor); |
| 315 |
DASSERT(monitor->window()); |
| 316 |
|
| 317 |
m_monitorsMutex.lock(); |
| 318 |
for (int i = 0; i < m_monitorCount; ++i) { |
| 319 |
if (m_monitors[i] == monitor) { |
| 320 |
m_monitorCount--; |
| 321 |
if (i < m_monitorCount) { |
| 322 |
memmove(&m_monitors[i], &m_monitors[i+1], |
| 323 |
(m_monitorCount-i) * sizeof(WindowMonitor*)); |
| 324 |
} |
| 325 |
#if !defined(QT_NO_DEBUG) |
| 326 |
m_monitors[m_monitorCount] = nullptr; |
| 327 |
#endif |
| 328 |
m_monitorsMutex.unlock(); |
| 329 |
return true; |
| 330 |
} |
| 331 |
} |
| 332 |
m_monitorsMutex.unlock(); |
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
WindowMonitorDeleter::~WindowMonitorDeleter() |
| 337 |
{ |
| 338 |
// FIXME(loicm) Not sure if that assertion could ever fail, so let's debug |
| 339 |
// assert it and test in release for now. |
| 340 |
DASSERT(m_applicationMonitor == UMApplicationMonitor::instance()); |
| 341 |
if (m_applicationMonitor == UMApplicationMonitor::instance()) { |
| 342 |
// Remove the monitor from the list making sure it's not been removed |
| 343 |
// (window going hidden) after this runnable was scheduled. |
| 344 |
if (UMApplicationMonitorPrivate::get(m_applicationMonitor)->removeMonitor(m_monitor)) { |
| 345 |
delete m_monitor; |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
void WindowMonitorDeleter::run() |
| 351 |
{ |
| 352 |
// run() guarantees a valid context. |
| 353 |
if (m_monitor->gpuResourcesInitialized()) { |
| 354 |
m_monitor->finalizeGpuResources(); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
void UMApplicationMonitorPrivate::stopMonitoring(WindowMonitor* monitor) |
| 359 |
{ |
| 360 |
DASSERT(monitor); |
| 361 |
DASSERT(monitor->window()); |
| 362 |
|
| 363 |
// To ensure there's no signals emitted on the render thread while and after |
| 364 |
// window monitor deletion, we schedule a render job that disconnects the |
| 365 |
// signals on the render thread and then request deletion on the right |
| 366 |
// thread with deleteLater(). |
| 367 |
monitor->window()->scheduleRenderJob(new WindowMonitorDeleter(q_func(), monitor), |
| 368 |
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) |
| 369 |
QQuickWindow::NoStage); |
| 370 |
#else |
| 371 |
QQuickWindow::BeforeSynchronizingStage); |
| 372 |
monitor->window()->update(); // Wake up the render loop. |
| 373 |
#endif |
| 374 |
} |
| 375 |
|
| 376 |
void UMApplicationMonitorPrivate::stop() |
| 377 |
{ |
| 378 |
DASSERT(m_flags & Started); |
| 379 |
|
| 380 |
if (m_updateInterval[UMEvent::Process] >= 0) { |
| 381 |
m_processTimer.stop(); |
| 382 |
} |
| 383 |
|
| 384 |
QGuiApplication::instance()->removeEventFilter(q_func()); |
| 385 |
|
| 386 |
m_monitorsMutex.lock(); |
| 387 |
for (int i = 0; i < m_monitorCount; ++i) { |
| 388 |
stopMonitoring(m_monitors[i]); |
| 389 |
} |
| 390 |
m_monitorsMutex.unlock(); |
| 391 |
|
| 392 |
DASSERT(m_loggingThread); |
| 393 |
m_loggingThread->deref(); |
| 394 |
m_loggingThread = nullptr; |
| 395 |
|
| 396 |
// Wait for window monitors complete deletion. |
| 397 |
m_monitorsMutex.lock(); |
| 398 |
while (m_monitorCount > 0) { |
| 399 |
// FIXME(loicm) Should we yield the thread here in the case of a |
| 400 |
// threaded QtQuick renderer? |
| 401 |
m_monitorsMutex.unlock(); |
| 402 |
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); |
| 403 |
m_monitorsMutex.lock(); |
| 404 |
} |
| 405 |
m_monitorsMutex.unlock(); |
| 406 |
|
| 407 |
m_flags &= ~Started; |
| 408 |
} |
| 409 |
|
| 410 |
bool UMApplicationMonitorPrivate::hasMonitor(WindowMonitor* monitor) |
| 411 |
{ |
| 412 |
DASSERT(monitor); |
| 413 |
DASSERT(monitor->window()); |
| 414 |
|
| 415 |
m_monitorsMutex.lock(); |
| 416 |
for (int i = 0; i < m_monitorCount; ++i) { |
| 417 |
if (m_monitors[i] == monitor) { |
| 418 |
m_monitorsMutex.unlock(); |
| 419 |
return true; |
| 420 |
} |
| 421 |
} |
| 422 |
m_monitorsMutex.unlock(); |
| 423 |
return false; |
| 424 |
} |
| 425 |
|
| 426 |
WindowMonitorFlagSetter::~WindowMonitorFlagSetter() |
| 427 |
{ |
| 428 |
// FIXME(loicm) Not sure if that assertion could ever fail, so let's debug |
| 429 |
// assert it and do the check in release for now. |
| 430 |
DASSERT(m_applicationMonitor == UMApplicationMonitor::instance()); |
| 431 |
if (m_applicationMonitor == UMApplicationMonitor::instance()) { |
| 432 |
// Make sure it's not been removed (window going hidden) after this |
| 433 |
// runnable was scheduled. |
| 434 |
if (UMApplicationMonitorPrivate::get(m_applicationMonitor)->hasMonitor(m_monitor)) { |
| 435 |
m_monitor->setFlags(m_flags); |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
void UMApplicationMonitorPrivate::setMonitoringFlags(quint32 flags) |
| 441 |
{ |
| 442 |
m_monitorsMutex.lock(); |
| 443 |
for (int i = 0; i < m_monitorCount; ++i) { |
| 444 |
DASSERT(m_monitors[i]); |
| 445 |
DASSERT(m_monitors[i]->window()); |
| 446 |
m_monitors[i]->window()->scheduleRenderJob( |
| 447 |
new WindowMonitorFlagSetter(m_monitors[i], flags), |
| 448 |
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) |
| 449 |
QQuickWindow::NoStage); |
| 450 |
#else |
| 451 |
QQuickWindow::BeforeSynchronizingStage); |
| 452 |
m_monitors[i]->window()->update(); // Wake up the render loop. |
| 453 |
#endif |
| 454 |
} |
| 455 |
m_monitorsMutex.unlock(); |
| 456 |
} |
| 457 |
|
| 458 |
void UMApplicationMonitor::setLoggingFilter(UMApplicationMonitor::LoggingFilters filter) |
| 459 |
{ |
| 460 |
Q_D(UMApplicationMonitor); |
| 461 |
|
| 462 |
const quint32 maskedFilter = filter & UMApplicationMonitorPrivate::FilterMask; |
| 463 |
if (maskedFilter != (d->m_flags & UMApplicationMonitorPrivate::FilterMask)) { |
| 464 |
d->m_flags = (d->m_flags & ~UMApplicationMonitorPrivate::FilterMask) | maskedFilter; |
| 465 |
if (d->m_flags & UMApplicationMonitorPrivate::Started) { |
| 466 |
d->setMonitoringFlags(d->m_flags); |
| 467 |
} |
| 468 |
Q_EMIT loggingFilterChanged(); |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
UMApplicationMonitor::LoggingFilters UMApplicationMonitor::loggingFilter() |
| 473 |
{ |
| 474 |
return static_cast<LoggingFilters>(d_func()->m_flags & UMApplicationMonitorPrivate::FilterMask); |
| 475 |
} |
| 476 |
|
| 477 |
QList<UMLogger*> UMApplicationMonitor::loggers() |
| 478 |
{ |
| 479 |
Q_D(UMApplicationMonitor); |
| 480 |
|
| 481 |
QList<UMLogger*> list; |
| 482 |
const int count = d->m_loggerCount; |
| 483 |
for (int i = 0; i < count; ++i) { |
| 484 |
list.append(d->m_loggers[i]); |
| 485 |
} |
| 486 |
return list; |
| 487 |
} |
| 488 |
|
| 489 |
bool UMApplicationMonitor::installLogger(UMLogger* logger) |
| 490 |
{ |
| 491 |
Q_D(UMApplicationMonitor); |
| 492 |
|
| 493 |
if (d->m_loggerCount < UMApplicationMonitorPrivate::maxLoggers && logger) { |
| 494 |
DASSERT(d->m_loggers[d->m_loggerCount] == nullptr); |
| 495 |
d->m_loggers[d->m_loggerCount++] = logger; |
| 496 |
if (d->m_flags & UMApplicationMonitorPrivate::Started) { |
| 497 |
DASSERT(d->m_loggingThread); |
| 498 |
d->m_loggingThread->setLoggers(d->m_loggers, d->m_loggerCount); |
| 499 |
} |
| 500 |
Q_EMIT loggersChanged(); |
| 501 |
return true; |
| 502 |
} else { |
| 503 |
return false; |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
bool UMApplicationMonitor::removeLogger(UMLogger* logger, bool free) |
| 508 |
{ |
| 509 |
if (!logger) { |
| 510 |
return false; |
| 511 |
} |
| 512 |
|
| 513 |
Q_D(UMApplicationMonitor); |
| 514 |
|
| 515 |
for (int i = d->m_loggerCount; i > 0; --i) { |
| 516 |
if (d->m_loggers[i] == logger) { |
| 517 |
if (i < --d->m_loggerCount) { |
| 518 |
d->m_loggers[i] = d->m_loggers[d->m_loggerCount]; |
| 519 |
} |
| 520 |
#if !defined(QT_NO_DEBUG) |
| 521 |
d->m_loggers[d->m_loggerCount] = nullptr; |
| 522 |
#endif |
| 523 |
if (d->m_flags & UMApplicationMonitorPrivate::Started) { |
| 524 |
DASSERT(d->m_loggingThread); |
| 525 |
d->m_loggingThread->setLoggers(d->m_loggers, d->m_loggerCount); |
| 526 |
} |
| 527 |
if (free) { |
| 528 |
delete logger; |
| 529 |
} |
| 530 |
Q_EMIT loggersChanged(); |
| 531 |
return true; |
| 532 |
} |
| 533 |
} |
| 534 |
return false; |
| 535 |
} |
| 536 |
|
| 537 |
void UMApplicationMonitor::clearLoggers(bool free) |
| 538 |
{ |
| 539 |
Q_D(UMApplicationMonitor); |
| 540 |
|
| 541 |
if (d->m_loggerCount > 0) { |
| 542 |
if (free) { |
| 543 |
const int count = d->m_loggerCount; |
| 544 |
for (int i = 0; i < count; ++i) { |
| 545 |
delete d->m_loggers[i]; |
| 546 |
} |
| 547 |
} |
| 548 |
d->m_loggerCount = 0; |
| 549 |
Q_EMIT loggersChanged(); |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
quint32 UMApplicationMonitor::registerGenericEvent() |
| 554 |
{ |
| 555 |
static quint32 id = 0; // 0 is reserved for UMApplicationMonitor events. |
| 556 |
return ++id; |
| 557 |
} |
| 558 |
|
| 559 |
bool UMApplicationMonitor::logGenericEvent(quint32 id, const char* string, quint32 size) |
| 560 |
{ |
| 561 |
Q_D(UMApplicationMonitor); |
| 562 |
|
| 563 |
if ((d->m_flags & UMApplicationMonitorPrivate::Logging) && (d->m_flags & GenericEvent)) { |
| 564 |
DASSERT(d->m_loggingThread); |
| 565 |
UMEvent event; |
| 566 |
event.type = UMEvent::Generic; |
| 567 |
event.timeStamp = UMEventUtils::timeStamp(); |
| 568 |
event.generic.id = id; |
| 569 |
// We don't bother fixing up non null-terminated string, just potential |
| 570 |
// overflows. |
| 571 |
// Note: GCC5 comoiler/linker cannot find UMGenericEvent::maxStringSize |
| 572 |
// if used in qMin(); force type to satisfy it |
| 573 |
event.generic.stringSize = qMin(size, quint32(UMGenericEvent::maxStringSize)); |
| 574 |
memcpy(event.generic.string, string, event.generic.stringSize); |
| 575 |
d->m_loggingThread->push(&event); |
| 576 |
return true; |
| 577 |
} else { |
| 578 |
return false; |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
bool UMApplicationMonitor::logEvent(Event event) |
| 583 |
{ |
| 584 |
switch (event) { |
| 585 |
case UserInterfaceReady: { |
| 586 |
return logGenericEvent(0, "UserInterfaceReady", sizeof("UserInterfaceReady")); |
| 587 |
} |
| 588 |
default: { |
| 589 |
DNOT_REACHED(); |
| 590 |
return false; |
| 591 |
} |
| 592 |
}; |
| 593 |
} |
| 594 |
|
| 595 |
void UMApplicationMonitor::setUpdateInterval(UMEvent::Type type, int interval) |
| 596 |
{ |
| 597 |
Q_D(UMApplicationMonitor); |
| 598 |
|
| 599 |
if (type == UMEvent::Process) { |
| 600 |
// Other types (like UMEvent::Frame) are ignored for now. |
| 601 |
if (interval != d->m_updateInterval[UMEvent::Process]) { |
| 602 |
if (interval >= 0) { |
| 603 |
d->m_processTimer.setInterval(interval); |
| 604 |
if ((d->m_flags & UMApplicationMonitorPrivate::Started) |
| 605 |
&& (d->m_updateInterval[UMEvent::Process] < 0)) { |
| 606 |
d->m_processTimer.start(); |
| 607 |
} |
| 608 |
} else if ((d->m_flags & UMApplicationMonitorPrivate::Started) |
| 609 |
&& (d->m_updateInterval[UMEvent::Process] >= 0)) { |
| 610 |
d->m_processTimer.stop(); |
| 611 |
} |
| 612 |
d->m_updateInterval[UMEvent::Process] = interval; |
| 613 |
Q_EMIT updateIntervalChanged(UMEvent::Process); |
| 614 |
} |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
int UMApplicationMonitor::updateInterval(UMEvent::Type type) |
| 619 |
{ |
| 620 |
return d_func()->m_updateInterval[type]; |
| 621 |
} |
| 622 |
|
| 623 |
void UMApplicationMonitor::closeDown() |
| 624 |
{ |
| 625 |
Q_D(UMApplicationMonitor); |
| 626 |
|
| 627 |
d->m_flags |= UMApplicationMonitorPrivate::ClosingDown; |
| 628 |
if (d->m_flags & UMApplicationMonitorPrivate::Started) { |
| 629 |
d->stop(); |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
void UMApplicationMonitor::processTimeout() |
| 634 |
{ |
| 635 |
d_func()->processTimeout(); |
| 636 |
} |
| 637 |
|
| 638 |
void UMApplicationMonitorPrivate::processTimeout() |
| 639 |
{ |
| 640 |
DASSERT(m_flags & Started); |
| 641 |
DASSERT(m_loggingThread); |
| 642 |
|
| 643 |
const bool processLogging = |
| 644 |
(m_flags & Logging) && (m_flags & UMApplicationMonitor::ProcessEvent); |
| 645 |
const bool overlay = m_flags & Overlay; |
| 646 |
|
| 647 |
if (processLogging || overlay) { |
| 648 |
m_eventUtils.updateProcessEvent(&m_processEvent); |
| 649 |
if (processLogging) { |
| 650 |
m_loggingThread->push(&m_processEvent); |
| 651 |
} |
| 652 |
if (overlay) { |
| 653 |
// FIXME(loicm) We've got two choices here, locking all the monitors |
| 654 |
// and pushing the new process event or using scheduleRenderJob. |
| 655 |
// We're using direct pushing for now but it would be nice to |
| 656 |
// measure the cost of both methods at high update freqs. The |
| 657 |
// former one implying locks and the second one dynamic memory |
| 658 |
// allocations (placement new to the rescue?) and copies, and |
| 659 |
// also I guess locks at the QtQuick level. |
| 660 |
m_monitorsMutex.lock(); |
| 661 |
for (int i = 0; i < m_monitorCount; ++i) { |
| 662 |
DASSERT(m_monitors[i]); |
| 663 |
m_monitors[i]->setProcessEvent(m_processEvent); |
| 664 |
} |
| 665 |
m_monitorsMutex.unlock(); |
| 666 |
} |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
bool UMApplicationMonitor::eventFilter(QObject* object, QEvent* event) |
| 671 |
{ |
| 672 |
if (event->type() == QEvent::Show) { |
| 673 |
if (QQuickWindow* window = qobject_cast<QQuickWindow*>(object)) { |
| 674 |
Q_D(UMApplicationMonitor); |
| 675 |
d->m_monitorsMutex.lock(); |
| 676 |
d->startMonitoring(window); |
| 677 |
d->m_monitorsMutex.unlock(); |
| 678 |
} |
| 679 |
} |
| 680 |
return QObject::eventFilter(object, event); |
| 681 |
} |
| 682 |
|
| 683 |
static const char* const defaultOverlayText = |
| 684 |
"%qtVersion (%qtPlatform) - %glVersion\n" |
| 685 |
"%cpuModel\n" // FIXME(loicm) Should be included by default? |
| 686 |
"%gpuModel\r" // FIXME(loicm) Should be included by default? |
| 687 |
" Window : %9windowId \n" |
| 688 |
" Size : %9windowSize px\r" |
| 689 |
" Frame : %9frameNumber \n" |
| 690 |
// FIXME(loicm) should be removed once we have a timing histogram with swap included. |
| 691 |
" Delta n-1 : %9deltaTime ms\n" |
| 692 |
" SG sync. : %9syncTime ms\n" |
| 693 |
" SG render : %9renderTime ms\n" |
| 694 |
" GPU : %9gpuTime ms\n" |
| 695 |
" Total : %9totalTime ms\r" |
| 696 |
" VSZ mem. : %9vszMemory kB\n" |
| 697 |
" RSS mem. : %9rssMemory kB\n" |
| 698 |
" Threads : %9threadCount \n" |
| 699 |
" CPU usage : %9cpuUsage %% "; |
| 700 |
|
| 701 |
WindowMonitor::WindowMonitor( |
| 702 |
UMApplicationMonitor* applicationMonitor, QQuickWindow* window, LoggingThread* loggingThread, |
| 703 |
quint32 flags, quint32 id) |
| 704 |
: m_applicationMonitor(applicationMonitor) |
| 705 |
, m_loggingThread(loggingThread) |
| 706 |
, m_window(window) |
| 707 |
, m_overlay(defaultOverlayText, id) |
| 708 |
, m_id(id) |
| 709 |
, m_flags(flags) |
| 710 |
, m_frameSize(window->width(), window->height()) |
| 711 |
{ |
| 712 |
DASSERT(applicationMonitor == UMApplicationMonitor::instance()); |
| 713 |
DASSERT(m_applicationMonitor); |
| 714 |
DASSERT(window); |
| 715 |
DASSERT(loggingThread); |
| 716 |
|
| 717 |
moveToThread(nullptr); |
| 718 |
|
| 719 |
QObject::connect(window, SIGNAL(sceneGraphInitialized()), this, |
| 720 |
SLOT(windowSceneGraphInitialized()), Qt::DirectConnection); |
| 721 |
QObject::connect(window, SIGNAL(sceneGraphInvalidated()), this, |
| 722 |
SLOT(windowSceneGraphInvalidated()), Qt::DirectConnection); |
| 723 |
QObject::connect(window, SIGNAL(beforeSynchronizing()), this, |
| 724 |
SLOT(windowBeforeSynchronizing()), Qt::DirectConnection); |
| 725 |
QObject::connect(window, SIGNAL(afterSynchronizing()), this, |
| 726 |
SLOT(windowAfterSynchronizing()), Qt::DirectConnection); |
| 727 |
QObject::connect(window, SIGNAL(beforeRendering()), this, SLOT(windowBeforeRendering()), |
| 728 |
Qt::DirectConnection); |
| 729 |
QObject::connect(window, SIGNAL(afterRendering()), this, SLOT(windowAfterRendering()), |
| 730 |
Qt::DirectConnection); |
| 731 |
QObject::connect(window, SIGNAL(frameSwapped()), this, SLOT(windowFrameSwapped()), |
| 732 |
Qt::DirectConnection); |
| 733 |
QObject::connect(window, SIGNAL(sceneGraphAboutToStop()), this, |
| 734 |
SLOT(windowSceneGraphAboutToStop()), Qt::DirectConnection); |
| 735 |
|
| 736 |
memset(&m_frameEvent, 0, sizeof(m_frameEvent)); |
| 737 |
m_frameEvent.type = UMEvent::Frame; |
| 738 |
m_frameEvent.frame.window = id; |
| 739 |
|
| 740 |
if ((flags & UMApplicationMonitorPrivate::Logging) |
| 741 |
&& (flags & UMApplicationMonitor::WindowEvent)) { |
| 742 |
UMEvent event; |
| 743 |
event.type = UMEvent::Window; |
| 744 |
event.timeStamp = UMEventUtils::timeStamp(); |
| 745 |
event.window.id = id; |
| 746 |
event.window.width = m_frameSize.width(); |
| 747 |
event.window.height = m_frameSize.height(); |
| 748 |
event.window.state = UMWindowEvent::Shown; |
| 749 |
loggingThread->push(&event); |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
WindowMonitor::~WindowMonitor() |
| 754 |
{ |
| 755 |
DASSERT(!(m_flags & GpuResourcesInitialized)); |
| 756 |
|
| 757 |
if ((m_flags & UMApplicationMonitorPrivate::Logging) |
| 758 |
&& (m_flags & UMApplicationMonitor::WindowEvent)) { |
| 759 |
UMEvent event; |
| 760 |
event.type = UMEvent::Window; |
| 761 |
event.timeStamp = UMEventUtils::timeStamp(); |
| 762 |
event.window.id = m_id; |
| 763 |
event.window.width = m_frameSize.width(); |
| 764 |
event.window.height = m_frameSize.height(); |
| 765 |
event.window.state = UMWindowEvent::Hidden; |
| 766 |
m_loggingThread->push(&event); |
| 767 |
} |
| 768 |
|
| 769 |
m_loggingThread->deref(); |
| 770 |
} |
| 771 |
|
| 772 |
void WindowMonitor::initializeGpuResources() |
| 773 |
{ |
| 774 |
DASSERT(!(m_flags & GpuResourcesInitialized)); |
| 775 |
|
| 776 |
// FIXME(loicm) We should actually provide an API call to let the user set |
| 777 |
// that behavior programmatically. |
| 778 |
static bool noGpuTimer = qEnvironmentVariableIsSet("UM_NO_GPU_TIMER"); |
| 779 |
|
| 780 |
m_overlay.initialize(); |
| 781 |
m_gpuTimer.initialize(); |
| 782 |
m_frameEvent.frame.number = 0; |
| 783 |
m_flags |= GpuResourcesInitialized | (!noGpuTimer ? GpuTimerAvailable : 0); |
| 784 |
} |
| 785 |
|
| 786 |
void WindowMonitor::windowSceneGraphInitialized() |
| 787 |
{ |
| 788 |
if (!(m_flags & GpuResourcesInitialized)) { |
| 789 |
initializeGpuResources(); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
void WindowMonitor::finalizeGpuResources() |
| 794 |
{ |
| 795 |
DASSERT(m_flags & GpuResourcesInitialized); |
| 796 |
|
| 797 |
if (m_flags & GpuTimerAvailable) { |
| 798 |
m_gpuTimer.finalize(); |
| 799 |
} |
| 800 |
m_overlay.finalize(); |
| 801 |
|
| 802 |
m_frameEvent.frame.number = 0; |
| 803 |
m_flags &= ~(GpuResourcesInitialized | GpuTimerAvailable); |
| 804 |
} |
| 805 |
|
| 806 |
void WindowMonitor::windowSceneGraphInvalidated() |
| 807 |
{ |
| 808 |
if (m_flags & GpuResourcesInitialized) { |
| 809 |
finalizeGpuResources(); |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
void WindowMonitor::windowBeforeSynchronizing() |
| 814 |
{ |
| 815 |
if (m_flags & GpuResourcesInitialized) { |
| 816 |
m_sceneGraphTimer.start(); |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
void WindowMonitor::windowAfterSynchronizing() |
| 821 |
{ |
| 822 |
if (m_flags & GpuResourcesInitialized) { |
| 823 |
m_frameEvent.frame.syncTime = m_sceneGraphTimer.nsecsElapsed(); |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
void WindowMonitor::windowBeforeRendering() |
| 828 |
{ |
| 829 |
const QSize frameSize = m_window->size(); |
| 830 |
if (frameSize != m_frameSize) { |
| 831 |
m_frameSize = frameSize; |
| 832 |
if ((m_flags & UMApplicationMonitorPrivate::Logging) && |
| 833 |
(m_flags & UMApplicationMonitor::WindowEvent)) { |
| 834 |
UMEvent event; |
| 835 |
event.type = UMEvent::Window; |
| 836 |
event.timeStamp = UMEventUtils::timeStamp(); |
| 837 |
event.window.id = m_id; |
| 838 |
event.window.width = frameSize.width(); |
| 839 |
event.window.height = frameSize.height(); |
| 840 |
event.window.state = UMWindowEvent::Resized; |
| 841 |
m_loggingThread->push(&event); |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
if (m_flags & GpuResourcesInitialized) { |
| 846 |
m_sceneGraphTimer.start(); |
| 847 |
if (m_flags & GpuTimerAvailable) { |
| 848 |
m_gpuTimer.start(); |
| 849 |
} |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
void WindowMonitor::windowAfterRendering() |
| 854 |
{ |
| 855 |
if (m_flags & GpuResourcesInitialized) { |
| 856 |
m_frameEvent.frame.renderTime = m_sceneGraphTimer.nsecsElapsed(); |
| 857 |
m_frameEvent.frame.gpuTime = (m_flags & GpuTimerAvailable) ? m_gpuTimer.stop() : 0; |
| 858 |
m_frameEvent.frame.number++; |
| 859 |
if (m_flags & UMApplicationMonitorPrivate::Overlay) { |
| 860 |
m_mutex.lock(); |
| 861 |
m_overlay.render(m_frameEvent, m_frameSize); |
| 862 |
m_mutex.unlock(); |
| 863 |
} |
| 864 |
m_sceneGraphTimer.start(); |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
void WindowMonitor::windowFrameSwapped() |
| 869 |
{ |
| 870 |
if (m_flags & GpuResourcesInitialized) { |
| 871 |
m_frameEvent.frame.deltaTime = m_deltaTimer.isValid() ? m_deltaTimer.nsecsElapsed() : 0; |
| 872 |
m_deltaTimer.start(); |
| 873 |
if ((m_flags & UMApplicationMonitorPrivate::Logging) && |
| 874 |
(m_flags & UMApplicationMonitor::FrameEvent)) { |
| 875 |
m_frameEvent.frame.swapTime = m_sceneGraphTimer.nsecsElapsed(); |
| 876 |
m_frameEvent.timeStamp = UMEventUtils::timeStamp(); |
| 877 |
m_loggingThread->push(&m_frameEvent); |
| 878 |
} |
| 879 |
} else { |
| 880 |
initializeGpuResources(); // Get everything ready for the next frame. |
| 881 |
if (m_flags & UMApplicationMonitorPrivate::Overlay) { |
| 882 |
m_window->update(); |
| 883 |
} |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
void WindowMonitor::windowSceneGraphAboutToStop() |
| 888 |
{ |
| 889 |
#if !defined(QT_NO_DEBUG) |
| 890 |
ASSERT(m_applicationMonitor == UMApplicationMonitor::instance()); |
| 891 |
ASSERT(UMApplicationMonitorPrivate::get(m_applicationMonitor)->removeMonitor(this) == true); |
| 892 |
#else |
| 893 |
UMApplicationMonitorPrivate::get(m_applicationMonitor)->removeMonitor(this); |
| 894 |
#endif |
| 895 |
|
| 896 |
if (m_flags & GpuResourcesInitialized) { |
| 897 |
finalizeGpuResources(); |
| 898 |
} |
| 899 |
delete this; |
| 900 |
} |
| 901 |
|
| 902 |
void WindowMonitor::setProcessEvent(const UMEvent& event) |
| 903 |
{ |
| 904 |
DASSERT(event.type == UMEvent::Process); |
| 905 |
|
| 906 |
if (m_flags & UMApplicationMonitorPrivate::Overlay) { |
| 907 |
m_mutex.lock(); |
| 908 |
m_overlay.setProcessEvent(event); |
| 909 |
m_mutex.unlock(); |
| 910 |
m_window->update(); |
| 911 |
} |
| 912 |
} |