Imported Upstream version 1.1.0
[gammaray-debian.git] / tools / timertop / timerinfo.cpp
blob08aef0acd3aa112c7d7928e4a9cdcdaf758dbf29
1 /*
2 timerinfo.cpp
4 This file is part of GammaRay, the Qt application inspection and
5 manipulation tool.
7 Copyright (C) 2010-2011 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8 Author: Thomas McGuire <thomas.mcguire@kdab.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "timerinfo.h"
25 #include <util.h>
27 #include <QObject>
29 using namespace GammaRay;
31 static const int maxTimeoutEvents = 1000;
32 static const int maxTimeSpan = 10000;
34 TimerInfo::TimerInfo(QTimer *timer)
35 : m_type(QTimerType),
36 m_timer(timer),
37 m_timerId(timer->timerId()),
38 m_totalWakeups(0),
39 m_lastReceiver(0)
43 TimerInfo::TimerInfo(int timerId)
44 : m_type(QObjectType),
45 m_timerId(timerId),
46 m_totalWakeups(0)
50 void TimerInfo::addEvent(const TimeoutEvent &timeoutEvent)
52 m_timeoutEvents.append(timeoutEvent);
53 removeOldEvents();
54 m_totalWakeups++;
57 int TimerInfo::numEvents() const
59 return m_timeoutEvents.size();
62 QTimer *TimerInfo::timer() const
64 return m_timer;
67 int TimerInfo::timerId() const
69 return m_timerId;
72 FunctionCallTimer *TimerInfo::functionCallTimer()
74 return &m_functionCallTimer;
77 QString TimerInfo::wakeupsPerSec() const
79 int totalWakeups = 0;
80 int start = 0;
81 int end = m_timeoutEvents.size() - 1;
82 for (int i = end; i >= 0; i--) {
83 const TimeoutEvent &event = m_timeoutEvents.at(i);
84 if (event.timeStamp.msecsTo(QTime::currentTime()) > maxTimeSpan) {
85 start = i;
86 break;
88 totalWakeups++;
91 if (totalWakeups > 0 && end > start) {
92 const QTime startTime = m_timeoutEvents[start].timeStamp;
93 const QTime endTime = m_timeoutEvents[end].timeStamp;
94 const int timeSpan = startTime.msecsTo(endTime);
95 const float wakeupsPerSec = totalWakeups / (float)timeSpan * 1000.0f;
96 return QString::number(wakeupsPerSec, 'f', 1);
98 return "0";
101 QString TimerInfo::timePerWakeup() const
103 if (m_type == QObjectType) {
104 return "N/A";
107 int totalWakeups = 0;
108 int totalTime = 0;
109 for (int i = m_timeoutEvents.size() - 1; i >= 0; i--) {
110 const TimeoutEvent &event = m_timeoutEvents.at(i);
111 if (event.timeStamp.msecsTo(QTime::currentTime()) > maxTimeSpan) {
112 break;
114 totalWakeups++;
115 totalTime += event.executionTime;
118 if (totalWakeups > 0) {
119 return QString::number(totalTime / (float)totalWakeups, 'f', 1);
121 return "N/A";
124 QString TimerInfo::maxWakeupTime() const
126 if (m_type == QObjectType) {
127 return "N/A";
130 int max = 0;
131 for (int i = 0; i < m_timeoutEvents.size(); i++) {
132 const TimeoutEvent &event = m_timeoutEvents.at(i);
133 if (event.executionTime > max) {
134 max = event.executionTime;
137 return QString::number(max);
140 int TimerInfo::totalWakeups() const
142 return m_totalWakeups;
145 QString TimerInfo::state() const
147 if (!m_timer){
148 return QObject::tr("None");
151 if (!m_timer->isActive()) {
152 return QObject::tr("Inactive");
153 } else {
154 if (m_timer->isSingleShot()) {
155 return QObject::tr("Singleshot (%1 ms)").arg(m_timer->interval());
156 } else {
157 return QObject::tr("Repeating (%1 ms)").arg(m_timer->interval());
162 void TimerInfo::removeOldEvents()
164 if (m_timeoutEvents.size() > maxTimeoutEvents) {
165 m_timeoutEvents.removeFirst();
169 void TimerInfo::setLastReceiver(QObject *receiver)
171 m_lastReceiver = receiver;
174 QString TimerInfo::displayName() const
176 if (timer()) {
177 return Util::displayString(timer());
178 } else {
179 if (m_lastReceiver) {
180 return Util::displayString(m_lastReceiver);
181 } else {
182 return QObject::tr("Unknown QObject");