summaryrefslogtreecommitdiffstats
path: root/app/perfstdin.cpp
blob: af091eb8c8ed5e211802f3772fffbd07be763029 (plain)
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
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd
** All rights reserved.
** For any questions to The Qt Company, please use contact form at http://www.qt.io/contact-us
**
** This file is part of the Qt Enterprise Perf Profiler Add-on.
**
** GNU General Public License Usage
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation and appearing in
** the file LICENSE.GPLv3 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.html.
**
** If you have questions regarding the use of this file, please use
** contact form at http://www.qt.io/contact-us
**
****************************************************************************/

#include "perfstdin.h"

#include <QTimer>

#include <cstring>
#include <cstdio>
#include <limits>

PerfStdin::PerfStdin(QObject *parent) : QIODevice(parent)
{
    connect(&m_timer, &QTimer::timeout, this, &PerfStdin::receiveData);
}

PerfStdin::~PerfStdin()
{
    if (isOpen())
        close();
}

bool PerfStdin::open(QIODevice::OpenMode mode)
{
    if (!(mode & QIODevice::ReadOnly) || (mode & QIODevice::WriteOnly))
        return false;

    if (QIODevice::open(mode)) {
        m_buffer.resize(s_minBufferSize);
        m_timer.start();
        return true;
    } else {
        return false;
    }
}

void PerfStdin::close()
{
    m_timer.stop();
    QIODevice::close();
}

qint64 PerfStdin::readData(char *data, qint64 maxlen)
{
    if (maxlen <= 0)
        return 0;

    qint64 read = 0;
    do {
        Q_ASSERT(m_buffer.length() >= m_bufferPos);
        Q_ASSERT(m_buffer.length() >= m_bufferUsed);
        Q_ASSERT(m_bufferPos <= m_bufferUsed);
        const size_t buffered = static_cast<size_t>(qMin(bufferedAvailable(), maxlen - read));
        memcpy(data + read, m_buffer.constData() + m_bufferPos, buffered);
        m_bufferPos += buffered;
        read += buffered;
    } while (read < maxlen && fillBuffer(maxlen) > 0);

    Q_ASSERT(read > 0 || bufferedAvailable() == 0);
    return (read == 0 && stdinAtEnd()) ? -1 : read;
}

qint64 PerfStdin::writeData(const char *data, qint64 len)
{
    Q_UNUSED(data);
    Q_UNUSED(len);
    return -1;
}

void PerfStdin::receiveData()
{
    if (fillBuffer() > 0)
        emit readyRead();
    else if (stdinAtEnd())
        close();
}

void PerfStdin::resizeBuffer(int newSize)
{
    QByteArray newBuffer(newSize, Qt::Uninitialized);
    std::memcpy(newBuffer.data(), m_buffer.data() + m_bufferPos,
                static_cast<size_t>(m_bufferUsed - m_bufferPos));
    qSwap(m_buffer, newBuffer);
    m_bufferUsed -= m_bufferPos;
    Q_ASSERT(m_buffer.length() >= m_bufferUsed);
    m_bufferPos = 0;
}

qint64 PerfStdin::fillBuffer(qint64 targetBufferSize)
{
    if (m_bufferUsed == m_bufferPos)
        m_bufferPos = m_bufferUsed = 0;

    targetBufferSize = qMin(targetBufferSize, static_cast<qint64>(s_maxBufferSize));
    if (targetBufferSize > m_buffer.length())
        resizeBuffer(static_cast<int>(targetBufferSize));

    if (m_bufferUsed == m_buffer.length()) {
        if (m_bufferPos == 0) {
            resizeBuffer(m_bufferUsed <= s_maxBufferSize / 2 ? m_bufferUsed * 2
                                                             : s_maxBufferSize);
        } else {
            resizeBuffer(m_bufferUsed);
        }
    }

    const size_t read = fread(m_buffer.data() + m_bufferUsed, 1,
                              static_cast<size_t>(m_buffer.length() - m_bufferUsed), stdin);
    m_bufferUsed += read;
    Q_ASSERT(m_buffer.length() >= m_bufferUsed);
    return static_cast<qint64>(read);
}

bool PerfStdin::stdinAtEnd() const
{
    return feof(stdin) || ferror(stdin);
}

bool PerfStdin::isSequential() const
{
    return true;
}

qint64 PerfStdin::bytesAvailable() const
{
    return bufferedAvailable() + QIODevice::bytesAvailable();
}