| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2019 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the examples of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:BSD$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** BSD License Usage |
| 18 |
** Alternatively, you may use this file under the terms of the BSD license |
| 19 |
** as follows: |
| 20 |
** |
| 21 |
** "Redistribution and use in source and binary forms, with or without |
| 22 |
** modification, are permitted provided that the following conditions are |
| 23 |
** met: |
| 24 |
** * Redistributions of source code must retain the above copyright |
| 25 |
** notice, this list of conditions and the following disclaimer. |
| 26 |
** * Redistributions in binary form must reproduce the above copyright |
| 27 |
** notice, this list of conditions and the following disclaimer in |
| 28 |
** the documentation and/or other materials provided with the |
| 29 |
** distribution. |
| 30 |
** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 |
** contributors may be used to endorse or promote products derived |
| 32 |
** from this software without specific prior written permission. |
| 33 |
** |
| 34 |
** |
| 35 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 |
** |
| 47 |
** $QT_END_LICENSE$ |
| 48 |
** |
| 49 |
****************************************************************************/ |
| 50 |
|
| 51 |
// Simple QRhiProfiler receiver app. Start it and then in a QRhi-based |
| 52 |
// application connect with a QTcpSocket to 127.0.0.1:30667 and set that as the |
| 53 |
// QRhiProfiler's device. |
| 54 |
|
| 55 |
#include <QTcpServer> |
| 56 |
#include <QTcpSocket> |
| 57 |
#include <QApplication> |
| 58 |
#include <QWidget> |
| 59 |
#include <QVBoxLayout> |
| 60 |
#include <QGroupBox> |
| 61 |
#include <QTextEdit> |
| 62 |
#include <QLabel> |
| 63 |
#include <QTime> |
| 64 |
#include <QtGui/private/qrhiprofiler_p.h> |
| 65 |
|
| 66 |
const int MIN_KNOWN_OP = 1; |
| 67 |
const int MAX_KNOWN_OP = 18; |
| 68 |
|
| 69 |
class Parser : public QObject |
| 70 |
{ |
| 71 |
Q_OBJECT |
| 72 |
|
| 73 |
public: |
| 74 |
void feed(const QByteArray &line); |
| 75 |
|
| 76 |
struct Event { |
| 77 |
QRhiProfiler::StreamOp op; |
| 78 |
qint64 timestamp; |
| 79 |
quint64 resource; |
| 80 |
QByteArray resourceName; |
| 81 |
|
| 82 |
struct Param { |
| 83 |
enum ValueType { |
| 84 |
Int64, |
| 85 |
Float |
| 86 |
}; |
| 87 |
QByteArray key; |
| 88 |
ValueType valueType; |
| 89 |
union { |
| 90 |
qint64 intValue; |
| 91 |
float floatValue; |
| 92 |
}; |
| 93 |
}; |
| 94 |
|
| 95 |
QVector<Param> params; |
| 96 |
|
| 97 |
const Param *param(const char *key) const { |
| 98 |
auto it = std::find_if(params.cbegin(), params.cend(), [key](const Param &p) { |
| 99 |
return !strcmp(p.key.constData(), key); |
| 100 |
}); |
| 101 |
return it == params.cend() ? nullptr : &*it; |
| 102 |
} |
| 103 |
}; |
| 104 |
|
| 105 |
signals: |
| 106 |
void eventReceived(const Event &e); |
| 107 |
}; |
| 108 |
|
| 109 |
void Parser::feed(const QByteArray &line) |
| 110 |
{ |
| 111 |
const QList<QByteArray> elems = line.split(','); |
| 112 |
if (elems.count() < 4) { |
| 113 |
qWarning("Malformed line '%s'", line.constData()); |
| 114 |
return; |
| 115 |
} |
| 116 |
bool ok = false; |
| 117 |
const int op = elems[0].toInt(&ok); |
| 118 |
if (!ok) { |
| 119 |
qWarning("Invalid op %s", elems[0].constData()); |
| 120 |
return; |
| 121 |
} |
| 122 |
if (op < MIN_KNOWN_OP || op > MAX_KNOWN_OP) { |
| 123 |
qWarning("Unknown op %d", op); |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
Event e; |
| 128 |
e.op = QRhiProfiler::StreamOp(op); |
| 129 |
e.timestamp = elems[1].toLongLong(); |
| 130 |
e.resource = elems[2].toULongLong(); |
| 131 |
e.resourceName = elems[3]; |
| 132 |
|
| 133 |
const int elemCount = elems.count(); |
| 134 |
for (int i = 4; i < elemCount; i += 2) { |
| 135 |
if (i + 1 < elemCount && !elems[i].isEmpty() && !elems[i + 1].isEmpty()) { |
| 136 |
QByteArray key = elems[i]; |
| 137 |
if (key.startsWith('F')) { |
| 138 |
key = key.mid(1); |
| 139 |
bool ok = false; |
| 140 |
const float value = elems[i + 1].toFloat(&ok); |
| 141 |
if (!ok) { |
| 142 |
qWarning("Failed to parse float %s in line '%s'", elems[i + 1].constData(), line.constData()); |
| 143 |
continue; |
| 144 |
} |
| 145 |
Event::Param param; |
| 146 |
param.key = key; |
| 147 |
param.valueType = Event::Param::Float; |
| 148 |
param.floatValue = value; |
| 149 |
e.params.append(param); |
| 150 |
} else { |
| 151 |
const qint64 value = elems[i + 1].toLongLong(); |
| 152 |
Event::Param param; |
| 153 |
param.key = key; |
| 154 |
param.valueType = Event::Param::Int64; |
| 155 |
param.intValue = value; |
| 156 |
e.params.append(param); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
emit eventReceived(e); |
| 162 |
} |
| 163 |
|
| 164 |
class Tracker : public QObject |
| 165 |
{ |
| 166 |
Q_OBJECT |
| 167 |
|
| 168 |
public slots: |
| 169 |
void handleEvent(const Parser::Event &e); |
| 170 |
|
| 171 |
signals: |
| 172 |
void buffersTouched(); |
| 173 |
void texturesTouched(); |
| 174 |
void swapchainsTouched(); |
| 175 |
void frameTimeTouched(); |
| 176 |
void gpuFrameTimeTouched(); |
| 177 |
void gpuMemAllocStatsTouched(); |
| 178 |
|
| 179 |
public: |
| 180 |
Tracker() { |
| 181 |
reset(); |
| 182 |
} |
| 183 |
|
| 184 |
static const int MAX_STAGING_SLOTS = 3; |
| 185 |
|
| 186 |
struct Buffer { |
| 187 |
Buffer() |
| 188 |
{ |
| 189 |
memset(stagingExtraSize, 0, sizeof(stagingExtraSize)); |
| 190 |
} |
| 191 |
quint64 lastTimestamp; |
| 192 |
QByteArray resourceName; |
| 193 |
qint64 effectiveSize = 0; |
| 194 |
int backingGpuBufCount = 1; |
| 195 |
qint64 stagingExtraSize[MAX_STAGING_SLOTS]; |
| 196 |
}; |
| 197 |
QHash<qint64, Buffer> m_buffers; |
| 198 |
qint64 m_totalBufferApproxByteSize; |
| 199 |
qint64 m_peakBufferApproxByteSize; |
| 200 |
qint64 m_totalStagingBufferApproxByteSize; |
| 201 |
qint64 m_peakStagingBufferApproxByteSize; |
| 202 |
|
| 203 |
struct Texture { |
| 204 |
Texture() |
| 205 |
{ |
| 206 |
memset(stagingExtraSize, 0, sizeof(stagingExtraSize)); |
| 207 |
} |
| 208 |
quint64 lastTimestamp; |
| 209 |
QByteArray resourceName; |
| 210 |
qint64 approxByteSize = 0; |
| 211 |
bool ownsNativeResource = true; |
| 212 |
qint64 stagingExtraSize[MAX_STAGING_SLOTS]; |
| 213 |
}; |
| 214 |
QHash<qint64, Texture> m_textures; |
| 215 |
qint64 m_totalTextureApproxByteSize; |
| 216 |
qint64 m_peakTextureApproxByteSize; |
| 217 |
qint64 m_totalTextureStagingBufferApproxByteSize; |
| 218 |
qint64 m_peakTextureStagingBufferApproxByteSize; |
| 219 |
|
| 220 |
struct SwapChain { |
| 221 |
quint64 lastTimestamp; |
| 222 |
QByteArray resourceName; |
| 223 |
qint64 approxByteSize = 0; |
| 224 |
}; |
| 225 |
QHash<qint64, SwapChain> m_swapchains; |
| 226 |
qint64 m_totalSwapChainApproxByteSize; |
| 227 |
qint64 m_peakSwapChainApproxByteSize; |
| 228 |
|
| 229 |
struct FrameTime { |
| 230 |
qint64 framesSinceResize = 0; |
| 231 |
int minDelta = 0; |
| 232 |
int maxDelta = 0; |
| 233 |
float avgDelta = 0; |
| 234 |
}; |
| 235 |
FrameTime m_lastFrameTime; |
| 236 |
|
| 237 |
struct GpuFrameTime { |
| 238 |
float minTime = 0; |
| 239 |
float maxTime = 0; |
| 240 |
float avgTime = 0; |
| 241 |
}; |
| 242 |
GpuFrameTime m_lastGpuFrameTime; |
| 243 |
|
| 244 |
struct GpuMemAllocStats { |
| 245 |
qint64 realAllocCount; |
| 246 |
qint64 subAllocCount; |
| 247 |
qint64 totalSize; |
| 248 |
qint64 unusedSize; |
| 249 |
}; |
| 250 |
GpuMemAllocStats m_lastGpuMemAllocStats; |
| 251 |
|
| 252 |
void reset() { |
| 253 |
m_buffers.clear(); |
| 254 |
m_textures.clear(); |
| 255 |
m_totalBufferApproxByteSize = 0; |
| 256 |
m_peakBufferApproxByteSize = 0; |
| 257 |
m_totalStagingBufferApproxByteSize = 0; |
| 258 |
m_peakStagingBufferApproxByteSize = 0; |
| 259 |
m_totalTextureApproxByteSize = 0; |
| 260 |
m_peakTextureApproxByteSize = 0; |
| 261 |
m_totalTextureStagingBufferApproxByteSize = 0; |
| 262 |
m_peakTextureStagingBufferApproxByteSize = 0; |
| 263 |
m_totalSwapChainApproxByteSize = 0; |
| 264 |
m_peakSwapChainApproxByteSize = 0; |
| 265 |
m_lastFrameTime = FrameTime(); |
| 266 |
m_lastGpuFrameTime = GpuFrameTime(); |
| 267 |
m_lastGpuMemAllocStats = GpuMemAllocStats(); |
| 268 |
emit buffersTouched(); |
| 269 |
emit texturesTouched(); |
| 270 |
emit swapchainsTouched(); |
| 271 |
emit frameTimeTouched(); |
| 272 |
emit gpuFrameTimeTouched(); |
| 273 |
emit gpuMemAllocStatsTouched(); |
| 274 |
} |
| 275 |
}; |
| 276 |
|
| 277 |
Q_DECLARE_TYPEINFO(Tracker::Buffer, Q_MOVABLE_TYPE); |
| 278 |
Q_DECLARE_TYPEINFO(Tracker::Texture, Q_MOVABLE_TYPE); |
| 279 |
Q_DECLARE_TYPEINFO(Tracker::FrameTime, Q_MOVABLE_TYPE); |
| 280 |
Q_DECLARE_TYPEINFO(Tracker::GpuFrameTime, Q_MOVABLE_TYPE); |
| 281 |
|
| 282 |
void Tracker::handleEvent(const Parser::Event &e) |
| 283 |
{ |
| 284 |
switch (e.op) { |
| 285 |
case QRhiProfiler::NewBuffer: |
| 286 |
{ |
| 287 |
Buffer b; |
| 288 |
b.lastTimestamp = e.timestamp; |
| 289 |
b.resourceName = e.resourceName; |
| 290 |
// type,0,usage,1,logical_size,84,effective_size,84,backing_gpu_buf_count,1,backing_cpu_buf_count,0 |
| 291 |
for (const Parser::Event::Param &p : e.params) { |
| 292 |
if (p.key == QByteArrayLiteral("effective_size")) |
| 293 |
b.effectiveSize = p.intValue; |
| 294 |
else if (p.key == QByteArrayLiteral("backing_gpu_buf_count")) |
| 295 |
b.backingGpuBufCount = p.intValue; |
| 296 |
} |
| 297 |
m_totalBufferApproxByteSize += b.effectiveSize * b.backingGpuBufCount; |
| 298 |
m_peakBufferApproxByteSize = qMax(m_peakBufferApproxByteSize, m_totalBufferApproxByteSize); |
| 299 |
m_buffers.insert(e.resource, b); |
| 300 |
emit buffersTouched(); |
| 301 |
} |
| 302 |
break; |
| 303 |
case QRhiProfiler::ReleaseBuffer: |
| 304 |
{ |
| 305 |
auto it = m_buffers.find(e.resource); |
| 306 |
if (it != m_buffers.end()) { |
| 307 |
m_totalBufferApproxByteSize -= it->effectiveSize * it->backingGpuBufCount; |
| 308 |
m_buffers.erase(it); |
| 309 |
emit buffersTouched(); |
| 310 |
} |
| 311 |
} |
| 312 |
break; |
| 313 |
|
| 314 |
case QRhiProfiler::NewBufferStagingArea: |
| 315 |
{ |
| 316 |
qint64 slot = -1; |
| 317 |
qint64 size = 0; |
| 318 |
for (const Parser::Event::Param &p : e.params) { |
| 319 |
if (p.key == QByteArrayLiteral("slot")) |
| 320 |
slot = p.intValue; |
| 321 |
else if (p.key == QByteArrayLiteral("size")) |
| 322 |
size = p.intValue; |
| 323 |
} |
| 324 |
if (slot >= 0 && slot < MAX_STAGING_SLOTS) { |
| 325 |
auto it = m_buffers.find(e.resource); |
| 326 |
if (it != m_buffers.end()) { |
| 327 |
it->stagingExtraSize[slot] = size; |
| 328 |
m_totalStagingBufferApproxByteSize += size; |
| 329 |
m_peakStagingBufferApproxByteSize = qMax(m_peakStagingBufferApproxByteSize, m_totalStagingBufferApproxByteSize); |
| 330 |
emit buffersTouched(); |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
break; |
| 335 |
case QRhiProfiler::ReleaseBufferStagingArea: |
| 336 |
{ |
| 337 |
qint64 slot = -1; |
| 338 |
for (const Parser::Event::Param &p : e.params) { |
| 339 |
if (p.key == QByteArrayLiteral("slot")) |
| 340 |
slot = p.intValue; |
| 341 |
} |
| 342 |
if (slot >= 0 && slot < MAX_STAGING_SLOTS) { |
| 343 |
auto it = m_buffers.find(e.resource); |
| 344 |
if (it != m_buffers.end()) { |
| 345 |
m_totalStagingBufferApproxByteSize -= it->stagingExtraSize[slot]; |
| 346 |
it->stagingExtraSize[slot] = 0; |
| 347 |
emit buffersTouched(); |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
break; |
| 352 |
|
| 353 |
case QRhiProfiler::NewTexture: |
| 354 |
{ |
| 355 |
Texture t; |
| 356 |
t.lastTimestamp = e.timestamp; |
| 357 |
t.resourceName = e.resourceName; |
| 358 |
// width,256,height,256,format,1,owns_native_resource,1,mip_count,9,layer_count,1,effective_sample_count,1,approx_byte_size,349524 |
| 359 |
for (const Parser::Event::Param &p : e.params) { |
| 360 |
if (p.key == QByteArrayLiteral("approx_byte_size")) |
| 361 |
t.approxByteSize = p.intValue; |
| 362 |
else if (p.key == QByteArrayLiteral("owns_native_resource")) |
| 363 |
t.ownsNativeResource = p.intValue; |
| 364 |
} |
| 365 |
if (t.ownsNativeResource) { |
| 366 |
m_totalTextureApproxByteSize += t.approxByteSize; |
| 367 |
m_peakTextureApproxByteSize = qMax(m_peakTextureApproxByteSize, m_totalTextureApproxByteSize); |
| 368 |
} |
| 369 |
m_textures.insert(e.resource, t); |
| 370 |
emit texturesTouched(); |
| 371 |
} |
| 372 |
break; |
| 373 |
case QRhiProfiler::ReleaseTexture: |
| 374 |
{ |
| 375 |
auto it = m_textures.find(e.resource); |
| 376 |
if (it != m_textures.end()) { |
| 377 |
if (it->ownsNativeResource) |
| 378 |
m_totalTextureApproxByteSize -= it->approxByteSize; |
| 379 |
m_textures.erase(it); |
| 380 |
emit texturesTouched(); |
| 381 |
} |
| 382 |
} |
| 383 |
break; |
| 384 |
|
| 385 |
case QRhiProfiler::NewTextureStagingArea: |
| 386 |
{ |
| 387 |
qint64 slot = -1; |
| 388 |
qint64 size = 0; |
| 389 |
for (const Parser::Event::Param &p : e.params) { |
| 390 |
if (p.key == QByteArrayLiteral("slot")) |
| 391 |
slot = p.intValue; |
| 392 |
else if (p.key == QByteArrayLiteral("size")) |
| 393 |
size = p.intValue; |
| 394 |
} |
| 395 |
if (slot >= 0 && slot < MAX_STAGING_SLOTS) { |
| 396 |
auto it = m_textures.find(e.resource); |
| 397 |
if (it != m_textures.end()) { |
| 398 |
it->stagingExtraSize[slot] = size; |
| 399 |
m_totalTextureStagingBufferApproxByteSize += size; |
| 400 |
m_peakTextureStagingBufferApproxByteSize = qMax(m_peakTextureStagingBufferApproxByteSize, m_totalTextureStagingBufferApproxByteSize); |
| 401 |
emit texturesTouched(); |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
break; |
| 406 |
case QRhiProfiler::ReleaseTextureStagingArea: |
| 407 |
{ |
| 408 |
qint64 slot = -1; |
| 409 |
for (const Parser::Event::Param &p : e.params) { |
| 410 |
if (p.key == QByteArrayLiteral("slot")) |
| 411 |
slot = p.intValue; |
| 412 |
} |
| 413 |
if (slot >= 0 && slot < MAX_STAGING_SLOTS) { |
| 414 |
auto it = m_textures.find(e.resource); |
| 415 |
if (it != m_textures.end()) { |
| 416 |
m_totalTextureStagingBufferApproxByteSize -= it->stagingExtraSize[slot]; |
| 417 |
it->stagingExtraSize[slot] = 0; |
| 418 |
emit texturesTouched(); |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
break; |
| 423 |
|
| 424 |
case QRhiProfiler::ResizeSwapChain: |
| 425 |
{ |
| 426 |
auto it = m_swapchains.find(e.resource); |
| 427 |
if (it != m_swapchains.end()) |
| 428 |
m_totalSwapChainApproxByteSize -= it->approxByteSize; |
| 429 |
|
| 430 |
SwapChain s; |
| 431 |
s.lastTimestamp = e.timestamp; |
| 432 |
s.resourceName = e.resourceName; |
| 433 |
// width,1280,height,720,buffer_count,2,msaa_buffer_count,0,effective_sample_count,1,approx_total_byte_size,7372800 |
| 434 |
for (const Parser::Event::Param &p : e.params) { |
| 435 |
if (p.key == QByteArrayLiteral("approx_total_byte_size")) |
| 436 |
s.approxByteSize = p.intValue; |
| 437 |
} |
| 438 |
m_totalSwapChainApproxByteSize += s.approxByteSize; |
| 439 |
m_peakSwapChainApproxByteSize = qMax(m_peakSwapChainApproxByteSize, m_totalSwapChainApproxByteSize); |
| 440 |
m_swapchains.insert(e.resource, s); |
| 441 |
emit swapchainsTouched(); |
| 442 |
} |
| 443 |
break; |
| 444 |
case QRhiProfiler::ReleaseSwapChain: |
| 445 |
{ |
| 446 |
auto it = m_swapchains.find(e.resource); |
| 447 |
if (it != m_swapchains.end()) { |
| 448 |
m_totalSwapChainApproxByteSize -= it->approxByteSize; |
| 449 |
m_swapchains.erase(it); |
| 450 |
emit swapchainsTouched(); |
| 451 |
} |
| 452 |
} |
| 453 |
break; |
| 454 |
|
| 455 |
case QRhiProfiler::GpuFrameTime: |
| 456 |
{ |
| 457 |
// Fmin_ms_gpu_frame_time,0.15488,Fmax_ms_gpu_frame_time,0.494592,Favg_ms_gpu_frame_time,0.33462 |
| 458 |
for (const Parser::Event::Param &p : e.params) { |
| 459 |
if (p.key == QByteArrayLiteral("min_ms_gpu_frame_time")) |
| 460 |
m_lastGpuFrameTime.minTime = p.floatValue; |
| 461 |
else if (p.key == QByteArrayLiteral("max_ms_gpu_frame_time")) |
| 462 |
m_lastGpuFrameTime.maxTime = p.floatValue; |
| 463 |
else if (p.key == QByteArrayLiteral("avg_ms_gpu_frame_time")) |
| 464 |
m_lastGpuFrameTime.avgTime = p.floatValue; |
| 465 |
} |
| 466 |
emit gpuFrameTimeTouched(); |
| 467 |
} |
| 468 |
break; |
| 469 |
case QRhiProfiler::FrameToFrameTime: |
| 470 |
{ |
| 471 |
// frames_since_resize,121,min_ms_frame_delta,9,max_ms_frame_delta,33,Favg_ms_frame_delta,16.1167 |
| 472 |
for (const Parser::Event::Param &p : e.params) { |
| 473 |
if (p.key == QByteArrayLiteral("frames_since_resize")) |
| 474 |
m_lastFrameTime.framesSinceResize = p.intValue; |
| 475 |
else if (p.key == QByteArrayLiteral("min_ms_frame_delta")) |
| 476 |
m_lastFrameTime.minDelta = p.intValue; |
| 477 |
else if (p.key == QByteArrayLiteral("max_ms_frame_delta")) |
| 478 |
m_lastFrameTime.maxDelta = p.intValue; |
| 479 |
else if (p.key == QByteArrayLiteral("avg_ms_frame_delta")) |
| 480 |
m_lastFrameTime.avgDelta = p.floatValue; |
| 481 |
} |
| 482 |
emit frameTimeTouched(); |
| 483 |
} |
| 484 |
break; |
| 485 |
|
| 486 |
case QRhiProfiler::GpuMemAllocStats: |
| 487 |
{ |
| 488 |
// real_alloc_count,2,sub_alloc_count,154,total_size,10752,unused_size,50320896 |
| 489 |
for (const Parser::Event::Param &p : e.params) { |
| 490 |
if (p.key == QByteArrayLiteral("real_alloc_count")) |
| 491 |
m_lastGpuMemAllocStats.realAllocCount = p.intValue; |
| 492 |
else if (p.key == QByteArrayLiteral("sub_alloc_count")) |
| 493 |
m_lastGpuMemAllocStats.subAllocCount = p.intValue; |
| 494 |
else if (p.key == QByteArrayLiteral("total_size")) |
| 495 |
m_lastGpuMemAllocStats.totalSize = p.intValue; |
| 496 |
else if (p.key == QByteArrayLiteral("unused_size")) |
| 497 |
m_lastGpuMemAllocStats.unusedSize = p.intValue; |
| 498 |
} |
| 499 |
emit gpuMemAllocStatsTouched(); |
| 500 |
} |
| 501 |
break; |
| 502 |
|
| 503 |
default: |
| 504 |
break; |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
class Server : public QTcpServer |
| 509 |
{ |
| 510 |
Q_OBJECT |
| 511 |
|
| 512 |
protected: |
| 513 |
void incomingConnection(qintptr socketDescriptor) override; |
| 514 |
|
| 515 |
signals: |
| 516 |
void clientConnected(); |
| 517 |
void clientDisconnected(); |
| 518 |
void receiveStarted(); |
| 519 |
void lineReceived(const QByteArray &line); |
| 520 |
|
| 521 |
private: |
| 522 |
bool m_valid = false; |
| 523 |
QTcpSocket m_socket; |
| 524 |
QByteArray m_buf; |
| 525 |
}; |
| 526 |
|
| 527 |
void Server::incomingConnection(qintptr socketDescriptor) |
| 528 |
{ |
| 529 |
if (m_valid) |
| 530 |
return; |
| 531 |
|
| 532 |
m_socket.setSocketDescriptor(socketDescriptor); |
| 533 |
m_valid = true; |
| 534 |
emit clientConnected(); |
| 535 |
connect(&m_socket, &QAbstractSocket::readyRead, this, [this] { |
| 536 |
bool receiveStartedSent = false; |
| 537 |
m_buf += m_socket.readAll(); |
| 538 |
while (m_buf.contains('\n')) { |
| 539 |
const int lfpos = m_buf.indexOf('\n'); |
| 540 |
const QByteArray line = m_buf.left(lfpos).trimmed(); |
| 541 |
m_buf = m_buf.mid(lfpos + 1); |
| 542 |
if (!receiveStartedSent) { |
| 543 |
receiveStartedSent = true; |
| 544 |
emit receiveStarted(); |
| 545 |
} |
| 546 |
emit lineReceived(line); |
| 547 |
} |
| 548 |
}); |
| 549 |
connect(&m_socket, &QAbstractSocket::disconnected, this, [this] { |
| 550 |
if (m_valid) { |
| 551 |
m_valid = false; |
| 552 |
emit clientDisconnected(); |
| 553 |
} |
| 554 |
}); |
| 555 |
} |
| 556 |
|
| 557 |
int main(int argc, char **argv) |
| 558 |
{ |
| 559 |
QApplication app(argc, argv); |
| 560 |
|
| 561 |
Tracker tracker; |
| 562 |
Parser parser; |
| 563 |
QObject::connect(&parser, &Parser::eventReceived, &tracker, &Tracker::handleEvent); |
| 564 |
|
| 565 |
Server server; |
| 566 |
if (!server.listen(QHostAddress::Any, 30667)) |
| 567 |
qFatal("Failed to start server: %s", qPrintable(server.errorString())); |
| 568 |
|
| 569 |
QVBoxLayout *layout = new QVBoxLayout; |
| 570 |
|
| 571 |
QLabel *infoLabel = new QLabel(QLatin1String("<i>Launch a Qt Quick application with QSG_RHI_PROFILE=1 and QSG_RHI_PROFILE_HOST set to the IP address.<br>" |
| 572 |
"(resource memory usage reporting works best with the Vulkan backend)</i>")); |
| 573 |
layout->addWidget(infoLabel); |
| 574 |
|
| 575 |
QGroupBox *groupBox = new QGroupBox(QLatin1String("RHI statistics")); |
| 576 |
QVBoxLayout *groupLayout = new QVBoxLayout; |
| 577 |
|
| 578 |
QLabel *buffersLabel = new QLabel; |
| 579 |
QObject::connect(&tracker, &Tracker::buffersTouched, buffersLabel, [buffersLabel, &tracker] { |
| 580 |
const QString msg = QString::asprintf("%d buffers with ca. %lld bytes of current memory (sub)allocations (peak %lld) + %lld bytes of known staging buffers (peak %lld)", |
| 581 |
tracker.m_buffers.count(), |
| 582 |
tracker.m_totalBufferApproxByteSize, tracker.m_peakBufferApproxByteSize, |
| 583 |
tracker.m_totalStagingBufferApproxByteSize, tracker.m_peakStagingBufferApproxByteSize); |
| 584 |
buffersLabel->setText(msg); |
| 585 |
}); |
| 586 |
groupLayout->addWidget(buffersLabel); |
| 587 |
|
| 588 |
QLabel *texturesLabel = new QLabel; |
| 589 |
QObject::connect(&tracker, &Tracker::texturesTouched, texturesLabel, [texturesLabel, &tracker] { |
| 590 |
const QString msg = QString::asprintf("%d textures with ca. %lld bytes of current memory (sub)allocations (peak %lld) + %lld bytes of known staging buffers (peak %lld)", |
| 591 |
tracker.m_textures.count(), |
| 592 |
tracker.m_totalTextureApproxByteSize, tracker.m_peakTextureApproxByteSize, |
| 593 |
tracker.m_totalTextureStagingBufferApproxByteSize, tracker.m_peakTextureStagingBufferApproxByteSize); |
| 594 |
texturesLabel->setText(msg); |
| 595 |
}); |
| 596 |
groupLayout->addWidget(texturesLabel); |
| 597 |
|
| 598 |
QLabel *swapchainsLabel = new QLabel; |
| 599 |
QObject::connect(&tracker, &Tracker::swapchainsTouched, swapchainsLabel, [swapchainsLabel, &tracker] { |
| 600 |
const QString msg = QString::asprintf("Estimated total swapchain color buffer size is %lld bytes (peak %lld)", |
| 601 |
tracker.m_totalSwapChainApproxByteSize, tracker.m_peakSwapChainApproxByteSize); |
| 602 |
swapchainsLabel->setText(msg); |
| 603 |
}); |
| 604 |
groupLayout->addWidget(swapchainsLabel); |
| 605 |
|
| 606 |
QLabel *frameTimeLabel = new QLabel; |
| 607 |
QObject::connect(&tracker, &Tracker::frameTimeTouched, frameTimeLabel, [frameTimeLabel, &tracker] { |
| 608 |
const QString msg = QString::asprintf("Frames since resize %lld Frame delta min %d ms max %d ms avg %f ms", |
| 609 |
tracker.m_lastFrameTime.framesSinceResize, |
| 610 |
tracker.m_lastFrameTime.minDelta, |
| 611 |
tracker.m_lastFrameTime.maxDelta, |
| 612 |
tracker.m_lastFrameTime.avgDelta); |
| 613 |
frameTimeLabel->setText(msg); |
| 614 |
}); |
| 615 |
groupLayout->addWidget(frameTimeLabel); |
| 616 |
|
| 617 |
QLabel *gpuFrameTimeLabel = new QLabel; |
| 618 |
QObject::connect(&tracker, &Tracker::gpuFrameTimeTouched, gpuFrameTimeLabel, [gpuFrameTimeLabel, &tracker] { |
| 619 |
const QString msg = QString::asprintf("GPU frame time min %f ms max %f ms avg %f ms", |
| 620 |
tracker.m_lastGpuFrameTime.minTime, |
| 621 |
tracker.m_lastGpuFrameTime.maxTime, |
| 622 |
tracker.m_lastGpuFrameTime.avgTime); |
| 623 |
gpuFrameTimeLabel->setText(msg); |
| 624 |
}); |
| 625 |
groupLayout->addWidget(gpuFrameTimeLabel); |
| 626 |
|
| 627 |
QLabel *gpuMemAllocStatsLabel = new QLabel; |
| 628 |
QObject::connect(&tracker, &Tracker::gpuMemAllocStatsTouched, gpuMemAllocStatsLabel, [gpuMemAllocStatsLabel, &tracker] { |
| 629 |
const QString msg = QString::asprintf("GPU memory allocator status: %lld real allocations %lld sub-allocations %lld total bytes %lld unused bytes", |
| 630 |
tracker.m_lastGpuMemAllocStats.realAllocCount, |
| 631 |
tracker.m_lastGpuMemAllocStats.subAllocCount, |
| 632 |
tracker.m_lastGpuMemAllocStats.totalSize, |
| 633 |
tracker.m_lastGpuMemAllocStats.unusedSize); |
| 634 |
gpuMemAllocStatsLabel->setText(msg); |
| 635 |
}); |
| 636 |
groupLayout->addWidget(gpuMemAllocStatsLabel); |
| 637 |
|
| 638 |
groupBox->setLayout(groupLayout); |
| 639 |
layout->addWidget(groupBox); |
| 640 |
|
| 641 |
QTextEdit *rawLog = new QTextEdit; |
| 642 |
rawLog->setReadOnly(true); |
| 643 |
layout->addWidget(rawLog); |
| 644 |
|
| 645 |
QObject::connect(&server, &Server::clientConnected, rawLog, [rawLog] { |
| 646 |
rawLog->append(QLatin1String("\nCONNECTED\n")); |
| 647 |
}); |
| 648 |
QObject::connect(&server, &Server::clientDisconnected, rawLog, [rawLog, &tracker] { |
| 649 |
rawLog->append(QLatin1String("\nDISCONNECTED\n")); |
| 650 |
tracker.reset(); |
| 651 |
}); |
| 652 |
QObject::connect(&server, &Server::receiveStarted, rawLog, [rawLog] { |
| 653 |
rawLog->setFontItalic(true); |
| 654 |
rawLog->append(QLatin1String("[") + QTime::currentTime().toString() + QLatin1String("]")); |
| 655 |
rawLog->setFontItalic(false); |
| 656 |
}); |
| 657 |
|
| 658 |
QObject::connect(&server, &Server::lineReceived, rawLog, [rawLog, &parser](const QByteArray &line) { |
| 659 |
rawLog->append(QString::fromUtf8(line)); |
| 660 |
parser.feed(line); |
| 661 |
}); |
| 662 |
|
| 663 |
QWidget w; |
| 664 |
w.resize(800, 600); |
| 665 |
w.setLayout(layout); |
| 666 |
w.show(); |
| 667 |
|
| 668 |
return app.exec(); |
| 669 |
} |
| 670 |
|
| 671 |
#include "qrhiprof.moc" |