Eric Fiselier | 133a720 | 2017-04-18 07:17:20 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "counter.h" |
| 16 | |
| 17 | namespace benchmark { |
| 18 | namespace internal { |
| 19 | |
Louis Dionne | 5208ec5 | 2021-07-12 17:15:34 | [diff] [blame] | 20 | double Finish(Counter const& c, IterationCount iterations, double cpu_time, |
Eric Fiselier | fcafd3e | 2018-07-10 04:02:00 | [diff] [blame] | 21 | double num_threads) { |
Eric Fiselier | 133a720 | 2017-04-18 07:17:20 | [diff] [blame] | 22 | double v = c.value; |
| 23 | if (c.flags & Counter::kIsRate) { |
| 24 | v /= cpu_time; |
| 25 | } |
| 26 | if (c.flags & Counter::kAvgThreads) { |
| 27 | v /= num_threads; |
| 28 | } |
Eric Fiselier | fcafd3e | 2018-07-10 04:02:00 | [diff] [blame] | 29 | if (c.flags & Counter::kIsIterationInvariant) { |
Mircea Trofin | a5b7971 | 2024-03-04 22:11:30 | [diff] [blame] | 30 | v *= static_cast<double>(iterations); |
Eric Fiselier | fcafd3e | 2018-07-10 04:02:00 | [diff] [blame] | 31 | } |
| 32 | if (c.flags & Counter::kAvgIterations) { |
Mircea Trofin | a5b7971 | 2024-03-04 22:11:30 | [diff] [blame] | 33 | v /= static_cast<double>(iterations); |
Eric Fiselier | fcafd3e | 2018-07-10 04:02:00 | [diff] [blame] | 34 | } |
Louis Dionne | 5208ec5 | 2021-07-12 17:15:34 | [diff] [blame] | 35 | |
| 36 | if (c.flags & Counter::kInvert) { // Invert is *always* last. |
| 37 | v = 1.0 / v; |
| 38 | } |
Eric Fiselier | 133a720 | 2017-04-18 07:17:20 | [diff] [blame] | 39 | return v; |
| 40 | } |
| 41 | |
Louis Dionne | 5208ec5 | 2021-07-12 17:15:34 | [diff] [blame] | 42 | void Finish(UserCounters* l, IterationCount iterations, double cpu_time, |
| 43 | double num_threads) { |
Eric Fiselier | fcafd3e | 2018-07-10 04:02:00 | [diff] [blame] | 44 | for (auto& c : *l) { |
| 45 | c.second.value = Finish(c.second, iterations, cpu_time, num_threads); |
Eric Fiselier | 133a720 | 2017-04-18 07:17:20 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
Eric Fiselier | fcafd3e | 2018-07-10 04:02:00 | [diff] [blame] | 49 | void Increment(UserCounters* l, UserCounters const& r) { |
Eric Fiselier | 133a720 | 2017-04-18 07:17:20 | [diff] [blame] | 50 | // add counters present in both or just in *l |
Eric Fiselier | fcafd3e | 2018-07-10 04:02:00 | [diff] [blame] | 51 | for (auto& c : *l) { |
Eric Fiselier | 133a720 | 2017-04-18 07:17:20 | [diff] [blame] | 52 | auto it = r.find(c.first); |
| 53 | if (it != r.end()) { |
Eric Fiselier | 1903976 | 2018-01-18 04:23:01 | [diff] [blame] | 54 | c.second.value = c.second + it->second; |
Eric Fiselier | 133a720 | 2017-04-18 07:17:20 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | // add counters present in r, but not in *l |
Eric Fiselier | fcafd3e | 2018-07-10 04:02:00 | [diff] [blame] | 58 | for (auto const& tc : r) { |
Eric Fiselier | 133a720 | 2017-04-18 07:17:20 | [diff] [blame] | 59 | auto it = l->find(tc.first); |
| 60 | if (it == l->end()) { |
| 61 | (*l)[tc.first] = tc.second; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | bool SameNames(UserCounters const& l, UserCounters const& r) { |
| 67 | if (&l == &r) return true; |
| 68 | if (l.size() != r.size()) { |
| 69 | return false; |
| 70 | } |
| 71 | for (auto const& c : l) { |
Eric Fiselier | 1903976 | 2018-01-18 04:23:01 | [diff] [blame] | 72 | if (r.find(c.first) == r.end()) { |
Eric Fiselier | 133a720 | 2017-04-18 07:17:20 | [diff] [blame] | 73 | return false; |
| 74 | } |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
Eric Fiselier | fcafd3e | 2018-07-10 04:02:00 | [diff] [blame] | 79 | } // end namespace internal |
| 80 | } // end namespace benchmark |