OpenCoverage

qbenchmarkperfevents.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/testlib/qbenchmarkperfevents.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 Intel Corporation.-
4** Contact: https://www.qt.io/licensing/-
5**-
6** This file is part of the QtCore module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL$-
9** Commercial License Usage-
10** Licensees holding valid commercial Qt licenses may use this file in-
11** accordance with the commercial license agreement provided with the-
12** Software or, alternatively, in accordance with the terms contained in-
13** a written agreement between you and The Qt Company. For licensing terms-
14** and conditions see https://www.qt.io/terms-conditions. For further-
15** information use the contact form at https://www.qt.io/contact-us.-
16**-
17** GNU Lesser General Public License Usage-
18** Alternatively, this file may be used under the terms of the GNU Lesser-
19** General Public License version 3 as published by the Free Software-
20** Foundation and appearing in the file LICENSE.LGPL3 included in the-
21** packaging of this file. Please review the following information to-
22** ensure the GNU Lesser General Public License version 3 requirements-
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.-
24**-
25** GNU General Public License Usage-
26** Alternatively, this file may be used under the terms of the GNU-
27** General Public License version 2.0 or (at your option) the GNU General-
28** Public license version 3 or any later version approved by the KDE Free-
29** Qt Foundation. The licenses are as published by the Free Software-
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3-
31** included in the packaging of this file. Please review the following-
32** information to ensure the GNU General Public License requirements will-
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and-
34** https://www.gnu.org/licenses/gpl-3.0.html.-
35**-
36** $QT_END_LICENSE$-
37**-
38****************************************************************************/-
39-
40#include "qbenchmarkperfevents_p.h"-
41#include "qbenchmarkmetric.h"-
42#include "qbenchmark_p.h"-
43-
44#ifdef QTESTLIB_USE_PERF_EVENTS-
45-
46// include the qcore_unix_p.h without core-private-
47// we only use inline functions anyway-
48#include "../corelib/kernel/qcore_unix_p.h"-
49-
50#include <sys/types.h>-
51#include <errno.h>-
52#include <fcntl.h>-
53#include <string.h>-
54#include <stdio.h>-
55-
56#include <sys/syscall.h>-
57#include <sys/ioctl.h>-
58-
59#include "3rdparty/linux_perf_event_p.h"-
60-
61// for PERF_TYPE_HW_CACHE, the config is a bitmask-
62// lowest 8 bits: cache type-
63// bits 8 to 15: cache operation-
64// bits 16 to 23: cache result-
65#define CACHE_L1D_READ (PERF_COUNT_HW_CACHE_L1D | PERF_COUNT_HW_CACHE_OP_READ << 8 | PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16)-
66#define CACHE_L1D_WRITE (PERF_COUNT_HW_CACHE_L1D | PERF_COUNT_HW_CACHE_OP_WRITE << 8 | PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16)-
67#define CACHE_L1D_PREFETCH (PERF_COUNT_HW_CACHE_L1D | PERF_COUNT_HW_CACHE_OP_PREFETCH << 8 | PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16)-
68#define CACHE_L1I_READ (PERF_COUNT_HW_CACHE_L1I | PERF_COUNT_HW_CACHE_OP_READ << 8 | PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16)-
69#define CACHE_L1I_PREFETCH (PERF_COUNT_HW_CACHE_L1I | PERF_COUNT_HW_CACHE_OP_PREFETCH << 8 | PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16)-
70#define CACHE_LLC_READ (PERF_COUNT_HW_CACHE_LL | PERF_COUNT_HW_CACHE_OP_READ << 8 | PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16)-
71#define CACHE_LLC_WRITE (PERF_COUNT_HW_CACHE_LL | PERF_COUNT_HW_CACHE_OP_WRITE << 8| PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16)-
72#define CACHE_LLC_PREFETCH (PERF_COUNT_HW_CACHE_LL | PERF_COUNT_HW_CACHE_OP_PREFETCH << 8 | PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16)-
73#define CACHE_L1D_READ_MISS (PERF_COUNT_HW_CACHE_L1D | PERF_COUNT_HW_CACHE_OP_READ << 8 | PERF_COUNT_HW_CACHE_RESULT_MISS << 16)-
74#define CACHE_L1D_WRITE_MISS (PERF_COUNT_HW_CACHE_L1D | PERF_COUNT_HW_CACHE_OP_WRITE << 8 | PERF_COUNT_HW_CACHE_RESULT_MISS << 16)-
75#define CACHE_L1D_PREFETCH_MISS (PERF_COUNT_HW_CACHE_L1D | PERF_COUNT_HW_CACHE_OP_PREFETCH << 8 | PERF_COUNT_HW_CACHE_RESULT_MISS << 16)-
76#define CACHE_L1I_READ_MISS (PERF_COUNT_HW_CACHE_L1I | PERF_COUNT_HW_CACHE_OP_READ << 8 | PERF_COUNT_HW_CACHE_RESULT_MISS << 16)-
77#define CACHE_L1I_PREFETCH_MISS (PERF_COUNT_HW_CACHE_L1I | PERF_COUNT_HW_CACHE_OP_PREFETCH << 8 | PERF_COUNT_HW_CACHE_RESULT_MISS << 16)-
78#define CACHE_LLC_READ_MISS (PERF_COUNT_HW_CACHE_LL | PERF_COUNT_HW_CACHE_OP_READ << 8 | PERF_COUNT_HW_CACHE_RESULT_MISS << 16)-
79#define CACHE_LLC_WRITE_MISS (PERF_COUNT_HW_CACHE_LL | PERF_COUNT_HW_CACHE_OP_WRITE << 8 | PERF_COUNT_HW_CACHE_RESULT_MISS << 16)-
80#define CACHE_LLC_PREFETCH_MISS (PERF_COUNT_HW_CACHE_LL | PERF_COUNT_HW_CACHE_OP_PREFETCH << 8 | PERF_COUNT_HW_CACHE_RESULT_MISS << 16)-
81#define CACHE_BRANCH_READ (PERF_COUNT_HW_CACHE_BPU | PERF_COUNT_HW_CACHE_OP_READ << 8 | PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16)-
82#define CACHE_BRANCH_READ_MISS (PERF_COUNT_HW_CACHE_BPU | PERF_COUNT_HW_CACHE_OP_READ << 8 | PERF_COUNT_HW_CACHE_RESULT_MISS << 16)-
83-
84QT_BEGIN_NAMESPACE-
85-
86static perf_event_attr attr;-
87-
88static void initPerf()-
89{-
90 static bool done;-
91 if (!done) {
!doneDescription
TRUEnever evaluated
FALSEnever evaluated
0
92 memset(&attr, 0, sizeof attr);-
93 attr.size = sizeof attr;-
94 attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING;-
95 attr.disabled = true; // we'll enable later-
96 attr.inherit = true; // let children processes inherit the monitoring-
97 attr.pinned = true; // keep it running in the hardware-
98 attr.inherit_stat = true; // aggregate all the info from child processes-
99 attr.task = true; // trace fork/exits-
100-
101 // set a default performance counter: CPU cycles-
102 attr.type = PERF_TYPE_HARDWARE;-
103 attr.config = PERF_COUNT_HW_CPU_CYCLES; // default-
104-
105 done = true;-
106 }
never executed: end of block
0
107}
never executed: end of block
0
108-
109/*!-
110 \class QBenchmarkPerfEvents-
111 \brief The Linux perf events benchmark backend-
112-
113 This benchmark backend uses the Linux Performance Counters interface,-
114 introduced with the Linux kernel v2.6.31. The interface is done by one-
115 system call (perf_event_open) which takes an attribute structure and-
116 returns a file descriptor.-
117-
118 More information:-
119 \li design docs: tools/perf/design.txt <http://lxr.linux.no/linux/tools/perf/design.txt>-
120 \li sample tool: tools/perf/builtin-stat.c <http://lxr.linux.no/linux/tools/perf/builtin-stat.c>-
121 (note: as of v3.3.1, the documentation is out-of-date with the kernel-
122 interface, so reading the source code of existing tools is necessary)-
123-
124 This benchlib backend monitors the current process as well as child process-
125 launched. We do not try to benchmark in kernel or hypervisor mode, as that-
126 usually requires elevated privileges.-
127 */-
128-
129static int perf_event_open(perf_event_attr *attr, pid_t pid, int cpu, int group_fd, unsigned long flags)-
130{-
131#ifdef SYS_perf_event_open-
132 return syscall(SYS_perf_event_open, attr, pid, cpu, group_fd, flags);
never executed: return syscall(298, attr, pid, cpu, group_fd, flags);
0
133#else-
134 Q_UNUSED(attr);-
135 Q_UNUSED(pid);-
136 Q_UNUSED(cpu);-
137 Q_UNUSED(group_fd);-
138 Q_UNUSED(flags);-
139 errno = ENOSYS;-
140 return -1;-
141#endif-
142}-
143-
144bool QBenchmarkPerfEventsMeasurer::isAvailable()-
145{-
146 // this generates an EFAULT because attr == NULL if perf_event_open is available-
147 // if the kernel is too old, it generates ENOSYS-
148 return perf_event_open(0, 0, 0, 0, 0) == -1 && errno != ENOSYS;
never executed: return perf_event_open(0, 0, 0, 0, 0) == -1 && (*__errno_location ()) != 38;
0
149}-
150-
151/* Event list structure-
152 The following table provides the list of supported events-
153-
154 Event type Event counter Unit Name and aliases-
155 HARDWARE CPU_CYCLES CPUCycles cycles cpu-cycles-
156 HARDWARE INSTRUCTIONS Instructions instructions-
157 HARDWARE CACHE_REFERENCES CacheReferences cache-references-
158 HARDWARE CACHE_MISSES CacheMisses cache-misses-
159 HARDWARE BRANCH_INSTRUCTIONS BranchInstructions branch-instructions branches-
160 HARDWARE BRANCH_MISSES BranchMisses branch-misses-
161 HARDWARE BUS_CYCLES BusCycles bus-cycles-
162 HARDWARE STALLED_CYCLES_FRONTEND StalledCycles stalled-cycles-frontend idle-cycles-frontend-
163 HARDWARE STALLED_CYCLES_BACKEND StalledCycles stalled-cycles-backend idle-cycles-backend-
164 SOFTWARE CPU_CLOCK WalltimeMilliseconds cpu-clock-
165 SOFTWARE TASK_CLOCK WalltimeMilliseconds task-clock-
166 SOFTWARE PAGE_FAULTS PageFaults page-faults faults-
167 SOFTWARE PAGE_FAULTS_MAJ MajorPageFaults major-faults-
168 SOFTWARE PAGE_FAULTS_MIN MinorPageFaults minor-faults-
169 SOFTWARE CONTEXT_SWITCHES ContextSwitches context-switches cs-
170 SOFTWARE CPU_MIGRATIONS CPUMigrations cpu-migrations migrations-
171 SOFTWARE ALIGNMENT_FAULTS AlignmentFaults alignment-faults-
172 SOFTWARE EMULATION_FAULTS EmulationFaults emulation-faults-
173 HW_CACHE L1D_READ CacheReads l1d-cache-reads l1d-cache-loads l1d-reads l1d-loads-
174 HW_CACHE L1D_WRITE CacheWrites l1d-cache-writes l1d-cache-stores l1d-writes l1d-stores-
175 HW_CACHE L1D_PREFETCH CachePrefetches l1d-cache-prefetches l1d-prefetches-
176 HW_CACHE L1I_READ CacheReads l1i-cache-reads l1i-cache-loads l1i-reads l1i-loads-
177 HW_CACHE L1I_PREFETCH CachePrefetches l1i-cache-prefetches l1i-prefetches-
178 HW_CACHE LLC_READ CacheReads llc-cache-reads llc-cache-loads llc-loads llc-reads-
179 HW_CACHE LLC_WRITE CacheWrites llc-cache-writes llc-cache-stores llc-writes llc-stores-
180 HW_CACHE LLC_PREFETCH CachePrefetches llc-cache-prefetches llc-prefetches-
181 HW_CACHE L1D_READ_MISS CacheReads l1d-cache-read-misses l1d-cache-load-misses l1d-read-misses l1d-load-misses-
182 HW_CACHE L1D_WRITE_MISS CacheWrites l1d-cache-write-misses l1d-cache-store-misses l1d-write-misses l1d-store-misses-
183 HW_CACHE L1D_PREFETCH_MISS CachePrefetches l1d-cache-prefetch-misses l1d-prefetch-misses-
184 HW_CACHE L1I_READ_MISS CacheReads l1i-cache-read-misses l1i-cache-load-misses l1i-read-misses l1i-load-misses-
185 HW_CACHE L1I_PREFETCH_MISS CachePrefetches l1i-cache-prefetch-misses l1i-prefetch-misses-
186 HW_CACHE LLC_READ_MISS CacheReads llc-cache-read-misses llc-cache-load-misses llc-read-misses llc-load-misses-
187 HW_CACHE LLC_WRITE_MISS CacheWrites llc-cache-write-misses llc-cache-store-misses llc-write-misses llc-store-misses-
188 HW_CACHE LLC_PREFETCH_MISS CachePrefetches llc-cache-prefetch-misses llc-prefetch-misses-
189 HW_CACHE BRANCH_READ BranchInstructions branch-reads branch-loads branch-predicts-
190 HW_CACHE BRANCH_READ_MISS BranchMisses branch-mispredicts branch-read-misses branch-load-misses-
191-
192 Use the following Perl script to re-generate the list-
193=== cut perl ===-
194#!/usr/bin/env perl-
195# Load all entries into %map-
196while (<STDIN>) {-
197 m/^\s*(.*)\s*$/;-
198 @_ = split /\s+/, $1;-
199 $type = shift @_;-
200 $id = ($type eq "HARDWARE" ? "PERF_COUNT_HW_" :-
201 $type eq "SOFTWARE" ? "PERF_COUNT_SW_" :-
202 $type eq "HW_CACHE" ? "CACHE_" : "") . shift @_;-
203 $unit = shift @_;-
204-
205 for $string (@_) {-
206 die "$string was already seen!" if defined($map{$string});-
207 $map{$string} = [-1, $type, $id, $unit];-
208 push @strings, $string;-
209 }-
210}-
211-
212# sort the map and print the string list-
213@strings = sort @strings;-
214print "static const char eventlist_strings[] = \n";-
215$counter = 0;-
216for $entry (@strings) {-
217 print " \"$entry\\0\"\n";-
218 $map{$entry}[0] = $counter;-
219 $counter += 1 + length $entry;-
220}-
221-
222# print the table-
223print " \"\\0\";\n\nstatic const Events eventlist[] = {\n";-
224for $entry (sort @strings) {-
225 printf " { %3d, PERF_TYPE_%s, %s, QTest::%s },\n",-
226 $map{$entry}[0],-
227 $map{$entry}[1],-
228 $map{$entry}[2],-
229 $map{$entry}[3];-
230}-
231print " { 0, PERF_TYPE_MAX, 0, QTest::Events }\n};\n";-
232=== cut perl ===-
233*/-
234-
235struct Events {-
236 unsigned offset;-
237 quint32 type;-
238 quint64 event_id;-
239 QTest::QBenchmarkMetric metric;-
240};-
241-
242/* -- BEGIN GENERATED CODE -- */-
243static const char eventlist_strings[] =-
244 "alignment-faults\0"-
245 "branch-instructions\0"-
246 "branch-load-misses\0"-
247 "branch-loads\0"-
248 "branch-mispredicts\0"-
249 "branch-misses\0"-
250 "branch-predicts\0"-
251 "branch-read-misses\0"-
252 "branch-reads\0"-
253 "branches\0"-
254 "bus-cycles\0"-
255 "cache-misses\0"-
256 "cache-references\0"-
257 "context-switches\0"-
258 "cpu-clock\0"-
259 "cpu-cycles\0"-
260 "cpu-migrations\0"-
261 "cs\0"-
262 "cycles\0"-
263 "emulation-faults\0"-
264 "faults\0"-
265 "idle-cycles-backend\0"-
266 "idle-cycles-frontend\0"-
267 "instructions\0"-
268 "l1d-cache-load-misses\0"-
269 "l1d-cache-loads\0"-
270 "l1d-cache-prefetch-misses\0"-
271 "l1d-cache-prefetches\0"-
272 "l1d-cache-read-misses\0"-
273 "l1d-cache-reads\0"-
274 "l1d-cache-store-misses\0"-
275 "l1d-cache-stores\0"-
276 "l1d-cache-write-misses\0"-
277 "l1d-cache-writes\0"-
278 "l1d-load-misses\0"-
279 "l1d-loads\0"-
280 "l1d-prefetch-misses\0"-
281 "l1d-prefetches\0"-
282 "l1d-read-misses\0"-
283 "l1d-reads\0"-
284 "l1d-store-misses\0"-
285 "l1d-stores\0"-
286 "l1d-write-misses\0"-
287 "l1d-writes\0"-
288 "l1i-cache-load-misses\0"-
289 "l1i-cache-loads\0"-
290 "l1i-cache-prefetch-misses\0"-
291 "l1i-cache-prefetches\0"-
292 "l1i-cache-read-misses\0"-
293 "l1i-cache-reads\0"-
294 "l1i-load-misses\0"-
295 "l1i-loads\0"-
296 "l1i-prefetch-misses\0"-
297 "l1i-prefetches\0"-
298 "l1i-read-misses\0"-
299 "l1i-reads\0"-
300 "llc-cache-load-misses\0"-
301 "llc-cache-loads\0"-
302 "llc-cache-prefetch-misses\0"-
303 "llc-cache-prefetches\0"-
304 "llc-cache-read-misses\0"-
305 "llc-cache-reads\0"-
306 "llc-cache-store-misses\0"-
307 "llc-cache-stores\0"-
308 "llc-cache-write-misses\0"-
309 "llc-cache-writes\0"-
310 "llc-load-misses\0"-
311 "llc-loads\0"-
312 "llc-prefetch-misses\0"-
313 "llc-prefetches\0"-
314 "llc-read-misses\0"-
315 "llc-reads\0"-
316 "llc-store-misses\0"-
317 "llc-stores\0"-
318 "llc-write-misses\0"-
319 "llc-writes\0"-
320 "major-faults\0"-
321 "migrations\0"-
322 "minor-faults\0"-
323 "page-faults\0"-
324 "stalled-cycles-backend\0"-
325 "stalled-cycles-frontend\0"-
326 "task-clock\0"-
327 "\0";-
328-
329static const Events eventlist[] = {-
330 { 0, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS, QTest::AlignmentFaults },-
331 { 17, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS, QTest::BranchInstructions },-
332 { 37, PERF_TYPE_HW_CACHE, CACHE_BRANCH_READ_MISS, QTest::BranchMisses },-
333 { 56, PERF_TYPE_HW_CACHE, CACHE_BRANCH_READ, QTest::BranchInstructions },-
334 { 69, PERF_TYPE_HW_CACHE, CACHE_BRANCH_READ_MISS, QTest::BranchMisses },-
335 { 88, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES, QTest::BranchMisses },-
336 { 102, PERF_TYPE_HW_CACHE, CACHE_BRANCH_READ, QTest::BranchInstructions },-
337 { 118, PERF_TYPE_HW_CACHE, CACHE_BRANCH_READ_MISS, QTest::BranchMisses },-
338 { 137, PERF_TYPE_HW_CACHE, CACHE_BRANCH_READ, QTest::BranchInstructions },-
339 { 150, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS, QTest::BranchInstructions },-
340 { 159, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES, QTest::BusCycles },-
341 { 170, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES, QTest::CacheMisses },-
342 { 183, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES, QTest::CacheReferences },-
343 { 200, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES, QTest::ContextSwitches },-
344 { 217, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK, QTest::WalltimeMilliseconds },-
345 { 227, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES, QTest::CPUCycles },-
346 { 238, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS, QTest::CPUMigrations },-
347 { 253, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES, QTest::ContextSwitches },-
348 { 256, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES, QTest::CPUCycles },-
349 { 263, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS, QTest::EmulationFaults },-
350 { 280, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS, QTest::PageFaults },-
351 { 287, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND, QTest::StalledCycles },-
352 { 307, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND, QTest::StalledCycles },-
353 { 328, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS, QTest::Instructions },-
354 { 341, PERF_TYPE_HW_CACHE, CACHE_L1D_READ_MISS, QTest::CacheReads },-
355 { 363, PERF_TYPE_HW_CACHE, CACHE_L1D_READ, QTest::CacheReads },-
356 { 379, PERF_TYPE_HW_CACHE, CACHE_L1D_PREFETCH_MISS, QTest::CachePrefetches },-
357 { 405, PERF_TYPE_HW_CACHE, CACHE_L1D_PREFETCH, QTest::CachePrefetches },-
358 { 426, PERF_TYPE_HW_CACHE, CACHE_L1D_READ_MISS, QTest::CacheReads },-
359 { 448, PERF_TYPE_HW_CACHE, CACHE_L1D_READ, QTest::CacheReads },-
360 { 464, PERF_TYPE_HW_CACHE, CACHE_L1D_WRITE_MISS, QTest::CacheWrites },-
361 { 487, PERF_TYPE_HW_CACHE, CACHE_L1D_WRITE, QTest::CacheWrites },-
362 { 504, PERF_TYPE_HW_CACHE, CACHE_L1D_WRITE_MISS, QTest::CacheWrites },-
363 { 527, PERF_TYPE_HW_CACHE, CACHE_L1D_WRITE, QTest::CacheWrites },-
364 { 544, PERF_TYPE_HW_CACHE, CACHE_L1D_READ_MISS, QTest::CacheReads },-
365 { 560, PERF_TYPE_HW_CACHE, CACHE_L1D_READ, QTest::CacheReads },-
366 { 570, PERF_TYPE_HW_CACHE, CACHE_L1D_PREFETCH_MISS, QTest::CachePrefetches },-
367 { 590, PERF_TYPE_HW_CACHE, CACHE_L1D_PREFETCH, QTest::CachePrefetches },-
368 { 605, PERF_TYPE_HW_CACHE, CACHE_L1D_READ_MISS, QTest::CacheReads },-
369 { 621, PERF_TYPE_HW_CACHE, CACHE_L1D_READ, QTest::CacheReads },-
370 { 631, PERF_TYPE_HW_CACHE, CACHE_L1D_WRITE_MISS, QTest::CacheWrites },-
371 { 648, PERF_TYPE_HW_CACHE, CACHE_L1D_WRITE, QTest::CacheWrites },-
372 { 659, PERF_TYPE_HW_CACHE, CACHE_L1D_WRITE_MISS, QTest::CacheWrites },-
373 { 676, PERF_TYPE_HW_CACHE, CACHE_L1D_WRITE, QTest::CacheWrites },-
374 { 687, PERF_TYPE_HW_CACHE, CACHE_L1I_READ_MISS, QTest::CacheReads },-
375 { 709, PERF_TYPE_HW_CACHE, CACHE_L1I_READ, QTest::CacheReads },-
376 { 725, PERF_TYPE_HW_CACHE, CACHE_L1I_PREFETCH_MISS, QTest::CachePrefetches },-
377 { 751, PERF_TYPE_HW_CACHE, CACHE_L1I_PREFETCH, QTest::CachePrefetches },-
378 { 772, PERF_TYPE_HW_CACHE, CACHE_L1I_READ_MISS, QTest::CacheReads },-
379 { 794, PERF_TYPE_HW_CACHE, CACHE_L1I_READ, QTest::CacheReads },-
380 { 810, PERF_TYPE_HW_CACHE, CACHE_L1I_READ_MISS, QTest::CacheReads },-
381 { 826, PERF_TYPE_HW_CACHE, CACHE_L1I_READ, QTest::CacheReads },-
382 { 836, PERF_TYPE_HW_CACHE, CACHE_L1I_PREFETCH_MISS, QTest::CachePrefetches },-
383 { 856, PERF_TYPE_HW_CACHE, CACHE_L1I_PREFETCH, QTest::CachePrefetches },-
384 { 871, PERF_TYPE_HW_CACHE, CACHE_L1I_READ_MISS, QTest::CacheReads },-
385 { 887, PERF_TYPE_HW_CACHE, CACHE_L1I_READ, QTest::CacheReads },-
386 { 897, PERF_TYPE_HW_CACHE, CACHE_LLC_READ_MISS, QTest::CacheReads },-
387 { 919, PERF_TYPE_HW_CACHE, CACHE_LLC_READ, QTest::CacheReads },-
388 { 935, PERF_TYPE_HW_CACHE, CACHE_LLC_PREFETCH_MISS, QTest::CachePrefetches },-
389 { 961, PERF_TYPE_HW_CACHE, CACHE_LLC_PREFETCH, QTest::CachePrefetches },-
390 { 982, PERF_TYPE_HW_CACHE, CACHE_LLC_READ_MISS, QTest::CacheReads },-
391 { 1004, PERF_TYPE_HW_CACHE, CACHE_LLC_READ, QTest::CacheReads },-
392 { 1020, PERF_TYPE_HW_CACHE, CACHE_LLC_WRITE_MISS, QTest::CacheWrites },-
393 { 1043, PERF_TYPE_HW_CACHE, CACHE_LLC_WRITE, QTest::CacheWrites },-
394 { 1060, PERF_TYPE_HW_CACHE, CACHE_LLC_WRITE_MISS, QTest::CacheWrites },-
395 { 1083, PERF_TYPE_HW_CACHE, CACHE_LLC_WRITE, QTest::CacheWrites },-
396 { 1100, PERF_TYPE_HW_CACHE, CACHE_LLC_READ_MISS, QTest::CacheReads },-
397 { 1116, PERF_TYPE_HW_CACHE, CACHE_LLC_READ, QTest::CacheReads },-
398 { 1126, PERF_TYPE_HW_CACHE, CACHE_LLC_PREFETCH_MISS, QTest::CachePrefetches },-
399 { 1146, PERF_TYPE_HW_CACHE, CACHE_LLC_PREFETCH, QTest::CachePrefetches },-
400 { 1161, PERF_TYPE_HW_CACHE, CACHE_LLC_READ_MISS, QTest::CacheReads },-
401 { 1177, PERF_TYPE_HW_CACHE, CACHE_LLC_READ, QTest::CacheReads },-
402 { 1187, PERF_TYPE_HW_CACHE, CACHE_LLC_WRITE_MISS, QTest::CacheWrites },-
403 { 1204, PERF_TYPE_HW_CACHE, CACHE_LLC_WRITE, QTest::CacheWrites },-
404 { 1215, PERF_TYPE_HW_CACHE, CACHE_LLC_WRITE_MISS, QTest::CacheWrites },-
405 { 1232, PERF_TYPE_HW_CACHE, CACHE_LLC_WRITE, QTest::CacheWrites },-
406 { 1243, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ, QTest::MajorPageFaults },-
407 { 1256, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS, QTest::CPUMigrations },-
408 { 1267, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN, QTest::MinorPageFaults },-
409 { 1280, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS, QTest::PageFaults },-
410 { 1292, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND, QTest::StalledCycles },-
411 { 1315, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND, QTest::StalledCycles },-
412 { 1339, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK, QTest::WalltimeMilliseconds },-
413 { 0, PERF_TYPE_MAX, 0, QTest::Events }-
414};-
415/* -- END GENERATED CODE -- */-
416-
417QTest::QBenchmarkMetric QBenchmarkPerfEventsMeasurer::metricForEvent(quint32 type, quint64 event_id)-
418{-
419 const Events *ptr = eventlist;-
420 for ( ; ptr->type != PERF_TYPE_MAX; ++ptr) {
ptr->type != PERF_TYPE_MAXDescription
TRUEnever evaluated
FALSEnever evaluated
0
421 if (ptr->type == type && ptr->event_id == event_id)
ptr->type == typeDescription
TRUEnever evaluated
FALSEnever evaluated
ptr->event_id == event_idDescription
TRUEnever evaluated
FALSEnever evaluated
0
422 return ptr->metric;
never executed: return ptr->metric;
0
423 }
never executed: end of block
0
424 return QTest::Events;
never executed: return QTest::Events;
0
425}-
426-
427void QBenchmarkPerfEventsMeasurer::setCounter(const char *name)-
428{-
429 initPerf();-
430 const char *colon = strchr(name, ':');-
431 int n = colon ? colon - name : strlen(name);
colonDescription
TRUEnever evaluated
FALSEnever evaluated
0
432 const Events *ptr = eventlist;-
433 for ( ; ptr->type != PERF_TYPE_MAX; ++ptr) {
ptr->type != PERF_TYPE_MAXDescription
TRUEnever evaluated
FALSEnever evaluated
0
434 int c = strncmp(name, eventlist_strings + ptr->offset, n);-
435 if (c == 0)
c == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
436 break;
never executed: break;
0
437 if (c < 0) {
c < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
438 fprintf(stderr, "ERROR: Performance counter type '%s' is unknown\n", name);-
439 exit(1);
never executed: exit(1);
0
440 }-
441 }
never executed: end of block
0
442-
443 attr.type = ptr->type;-
444 attr.config = ptr->event_id;-
445-
446 // now parse the attributes-
447 if (!colon)
!colonDescription
TRUEnever evaluated
FALSEnever evaluated
0
448 return;
never executed: return;
0
449 while (*++colon) {
*++colonDescription
TRUEnever evaluated
FALSEnever evaluated
0
450 switch (*colon) {-
451 case 'u':
never executed: case 'u':
0
452 attr.exclude_user = true;-
453 break;
never executed: break;
0
454 case 'k':
never executed: case 'k':
0
455 attr.exclude_kernel = true;-
456 break;
never executed: break;
0
457 case 'h':
never executed: case 'h':
0
458 attr.exclude_hv = true;-
459 break;
never executed: break;
0
460 case 'G':
never executed: case 'G':
0
461 attr.exclude_guest = true;-
462 break;
never executed: break;
0
463 case 'H':
never executed: case 'H':
0
464 attr.exclude_host = true;-
465 break;
never executed: break;
0
466 default:
never executed: default:
0
467 fprintf(stderr, "ERROR: Unknown attribute '%c'\n", *colon);-
468 exit(1);
never executed: exit(1);
0
469 }-
470 }-
471}
never executed: end of block
0
472-
473void QBenchmarkPerfEventsMeasurer::listCounters()-
474{-
475 if (!isAvailable()) {
!isAvailable()Description
TRUEnever evaluated
FALSEnever evaluated
0
476 printf("Performance counters are not available on this system\n");-
477 return;
never executed: return;
0
478 }-
479-
480 printf("The following performance counters are available:\n");-
481 const Events *ptr = eventlist;-
482 for ( ; ptr->type != PERF_TYPE_MAX; ++ptr) {
ptr->type != PERF_TYPE_MAXDescription
TRUEnever evaluated
FALSEnever evaluated
0
483 printf(" %-30s [%s]\n", eventlist_strings + ptr->offset,-
484 ptr->type == PERF_TYPE_HARDWARE ? "hardware" :-
485 ptr->type == PERF_TYPE_SOFTWARE ? "software" :-
486 ptr->type == PERF_TYPE_HW_CACHE ? "cache" : "other");-
487 }
never executed: end of block
0
488-
489 printf("\nAttributes can be specified by adding a colon and the following:\n"-
490 " u - exclude measuring in the userspace\n"-
491 " k - exclude measuring in kernel mode\n"-
492 " h - exclude measuring in the hypervisor\n"-
493 " G - exclude measuring when running virtualized (guest VM)\n"-
494 " H - exclude measuring when running non-virtualized (host system)\n"-
495 "Attributes can be combined, for example: -perfcounter branch-mispredicts:kh\n");-
496}
never executed: end of block
0
497-
498QBenchmarkPerfEventsMeasurer::QBenchmarkPerfEventsMeasurer()-
499 : fd(-1)-
500{-
501}
never executed: end of block
0
502-
503QBenchmarkPerfEventsMeasurer::~QBenchmarkPerfEventsMeasurer()-
504{-
505 qt_safe_close(fd);-
506}
never executed: end of block
0
507-
508void QBenchmarkPerfEventsMeasurer::init()-
509{-
510}-
511-
512void QBenchmarkPerfEventsMeasurer::start()-
513{-
514-
515 initPerf();-
516 if (fd == -1) {
fd == -1Description
TRUEnever evaluated
FALSEnever evaluated
0
517 // pid == 0 -> attach to the current process-
518 // cpu == -1 -> monitor on all CPUs-
519 // group_fd == -1 -> this is the group leader-
520 // flags == 0 -> reserved, must be zero-
521 fd = perf_event_open(&attr, 0, -1, -1, 0);-
522 if (fd == -1) {
fd == -1Description
TRUEnever evaluated
FALSEnever evaluated
0
523 perror("QBenchmarkPerfEventsMeasurer::start: perf_event_open");-
524 exit(1);
never executed: exit(1);
0
525 } else {-
526 ::fcntl(fd, F_SETFD, FD_CLOEXEC);-
527 }
never executed: end of block
0
528 }-
529-
530 // enable the counter-
531 ::ioctl(fd, PERF_EVENT_IOC_RESET);-
532 ::ioctl(fd, PERF_EVENT_IOC_ENABLE);-
533}
never executed: end of block
0
534-
535qint64 QBenchmarkPerfEventsMeasurer::checkpoint()-
536{-
537 ::ioctl(fd, PERF_EVENT_IOC_DISABLE);-
538 qint64 value = readValue();-
539 ::ioctl(fd, PERF_EVENT_IOC_ENABLE);-
540 return value;
never executed: return value;
0
541}-
542-
543qint64 QBenchmarkPerfEventsMeasurer::stop()-
544{-
545 // disable the counter-
546 ::ioctl(fd, PERF_EVENT_IOC_DISABLE);-
547 return readValue();
never executed: return readValue();
0
548}-
549-
550bool QBenchmarkPerfEventsMeasurer::isMeasurementAccepted(qint64)-
551{-
552 return true;
never executed: return true;
0
553}-
554-
555int QBenchmarkPerfEventsMeasurer::adjustIterationCount(int)-
556{-
557 return 1;
never executed: return 1;
0
558}-
559-
560int QBenchmarkPerfEventsMeasurer::adjustMedianCount(int)-
561{-
562 return 1;
never executed: return 1;
0
563}-
564-
565QTest::QBenchmarkMetric QBenchmarkPerfEventsMeasurer::metricType()-
566{-
567 return metricForEvent(attr.type, attr.config);
never executed: return metricForEvent(attr.type, attr.config);
0
568}-
569-
570static quint64 rawReadValue(int fd)-
571{-
572 /* from the kernel docs:-
573 * struct read_format {-
574 * { u64 value;-
575 * { u64 time_enabled; } && PERF_FORMAT_TOTAL_TIME_ENABLED-
576 * { u64 time_running; } && PERF_FORMAT_TOTAL_TIME_RUNNING-
577 * { u64 id; } && PERF_FORMAT_ID-
578 * } && !PERF_FORMAT_GROUP-
579 */-
580-
581 struct read_format {-
582 quint64 value;-
583 quint64 time_enabled;-
584 quint64 time_running;-
585 } results;-
586-
587 size_t nread = 0;-
588 while (nread < sizeof results) {
nread < sizeof resultsDescription
TRUEnever evaluated
FALSEnever evaluated
0
589 char *ptr = reinterpret_cast<char *>(&results);-
590 qint64 r = qt_safe_read(fd, ptr + nread, sizeof results - nread);-
591 if (r == -1) {
r == -1Description
TRUEnever evaluated
FALSEnever evaluated
0
592 perror("QBenchmarkPerfEventsMeasurer::readValue: reading the results");-
593 exit(1);
never executed: exit(1);
0
594 }-
595 nread += quint64(r);-
596 }
never executed: end of block
0
597-
598 if (results.time_running == results.time_enabled)
results.time_r...s.time_enabledDescription
TRUEnever evaluated
FALSEnever evaluated
0
599 return results.value;
never executed: return results.value;
0
600-
601 // scale the results, though this shouldn't happen!-
602 return results.value * (double(results.time_running) / double(results.time_enabled));
never executed: return results.value * (double(results.time_running) / double(results.time_enabled));
0
603}-
604-
605qint64 QBenchmarkPerfEventsMeasurer::readValue()-
606{-
607 quint64 raw = rawReadValue(fd);-
608 if (metricType() == QTest::WalltimeMilliseconds) {
metricType() =...meMillisecondsDescription
TRUEnever evaluated
FALSEnever evaluated
0
609 // perf returns nanoseconds-
610 return raw / 1000000;
never executed: return raw / 1000000;
0
611 }-
612 return raw;
never executed: return raw;
0
613}-
614-
615QT_END_NAMESPACE-
616-
617#endif-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9