Debian Codegraph @

src/LomiriMetrics/events.cpp

raw
1 // Copyright © 2016 Canonical Ltd.
2 // Author: Loïc Molinari <[email protected]>
3 //
4 // This file is part of Lomiri UI Toolkit.
5 //
6 // Lomiri UI Toolkit is free software: you can redistribute it and/or modify it
7 // under the terms of the GNU Lesser General Public License as published by the
8 // Free Software Foundation; version 3.
9 //
10 // Lomiri UI Toolkit is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 // for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with Lomiri UI Toolkit. If not, see <http://www.gnu.org/licenses/>.
17
18 #include "events_p.h"
19
20 #include <string.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <cstdio>
24
25 #include <QtCore/QElapsedTimer>
26
27 #include "lomirimetricsglobal_p.h"
28
29 const int bufferSize = 128;
30 const int bufferAlignment = 64;
31
32 UMEventUtils::UMEventUtils()
33 : d_ptr(new EventUtilsPrivate)
34 {
35 }
36
37 EventUtilsPrivate::EventUtilsPrivate()
38 {
39 #if !defined(QT_NO_DEBUG)
40 ASSERT(m_buffer = static_cast<char*>(alignedAlloc(bufferAlignment, bufferSize)));
41 #else
42 m_buffer = static_cast<char*>(alignedAlloc(bufferAlignment, bufferSize));
43 #endif
44 m_cpuTimer.start();
45 m_cpuTicks = times(&m_cpuTimes);
46 m_cpuOnlineCores = sysconf(_SC_NPROCESSORS_ONLN);
47 m_pageSize = sysconf(_SC_PAGESIZE);
48 }
49
50 UMEventUtils::~UMEventUtils()
51 {
52 delete d_ptr;
53 }
54
55 EventUtilsPrivate::~EventUtilsPrivate()
56 {
57 free(m_buffer);
58 }
59
60 void UMEventUtils::updateProcessEvent(UMEvent* event)
61 {
62 DASSERT(event);
63 Q_D(EventUtils);
64
65 event->type = UMEvent::Process;
66 event->timeStamp = UMEventUtils::timeStamp();
67 d->updateCpuUsage(event);
68 d->updateProcStatMetrics(event);
69 }
70
71 void EventUtilsPrivate::updateCpuUsage(UMEvent* event)
72 {
73 // times() is a Linux syscall giving CPU times used by the process. The
74 // granularity of the unit returned by the (some?) kernel (clock ticks)
75 // prevents us from getting precise timings at a high frequency, so we have
76 // to throttle to 200 ms (5 Hz).
77 const qint64 throttlingFrequency = 200;
78 if (m_cpuTimer.elapsed() > throttlingFrequency) {
79 struct tms newCpuTimes;
80 const clock_t newTicks = times(&newCpuTimes);
81 const clock_t ticks = newTicks - m_cpuTicks;
82 const clock_t userTime = newCpuTimes.tms_utime - m_cpuTimes.tms_utime;
83 const clock_t systemTime = newCpuTimes.tms_stime - m_cpuTimes.tms_stime;
84 event->process.cpuUsage = ((userTime + systemTime) * 100) / (ticks * m_cpuOnlineCores);
85 m_cpuTimer.start();
86 memcpy(&m_cpuTimes, &newCpuTimes, sizeof(struct tms));
87 m_cpuTicks = newTicks;
88 }
89 }
90
91 void EventUtilsPrivate::updateProcStatMetrics(UMEvent* event)
92 {
93 int fd = open("/proc/self/stat", O_RDONLY);
94 if (fd == -1) {
95 DWARN("EventUtils: can't open '/proc/self/stat'");
96 return;
97 }
98 int readSize;
99 if ((readSize = read(fd, m_buffer, bufferSize)) == 0) {
100 DWARN("EventUtils: can't read '/proc/self/stat'");
101 close(fd);
102 return;
103 }
104
105 // Entries starting from 1 (as listed by 'man proc').
106 const int numThreadsEntry = 20;
107 const int vsizeEntry = 23;
108 const int rssEntry = 24;
109 const int lastEntry = rssEntry;
110
111 // Get the indices of num_threads, vsize and rss entries and check if
112 // the buffer is big enough.
113 int sourceIndex = 0, spaceCount = 0;
114 quint16 entryIndices[lastEntry + 1];
115 entryIndices[sourceIndex] = 0;
116 while (spaceCount < lastEntry) {
117 if (sourceIndex < readSize) {
118 if (m_buffer[sourceIndex++] == ' ') {
119 entryIndices[++spaceCount] = sourceIndex;
120 }
121 } else {
122 DASSERT(readSize == bufferSize); // Missing entries in /proc/self/stat.
123 DNOT_REACHED(); // Consider increasing bufferSize.
124 close(fd);
125 return;
126 }
127 }
128
129 unsigned long vsize;
130 long threadCount, rss;
131 #if !defined(QT_NO_DEBUG)
132 int value = sscanf(&m_buffer[entryIndices[numThreadsEntry-1]], "%ld", &threadCount);
133 ASSERT(value == 1);
134 value = sscanf(&m_buffer[entryIndices[vsizeEntry-1]], "%lu %ld", &vsize, &rss);
135 ASSERT(value == 2);
136 #else
137 sscanf(&m_buffer[entryIndices[numThreadsEntry-1]], "%ld", &threadCount);
138 sscanf(&m_buffer[entryIndices[vsizeEntry-1]], "%lu %ld", &vsize, &rss);
139 #endif
140
141 event->process.vszMemory = vsize >> 10;
142 event->process.rssMemory = (rss * m_pageSize) >> 10;
143 event->process.threadCount = threadCount;
144
145 close(fd);
146 }
147
148 // static.
149 quint64 UMEventUtils::timeStamp()
150 {
151 static QElapsedTimer timer;
152
153 if (Q_LIKELY(timer.isValid())) {
154 return timer.nsecsElapsed();
155 } else {
156 timer.start();
157 return 0;
158 }
159 }