Project
Loading...
Searching...
No Matches
CapacityEstimator.h
Go to the documentation of this file.
1// Copyright 2019-2026 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
15
16#ifndef TRACKINGITSU_INCLUDE_CAPACITYESTIMATOR_H_
17#define TRACKINGITSU_INCLUDE_CAPACITYESTIMATOR_H_
18
19#include <algorithm>
20#include <cstddef>
21#include <cstdint>
22#include <limits>
23#include <memory>
24
25namespace o2::its
26{
27
39constexpr const char* const SlabSiteNames[SlabSite::NSlabSite]{"Tracklets", "Cells", "Neighbours", "RoadCandidates", "Roads", "TrackSeeds", "TracksExtended", "Tracks"};
40
42{
43 public:
44 struct Config {
45 float alpha{0.2f};
46 float marginInit{1.30f};
47 float marginMin{1.10f};
48 float marginMax{4.00f};
49 float marginUp{1.50f};
50 float marginOverflowSlack{1.05f};
51 float marginDown{0.98f};
52 float lowWatermark{0.60f};
53 uint32_t decayAfter{2};
54 size_t floorSlots{1024};
55 };
56
57 using KeyType = uint64_t;
58
59 struct Decoded {
63 int slot;
64 };
65
66 static constexpr KeyType makeKey(SlabSite site, int iteration, int variant, int slot) noexcept
67 {
68 return (static_cast<KeyType>(site) << 56) |
69 (static_cast<KeyType>(iteration & 0xFF) << 48) |
70 (static_cast<KeyType>(variant & 0xFFFF) << 32) |
71 static_cast<KeyType>(static_cast<uint32_t>(slot));
72 }
73
74 static constexpr Decoded decodeKey(KeyType key) noexcept
75 {
76 return {
77 .site = static_cast<SlabSite>((key >> 56) & 0xFF),
78 .iteration = static_cast<int>((key >> 48) & 0xFF),
79 .variant = static_cast<int>((key >> 32) & 0xFFFF),
80 .slot = static_cast<int>(static_cast<uint32_t>(key & 0xFFFFFFFF))};
81 }
82
83 static constexpr int makeVariant(int high, int low) noexcept
84 {
85 return ((high & 0xFF) << 8) | (low & 0xFF);
86 }
87
88 static constexpr int getVariantHigh(int variant) noexcept
89 {
90 return (variant >> 8) & 0xFF;
91 }
92
93 static constexpr int getVariantLow(int variant) noexcept
94 {
95 return variant & 0xFF;
96 }
97
99 explicit CapacityEstimator(Config cfg);
103
104 void reset();
105 size_t capacity(uint64_t key, double scale) const;
106 size_t peakCapacity(uint64_t key) const;
107 double expected(uint64_t key, double scale) const;
108 void update(uint64_t key, double scale, size_t emitted, size_t capacityUsed, bool overflowed, bool memoryLimited);
109 void print() const;
110
111 private:
112 struct Impl;
113 std::unique_ptr<Impl> mImpl;
114};
115
116template <typename Emit>
117int runOnSlab(CapacityEstimator& estimator, const CapacityEstimator::KeyType key, const double scale, Emit&& emit, const size_t floorCapacity = 0)
118{
119 const auto toInt = [](const size_t v) { return static_cast<int>(std::min(v, static_cast<size_t>(std::numeric_limits<int>::max()))); };
120 const int initialCapacity = toInt(estimator.capacity(key, scale));
121 int capacity = std::max(initialCapacity, toInt(floorCapacity));
122 int emitted = 0;
123 bool overflowed = false;
124 bool needsRetry = false;
125 do {
126 const int attemptCapacity = capacity;
127 emitted = emit(attemptCapacity);
128 needsRetry = emitted > attemptCapacity;
129 overflowed |= needsRetry;
130 capacity = emitted;
131 } while (needsRetry);
132 estimator.update(key, scale, emitted, initialCapacity, overflowed, false);
133 return emitted;
134}
135
136} // namespace o2::its
137
138#endif /* TRACKINGITSU_INCLUDE_CAPACITYESTIMATOR_H_ */
StringRef key
static constexpr int getVariantLow(int variant) noexcept
size_t capacity(uint64_t key, double scale) const
void update(uint64_t key, double scale, size_t emitted, size_t capacityUsed, bool overflowed, bool memoryLimited)
static constexpr int makeVariant(int high, int low) noexcept
static constexpr KeyType makeKey(SlabSite site, int iteration, int variant, int slot) noexcept
CapacityEstimator & operator=(const CapacityEstimator &)=delete
size_t peakCapacity(uint64_t key) const
static constexpr Decoded decodeKey(KeyType key) noexcept
CapacityEstimator(const CapacityEstimator &)=delete
static constexpr int getVariantHigh(int variant) noexcept
GLfloat GLfloat GLfloat alpha
Definition glcorearb.h:279
const GLdouble * v
Definition glcorearb.h:832
constexpr const char *const SlabSiteNames[SlabSite::NSlabSite]
int runOnSlab(CapacityEstimator &estimator, const CapacityEstimator::KeyType key, const double scale, Emit &&emit, const size_t floorCapacity=0)
std::map< std::string, ID > expected