Project
Loading...
Searching...
No Matches
RateLimiter.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
19#include "Framework/Signpost.h"
20
21#include <fairmq/Device.h>
22#include <uv.h>
23#include <fairmq/shmem/Monitor.h>
24#include <fairmq/shmem/Common.h>
25#include <chrono>
26#include <thread>
27
29
30using namespace o2::framework;
31
32int RateLimiter::check(ProcessingContext& ctx, int maxInFlight, size_t minSHM)
33{
34 O2_SIGNPOST_ID_GENERATE(sid, rate_limiting);
35 if (!maxInFlight && !minSHM) {
36 return 0;
37 }
38 auto device = ctx.services().get<RawDeviceService>().device();
39 auto& deviceState = ctx.services().get<DeviceState>();
40 if (maxInFlight && device->GetChannels().count("metric-feedback")) {
41 auto& dtc = ctx.services().get<DataTakingContext>();
42 const auto& device = ctx.services().get<RawDeviceService>().device();
43 const auto& deviceContext = ctx.services().get<DeviceContext>();
44 bool timeout = deviceContext.exitTransitionTimeout;
45 bool timeoutForMessage = dtc.deploymentMode == DeploymentMode::OnlineDDS || dtc.deploymentMode == DeploymentMode::OnlineECS;
46 bool waitMessage = false;
47 int recvTimeout = 0;
48 auto startTime = std::chrono::system_clock::now();
49 static constexpr float MESSAGE_DELAY_TIME = 15.f;
50 while ((mSentTimeframes - mConsumedTimeframes) >= maxInFlight) {
51 if (recvTimeout != 0 && !waitMessage && (timeoutForMessage == false || std::chrono::duration_cast<std::chrono::duration<float>>(std::chrono::system_clock::now() - startTime).count() > MESSAGE_DELAY_TIME)) {
52 if (dtc.deploymentMode == DeploymentMode::OnlineDDS || dtc.deploymentMode == DeploymentMode::OnlineECS || dtc.deploymentMode == DeploymentMode::FST) {
53 O2_SIGNPOST_EVENT_EMIT_ALARM(rate_limiting, sid, "timeframe_ratelimit",
54 "Maximum number of TF in flight reached (%d: published %llu - finished %llu), waiting",
55 maxInFlight, mSentTimeframes, mConsumedTimeframes);
56 } else {
57 O2_SIGNPOST_EVENT_EMIT_INFO(rate_limiting, sid, "timeframe_ratelimit",
58 "Maximum number of TF in flight reached (%d: published %llu - finished %llu), waiting",
59 maxInFlight, mSentTimeframes, mConsumedTimeframes);
60 }
61 waitMessage = true;
62 timeoutForMessage = false;
63 }
64 auto msg = device->NewMessageFor("metric-feedback", 0, 0);
65 int64_t count = 0;
66 do {
67 count = device->Receive(msg, "metric-feedback", 0, recvTimeout);
68 if (timeout && count <= 0 && device->NewStatePending()) {
69 return 1;
70 }
71 } while (count <= 0 && recvTimeout > 0 && !timeoutForMessage);
72
73 if (count <= 0) {
74 recvTimeout = timeout || timeoutForMessage ? 1000 : -1;
75 continue;
76 }
77 assert(msg->GetSize() == 8);
78 mConsumedTimeframes = *(int64_t*)msg->GetData();
79 O2_SIGNPOST_EVENT_EMIT(rate_limiting, sid, "timeframe_ratelimit",
80 "Received %llu as consumed timeframes",
81 mConsumedTimeframes);
82 }
83 if (waitMessage) {
84 if (dtc.deploymentMode == DeploymentMode::OnlineDDS || dtc.deploymentMode == DeploymentMode::OnlineECS || dtc.deploymentMode == DeploymentMode::FST) {
85 O2_SIGNPOST_EVENT_EMIT_IMPORTANT(rate_limiting, sid, "timeframe_ratelimit",
86 "%lli / %d TF in flight, continue to publish",
87 (mSentTimeframes - mConsumedTimeframes), maxInFlight);
88 } else {
89 O2_SIGNPOST_EVENT_EMIT_INFO(rate_limiting, sid, "timeframe_ratelimit",
90 "%lli / %d TF in flight, continue to publish",
91 (mSentTimeframes - mConsumedTimeframes), maxInFlight);
92 }
93 }
94
95 bool doSmothThrottling = getenv("DPL_SMOOTH_RATE_LIMITING") && atoi(getenv("DPL_SMOOTH_RATE_LIMITING"));
96 if (doSmothThrottling) {
97 constexpr float factorStart = 0.7f;
98 constexpr float factorFinal = 0.98f;
99 constexpr float factorOfAverage = 0.7f;
100 constexpr int64_t iterationsFinal = 2;
101 auto curTime = std::chrono::system_clock::now();
102 if (mTfTimes.size() != maxInFlight) {
103 mTfTimes.resize(maxInFlight);
104 mTimeCountingSince = mSentTimeframes;
105 mFirstTime = curTime;
106 }
107 if (mSentTimeframes >= mTimeCountingSince + maxInFlight) {
108 float iterationDuration = std::chrono::duration_cast<std::chrono::duration<float>>(curTime - mTfTimes[mSentTimeframes % maxInFlight]).count();
109 float totalAverage = std::chrono::duration_cast<std::chrono::duration<float>>(curTime - mFirstTime).count() / (mSentTimeframes - mTimeCountingSince);
110 if (mSmothDelay == 0.f) {
111 mSmothDelay = iterationDuration / maxInFlight * factorStart;
112 LOG(debug) << "TF Throttling delay initialized to " << mSmothDelay;
113 } else {
114 float factor;
115 if (mSentTimeframes < maxInFlight) {
116 factor = factorStart;
117 } else if (mSentTimeframes >= (iterationsFinal + 1) * maxInFlight) {
118 factor = factorFinal;
119 } else {
120 factor = factorStart + (factorFinal - factorStart) * (float)(mSentTimeframes - maxInFlight) / (float)(iterationsFinal * maxInFlight);
121 }
122 float newDelay = iterationDuration / maxInFlight * factor;
123 if (newDelay > totalAverage) {
124 LOG(debug) << "TF Throttling: Correcting delay down to average " << newDelay << " --> " << totalAverage;
125 newDelay = totalAverage;
126 } else if (newDelay < factorOfAverage * totalAverage) {
127 LOG(debug) << "TF Throttling: Correcting delay up to " << factorOfAverage << " * average " << newDelay << " --> " << factorOfAverage * totalAverage;
128 newDelay = factorOfAverage * totalAverage;
129 }
130 mSmothDelay = (float)(maxInFlight - 1) / (float)maxInFlight * mSmothDelay + newDelay / (float)maxInFlight;
131 LOG(debug) << "TF Throttling delay updated to " << mSmothDelay << " (factor " << factor << " Duration " << iterationDuration / maxInFlight << " = " << iterationDuration << " / " << maxInFlight << " --> " << newDelay << ")";
132 }
133 float elapsed = std::chrono::duration_cast<std::chrono::duration<float>>(curTime - mLastTime).count();
134 if (elapsed < mSmothDelay) {
135 LOG(debug) << "TF Throttling: Elapsed " << elapsed << " --> Waiting for " << mSmothDelay - elapsed;
136 uv_run(deviceState.loop, UV_RUN_NOWAIT);
137 std::this_thread::sleep_for(std::chrono::microseconds((size_t)((mSmothDelay - elapsed) * 1.e6f)));
138 }
139 }
140 mLastTime = std::chrono::system_clock::now();
141 mTfTimes[mSentTimeframes % maxInFlight] = curTime;
142 }
143 }
144 if (minSHM) {
145 int waitMessage = 0;
146 auto& runningWorkflow = ctx.services().get<RunningWorkflowInfo const>();
147 while (true) {
148 long freeMemory = -1;
149 try {
150 freeMemory = fair::mq::shmem::Monitor::GetFreeMemory(fair::mq::shmem::ShmId{fair::mq::shmem::makeShmIdStr(device->fConfig->GetProperty<uint64_t>("shmid"))}, runningWorkflow.shmSegmentId);
151 } catch (...) {
152 }
153 if (freeMemory == -1) {
154 try {
155 freeMemory = fair::mq::shmem::Monitor::GetFreeMemory(fair::mq::shmem::SessionId{device->fConfig->GetProperty<std::string>("session")}, runningWorkflow.shmSegmentId);
156 } catch (...) {
157 }
158 }
159 if (freeMemory == -1) {
160 throw std::runtime_error("Could not obtain free SHM memory");
161 }
162 uint64_t freeSHM = freeMemory;
163 if (freeSHM > minSHM) {
164 if (waitMessage) {
165 LOG(important) << "Sufficient SHM memory free (" << freeSHM << " >= " << minSHM << "), continuing to publish";
166 }
167 static bool showReport = getenv("DPL_REPORT_PROCESSING") && atoi(getenv("DPL_REPORT_PROCESSING"));
168 if (showReport) {
169 LOG(info) << "Free SHM Report: " << freeSHM;
170 }
171 break;
172 }
173 if (waitMessage == 0) {
174 LOG(alarm) << "Free SHM memory too low: " << freeSHM << " < " << minSHM << ", waiting";
175 waitMessage = 1;
176 }
177 usleep(30000);
178 }
179 }
180 mSentTimeframes++;
181 return 0;
182}
std::ostringstream debug
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char Directory with events to Events directory is and not inside events Show standalone event Run event generator maximum time bin of continuous for triggered for automatic continuous for automatic continuous triggered uint64_t
configStandalone Reconstruction rec_tpc multiplier for the combined cluster track error during track following area in cm for the search of for z only used if decrease removal by factor
configStandalone Reconstruction rec_tpc multiplier for the combined cluster track error during track following area in cm for the search of for z only used if decrease removal by when above this local occupancy rowx Shift of TPC revert cluster flag edge bit distance to edge exceeds this ZOffset for secondary multiple of radius max P of electron K BB bands crossing max P of electron p BB bands crossing max P of electron d BB bands crossing max P of electron t BB bands crossing min accepted dEdxMaxTPC of the track Additive extra cluster error for Y2 if edge flag set Additive extra cluster error for Y2 if single set Additive extra cluster error for Y2 if or single set Additive extra cluster error for Z2 if or single set Extra errors to add to track when crossing depending on addErrorsCECrossing Inner portion of y range in sector that is not used in searching for extrapolated track candidates factor2YS for track merging factor2K for track merging Margin in cm of Y position when rejecting edge clusters based on uncorrected track Y Max fraction of shared clusters for tracklet Except pad from cluster finding if total number of charges in a fragment is above this Except pad from cluster finding if number of consecutive charges in a fragment is above this Threshold where a timebin is considered disabling the noisy pad check for that pad Treat signal above this minimum as saturated Smoothing factor for the exponential Highly Ionising Particle tail filter Number of timebins to use for the averaging Add additional custom track errors when crossing relative as fraction of Do N fit passes in final fit of< 0:reject at max -n hits") AddOptionRTC(rejectIFCLowRadiusCluster, uint8_t, 1, "", 0, "Reject clusters that get the IFC mask error during refit") AddOptionRTC(dEdxTruncLow, uint8_t, 2, "", 0, "Low truncation threshold, fraction of 128") AddOptionRTC(dEdxTruncHigh, uint8_t, 77, "", 0, "High truncation threshold, fraction of 128") AddOptionRTC(extrapolationTracking, int8_t, 1, "", 0, "Enable Extrapolation Tracking(prolong tracks to adjacent sectors to find short segments)") AddOptionRTC(disableRefitAttachment, uint8_t, 0, "", 0, "Bitmask to disable certain attachment steps during refit(1:attachment, 2:propagation, 4:loop following, 8:mirroring)") AddOptionRTC(rejectionStrategy, uint8_t, o2::gpu::GPUSettings::RejectionStrategyA, "", 0, "Enable rejection of TPC clusters for compression(0=no, 1=strategy A, 2=strategy B)") AddOptionRTC(mergeLoopersAfterburner, uint8_t, 1, "", 0, "Run afterburner for additional looper merging") AddOptionRTC(compressionTypeMask, uint8_t, o2::gpu::GPUSettings::CompressionFull, "", 0, "TPC Compression mode bits(1=truncate charge/width LSB, 2=differences, 4=track-model)") AddOptionRTC(compressionSortOrder, uint8_t, o2::gpu::GPUSettings::SortTime, "", 0, "Sort order of TPC compression(0=time, 1=pad, 2=Z-time-pad, 3=Z-pad-time, 4=no sorting(use incoming order))") AddOptionRTC(sigBitsCharge, uint8_t, 4, "", 0, "Number of significant bits for TPC cluster charge in compression mode 1") AddOptionRTC(sigBitsWidth, uint8_t, 3, "", 0, "Number of significant bits for TPC cluster width in compression mode 1") AddOptionRTC(dropLoopers, uint8_t, 0, "", 0, "Drop looping tracks starting from second loop") AddOptionRTC(mergerInterpolateErrors, uint8_t, 1, "", 0, "Use interpolation instead of extrapolation for chi2 based cluster rejection") AddOptionRTC(mergerInterpolateRejectAlsoOnCurrentPosition, uint8_t, 1, "", 0, "When using mergerInterpolateErrors, reject based on chi2 twice computed with interpolated and current track position starting from NDF > mergerNonInterpolateRejectMinNDF Merge tracks accross the central electrode Enable PID response Add sub threshold clusters in TPC dEdx computation OR mask of TPC flags that will reject the cluster in alternative dEdx Reject edge clusters based on uncorrected track Y during track fit Increase decrease removal by when on this number of pads from the edge h configStandalone Reconstruction rec_trd Max chi2 for TRD tracklets to be matched to a track Chi2 cut for strict matching mode Number of sigmas for ITS TPC track time error estimate Addition to search road around track prolongation along Y in cm Optimal tracklet rphi resolution in maximum chi2 per attached tracklet for TRD tracks take into account the full likelihood of the slope including exponential tails Tracks with less attached tracklets are discarded after the tracking Post trigger Pile up integration time in BCs fits all iterations but the last one in projections Use material lookup table for TPC refit Apply workarounds to avoid cyclic merge should not be needed tpc dyn configStandalone Processing proc_rtc Replace constant variables static by constexpr expressions Compile RTC in deterministic with NO_FAST_MATH flags and GPUCA_DETERMINISTIC_MODE define Use RTC to optimize GPU code h configStandalone Processing proc_rtctech Use a file lock to serialize access to the cache folder Print launch bounds used for RTC code as debugging for exit after Allow RTC with slave GPUReconstruction instances with optConstexpr and optSpecialcode Maximum number of RTC cache files per backend in the cache the oldest are Prepend RTC compilation commands by this string Load a parameter object containing the launch bounds from a file h configStandalone Processing proc_param TPC Cluster Z Error Parameterization h configStandalone Processing settings for neural network proc_nn std::string Specify inference device(cpu(default), rocm, cuda)") AddOption(nnInferenceDeviceId
configStandalone Reconstruction rec_tpc float
#define O2_DECLARE_DYNAMIC_LOG(name)
Definition Signpost.h:490
#define O2_SIGNPOST_EVENT_EMIT_INFO(log, id, name, format,...)
Definition Signpost.h:532
#define O2_SIGNPOST_EVENT_EMIT_ALARM(log, id, name, format,...)
Definition Signpost.h:584
#define O2_SIGNPOST_ID_GENERATE(name, log)
Definition Signpost.h:507
#define O2_SIGNPOST_EVENT_EMIT_IMPORTANT(log, id, name, format,...)
Definition Signpost.h:594
#define O2_SIGNPOST_EVENT_EMIT(log, id, name, format,...)
Definition Signpost.h:523
int check(ProcessingContext &ctx, int maxInFlight, size_t minSHM)
GLint GLsizei count
Definition glcorearb.h:399
GLbitfield GLuint64 timeout
Definition glcorearb.h:1573
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
Running state information of a given device.
Definition DeviceState.h:34
Information about the running workflow.
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
uint64_t const void const *restrict const msg
Definition x9.h:153