kis_update_time_monitor.cpp 5.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 *  Copyright (c) 2011 Dmitry Kazakov <dimula73@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kis_update_time_monitor.h"

21
#include <QGlobalStatic>
22 23 24 25 26 27 28 29 30 31 32
#include <QHash>
#include <QSet>
#include <QMutex>
#include <QMutexLocker>
#include <QPointF>
#include <QRect>
#include <QRegion>
#include <QFile>
#include <QDir>

#include <QElapsedTimer>
33

34 35 36 37 38 39 40
#include <QFileInfo>

#include "kis_debug.h"
#include "kis_global.h"
#include "kis_image_config.h"


Boudewijn Rempt's avatar
Boudewijn Rempt committed
41
#include <brushengine/kis_paintop_preset.h>
42

43 44
Q_GLOBAL_STATIC(KisUpdateTimeMonitor, s_instance)

45 46 47

struct StrokeTicket
{
48 49 50
    StrokeTicket()
        : m_jobTime(0)
        , m_updateTime(0) {}
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

    QRegion dirtyRegion;

    void start() {
        m_timer.start();
    }

    void jobCompleted() {
        m_jobTime = m_timer.restart();
    }

    void updateCompleted() {
        m_updateTime = m_timer.restart();
    }

    qint64 jobTime() const {
        return m_jobTime;
    }

    qint64 updateTime() const {
        return m_updateTime;
    }

private:
    QElapsedTimer m_timer;
    qint64 m_jobTime;
    qint64 m_updateTime;
};

80
struct Q_DECL_HIDDEN KisUpdateTimeMonitor::Private
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
{
    Private()
        : jobsTime(0),
          responseTime(0),
          numTickets(0),
          numUpdates(0),
          mousePath(0.0),
          loggingEnabled(false)
    {
        loggingEnabled = KisImageConfig().enablePerfLog();
    }

    QHash<void*, StrokeTicket*> preliminaryTickets;
    QSet<StrokeTicket*> finishedTickets;

    qint64 jobsTime;
    qint64 responseTime;
    qint32 numTickets;
    qint32 numUpdates;
    QMutex mutex;

    qreal mousePath;
    QPointF lastMousePos;
    QElapsedTimer strokeTime;
    KisPaintOpPresetSP preset;

    bool loggingEnabled;
};

KisUpdateTimeMonitor::KisUpdateTimeMonitor()
    : m_d(new Private)
{
    if (m_d->loggingEnabled) {
        QDir dir;
        if (dir.exists("log")) {
            dir.remove("log");
        }
        dir.mkdir("log");
    }
}

KisUpdateTimeMonitor::~KisUpdateTimeMonitor()
{
    delete m_d;
}

KisUpdateTimeMonitor* KisUpdateTimeMonitor::instance()
{
    return s_instance;
}

void KisUpdateTimeMonitor::startStrokeMeasure()
{
    if (!m_d->loggingEnabled) return;

    QMutexLocker locker(&m_d->mutex);

    m_d->jobsTime = 0;
    m_d->responseTime = 0;
    m_d->numTickets = 0;
    m_d->numUpdates = 0;
    m_d->mousePath = 0;

    m_d->lastMousePos = QPointF();
    m_d->preset = 0;
    m_d->strokeTime.start();
}

void KisUpdateTimeMonitor::endStrokeMeasure()
{
    if (!m_d->loggingEnabled) return;

    QMutexLocker locker(&m_d->mutex);

    if(m_d->numTickets) {
        printValues();
    }
}

void KisUpdateTimeMonitor::reportPaintOpPreset(KisPaintOpPresetSP preset)
{
    if (!m_d->loggingEnabled) return;

    m_d->preset = preset;
}

void KisUpdateTimeMonitor::reportMouseMove(const QPointF &pos)
{
    if (!m_d->loggingEnabled) return;

    QMutexLocker locker(&m_d->mutex);

    if (!m_d->lastMousePos.isNull()) {
        qreal distance = kisDistance(m_d->lastMousePos, pos);
        m_d->mousePath += distance;
    }

    m_d->lastMousePos = pos;
}

void KisUpdateTimeMonitor::printValues()
{
    qint64 strokeTime = m_d->strokeTime.elapsed();
    qreal responseTime = qreal(m_d->responseTime) / m_d->numTickets;
    qreal nonUpdateTime = qreal(m_d->jobsTime) / m_d->numTickets;
    qreal jobsPerUpdate = qreal(m_d->numTickets) / m_d->numUpdates;
    qreal mouseSpeed = qreal(m_d->mousePath) / strokeTime;

    QString prefix;

    if (m_d->preset) {
        KisPaintOpPresetSP preset = m_d->preset->clone();
        prefix = QString("%1.").arg(preset->name());
        preset->setFilename(QString("log/%1.kpp").arg(preset->name()));
        preset->save();
    }

    QFile logFile(QString("log/%1stroke.rdata").arg(prefix));
    logFile.open(QIODevice::Append);
    QTextStream stream(&logFile);
    stream << mouseSpeed << "\t"
           << jobsPerUpdate << "\t"
           << nonUpdateTime << "\t"
           << responseTime << "\n";
    logFile.close();
}

void KisUpdateTimeMonitor::reportJobStarted(void *key)
{
    if (!m_d->loggingEnabled) return;

    QMutexLocker locker(&m_d->mutex);

    StrokeTicket *ticket = new StrokeTicket();
    ticket->start();

    m_d->preliminaryTickets.insert(key, ticket);
}

void KisUpdateTimeMonitor::reportJobFinished(void *key, const QVector<QRect> &rects)
{
    if (!m_d->loggingEnabled) return;

    QMutexLocker locker(&m_d->mutex);

    StrokeTicket *ticket = m_d->preliminaryTickets.take(key);
227
    if( ticket ){
228
        ticket->jobCompleted();
229

230 231 232 233
        Q_FOREACH (const QRect &rect, rects) {
            ticket->dirtyRegion += rect;
        }
        m_d->finishedTickets.insert(ticket);
234 235 236 237 238 239 240 241 242
    }
}

void KisUpdateTimeMonitor::reportUpdateFinished(const QRect &rect)
{
    if (!m_d->loggingEnabled) return;

    QMutexLocker locker(&m_d->mutex);

243
    Q_FOREACH (StrokeTicket *ticket, m_d->finishedTickets) {
244 245 246 247 248 249 250 251 252 253 254 255 256
        ticket->dirtyRegion -= rect;
        if(ticket->dirtyRegion.isEmpty()) {
            ticket->updateCompleted();
            m_d->jobsTime += ticket->jobTime();
            m_d->responseTime += ticket->jobTime() + ticket->updateTime();
            m_d->numTickets++;

            m_d->finishedTickets.remove(ticket);
            delete ticket;
        }
    }
    m_d->numUpdates++;
}