gem5  v22.1.0.0
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 GEM5_DEPRECATED_NAMESPACE(Stats, statistics);
50 namespace statistics
51 {
52 
53 void
55 {
56  assert(bucket_size > 0);
57  if (val < min_track)
58  underflow += number;
59  else if (val > max_track)
60  overflow += number;
61  else {
62  cvec[std::floor((val - min_track) / bucket_size)] += number;
63  }
64 
65  if (val < min_val)
66  min_val = val;
67 
68  if (val > max_val)
69  max_val = val;
70 
71  sum += val * number;
72  squares += val * val * number;
73  samples += number;
74 }
75 
76 void
78 {
79  int size = cvec.size();
80  int zero = size / 2; // round down!
81  int top_half = zero + (size - zero + 1) / 2; // round up!
82  int bottom_half = (size - zero) / 2; // round down!
83 
84  // grow down
85  int low_pair = zero - 1;
86  for (int i = zero - 1; i >= bottom_half; i--) {
87  cvec[i] = cvec[low_pair];
88  if (low_pair - 1 >= 0)
89  cvec[i] += cvec[low_pair - 1];
90  low_pair -= 2;
91  }
92  assert(low_pair == 0 || low_pair == -1 || low_pair == -2);
93 
94  for (int i = bottom_half - 1; i >= 0; i--)
95  cvec[i] = Counter();
96 
97  // grow up
98  int high_pair = zero;
99  for (int i = zero; i < top_half; i++) {
100  cvec[i] = cvec[high_pair];
101  if (high_pair + 1 < size)
102  cvec[i] += cvec[high_pair + 1];
103  high_pair += 2;
104  }
105  assert(high_pair == size || high_pair == size + 1);
106 
107  for (int i = top_half; i < size; i++)
108  cvec[i] = Counter();
109 
110  max_bucket *= 2;
111  min_bucket *= 2;
112  bucket_size *= 2;
113 }
114 
115 void
117 {
118  const int size = cvec.size();
119  const int zero = size / 2; // round down!
120  const bool even = ((size - 1) % 2) == 0;
121 
122  // Make sure that zero becomes the lower bound of the middle bucket. On
123  // an even number of buckets the last bucket does not change its lower
124  // bound, therefore it does not need to absorb any other bucket
125  int pair = size - 1;
126  if (even) {
127  pair--;
128  }
129  for (int i = pair; i >= zero; --i) {
130  cvec[i] = cvec[pair];
131  if (pair - 1 >= 0)
132  cvec[i] += cvec[pair - 1];
133  pair -= 2;
134  }
135 
136  for (int i = zero - 1; i >= 0; i--)
137  cvec[i] = Counter();
138 
139  // Double the range by using the negative of the lower bound of the last
140  // bucket as the new lower bound of the first bucket
142 
143  // A special case must be handled when there is an odd number of
144  // buckets so that zero is kept as the lower bound of the middle bucket
145  if (!even) {
148  }
149 
150  // Only update the bucket size once the range has been updated
151  bucket_size *= 2;
152 }
153 
154 void
156 {
157  int size = cvec.size();
158  int half = (size + 1) / 2; // round up!
159 
160  int pair = 0;
161  for (int i = 0; i < half; i++) {
162  cvec[i] = cvec[pair];
163  if (pair + 1 < size)
164  cvec[i] += cvec[pair + 1];
165  pair += 2;
166  }
167  assert(pair == size || pair == size + 1);
168 
169  for (int i = half; i < size; i++)
170  cvec[i] = Counter();
171 
172  max_bucket *= 2;
173  bucket_size *= 2;
174 }
175 
176 void
178 {
179  assert(min_bucket < max_bucket);
180  if (val < min_bucket) {
181  if (min_bucket == 0)
182  growDown();
183 
184  while (val < min_bucket)
185  growOut();
186  } else if (val >= max_bucket + bucket_size) {
187  if (min_bucket == 0) {
188  while (val >= max_bucket + bucket_size)
189  growUp();
190  } else {
191  while (val >= max_bucket + bucket_size)
192  growOut();
193  }
194  }
195 
196  assert(bucket_size > 0);
197  size_type index =
198  (int64_t)std::floor((val - min_bucket) / bucket_size);
199 
200  assert(index < size());
201  cvec[index] += number;
202 
203  sum += val * number;
204  squares += val * val * number;
205  logs += std::log(val) * number;
206  samples += number;
207 }
208 
209 void
211 {
212  int b_size = hs->size();
213  assert(size() == b_size);
214  assert(min_bucket == hs->min_bucket);
215 
216  sum += hs->sum;
217  logs += hs->logs;
218  squares += hs->squares;
219  samples += hs->samples;
220 
221  while (bucket_size > hs->bucket_size)
222  hs->growUp();
223  while (bucket_size < hs->bucket_size)
224  growUp();
225 
226  for (uint32_t i = 0; i < b_size; i++)
227  cvec[i] += hs->cvec[i];
228 }
229 
230 } // namespace statistics
231 } // namespace gem5
Counter min_val
The smallest value sampled.
Definition: storage.hh:244
Counter sum
The current sum.
Definition: storage.hh:252
Counter samples
The number of samples.
Definition: storage.hh:256
VCounter cvec
Counter for each bucket.
Definition: storage.hh:258
Counter underflow
The number of values sampled less than min.
Definition: storage.hh:248
Counter squares
The sum of squares.
Definition: storage.hh:254
void sample(Counter val, int number)
Add a value to the distribution for the given number of times.
Definition: storage.cc:54
Counter min_track
The minimum value to track.
Definition: storage.hh:237
Counter bucket_size
The number of entries in each bucket.
Definition: storage.hh:241
Counter overflow
The number of values sampled more than max.
Definition: storage.hh:250
Counter max_val
The largest value sampled.
Definition: storage.hh:246
Counter max_track
The maximum value to track.
Definition: storage.hh:239
Templatized storage and interface for a histogram stat.
Definition: storage.hh:400
Counter sum
The current sum.
Definition: storage.hh:410
Counter samples
The number of samples.
Definition: storage.hh:416
void add(HistStor *other)
Adds the contents of the given storage to this storage.
Definition: storage.cc:210
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:155
Counter min_bucket
Lower bound of the first bucket's range.
Definition: storage.hh:403
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:77
Counter bucket_size
The number of entries in each bucket.
Definition: storage.hh:407
size_type size() const
Return the number of buckets in this distribution.
Definition: storage.hh:497
VCounter cvec
Counter for each bucket.
Definition: storage.hh:418
bool zero() const
Returns true if any calls to sample have been made.
Definition: storage.hh:504
void sample(Counter val, int number)
Add a value to the distribution for the given number of times.
Definition: storage.cc:177
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:116
Counter logs
The sum of logarithm of each sample, used to compute geometric mean.
Definition: storage.hh:412
Counter max_bucket
Lower bound of the last bucket's range.
Definition: storage.hh:405
Counter squares
The sum of squares.
Definition: storage.hh:414
Bitfield< 7 > i
Definition: misc_types.hh:67
Bitfield< 30, 0 > index
Bitfield< 63 > val
Definition: misc.hh:776
unsigned int size_type
Definition: types.hh:60
double Counter
All counters are of 64-bit values.
Definition: types.hh:47
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
GEM5_DEPRECATED_NAMESPACE(GuestABI, guest_abi)

Generated on Wed Dec 21 2022 10:22:29 for gem5 by doxygen 1.9.1