From 838968e2f4346107df1230973e31513719fb377e Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Tue, 6 Jun 2023 08:27:56 -0700 Subject: [PATCH] Enable EventPipe support in MacOS (#86766) * Enable EventPipe support in MacOS * Fix FreeBSD build break * FB --- eng/pipelines/runtime.yml | 4 +- .../Microsoft.NETCore.Native.Unix.targets | 2 +- src/coreclr/nativeaot/Directory.Build.props | 3 +- .../nativeaot/Runtime/eventpipe/ds-rt-aot.cpp | 68 ++++++++++++++++++- 4 files changed, 71 insertions(+), 6 deletions(-) diff --git a/eng/pipelines/runtime.yml b/eng/pipelines/runtime.yml index 23fbbff0770c5c..9fe2862edc9e83 100644 --- a/eng/pipelines/runtime.yml +++ b/eng/pipelines/runtime.yml @@ -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: @@ -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: diff --git a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets index ad2c022f4be7fc..76d6dd23f33899 100644 --- a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets +++ b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets @@ -48,7 +48,7 @@ The .NET Foundation licenses this file to you under the MIT license. @executable_path libeventpipe-disabled - libeventpipe-enabled + libeventpipe-enabled diff --git a/src/coreclr/nativeaot/Directory.Build.props b/src/coreclr/nativeaot/Directory.Build.props index 76c5cb9eecbb72..dce8e37c32fe38 100644 --- a/src/coreclr/nativeaot/Directory.Build.props +++ b/src/coreclr/nativeaot/Directory.Build.props @@ -65,8 +65,7 @@ FEATURE_OBJCMARSHAL;$(DefineConstants) - false - true + true FEATURE_PERFTRACING;$(DefineConstants) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp b/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp index 9bb8d088a8d99b..25272643d18a30 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp @@ -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 + +#ifdef __APPLE__ +#include +#endif + +#ifdef __NetBSD__ +#include +#include +#include +#include +#endif + +#ifdef __FreeBSD__ +#include +#include +#endif + #include #ifdef ENABLE_PERFTRACING @@ -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, @@ -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//stat file to get the start time for the process. // We return this value (which is expressed in jiffies since boot time).