gem5  v22.1.0.0
storage.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 Daniel R. Carvalho
3  * Copyright (c) 2003-2005 The Regents of The University of Michigan
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met: redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer;
10  * redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution;
13  * neither the name of the copyright holders nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef __BASE_STATS_STORAGE_HH__
31 #define __BASE_STATS_STORAGE_HH__
32 
33 #include <cassert>
34 #include <cmath>
35 
36 #include "base/cast.hh"
37 #include "base/compiler.hh"
38 #include "base/logging.hh"
39 #include "base/stats/types.hh"
40 #include "sim/cur_tick.hh"
41 
42 namespace gem5
43 {
44 
45 GEM5_DEPRECATED_NAMESPACE(Stats, statistics);
46 namespace statistics
47 {
48 
50 {
51  virtual ~StorageParams() = default;
52 };
53 
57 class StatStor
58 {
59  private:
62 
63  public:
64  struct Params : public StorageParams {};
65 
70  StatStor(const StorageParams* const storage_params)
71  : data(Counter())
72  { }
73 
78  void set(Counter val) { data = val; }
79 
84  void inc(Counter val) { data += val; }
85 
90  void dec(Counter val) { data -= val; }
91 
96  Counter value() const { return data; }
97 
102  Result result() const { return (Result)data; }
103 
107  void prepare(const StorageParams* const storage_params) { }
108 
112  void reset(const StorageParams* const storage_params) { data = Counter(); }
113 
117  bool zero() const { return data == Counter(); }
118 };
119 
127 class AvgStor
128 {
129  private:
135  mutable Result total;
137  mutable Tick last;
138 
139  public:
140  struct Params : public StorageParams {};
141 
145  AvgStor(const StorageParams* const storage_params)
146  : current(0), lastReset(0), total(0), last(0)
147  { }
148 
154  void
156  {
157  total += current * (curTick() - last);
158  last = curTick();
159  current = val;
160  }
161 
166  void inc(Counter val) { set(current + val); }
167 
172  void dec(Counter val) { set(current - val); }
173 
178  Counter value() const { return current; }
179 
184  Result
185  result() const
186  {
187  assert(last == curTick());
188  return (Result)(total + current) / (Result)(curTick() - lastReset + 1);
189  }
190 
194  bool zero() const { return total == 0.0; }
195 
199  void
200  prepare(const StorageParams* const storage_params)
201  {
202  total += current * (curTick() - last);
203  last = curTick();
204  }
205 
209  void
210  reset(const StorageParams* const storage_params)
211  {
212  total = 0.0;
213  last = curTick();
214  lastReset = curTick();
215  }
216 
217 };
218 
220 struct DistParams : public StorageParams
221 {
222  const DistType type;
224 };
225 
233 class DistStor
234 {
235  private:
242 
259 
260  public:
262  struct Params : public DistParams
263  {
272 
273  Params(Counter _min, Counter _max, Counter _bucket_size)
274  : DistParams(Dist), min(_min), max(_max), bucket_size(_bucket_size),
275  buckets(0)
276  {
277  fatal_if(bucket_size <= 0,
278  "Bucket size (%f) must be greater than zero", bucket_size);
279  warn_if(std::floor((max - min + 1.0) / bucket_size) !=
280  std::ceil((max - min + 1.0) / bucket_size),
281  "Bucket size (%f) does not divide range [%f:%f] into equal-" \
282  "sized buckets. Rounding up.", bucket_size, min + 1.0, max);
283 
284  buckets = std::ceil((max - min + 1.0) / bucket_size);
285  }
286  };
287 
288  DistStor(const StorageParams* const storage_params)
289  : cvec(safe_cast<const Params *>(storage_params)->buckets)
290  {
291  reset(storage_params);
292  }
293 
299  void sample(Counter val, int number);
300 
305  size_type size() const { return cvec.size(); }
306 
311  bool
312  zero() const
313  {
314  return samples == Counter();
315  }
316 
317  void
318  prepare(const StorageParams* const storage_params, DistData &data)
319  {
320  const Params *params = safe_cast<const Params *>(storage_params);
321 
322  assert(params->type == Dist);
323  data.type = params->type;
324  data.min = params->min;
325  data.max = params->max;
326  data.bucket_size = params->bucket_size;
327 
328  data.min_val = (min_val == CounterLimits::max()) ? 0 : min_val;
329  data.max_val = (max_val == CounterLimits::min()) ? 0 : max_val;
330  data.underflow = underflow;
331  data.overflow = overflow;
332 
333  data.cvec.resize(params->buckets);
334  for (off_type i = 0; i < params->buckets; ++i)
335  data.cvec[i] = cvec[i];
336 
337  data.sum = sum;
338  data.squares = squares;
339  data.samples = samples;
340  }
341 
345  void
346  reset(const StorageParams* const storage_params)
347  {
348  const Params *params = safe_cast<const Params *>(storage_params);
349  min_track = params->min;
350  max_track = params->max;
351  bucket_size = params->bucket_size;
352 
353  min_val = CounterLimits::max();
354  max_val = CounterLimits::min();
355  underflow = Counter();
356  overflow = Counter();
357 
358  size_type size = cvec.size();
359  for (off_type i = 0; i < size; ++i)
360  cvec[i] = Counter();
361 
362  sum = Counter();
363  squares = Counter();
364  samples = Counter();
365  }
366 };
367 
399 class HistStor
400 {
401  private:
408 
419 
429  void growUp();
430 
442  void growOut();
443 
456  void growDown();
457 
458  public:
460  struct Params : public DistParams
461  {
464 
465  Params(size_type _buckets)
466  : DistParams(Hist)
467  {
468  fatal_if(_buckets < 2,
469  "There must be at least two buckets in a histogram");
470  buckets = _buckets;
471  }
472  };
473 
474  HistStor(const StorageParams* const storage_params)
475  : cvec(safe_cast<const Params *>(storage_params)->buckets)
476  {
477  reset(storage_params);
478  }
479 
484  void add(HistStor *other);
485 
491  void sample(Counter val, int number);
492 
497  size_type size() const { return cvec.size(); }
498 
503  bool
504  zero() const
505  {
506  return samples == Counter();
507  }
508 
509  void
510  prepare(const StorageParams* const storage_params, DistData &data)
511  {
512  const Params *params = safe_cast<const Params *>(storage_params);
513 
514  assert(params->type == Hist);
515  data.type = params->type;
516  data.min = min_bucket;
517  data.max = max_bucket + bucket_size - 1;
518  data.bucket_size = bucket_size;
519 
520  data.min_val = min_bucket;
521  data.max_val = max_bucket;
522 
523  int buckets = params->buckets;
524  data.cvec.resize(buckets);
525  for (off_type i = 0; i < buckets; ++i)
526  data.cvec[i] = cvec[i];
527 
528  data.sum = sum;
529  data.logs = logs;
530  data.squares = squares;
531  data.samples = samples;
532  }
533 
537  void
538  reset(const StorageParams* const storage_params)
539  {
540  const Params *params = safe_cast<const Params *>(storage_params);
541  min_bucket = 0;
542  max_bucket = params->buckets - 1;
543  bucket_size = 1;
544 
545  size_type size = cvec.size();
546  for (off_type i = 0; i < size; ++i)
547  cvec[i] = Counter();
548 
549  sum = Counter();
550  squares = Counter();
551  samples = Counter();
552  logs = Counter();
553  }
554 };
555 
561 {
562  private:
569 
570  public:
571  struct Params : public DistParams
572  {
574  };
575 
579  SampleStor(const StorageParams* const storage_params)
581  { }
582 
590  void
591  sample(Counter val, int number)
592  {
593  sum += val * number;
594  squares += val * val * number;
595  samples += number;
596  }
597 
602  size_type size() const { return 1; }
603 
608  bool zero() const { return samples == Counter(); }
609 
610  void
611  prepare(const StorageParams* const storage_params, DistData &data)
612  {
613  const Params *params = safe_cast<const Params *>(storage_params);
614 
615  assert(params->type == Deviation);
616  data.type = params->type;
617  data.sum = sum;
618  data.squares = squares;
619  data.samples = samples;
620  }
621 
625  void
626  reset(const StorageParams* const storage_params)
627  {
628  sum = Counter();
629  squares = Counter();
630  samples = Counter();
631  }
632 };
633 
639 {
640  private:
645 
646  public:
647  struct Params : public DistParams
648  {
650  };
651 
655  AvgSampleStor(const StorageParams* const storage_params)
656  : sum(Counter()), squares(Counter())
657  {}
658 
665  void
666  sample(Counter val, int number)
667  {
668  sum += val * number;
669  squares += val * val * number;
670  }
671 
676  size_type size() const { return 1; }
677 
682  bool zero() const { return sum == Counter(); }
683 
684  void
685  prepare(const StorageParams* const storage_params, DistData &data)
686  {
687  const Params *params = safe_cast<const Params *>(storage_params);
688 
689  assert(params->type == Deviation);
690  data.type = params->type;
691  data.sum = sum;
692  data.squares = squares;
693  data.samples = curTick();
694  }
695 
699  void
700  reset(const StorageParams* const storage_params)
701  {
702  sum = Counter();
703  squares = Counter();
704  }
705 };
706 
715 {
716  private:
721 
722  public:
724  struct Params : public DistParams
725  {
727  };
728 
729  SparseHistStor(const StorageParams* const storage_params)
730  {
731  reset(storage_params);
732  }
733 
739  void
740  sample(Counter val, int number)
741  {
742  cmap[val] += number;
743  samples += number;
744  }
745 
750  size_type size() const { return cmap.size(); }
751 
756  bool
757  zero() const
758  {
759  return samples == Counter();
760  }
761 
762  void
763  prepare(const StorageParams* const storage_params, SparseHistData &data)
764  {
765  MCounter::iterator it;
766  data.cmap.clear();
767  for (it = cmap.begin(); it != cmap.end(); it++) {
768  data.cmap[(*it).first] = (*it).second;
769  }
770 
771  data.samples = samples;
772  }
773 
777  void
778  reset(const StorageParams* const storage_params)
779  {
780  cmap.clear();
781  samples = 0;
782  }
783 };
784 
785 } // namespace statistics
786 } // namespace gem5
787 
788 #endif // __BASE_STATS_STORAGE_HH__
const char data[]
Templatized storage for distribution that calculates per tick mean and variance.
Definition: storage.hh:639
bool zero() const
Return true if no samples have been added.
Definition: storage.hh:682
void reset(const StorageParams *const storage_params)
Reset stat value to default.
Definition: storage.hh:700
void sample(Counter val, int number)
Add a value to the distribution for the given number of times.
Definition: storage.hh:666
size_type size() const
Return the number of entries, in this case 1.
Definition: storage.hh:676
AvgSampleStor(const StorageParams *const storage_params)
Create and initialize this storage.
Definition: storage.hh:655
void prepare(const StorageParams *const storage_params, DistData &data)
Definition: storage.hh:685
Counter squares
Current sum of squares.
Definition: storage.hh:644
Counter sum
Current total.
Definition: storage.hh:642
Templatized storage and interface to a per-tick average stat.
Definition: storage.hh:128
Counter current
The current count.
Definition: storage.hh:131
Counter value() const
Return the current count.
Definition: storage.hh:178
void reset(const StorageParams *const storage_params)
Reset stat value to default.
Definition: storage.hh:210
Result total
The total count for all tick.
Definition: storage.hh:135
void inc(Counter val)
Increment the current count by the provided value, calls set.
Definition: storage.hh:166
Result result() const
Return the current average.
Definition: storage.hh:185
Tick last
The tick that current last changed.
Definition: storage.hh:137
void dec(Counter val)
Deccrement the current count by the provided value, calls set.
Definition: storage.hh:172
Tick lastReset
The tick of the last reset.
Definition: storage.hh:133
void prepare(const StorageParams *const storage_params)
Prepare stat data for dumping or serialization.
Definition: storage.hh:200
AvgStor(const StorageParams *const storage_params)
Build and initializes this stat storage.
Definition: storage.hh:145
void set(Counter val)
Set the current count to the one provided, update the total and last set values.
Definition: storage.hh:155
Templatized storage and interface for a distribution stat.
Definition: storage.hh:234
size_type size() const
Return the number of buckets in this distribution.
Definition: storage.hh:305
Counter min_val
The smallest value sampled.
Definition: storage.hh:244
Counter sum
The current sum.
Definition: storage.hh:252
DistStor(const StorageParams *const storage_params)
Definition: storage.hh:288
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 prepare(const StorageParams *const storage_params, DistData &data)
Definition: storage.hh:318
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
bool zero() const
Returns true if any calls to sample have been made.
Definition: storage.hh:312
void reset(const StorageParams *const storage_params)
Reset stat value to default.
Definition: storage.hh:346
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 reset(const StorageParams *const storage_params)
Reset stat value to default.
Definition: storage.hh:538
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
HistStor(const StorageParams *const storage_params)
Definition: storage.hh:474
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
void prepare(const StorageParams *const storage_params, DistData &data)
Definition: storage.hh:510
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
Templatized storage and interface for a distribution that calculates mean and variance.
Definition: storage.hh:561
Counter sum
The current sum.
Definition: storage.hh:564
size_type size() const
Return the number of entries in this stat, 1.
Definition: storage.hh:602
bool zero() const
Return true if no samples have been added.
Definition: storage.hh:608
Counter squares
The sum of squares.
Definition: storage.hh:566
void reset(const StorageParams *const storage_params)
Reset stat value to default.
Definition: storage.hh:626
void prepare(const StorageParams *const storage_params, DistData &data)
Definition: storage.hh:611
Counter samples
The number of samples.
Definition: storage.hh:568
SampleStor(const StorageParams *const storage_params)
Create and initialize this storage.
Definition: storage.hh:579
void sample(Counter val, int number)
Add a value the given number of times to this running average.
Definition: storage.hh:591
Templatized storage and interface for a sparse histogram stat.
Definition: storage.hh:715
size_type size() const
Return the number of buckets in this distribution.
Definition: storage.hh:750
void reset(const StorageParams *const storage_params)
Reset stat value to default.
Definition: storage.hh:778
SparseHistStor(const StorageParams *const storage_params)
Definition: storage.hh:729
void sample(Counter val, int number)
Add a value to the distribution for the given number of times.
Definition: storage.hh:740
Counter samples
Counter for number of samples.
Definition: storage.hh:718
void prepare(const StorageParams *const storage_params, SparseHistData &data)
Definition: storage.hh:763
bool zero() const
Returns true if any calls to sample have been made.
Definition: storage.hh:757
MCounter cmap
Counter for each bucket.
Definition: storage.hh:720
Templatized storage and interface for a simple scalar stat.
Definition: storage.hh:58
void prepare(const StorageParams *const storage_params)
Prepare stat data for dumping or serialization.
Definition: storage.hh:107
Result result() const
Return the value of this stat as a result type.
Definition: storage.hh:102
StatStor(const StorageParams *const storage_params)
Builds this storage element and calls the base constructor of the datatype.
Definition: storage.hh:70
void dec(Counter val)
Decrement the stat by the given value.
Definition: storage.hh:90
Counter value() const
Return the value of this stat as its base type.
Definition: storage.hh:96
void set(Counter val)
The the stat to the given value.
Definition: storage.hh:78
void inc(Counter val)
Increment the stat by the given value.
Definition: storage.hh:84
Counter data
The statistic value.
Definition: storage.hh:61
void reset(const StorageParams *const storage_params)
Reset stat value to default.
Definition: storage.hh:112
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:226
#define warn_if(cond,...)
Conditional warning macro that checks the supplied condition and only prints a warning if the conditi...
Definition: logging.hh:273
Bitfield< 7 > i
Definition: misc_types.hh:67
Bitfield< 51 > t
Definition: pagetable.hh:56
Bitfield< 63 > val
Definition: misc.hh:776
unsigned int size_type
Definition: types.hh:60
std::map< Counter, int > MCounter
map of counters
Definition: types.hh:51
double Counter
All counters are of 64-bit values.
Definition: types.hh:47
unsigned int off_type
Definition: types.hh:61
double Result
All results are doubles.
Definition: types.hh:56
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Tick curTick()
The universal simulation clock.
Definition: cur_tick.hh:46
uint64_t Tick
Tick count type.
Definition: types.hh:58
GEM5_DEPRECATED_NAMESPACE(GuestABI, guest_abi)
T safe_cast(U ptr)
Definition: cast.hh:62
General container for distribution data.
Definition: types.hh:67
The parameters for a distribution stat.
Definition: storage.hh:221
The parameters for a distribution stat.
Definition: storage.hh:263
size_type buckets
The number of buckets.
Definition: storage.hh:271
Counter max
The maximum value to track.
Definition: storage.hh:267
Counter bucket_size
The number of entries in each bucket.
Definition: storage.hh:269
Counter min
The minimum value to track.
Definition: storage.hh:265
Params(Counter _min, Counter _max, Counter _bucket_size)
Definition: storage.hh:273
The parameters for a distribution stat.
Definition: storage.hh:461
size_type buckets
The number of buckets.
Definition: storage.hh:463
Params(size_type _buckets)
Definition: storage.hh:465
Data structure of sparse histogram.
Definition: types.hh:86
The parameters for a sparse histogram stat.
Definition: storage.hh:725
virtual ~StorageParams()=default

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