From 5aed2a0b08f245ede233d2329d3b29731cd26731 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Thu, 25 May 2023 11:29:04 -0700 Subject: [PATCH 1/3] Enable EventPipe support in MacOS --- 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 | 65 ++++++++++++++++++- 4 files changed, 68 insertions(+), 6 deletions(-) diff --git a/eng/pipelines/runtime.yml b/eng/pipelines/runtime.yml index 23fbbff0770c5c..d04fe2f547ca98 100644 --- a/eng/pipelines/runtime.yml +++ b/eng/pipelines/runtime.yml @@ -212,7 +212,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: @@ -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: diff --git a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets index 2e124627d0a182..d0b663f6c80d8f 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..b26ec2aa674b4b 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp @@ -1,6 +1,22 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#ifdef __APPLE__ +#include +#endif + +#ifdef __NetBSD__ +#include +#include +#include +#include +#endif + +#ifdef __FreeBSD__ +#include +#include +#endif + #include #ifdef ENABLE_PERFTRACING @@ -26,7 +42,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). From af22e8326568231652f89a699891dc85646f8a42 Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Wed, 31 May 2023 10:58:15 -0700 Subject: [PATCH 2/3] Fix FreeBSD build break --- src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp b/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp index b26ec2aa674b4b..9c4ce776d5f2aa 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp @@ -1,6 +1,8 @@ // 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 From 1c88d81f01975e920eea7771dedaa1c58eda938a Mon Sep 17 00:00:00 2001 From: Lakshan Fernando Date: Fri, 2 Jun 2023 10:47:27 -0700 Subject: [PATCH 3/3] FB --- eng/pipelines/runtime.yml | 4 ++-- src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/eng/pipelines/runtime.yml b/eng/pipelines/runtime.yml index d04fe2f547ca98..9fe2862edc9e83 100644 --- a/eng/pipelines/runtime.yml +++ b/eng/pipelines/runtime.yml @@ -212,7 +212,7 @@ extends: extraStepsTemplate: /eng/pipelines/coreclr/nativeaot-post-build-steps.yml extraStepsParameters: creator: dotnet-bot - testBuildArgs: 'nativeaot tree ";nativeaot;tracing/eventpipe/simpleprovidervalidation;"' + testBuildArgs: nativeaot tree nativeaot 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/Runtime/eventpipe/ds-rt-aot.cpp b/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp index 9c4ce776d5f2aa..25272643d18a30 100644 --- a/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp +++ b/src/coreclr/nativeaot/Runtime/eventpipe/ds-rt-aot.cpp @@ -31,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,