Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Basic infrastructure for binder tracing #27383

Merged
merged 9 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,25 @@ private void ActivityChanging(AsyncLocalValueChangedArgs<ActivityInfo?> args)
// currently we do nothing, as that seems better than setting to Guid.Emtpy.
}

// Assembly bind runtime activity name
private const string AssemblyBindName = "AssemblyBind";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we like to call this assembly binding for historic reasons, but our documentation and customers call this assembly loading. E.g.: https://docs.microsoft.com/en-us/dotnet/core/dependency-loading/loading-managed

I would stick with assembly loading for anything that is public facing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that mean we don't want to call it assembly binding in the events either? I put them under the Binder keyword (that used to be Fusion); I didn't want to have them under Loader, since I think we'd want to enable them separately. The new events are also under the AssemblyBinder task and named AssemblyBind*.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to call this AssemblyLoader / AssemblyLoad ?

I would be interested to know what other people (e.g. PMs) think about the right public facing names for this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe also AssemblyResolver/AssemblyResolve? Since we have the AssemblyLoadContext.Resolving and AppDomain.AssemblyResolve events.

Will check with PMs.


/// <summary>
/// Called by the runtime to start an assembly bind activity
/// </summary>
private static void StartAssemblyBind(ref Guid activityId, ref Guid relatedActivityId)
{
Instance.OnStart(NativeRuntimeEventSource.Log.Name, AssemblyBindName, 0, ref activityId, ref relatedActivityId, EventActivityOptions.Recursive);
}

/// <summary>
/// Called by the runtime to stop an assembly bind activity
/// </summary>
private static void StopAssemblyBind(ref Guid activityId)
{
Instance.OnStop(NativeRuntimeEventSource.Log.Name, AssemblyBindName, 0, ref activityId);
}

/// <summary>
/// Async local variables have the property that the are automatically copied whenever a task is created and used
/// while that task is running. Thus m_current 'flows' to any task that is caused by the current thread that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,8 @@ public static Assembly GetCallingAssembly()

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern uint GetAssemblyCount();

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool IsBinderTracingEnabled();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering whether AssemblyLoadContext would be a more appropriate place for this. Assembly is a grab bag of everything. AssemblyLoadContext is very specific to assembly loading and presumably quite a bit of the tracing will live on ALC.

Also, this method can be called just IsTracingEnabled.

}
}
4 changes: 4 additions & 0 deletions src/binder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(BINDER_COMMON_SOURCES
assemblyidentitycache.cpp
coreclrbindercommon.cpp
fusionassemblyname.cpp
bindertracing.cpp
)

set(BINDER_COMMON_HEADERS
Expand All @@ -38,6 +39,7 @@ set(BINDER_COMMON_HEADERS
inc/assemblyversion.hpp
inc/assemblyversion.inl
inc/bindertypes.hpp
inc/bindertracing.h
inc/bindinglog.hpp
inc/bindinglog.inl
inc/bindresult.hpp
Expand All @@ -64,11 +66,13 @@ set(BINDER_COMMON_HEADERS

set(BINDER_SOURCES
${BINDER_COMMON_SOURCES}
activitytracker.cpp
clrprivbinderassemblyloadcontext.cpp
)

set(BINDER_HEADERS
${BINDER_COMMON_HEADERS}
inc/activitytracker.h
inc/clrprivbinderassemblyloadcontext.h
inc/contextentry.hpp
)
Expand Down
39 changes: 39 additions & 0 deletions src/binder/activitytracker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// ============================================================
//
// activitytracker.cpp
//


//
// Helpers for interaction with the managed ActivityTracker
//
// ============================================================

#include "common.h"
#include "activitytracker.h"

void ActivityTracker::Start(/*out*/ GUID *activityId, /*out*/ GUID *relatedActivityId)
{
GCX_COOP();

PREPARE_NONVIRTUAL_CALLSITE(METHOD__ACTIVITY_TRACKER__START_ASSEMBLY_BIND);
DECLARE_ARGHOLDER_ARRAY(args, 1);
elinor-fung marked this conversation as resolved.
Show resolved Hide resolved
args[ARGNUM_0] = PTR_TO_ARGHOLDER(activityId);
args[ARGNUM_1] = PTR_TO_ARGHOLDER(relatedActivityId);

CALL_MANAGED_METHOD_NORET(args)
elinor-fung marked this conversation as resolved.
Show resolved Hide resolved
}

void ActivityTracker::Stop(/*out*/ GUID *activityId)
{
GCX_COOP();

PREPARE_NONVIRTUAL_CALLSITE(METHOD__ACTIVITY_TRACKER__STOP_ASSEMBLY_BIND);
DECLARE_ARGHOLDER_ARRAY(args, 1);
args[ARGNUM_0] = PTR_TO_ARGHOLDER(activityId);

CALL_MANAGED_METHOD_NORET(args)
}
116 changes: 116 additions & 0 deletions src/binder/bindertracing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// ============================================================
//
// bindertracing.cpp
//


//
// Implements helpers for binder tracing
//
// ============================================================

#include "common.h"
#include "bindertracing.h"

#include "activitytracker.h"

#ifdef FEATURE_EVENT_TRACE
#include "eventtracebase.h"
#endif // FEATURE_EVENT_TRACE

using namespace BINDER_SPACE;

namespace
{
thread_local bool s_trackingBind = false;
elinor-fung marked this conversation as resolved.
Show resolved Hide resolved

void FireAssemblyBindStart(const BinderTracing::AssemblyBindEvent::BindRequest &request)
{
#ifdef FEATURE_EVENT_TRACE
if (!EventEnabledAssemblyBindStart())
return;

GUID activityId = GUID_NULL;
GUID relatedActivityId = GUID_NULL;
ActivityTracker::Start(&activityId, &relatedActivityId);

FireEtwAssemblyBindStart(
GetClrInstanceId(),
request.AssemblyName,
request.AssemblyPath,
request.ParentAssembly,
request.AssemblyLoadContext,
&activityId,
&relatedActivityId);
#endif // FEATURE_EVENT_TRACE
}

void FireAssemblyBindStop(const BinderTracing::AssemblyBindEvent::BindRequest &request, bool success, const WCHAR *resultName, const WCHAR *resultPath, bool cached)
{
#ifdef FEATURE_EVENT_TRACE
if (!EventEnabledAssemblyBindStop())
return;

GUID activityId = GUID_NULL;
ActivityTracker::Stop(&activityId);

FireEtwAssemblyBindStop(
GetClrInstanceId(),
request.AssemblyName,
request.AssemblyPath,
request.ParentAssembly,
request.AssemblyLoadContext,
success,
resultName,
resultPath,
cached,
&activityId);
#endif // FEATURE_EVENT_TRACE
}
}

bool BinderTracing::IsEnabled()
{
#ifdef FEATURE_EVENT_TRACE
// Just check for the AssemblyBindStart event being enabled.
return EventEnabledAssemblyBindStart();
#endif // FEATURE_EVENT_TRACE
return false;
}

namespace BinderTracing
{
AssemblyBindEvent::AssemblyBindEvent(AssemblySpec *assemblySpec)
: m_bindRequest { assemblySpec }
, m_prevTrackingBind { s_trackingBind }
, m_success { false }
, m_cached { false }
{
_ASSERTE(assemblySpec != nullptr);

// ActivityTracker or EventSource may have triggered the system satellite load.
// Don't track system satellite binding to avoid potential infinite recursion.
s_trackingBind = BinderTracing::IsEnabled() && !m_bindRequest.AssemblySpec->IsMscorlibSatellite();
if (s_trackingBind)
{
m_bindRequest.AssemblySpec->GetFileOrDisplayName(ASM_DISPLAYF_VERSION | ASM_DISPLAYF_CULTURE | ASM_DISPLAYF_PUBLIC_KEY_TOKEN, m_bindRequest.AssemblyName);
FireAssemblyBindStart(m_bindRequest);
}
}

AssemblyBindEvent::~AssemblyBindEvent()
{
if (s_trackingBind)
FireAssemblyBindStop(m_bindRequest, m_success, m_resultName.GetUnicode(), m_resultPath.GetUnicode(), m_cached);

s_trackingBind = m_prevTrackingBind;
}

void AssemblyBindEvent::SetResult(PEAssembly *assembly)
{
m_success = assembly != nullptr;
}
}
17 changes: 17 additions & 0 deletions src/binder/inc/activitytracker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// activitytracker.h
//

#ifndef __ACTIVITY_TRACKER_H__
#define __ACTIVITY_TRACKER_H__

namespace ActivityTracker
{
void Start(/*out*/ GUID *activityId, /*out*/ GUID *relatedActivityId);
void Stop(/*out*/ GUID *activityId);
};

#endif // __ACTIVITY_TRACKER_H__
50 changes: 50 additions & 0 deletions src/binder/inc/bindertracing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// bindertracing.h
//

#ifndef __BINDER_TRACING_H__
#define __BINDER_TRACING_H__

class AssemblySpec;
class PEAssembly;

namespace BinderTracing
{
bool IsEnabled();

// If tracing is enabled, this class fires an assembly bind start event on construction
// and the corresponding stop event on destruction
class AssemblyBindEvent
elinor-fung marked this conversation as resolved.
Show resolved Hide resolved
{
public:
// This class assumes the assembly spec will have a longer lifetime than itself
AssemblyBindEvent(AssemblySpec *assemblySpec);
~AssemblyBindEvent();

void SetResult(PEAssembly *assembly);

struct BindRequest
{
AssemblySpec *AssemblySpec;
SString AssemblyName;
SString AssemblyPath;
SString ParentAssembly;
SString AssemblyLoadContext;
};

private:
BindRequest m_bindRequest;

bool m_prevTrackingBind;

bool m_success;
SString m_resultName;
SString m_resultPath;
bool m_cached;
};
};

#endif // __BINDER_TRACING_H__
Loading