| 1 |
/* |
| 2 |
* Copyright 2014 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Author: Florian Boucault <[email protected]> |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "upmcpuusage.h" |
| 20 |
|
| 21 |
#include <unistd.h> |
| 22 |
|
| 23 |
UPMCpuUsage::UPMCpuUsage(QQuickItem *parent) : |
| 24 |
QQuickItem(parent), |
| 25 |
m_window(NULL), |
| 26 |
m_graphModel(new UPMGraphModel(this)), |
| 27 |
m_period(5000), |
| 28 |
m_samplingInterval(500), |
| 29 |
m_timeAtLastFrame(0) |
| 30 |
{ |
| 31 |
m_timingFactor = 100.0f / sysconf(_SC_NPROCESSORS_ONLN); |
| 32 |
m_previousClock = times(&m_previousTimes); |
| 33 |
|
| 34 |
QObject::connect(&m_timer, &QTimer::timeout, this, &UPMCpuUsage::appendCpuTime); |
| 35 |
m_timer.setSingleShot(false); |
| 36 |
m_timer.setInterval(m_samplingInterval); |
| 37 |
m_timer.start(); |
| 38 |
} |
| 39 |
|
| 40 |
UPMGraphModel* UPMCpuUsage::graphModel() const |
| 41 |
{ |
| 42 |
return m_graphModel; |
| 43 |
} |
| 44 |
|
| 45 |
int UPMCpuUsage::period() const |
| 46 |
{ |
| 47 |
return m_period; |
| 48 |
} |
| 49 |
|
| 50 |
void UPMCpuUsage::setPeriod(int period) |
| 51 |
{ |
| 52 |
if (period != m_period) { |
| 53 |
m_period = period; |
| 54 |
m_graphModel->setSamples(m_period / m_samplingInterval); |
| 55 |
Q_EMIT periodChanged(); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
int UPMCpuUsage::samplingInterval() const |
| 60 |
{ |
| 61 |
return m_samplingInterval; |
| 62 |
} |
| 63 |
|
| 64 |
void UPMCpuUsage::setSamplingInterval(int samplingInterval) |
| 65 |
{ |
| 66 |
if (samplingInterval != m_samplingInterval) { |
| 67 |
m_samplingInterval = samplingInterval; |
| 68 |
m_graphModel->setSamples(m_period / m_samplingInterval); |
| 69 |
m_timer.setInterval(m_samplingInterval); |
| 70 |
Q_EMIT samplingIntervalChanged(); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// FIXME: can be replaced with connecting to windowChanged() signal introduced in Qt5.2 |
| 75 |
void UPMCpuUsage::itemChange(ItemChange change, const ItemChangeData & value) |
| 76 |
{ |
| 77 |
if (change == QQuickItem::ItemSceneChange) { |
| 78 |
connectToWindow(value.window); |
| 79 |
} |
| 80 |
QQuickItem::itemChange(change, value); |
| 81 |
} |
| 82 |
|
| 83 |
void UPMCpuUsage::connectToWindow(QQuickWindow* window) |
| 84 |
{ |
| 85 |
if (window != m_window) { |
| 86 |
if (m_window != NULL) { |
| 87 |
QObject::disconnect(m_window, &QQuickWindow::beforeSynchronizing, |
| 88 |
this, &UPMCpuUsage::onFrameRendered); |
| 89 |
} |
| 90 |
|
| 91 |
if (window != NULL) { |
| 92 |
QObject::connect(window, &QQuickWindow::beforeSynchronizing, |
| 93 |
this, &UPMCpuUsage::onFrameRendered); |
| 94 |
} |
| 95 |
|
| 96 |
m_window = window; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
void UPMCpuUsage::onFrameRendered() |
| 101 |
{ |
| 102 |
/* A frame has been rendered: |
| 103 |
- if measuring CPU usage is diabled then restart it |
| 104 |
- otherwise store the time of the rendering |
| 105 |
*/ |
| 106 |
if (!m_timer.isActive()) { |
| 107 |
m_timer.start(); |
| 108 |
} else { |
| 109 |
m_timeAtLastFrame = m_timer.remainingTime(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
void UPMCpuUsage::appendCpuTime() |
| 114 |
{ |
| 115 |
// if last frame was over 80% of the timer's interval ago, then stop measuring CPU usage |
| 116 |
if (m_timeAtLastFrame >= 0.8 * m_timer.interval()) { |
| 117 |
m_timer.stop(); |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
struct tms newTimes; |
| 122 |
clock_t newClock = times(&newTimes); |
| 123 |
|
| 124 |
float elapsed = (newTimes.tms_utime + newTimes.tms_stime - m_previousTimes.tms_utime - m_previousTimes.tms_stime) |
| 125 |
/ (float)(newClock - m_previousClock); |
| 126 |
|
| 127 |
memcpy(&m_previousTimes, &newTimes, sizeof(tms)); |
| 128 |
m_previousClock = newClock; |
| 129 |
|
| 130 |
int width = ((qreal)m_graphModel->samples() / m_period) * m_samplingInterval; |
| 131 |
m_graphModel->appendValue(width, elapsed * m_timingFactor); |
| 132 |
} |