gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
storage.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 Daniel R. Carvalho
3  * Copyright (c) 2019 Arm Limited
4  * All rights reserved.
5  *
6  * The license below extends only to copyright in the software and shall
7  * not be construed as granting a license to any other intellectual
8  * property including but not limited to intellectual property relating
9  * to a hardware implementation of the functionality of the software
10  * licensed hereunder. You may use the software subject to the license
11  * terms below provided that you ensure that this notice is replicated
12  * unmodified and in its entirety in all distributions of the software,
13  * modified or unmodified, in source code or in binary form.
14  *
15  * Copyright (c) 2003-2005 The Regents of The University of Michigan
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions are
20  * met: redistributions of source code must retain the above copyright
21  * notice, this list of conditions and the following disclaimer;
22  * redistributions in binary form must reproduce the above copyright
23  * notice, this list of conditions and the following disclaimer in the
24  * documentation and/or other materials provided with the distribution;
25  * neither the name of the copyright holders nor the names of its
26  * contributors may be used to endorse or promote products derived from
27  * this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 #include "base/stats/storage.hh"
43 
44 #include <cmath>
45 
46 namespace gem5
47 {
48 
49 namespace statistics
50 {
51 
52 void
54 {
55  assert(bucket_size > 0);
56  if (val < min_track)
57  underflow += number;
58  else if (val > max_track)
59  overflow += number;
60  else {
61  cvec[std::floor((val - min_track) / bucket_size)] += number;
62  }
63 
64  if (val < min_val)
65  min_val = val;
66 
67  if (val > max_val)
68  max_val = val;
69 
70  sum += val * number;
71  squares += val * val * number;
72  samples += number;
73 }
74 
75 void
77 {
78  int size = cvec.size();
79  int zero = size / 2; // round down!
80  int top_half = zero + (size - zero + 1) / 2; // round up!
81  int bottom_half = (size - zero) / 2; // round down!
82 
83  // grow down
84  int low_pair = zero - 1;
85  for (int i = zero - 1; i >= bottom_half; i--) {
86  cvec[i] = cvec[low_pair];
87  if (low_pair - 1 >= 0)
88  cvec[i] += cvec[low_pair - 1];
89  low_pair -= 2;
90  }
91  assert(low_pair == 0 || low_pair == -1 || low_pair == -2);
92 
93  for (int i = bottom_half - 1; i >= 0; i--)
94  cvec[i] = Counter();
95 
96  // grow up
97  int high_pair = zero;
98  for (int i = zero; i < top_half; i++) {
99  cvec[i] = cvec[high_pair];
100  if (high_pair + 1 < size)
101  cvec[i] += cvec[high_pair + 1];
102  high_pair += 2;
103  }
104  assert(high_pair == size || high_pair == size + 1);
105 
106  for (int i = top_half; i < size; i++)
107  cvec[i] = Counter();
108 
109  max_bucket *= 2;
110  min_bucket *= 2;
111  bucket_size *= 2;
112 }
113 
114 void
116 {
117  const int size = cvec.size();
118  const int zero = size / 2; // round down!
119  const bool even = ((size - 1) % 2) == 0;
120 
121  // Make sure that zero becomes the lower bound of the middle bucket. On
122  // an even number of buckets the last bucket does not change its lower
123  // bound, therefore it does not need to absorb any other bucket
124  int pair = size - 1;
125  if (even) {
126  pair--;
127  }
128  for (int i = pair; i >= zero; --i) {
129  cvec[i] = cvec[pair];
130  if (pair - 1 >= 0)
131  cvec[i] += cvec[pair - 1];
132  pair -= 2;
133  }
134 
135  for (int i = zero - 1; i >= 0; i--)
136  cvec[i] = Counter();
137 
138  // Double the range by using the negative of the lower bound of the last
139  // bucket as the new lower bound of the first bucket
141 
142  // A special case must be handled when there is an odd number of
143  // buckets so that zero is kept as the lower bound of the middle bucket
144  if (!even) {
147  }
148 
149  // Only update the bucket size once the range has been updated
150  bucket_size *= 2;
151 }
152 
153 void
155 {
156  int size = cvec.size();
157  int half = (size + 1) / 2; // round up!
158 
159  int pair = 0;
160  for (int i = 0; i < half; i++) {
161  cvec[i] = cvec[pair];
162  if (pair + 1 < size)
163  cvec[i] += cvec[pair + 1];
164  pair += 2;
165  }
166  assert(pair == size || pair == size + 1);
167 
168  for (int i = half; i < size; i++)
169  cvec[i] = Counter();
170 
171  max_bucket *= 2;
172  bucket_size *= 2;
173 }
174 
175 void
177 {
178  assert(min_bucket < max_bucket);
179  if (val < min_bucket) {
180  if (min_bucket == 0)
181  growDown();
182 
183  while (val < min_bucket)
184  growOut();
185  } else if (val >= max_bucket + bucket_size) {
186  if (min_bucket == 0) {
187  while (val >= max_bucket + bucket_size)
188  growUp();
189  } else {
190  while (val >= max_bucket + bucket_size)
191  growOut();
192  }
193  }
194 
195  assert(bucket_size > 0);
196  size_type index =
197  (int64_t)std::floor((val - min_bucket) / bucket_size);
198 
199  assert(index < size());
200  cvec[index] += number;
201 
202  sum += val * number;
203  squares += val * val * number;
204  logs += std::log(val) * number;
205  samples += number;
206 }
207 
208 void
210 {
211  int b_size = hs->size();
212  assert(size() == b_size);
213  assert(min_bucket == hs->min_bucket);
214 
215  sum += hs->sum;
216  logs += hs->logs;
217  squares += hs->squares;
218  samples += hs->samples;
219 
220  while (bucket_size > hs->bucket_size)
221  hs->growUp();
222  while (bucket_size < hs->bucket_size)
223  growUp();
224 
225  for (uint32_t i = 0; i < b_size; i++)
226  cvec[i] += hs->cvec[i];
227 }
228 
229 } // namespace statistics
230 } // namespace gem5
gem5::statistics::HistStor::cvec
VCounter cvec
Counter for each bucket.
Definition: storage.hh:417
gem5::statistics::HistStor::sample
void sample(Counter val, int number)
Add a value to the distribution for the given number of times.
Definition: storage.cc:176
gem5::statistics::HistStor::logs
Counter logs
The sum of logarithm of each sample, used to compute geometric mean.
Definition: storage.hh:411
gem5::MipsISA::index
Bitfield< 30, 0 > index
Definition: pra_constants.hh:47
gem5::statistics::DistStor::min_val
Counter min_val
The smallest value sampled.
Definition: storage.hh:243
gem5::statistics::HistStor::size
size_type size() const
Return the number of buckets in this distribution.
Definition: storage.hh:496
gem5::statistics::HistStor::zero
bool zero() const
Returns true if any calls to sample have been made.
Definition: storage.hh:503
gem5::X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:776
gem5::statistics::HistStor::bucket_size
Counter bucket_size
The number of entries in each bucket.
Definition: storage.hh:406
gem5::statistics::HistStor::sum
Counter sum
The current sum.
Definition: storage.hh:409
gem5::statistics::DistStor::min_track
Counter min_track
The minimum value to track.
Definition: storage.hh:236
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:67
gem5::statistics::DistStor::max_track
Counter max_track
The maximum value to track.
Definition: storage.hh:238
gem5::statistics::DistStor::cvec
VCounter cvec
Counter for each bucket.
Definition: storage.hh:257
storage.hh
gem5::statistics::HistStor::max_bucket
Counter max_bucket
Lower bound of the last bucket's range.
Definition: storage.hh:404
gem5::statistics::HistStor::growUp
void growUp()
Given a bucket size B, and a range of values [0, N], this function doubles the bucket size to double ...
Definition: storage.cc:154
gem5::statistics::HistStor::samples
Counter samples
The number of samples.
Definition: storage.hh:415
gem5::statistics::DistStor::sum
Counter sum
The current sum.
Definition: storage.hh:251
gem5::statistics::DistStor::max_val
Counter max_val
The largest value sampled.
Definition: storage.hh:245
gem5::statistics::DistStor::overflow
Counter overflow
The number of values sampled more than max.
Definition: storage.hh:249
gem5::statistics::DistStor::sample
void sample(Counter val, int number)
Add a value to the distribution for the given number of times.
Definition: storage.cc:53
gem5::statistics::HistStor::growDown
void growDown()
Given a bucket size B, and a range of values [0, N], this function doubles the bucket size to double ...
Definition: storage.cc:115
gem5::statistics::size_type
unsigned int size_type
Definition: types.hh:59
gem5::statistics::DistStor::squares
Counter squares
The sum of squares.
Definition: storage.hh:253
gem5::statistics::DistStor::underflow
Counter underflow
The number of values sampled less than min.
Definition: storage.hh:247
gem5::statistics::HistStor::min_bucket
Counter min_bucket
Lower bound of the first bucket's range.
Definition: storage.hh:402
gem5::statistics::Counter
double Counter
All counters are of 64-bit values.
Definition: types.hh:46
gem5::statistics::HistStor::squares
Counter squares
The sum of squares.
Definition: storage.hh:413
gem5::statistics::HistStor
Templatized storage and interface for a histogram stat.
Definition: storage.hh:398
gem5::statistics::DistStor::bucket_size
Counter bucket_size
The number of entries in each bucket.
Definition: storage.hh:240
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::statistics::HistStor::growOut
void growOut()
Given a bucket size B, and a range of values [M, N], where M < 0, this function doubles the bucket si...
Definition: storage.cc:76
gem5::statistics::HistStor::add
void add(HistStor *other)
Adds the contents of the given storage to this storage.
Definition: storage.cc:209
gem5::statistics::DistStor::samples
Counter samples
The number of samples.
Definition: storage.hh:255

Generated on Sun Jul 30 2023 01:56:51 for gem5 by doxygen 1.8.17