Skip to content

Commit 616b41e

Browse files
[IAST] Delay dataflow creation (#7451)
## Summary of changes Delay dataflow instance creation until activation is received from managed side ## Reason for change ## Implementation details ## Test coverage ## Other details <!-- Fixes #{issue} --> <!-- ⚠️ Note: Where possible, please obtain 2 approvals prior to merging. Unless CODEOWNERS specifies otherwise, for external teams it is typically best to have one review from a team member, and one review from apm-dotnet. Trivial changes do not require 2 reviews. MergeQueue is NOT enabled in this repository. If you have write access to the repo, the PR has 1-2 approvals (see above), and all of the required checks have passed, you can use the Squash and Merge button to merge the PR. If you don't have write access, or you need help, reach out in the #apm-dotnet channel in Slack. -->
1 parent 88c29fe commit 616b41e

File tree

4 files changed

+34
-35
lines changed

4 files changed

+34
-35
lines changed

tracer/src/Datadog.Tracer.Native/cor_profiler.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,7 @@ HRESULT STDMETHODCALLTYPE CorProfiler::Initialize(IUnknown* cor_profiler_info_un
352352
}
353353

354354
// CallSite stuff
355-
if (IsCallSiteManagedActivationEnabled())
356-
{
357-
_dataflow = new iast::Dataflow(info_, rejit_handler, runtime_information_);
358-
}
359-
else
355+
if (!IsCallSiteManagedActivationEnabled())
360356
{
361357
Logger::Info("Callsite managed activation is disabled.");
362358
bool isRaspEnabled = IsRaspEnabled();
@@ -2177,10 +2173,17 @@ int CorProfiler::RegisterIastAspects(WCHAR** aspects, int aspectsLength, UINT32
21772173
{
21782174
auto _ = trace::Stats::Instance()->InitializeProfilerMeasure();
21792175

2180-
if (_dataflow != nullptr)
2176+
auto dataflow = _dataflow;
2177+
if (dataflow == nullptr && IsCallSiteManagedActivationEnabled())
2178+
{
2179+
dataflow = new iast::Dataflow(info_, rejit_handler, runtime_information_);
2180+
}
2181+
2182+
if (dataflow != nullptr)
21812183
{
21822184
Logger::Info("Registering Callsite Aspects.");
2183-
_dataflow->LoadAspects(aspects, aspectsLength, enabledCategories, platform);
2185+
dataflow->LoadAspects(aspects, aspectsLength, enabledCategories, platform);
2186+
_dataflow = dataflow;
21842187
return aspectsLength;
21852188
}
21862189
else

tracer/src/Datadog.Tracer.Native/iast/dataflow.cpp

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -179,32 +179,29 @@ Dataflow::Dataflow(ICorProfilerInfo* profiler, std::shared_ptr<RejitHandler> rej
179179
trace::Logger::Error("Dataflow::Dataflow -> Something very wrong happened, as QI on ICorProfilerInfo3 failed. Disabling Dataflow. HRESULT : ", Hex(hr));
180180
}
181181

182-
_initialized = (_profiler != nullptr);
183-
_aspectsLoaded = false;
182+
_initialized = false;
184183
}
184+
185185
Dataflow::~Dataflow()
186186
{
187-
Destroy();
188-
}
189-
190-
void Dataflow::Destroy()
191-
{
192-
if (_initialized)
193-
{
194-
_initialized = false;
195-
_aspectsLoaded = false;
196-
REL(_profiler);
197-
DEL_MAP_VALUES(_modules);
198-
DEL_MAP_VALUES(_appDomains);
199-
DEL_MAP_VALUES(_moduleAspects);
200-
}
187+
_initialized = false;
188+
REL(_profiler);
189+
DEL_MAP_VALUES(_modules);
190+
DEL_MAP_VALUES(_appDomains);
191+
DEL_MAP_VALUES(_moduleAspects);
201192
}
202193

203194
void Dataflow::LoadAspects(WCHAR** aspects, int aspectsLength, UINT32 enabledCategories, UINT32 platform)
204195
{
205196
// Init aspects
206197
DBG("Dataflow::LoadAspects -> Processing aspects... ", aspectsLength, " Enabled categories: ", enabledCategories, " Platform: ", platform);
207198

199+
if (aspectsLength > 10)
200+
{
201+
_aspectClasses.reserve(aspectsLength / 10);
202+
_aspects.reserve(aspectsLength);
203+
}
204+
208205
DataflowAspectClass* aspectClass = nullptr;
209206
for (int x = 0; x < aspectsLength; x++)
210207
{
@@ -243,7 +240,7 @@ void Dataflow::LoadAspects(WCHAR** aspects, int aspectsLength, UINT32 enabledCat
243240
DEL_MAP_VALUES(moduleAspects);
244241

245242
trace::Logger::Info("Dataflow::LoadAspects -> read ", _aspects.size(), " aspects");
246-
_aspectsLoaded = true;
243+
_initialized = true;
247244
}
248245

249246
void Dataflow::LoadSecurityControls()
@@ -369,7 +366,7 @@ void Dataflow::LoadSecurityControls()
369366

370367
HRESULT Dataflow::AppDomainShutdown(AppDomainID appDomainId)
371368
{
372-
if (!_aspectsLoaded)
369+
if (!_initialized)
373370
{
374371
return S_OK;
375372
}
@@ -388,7 +385,7 @@ HRESULT Dataflow::AppDomainShutdown(AppDomainID appDomainId)
388385

389386
HRESULT Dataflow::ModuleLoaded(ModuleID moduleId, ModuleInfo** pModuleInfo)
390387
{
391-
if (!_aspectsLoaded)
388+
if (!_initialized)
392389
{
393390
return S_OK;
394391
}
@@ -399,7 +396,7 @@ HRESULT Dataflow::ModuleLoaded(ModuleID moduleId, ModuleInfo** pModuleInfo)
399396

400397
HRESULT Dataflow::ModuleUnloaded(ModuleID moduleId)
401398
{
402-
if (!_aspectsLoaded)
399+
if (!_initialized)
403400
{
404401
return S_OK;
405402
}
@@ -618,7 +615,7 @@ MethodInfo* Dataflow::GetMethodInfo(ModuleID moduleId, mdMethodDef methodId)
618615

619616
bool Dataflow::IsInlineEnabled(ModuleID calleeModuleId, mdToken calleeMethodId)
620617
{
621-
if (!_aspectsLoaded)
618+
if (!_initialized)
622619
{
623620
return true;
624621
}
@@ -632,7 +629,7 @@ bool Dataflow::IsInlineEnabled(ModuleID calleeModuleId, mdToken calleeMethodId)
632629
}
633630
bool Dataflow::JITCompilationStarted(ModuleID moduleId, mdToken methodId)
634631
{
635-
if (!_aspectsLoaded)
632+
if (!_initialized)
636633
{
637634
return false;
638635
}
@@ -642,7 +639,7 @@ bool Dataflow::JITCompilationStarted(ModuleID moduleId, mdToken methodId)
642639
}
643640
MethodInfo* Dataflow::JITProcessMethod(ModuleID moduleId, mdToken methodId, trace::FunctionControlWrapper* pFunctionControl)
644641
{
645-
if (!_aspectsLoaded)
642+
if (!_initialized)
646643
{
647644
return nullptr;
648645
}
@@ -765,7 +762,6 @@ bool Dataflow::InstrumentInstruction(DataflowContext& context, std::vector<Dataf
765762

766763
void Dataflow::Shutdown()
767764
{
768-
Destroy();
769765
}
770766
RejitHandlerModule* Dataflow::GetOrAddModule(ModuleID moduleId)
771767
{
@@ -784,7 +780,7 @@ void Dataflow::AddNGenInlinerModule(ModuleID moduleId)
784780

785781
HRESULT Dataflow::RejitMethod(trace::FunctionControlWrapper& functionControl)
786782
{
787-
if (!_aspectsLoaded)
783+
if (!_initialized)
788784
{
789785
return S_FALSE;
790786
}

tracer/src/Datadog.Tracer.Native/iast/dataflow.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ namespace iast
6565

6666
void LoadSecurityControls();
6767
protected:
68-
bool _initialized = true;
69-
bool _aspectsLoaded = false;
68+
bool _initialized = false;
7069
bool _setILOnJit = false;
7170

7271
std::vector<DataflowAspectClass*> _aspectClasses;
@@ -80,7 +79,6 @@ namespace iast
8079
static bool InstrumentInstruction(DataflowContext& context, std::vector<DataflowAspectReference*>& aspects);
8180

8281
public:
83-
void Destroy();
8482
HRESULT AppDomainShutdown(AppDomainID appDomainId);
8583
HRESULT ModuleLoaded(ModuleID moduleId, ModuleInfo** pModuleInfo = nullptr);
8684
HRESULT ModuleUnloaded(ModuleID moduleId);

tracer/src/Datadog.Tracer.Native/rejit_handler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,13 @@ void RejitHandler::EnqueueRequestRejit(std::vector<MethodIdentifier>& rejitReque
325325
RejitHandler::RejitHandler(ICorProfilerInfo7* pInfo, std::shared_ptr<RejitWorkOffloader> work_offloader) :
326326
m_profilerInfo(pInfo), m_profilerInfo10(nullptr), m_work_offloader(work_offloader)
327327
{
328+
m_rejitters.reserve(5);
328329
}
329330

330331
RejitHandler::RejitHandler(ICorProfilerInfo10* pInfo, std::shared_ptr<RejitWorkOffloader> work_offloader) :
331332
m_profilerInfo(pInfo), m_profilerInfo10(pInfo), m_work_offloader(work_offloader)
332333
{
334+
m_rejitters.reserve(5);
333335
}
334336

335337
void RejitHandler::EnqueueForRejit(std::vector<ModuleID>& modulesVector, std::vector<mdMethodDef>& modulesMethodDef,

0 commit comments

Comments
 (0)