Skip to content

Commit

Permalink
Enable EventPipe support in MacOS (#86766)
Browse files Browse the repository at this point in the history
* Enable EventPipe support in MacOS

* Fix FreeBSD build break

* FB
  • Loading branch information
LakshanF authored Jun 6, 2023
1 parent cc4d46e commit 838968e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
4 changes: 2 additions & 2 deletions eng/pipelines/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ extends:
extraStepsTemplate: /eng/pipelines/coreclr/nativeaot-post-build-steps.yml
extraStepsParameters:
creator: dotnet-bot
testBuildArgs: 'nativeaot tree ";nativeaot;Loader;Interop;tracing/eventpipe/config;tracing/eventpipe/simpleprovidervalidation;" test tracing/eventcounter/runtimecounters.csproj /p:BuildNativeAotFrameworkObjects=true'
testBuildArgs: 'nativeaot tree ";nativeaot;Loader;Interop;tracing/eventpipe/config;" test tracing/eventcounter/runtimecounters.csproj /p:BuildNativeAotFrameworkObjects=true'
liveLibrariesBuildConfig: Release
testRunNamePrefixSuffix: NativeAOT_$(_BuildConfig)
extraVariablesTemplates:
Expand Down Expand Up @@ -290,7 +290,7 @@ extends:
extraStepsTemplate: /eng/pipelines/coreclr/nativeaot-post-build-steps.yml
extraStepsParameters:
creator: dotnet-bot
testBuildArgs: nativeaot tree nativeaot
testBuildArgs: 'nativeaot tree ";nativeaot;tracing/eventpipe/simpleprovidervalidation;"'
liveLibrariesBuildConfig: Release
testRunNamePrefixSuffix: NativeAOT_$(_BuildConfig)
extraVariablesTemplates:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ The .NET Foundation licenses this file to you under the MIT license.
<IlcRPath Condition="'$(IlcRPath)' == '' and '$(_IsApplePlatform)' == 'true'">@executable_path</IlcRPath>

<EventPipeName>libeventpipe-disabled</EventPipeName>
<EventPipeName Condition="'$(EnableNativeEventPipe)' == 'true' or ('$(EventSourceSupport)' == 'true' and '$(_IsApplePlatform)' != 'true')">libeventpipe-enabled</EventPipeName>
<EventPipeName Condition="'$(EnableNativeEventPipe)' == 'true' or '$(EventSourceSupport)' == 'true'">libeventpipe-enabled</EventPipeName>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/nativeaot/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@
<DefineConstants Condition="'$(FeatureObjCMarshal)' == 'true'">FEATURE_OBJCMARSHAL;$(DefineConstants)</DefineConstants>
</PropertyGroup>
<PropertyGroup>
<FeaturePerfTracing>false</FeaturePerfTracing>
<FeaturePerfTracing Condition="'$(TargetsWindows)' == 'true' or ('$(TargetsUnix)' == 'true' and '$(TargetsOSX)' != 'true' and '$(TargetsiOS)' != 'true' and '$(TargetsMacCatalyst)' != 'true' and '$(TargetstvOS)' != 'true')">true</FeaturePerfTracing>
<FeaturePerfTracing>true</FeaturePerfTracing>
</PropertyGroup>
<PropertyGroup>
<DefineConstants Condition="'$(FeaturePerfTracing)' == 'true'">FEATURE_PERFTRACING;$(DefineConstants)</DefineConstants>
Expand Down
68 changes: 67 additions & 1 deletion src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include <sys/types.h>

#ifdef __APPLE__
#include <sys/sysctl.h>
#endif

#ifdef __NetBSD__
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <kvm.h>
#endif

#ifdef __FreeBSD__
#include <sys/sysctl.h>
#include <sys/user.h>
#endif

#include <eventpipe/ep-rt-config.h>

#ifdef ENABLE_PERFTRACING
Expand All @@ -13,6 +31,7 @@

bool aot_ipc_get_process_id_disambiguation_key(uint32_t process_id, uint64_t *key);

// Consider moving this code to shared library code. See https://github.com/dotnet/runtime/issues/87069
bool
aot_ipc_get_process_id_disambiguation_key(
uint32_t process_id,
Expand All @@ -26,7 +45,54 @@ aot_ipc_get_process_id_disambiguation_key(
*key = 0;

// Mono implementation, restricted just to Unix
#ifdef TARGET_UNIX
#if defined (__APPLE__) || defined (__FreeBSD__)
// On OS X, we return the process start time expressed in Unix time (the number of seconds
// since the start of the Unix epoch).
struct kinfo_proc info = {};
size_t size = sizeof (info);
int mib [4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_id };

const int result_sysctl = sysctl (mib, sizeof(mib)/sizeof(*mib), &info, &size, NULL, 0);
if (result_sysctl == 0) {
#if defined (__APPLE__)
struct timeval proc_starttime = info.kp_proc.p_starttime;
#else // __FreeBSD__
struct timeval proc_starttime = info.ki_start;
#endif
long seconds_since_epoch = proc_starttime.tv_sec;
*key = seconds_since_epoch;
return true;
} else {
EP_ASSERT (!"Failed to get start time of a process.");
return false;
}
#elif defined (__NetBSD__)
// On NetBSD, we return the process start time expressed in Unix time (the number of seconds
// since the start of the Unix epoch).
kvm_t *kd;
int cnt;
struct kinfo_proc2 *info;

kd = kvm_open (NULL, NULL, NULL, KVM_NO_FILES, "kvm_open");
if (!kd) {
EP_ASSERT (!"Failed to get start time of a process.");
return false;
}

info = kvm_getproc2 (kd, KERN_PROC_PID, process_id, sizeof (struct kinfo_proc2), &cnt);
if (!info || cnt < 1) {
kvm_close (kd);
EP_ASSERT (!"Failed to get start time of a process.");
return false;
}

kvm_close (kd);

long seconds_since_epoch = info->p_ustart_sec;
*key = seconds_since_epoch;

return true;
#elif defined (__linux__)

// Here we read /proc/<pid>/stat file to get the start time for the process.
// We return this value (which is expressed in jiffies since boot time).
Expand Down

0 comments on commit 838968e

Please sign in to comment.