Project
Loading...
Searching...
No Matches
ComputingQuotaEvaluator.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 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.
11
16#include "Framework/Signpost.h"
17#include <Monitoring/Monitoring.h>
18
19#include <vector>
20#include <uv.h>
21#include <cassert>
22#include <fmt/core.h>
23#include <fmt/format.h>
24#include <fmt/ranges.h>
25
27
28namespace o2::framework
29{
30
32 : mRef(ref)
33{
34 auto& state = mRef.get<DeviceState>();
35 // The first offer is valid, but does not contain any resource
36 // so this will only work with some device which does not require
37 // any CPU. Notice this will have troubles if a given DPL process
38 // runs for more than a year.
40 .cpu = 0,
41 .memory = 0,
42 .sharedMemory = 0,
43 .timeslices = 0,
44 .runtime = -1,
45 .score = OfferScore::Unneeded,
46 .valid = true};
47 mInfos[0] = {
48 uv_now(state.loop),
49 0,
50 0};
51
52 // Creating a timer to check for expired offers
53 mTimer = (uv_timer_t*)malloc(sizeof(uv_timer_t));
54 uv_timer_init(state.loop, mTimer);
55}
56
58 std::vector<int> invalidOffers;
59 std::vector<int> otherUser;
60 std::vector<int> unexpiring;
61 std::vector<int> selectedOffers;
62 std::vector<int> expired;
63};
64
65bool ComputingQuotaEvaluator::selectOffer(int task, ComputingQuotaRequest const& selector, uint64_t now)
66{
67 O2_SIGNPOST_ID_GENERATE(qid, quota);
68
69 auto selectOffer = [&offers = this->mOffers, &infos = this->mInfos, task](int ref, uint64_t now) {
70 auto& selected = offers[ref];
71 auto& info = infos[ref];
72 selected.user = task;
73 if (info.firstUsed == 0) {
74 info.firstUsed = now;
75 }
76 info.lastUsed = now;
77 };
78
79 ComputingQuotaOffer accumulated;
80 static QuotaEvaluatorStats stats;
81
82 stats.invalidOffers.clear();
83 stats.otherUser.clear();
84 stats.unexpiring.clear();
85 stats.selectedOffers.clear();
86 stats.expired.clear();
87
88 auto summarizeWhatHappended = [ref = mRef](bool enough, std::vector<int> const& result, ComputingQuotaOffer const& totalOffer, QuotaEvaluatorStats& stats) -> bool {
89 auto& dpStats = ref.get<DataProcessingStats>();
90 if (result.size() == 1 && result[0] == 0) {
91 // LOG(LOGLEVEL) << "No particular resource was requested, so we schedule task anyways";
92 return enough;
93 }
94 O2_SIGNPOST_ID_GENERATE(sid, quota);
95 if (enough) {
96 O2_SIGNPOST_START(quota, sid, "summary", "%zu offers were selected for a total of: cpu %d, memory %lli, shared memory %lli",
97 result.size(), totalOffer.cpu, totalOffer.memory, totalOffer.sharedMemory);
98 for (auto& offer : result) {
99 // We pretend each offer id is a pointer, to have a unique id.
100 O2_SIGNPOST_ID_FROM_POINTER(oid, quota, (void*)(int64_t)(offer * 8));
101 O2_SIGNPOST_START(quota, oid, "offers", "Offer %d has been selected.", offer);
102 }
103 dpStats.updateStats({static_cast<short>(ProcessingStatsId::RESOURCES_SATISFACTORY), DataProcessingStats::Op::Add, 1});
104 } else {
105 O2_SIGNPOST_START(quota, sid, "summary", "Not enough resources to select offers.");
106 dpStats.updateStats({static_cast<short>(ProcessingStatsId::RESOURCES_MISSING), DataProcessingStats::Op::Add, 1});
107 if (result.size()) {
108 dpStats.updateStats({static_cast<short>(ProcessingStatsId::RESOURCES_INSUFFICIENT), DataProcessingStats::Op::Add, 1});
109 }
110 }
111 if (stats.invalidOffers.size()) {
112 O2_SIGNPOST_EVENT_EMIT(quota, sid, "summary", "The following offers were invalid: %s", fmt::format("{}", fmt::join(stats.invalidOffers, ", ")).c_str());
113 }
114 if (stats.otherUser.size()) {
115 O2_SIGNPOST_EVENT_EMIT(quota, sid, "summary", "The following offers were owned by other users: %s", fmt::format("{}", fmt::join(stats.otherUser, ", ")).c_str());
116 }
117 if (stats.expired.size()) {
118 O2_SIGNPOST_EVENT_EMIT(quota, sid, "summary", "The following offers are expired: %s", fmt::format("{}", fmt::join(stats.expired, ", ")).c_str());
119 }
120 if (stats.unexpiring.size() > 1) {
121 O2_SIGNPOST_EVENT_EMIT(quota, sid, "summary", "The following offers will never expire: %s", fmt::format("{}", fmt::join(stats.unexpiring, ", ")).c_str());
122 }
123 O2_SIGNPOST_END(quota, sid, "summary", "Done selecting offers.");
124
125 return enough;
126 };
127
128 bool enough = false;
129 int64_t minValidity = 0;
130
131 for (int i = 0; i != mOffers.size(); ++i) {
132 auto& offer = mOffers[i];
133 auto& info = mInfos[i];
134 if (enough) {
135 O2_SIGNPOST_EVENT_EMIT(quota, qid, "select", "We have enough offers. We can continue for computation.");
136 break;
137 }
138 // Ignore:
139 // - Invalid offers
140 // - Offers which belong to another task
141 // - Expired offers
142 if (offer.valid == false) {
143 O2_SIGNPOST_EVENT_EMIT(quota, qid, "select", "Offer %d is not valid. Skipping", i);
144 stats.invalidOffers.push_back(i);
145 continue;
146 }
147 if (offer.user != -1 && offer.user != task) {
148 O2_SIGNPOST_EVENT_EMIT(quota, qid, "select", "Offer %d already offered to some other user", i);
149 stats.otherUser.push_back(i);
150 continue;
151 }
152 if (offer.runtime < 0) {
153 stats.unexpiring.push_back(i);
154 } else if (offer.runtime + info.received < now) {
155 O2_SIGNPOST_EVENT_EMIT(quota, qid, "select", "Offer %d expired since %llu milliseconds and holds %llu MB and %llu timeslices",
156 i, now - offer.runtime - info.received, offer.sharedMemory / 1000000, offer.timeslices);
158 stats.expired.push_back(i);
159 continue;
160 } else {
161 O2_SIGNPOST_EVENT_EMIT(quota, qid, "select", "Offer %d still valid for %llu milliseconds, providing %llu MB and %llu timeslices",
162 i, offer.runtime + info.received - now, offer.sharedMemory / 1000000, offer.timeslices);
163 if (minValidity == 0) {
164 minValidity = offer.runtime + info.received - now;
165 }
166 minValidity = std::min(minValidity, (int64_t)(offer.runtime + info.received - now));
167 }
169 assert(offer.sharedMemory >= 0);
170 auto tmp = accumulated;
171 tmp.cpu += offer.cpu;
172 tmp.memory += offer.memory;
173 tmp.sharedMemory += offer.sharedMemory;
174 tmp.timeslices += offer.timeslices;
175 offer.score = selector(offer, accumulated);
176 switch (offer.score) {
178 O2_SIGNPOST_EVENT_EMIT(quota, qid, "select", "Offer %d considered not needed. Skipping", i);
179 continue;
181 O2_SIGNPOST_EVENT_EMIT(quota, qid, "select", "Offer %d considered Unsuitable. Skipping", i);
182 continue;
183 case OfferScore::More:
184 selectOffer(i, now);
185 accumulated = tmp;
186 stats.selectedOffers.push_back(i);
187 O2_SIGNPOST_EVENT_EMIT(quota, qid, "select", "Offer %d selected but not enough. %llu MB, %d cores and %llu timeslices are not enough.",
188 i, tmp.sharedMemory / 1000000, tmp.cpu, tmp.timeslices);
189 continue;
191 selectOffer(i, now);
192 accumulated = tmp;
193 stats.selectedOffers.push_back(i);
194 enough = true;
195 O2_SIGNPOST_EVENT_EMIT(quota, qid, "select", "Selected %zu offers providing %llu MB, %d cores and %llu timeslices are deemed enough.",
196 stats.selectedOffers.size(), tmp.sharedMemory / 1000000, tmp.cpu, tmp.timeslices);
197 break;
198 };
199 }
200
201 if (minValidity != 0) {
202 O2_SIGNPOST_EVENT_EMIT(quota, qid, "select", "Next offer to expire in %llu milliseconds", minValidity);
203 uv_timer_start(mTimer, [](uv_timer_t* handle) {
204 O2_SIGNPOST_ID_GENERATE(tid, quota);
205 O2_SIGNPOST_EVENT_EMIT(quota, tid, "select", "Offer should be expired by now, checking again."); }, minValidity + 100, 0);
206 }
207 // If we get here it means we never got enough offers, so we return false.
208 return summarizeWhatHappended(enough, stats.selectedOffers, accumulated, stats);
209}
210
211void ComputingQuotaEvaluator::consume(int id, ComputingQuotaConsumer& consumer, std::function<void(ComputingQuotaOffer const& accumulatedConsumed, ComputingQuotaStats& reportConsumedOffer)>& reportConsumedOffer)
212{
213 // This will report how much of the offers has to be considered consumed.
214 // Notice that actual memory usage might be larger, because we can over
215 // allocate.
216 consumer(id, mOffers, mStats, reportConsumedOffer);
217}
218
220{
221 for (int oi = 0; oi < mOffers.size(); ++oi) {
222 auto& offer = mOffers[oi];
223 if (offer.user != taskId) {
224 continue;
225 }
226 offer.user = -1;
227 // Disposing the offer so that the resource can be recyled.
230 if (oi == 0) {
231 return;
232 }
233 if (offer.valid == false) {
234 continue;
235 }
236 if (offer.sharedMemory <= 0) {
237 O2_SIGNPOST_ID_FROM_POINTER(oid, quota, (void*)(int64_t)(oi * 8));
238 O2_SIGNPOST_END(quota, oid, "offers", "Offer %d back to not needed.", oi);
239 offer.valid = false;
240 offer.score = OfferScore::Unneeded;
241 }
242 }
243}
244
246void ComputingQuotaEvaluator::updateOffers(std::vector<ComputingQuotaOffer>& pending, uint64_t now)
247{
248 O2_SIGNPOST_ID_GENERATE(oid, quota);
249 O2_SIGNPOST_START(quota, oid, "updateOffers", "Starting to process %zu received offers", pending.size());
250 int lastValid = -1;
251 for (size_t oi = 0; oi < mOffers.size(); oi++) {
252 auto& storeOffer = mOffers[oi];
253 auto& info = mInfos[oi];
254 if (pending.empty()) {
255 O2_SIGNPOST_END(quota, oid, "updateOffers", "No more pending offers to process");
256 return;
257 }
258 if (storeOffer.valid == true) {
259 O2_SIGNPOST_EVENT_EMIT(quota, oid, "updateOffers", "Skipping update of offer %zu because it's still valid", oi);
260 // In general we want to fill an invalid offer. If we do not find any
261 // we add to the last valid offer we found.
262 lastValid = oi;
263 continue;
264 }
265 info.received = now;
266 auto& offer = pending.back();
267 O2_SIGNPOST_EVENT_EMIT(quota, oid, "updateOffers", "Updating of offer %zu at %llu. Cpu: %d, Shared Memory %lli, Timeslices: %lli",
268 oi, now, offer.cpu, offer.sharedMemory, offer.timeslices);
269 storeOffer = offer;
270 storeOffer.valid = true;
271 pending.pop_back();
272 }
273 if (lastValid == -1) {
274 O2_SIGNPOST_END_WITH_ERROR(quota, oid, "updateOffers", "ComputingQuotaOffer losts. This should never happen.");
275 return;
276 }
277 auto& lastValidOffer = mOffers[lastValid];
278 for (auto& stillPending : pending) {
279 lastValidOffer.cpu += stillPending.cpu;
280 lastValidOffer.memory += stillPending.memory;
281 lastValidOffer.sharedMemory += stillPending.sharedMemory;
282 lastValidOffer.timeslices += stillPending.timeslices;
283 lastValidOffer.runtime = std::max(lastValidOffer.runtime, stillPending.runtime);
284 }
285 pending.clear();
286 auto& updatedOffer = mOffers[lastValid];
287 O2_SIGNPOST_END(quota, oid, "updateOffers", "Remaining offers cohalesced to %d. New values: Cpu%d, Shared Memory %lli, Timeslices %lli",
288 lastValid, updatedOffer.cpu, updatedOffer.sharedMemory, updatedOffer.timeslices);
289}
290
291void ComputingQuotaEvaluator::handleExpired(std::function<void(ComputingQuotaOffer const&, ComputingQuotaStats const& stats)> expirator)
292{
293 static int nothingToDoCount = mExpiredOffers.size();
294 O2_SIGNPOST_ID_GENERATE(qid, quota);
295 if (mExpiredOffers.size()) {
296 O2_SIGNPOST_EVENT_EMIT(quota, qid, "handleExpired", "Handling %zu expired offers", mExpiredOffers.size());
297 nothingToDoCount = 0;
298 } else {
299 if (nothingToDoCount == 0) {
300 nothingToDoCount++;
301 O2_SIGNPOST_EVENT_EMIT(quota, qid, "handleExpired", "No expired offers");
302 }
303 }
306 for (auto& ref : mExpiredOffers) {
307 auto& offer = mOffers[ref.index];
308 O2_SIGNPOST_ID_FROM_POINTER(oid, quota, (void*)(int64_t)(ref.index * 8));
309 if (offer.sharedMemory < 0 && offer.timeslices < 0) {
310 O2_SIGNPOST_END(quota, oid, "handleExpired", "Offer %d does not have any more resources. Marking it as invalid.", ref.index);
311 offer.valid = false;
312 offer.score = OfferScore::Unneeded;
313 continue;
314 }
315 // FIXME: offers should go through the driver client, not the monitoring
316 // api.
317 O2_SIGNPOST_END(quota, oid, "handleExpired", "Offer %d expired. Giving back %llu MB, %d cores and %llu timeslices",
318 ref.index, offer.sharedMemory / 1000000, offer.cpu, offer.timeslices);
319 mStats.totalExpiredBytes += std::max<int64_t>(offer.sharedMemory, 0);
320 mStats.totalExpiredTimeslices += std::max<int64_t>(offer.timeslices, 0);
322 expirator(offer, mStats);
323 // driverClient.tell("expired shmem {}", offer.sharedMemory);
324 // driverClient.tell("expired cpu {}", offer.cpu);
325 offer.sharedMemory = -1;
326 offer.timeslices = -1;
327 offer.valid = false;
328 offer.score = OfferScore::Unneeded;
329 }
330 mExpiredOffers.clear();
331}
332
333} // namespace o2::framework
benchmark::State & state
struct uv_timer_s uv_timer_t
int32_t i
#define O2_DECLARE_DYNAMIC_LOG(name)
Definition Signpost.h:489
#define O2_SIGNPOST_ID_FROM_POINTER(name, log, pointer)
Definition Signpost.h:505
#define O2_SIGNPOST_END(log, id, name, format,...)
Definition Signpost.h:608
#define O2_SIGNPOST_ID_GENERATE(name, log)
Definition Signpost.h:506
#define O2_SIGNPOST_EVENT_EMIT(log, id, name, format,...)
Definition Signpost.h:522
#define O2_SIGNPOST_END_WITH_ERROR(log, id, name, format,...)
Definition Signpost.h:616
#define O2_SIGNPOST_START(log, id, name, format,...)
Definition Signpost.h:602
void consume(int taskId, ComputingQuotaConsumer &consumed, std::function< void(ComputingQuotaOffer const &accumulatedConsumed, ComputingQuotaStats &)> &reportConsumedOffer)
void updateOffers(std::vector< ComputingQuotaOffer > &offers, uint64_t now)
now the time (e.g. uv_now) when invoked.
std::array< ComputingQuotaInfo, MAX_INFLIGHT_OFFERS > mInfos
Information about a given computing offer (e.g. when it was started to be used)
bool selectOffer(int task, ComputingQuotaRequest const &request, uint64_t now)
void handleExpired(std::function< void(ComputingQuotaOffer const &, ComputingQuotaStats const &)> reportExpired)
void dispose(int taskId)
Dispose offers for a given taskId.
std::array< ComputingQuotaOffer, MAX_INFLIGHT_OFFERS > mOffers
All the available offerts.
std::vector< ComputingQuotaOfferRef > mExpiredOffers
The offers which expired and need to be given back.
GLuint64EXT * result
Definition glcorearb.h:5662
GLint ref
Definition glcorearb.h:291
Defining PrimaryVertex explicitly as messageable.
std::function< void(int id, std::array< ComputingQuotaOffer, 32 > &, ComputingQuotaStats &, std::function< void(ComputingQuotaOffer const &, ComputingQuotaStats &stats)>)> ComputingQuotaConsumer
std::function< OfferScore(ComputingQuotaOffer const &offer, ComputingQuotaOffer const &accumulated)> ComputingQuotaRequest
int cpu
How many cores it can use.
Statistics on the offers consumed, expired.
Helper struct to hold statistics about the data processing happening.
@ Add
Update the rate of the metric given the amount since the last time.
Running state information of a given device.
Definition DeviceState.h:34