Skip to content

converting eventsource metadata list to a dict #117031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 27, 2025
Merged

converting eventsource metadata list to a dict #117031

merged 11 commits into from
Jun 27, 2025

Conversation

rcj1
Copy link
Contributor

@rcj1 rcj1 commented Jun 25, 2025

Closes #99816

This pull request implements the request described in #99816 of reducing the memory usage of EventSource in the case of high EventIDs by using a dictionary of EventMetadata instead of an array. This change resulted in no visible slowdown of WriteEvent, and memory usage was brought down to consistent levels across high and low EventIDs that matches the preexisting memory usage levels in the case of low EventIDs.

@Copilot Copilot AI review requested due to automatic review settings June 25, 2025 21:20
@rcj1 rcj1 requested a review from hoyosjs June 25, 2025 21:20
Copy link
Contributor

Tagging subscribers to this area: @tarekgh, @tommcdon, @steveisok, @pjanotti
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR replaces the fixed-size EventMetadata[] storage with a Dictionary<int, EventMetadata> to allow sparse and dynamic event ID mappings.

  • Swaps all m_eventData array uses to Dictionary<int, EventMetadata> and updates access patterns to use CollectionsMarshal.
  • Converts loops over array indices into dictionary key/value iterations and adapts initializers.
  • Adds using System.Runtime.InteropServices for CollectionsMarshal APIs.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/NativeRuntimeEventSource.cs Switched m_eventData to a dictionary, updated bounds check and payload decoding to use CollectionsMarshal.
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs Replaced array-based loops and initializers with dictionary equivalents; updated all indexing to use GetValueRefOrNullRef / GetValueRefOrAddDefault.

@rcj1 rcj1 enabled auto-merge (squash) June 25, 2025 23:29
@rcj1
Copy link
Contributor Author

rcj1 commented Jun 27, 2025

Blocked by #117039

Copy link
Member

@noahfalk noahfalk left a comment

Choose a reason for hiding this comment

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

LGTM aside from a little bit of formatting stuff and a couple unneeded null checks.

Perf-wise I'd be a bit surprised if this change didn't incur at least a few % extra CPU cost in WriteEvent for a scenario with an active EventListener but I think it would be an OK tradeoff for the sparse event id memory benefits.

@rcj1 rcj1 merged commit 64e178b into main Jun 27, 2025
136 of 143 checks passed
rcj1 added a commit that referenced this pull request Jun 27, 2025
@jkotas jkotas deleted the gh99816 branch June 27, 2025 14:31
@rcj1
Copy link
Contributor Author

rcj1 commented Jun 27, 2025

After implementing the fix, there is no significant and consistent slowdown based on the benchmarking done in the second code block below. In addition, the memory usage across the three scenarios has decreased to the original level of the LowIDs scenario, based on the first code block below.

Benchmark Time LowID private bytes HighID private bytes DecreasingIDs private bytes
Before 39.22us 8652K 26196K 14296K
After 39.56us 8408K 8732K 8352K
using System.Diagnostics.Tracing;

class Program
{
    static void Main(string[] args)
    {
        if (args.Length != 1)
        {
            Console.WriteLine("Usage: EventSourceMemUsage.exe <LowIDs/HighIDs/DecreasingIDs>");
        }

        switch (args[0])
        {
            case "LowIDs":
                TestLowIDsEventSource.Log.EventWriteInfo("This is an informational message");
                TestLowIDsEventSource.Log.EventWriteError("This is an informational message");
                break;
            case "HighIDs":
                TestHighIDsEventSource.Log.EventWriteInfo("This is an informational message");
                TestHighIDsEventSource.Log.EventWriteError("This is an informational message");
                break;
            case "DecreasingIDs":
                TestDecreasingEventSource.Log.EventWriteInfo("This is an informational message");
                TestDecreasingEventSource.Log.EventWriteError("This is an informational message");
                break;
            default:
                Console.WriteLine("Please enter one of LowIDs/HighIDs/DecreasingIDs");
                Environment.Exit(-1);
                break;
        }

        Console.ReadLine();
    }
}

[EventSource(Name = "Application Error", Guid = "a0e9b465-b939-57d7-b27d-95d8e925ff57")]
public sealed class TestLowIDsEventSource : EventSource
{
    public static TestLowIDsEventSource Log = new TestLowIDsEventSource();

    [Event(1, Level = EventLevel.Informational, Channel = EventChannel.Admin)]
    public void EventWriteInfo(string Message) => WriteEvent(1, Message);

    [Event(2, Level = EventLevel.Error, Channel = EventChannel.Admin)]
    public void EventWriteError(string Message) => WriteEvent(2, Message);
}

[EventSource(Name = "Application Error", Guid = "a0e9b465-b939-57d7-b27d-95d8e925ff57")]
public sealed class TestHighIDsEventSource : EventSource
{
    public static TestHighIDsEventSource Log = new TestHighIDsEventSource();

    [Event(60000)]
    public void EventWriteInfo(string Message) => WriteEvent(60000, Message);

    [Event(60001)]
    public void EventWriteError(string Message) => WriteEvent(60001, Message);
}

[EventSource(Name = "Application Error", Guid = "a0e9b465-b939-57d7-b27d-95d8e925ff57")]
public sealed class TestDecreasingEventSource : EventSource
{
    public static TestDecreasingEventSource Log = new TestDecreasingEventSource();

    [Event(60001)]
    public void EventWriteError(string Message) => WriteEvent(60001, Message);

    [Event(60000)]
    public void EventWriteInfo(string Message) => WriteEvent(60000, Message);
}
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

namespace BasicEventSourceTests
{
    public class MultipleListenerEventSource : EventSource
    {
        public static MultipleListenerEventSource Log = new MultipleListenerEventSource();

        [Event(1)]
        public void TestEvent1(string message) => WriteEvent(1, message);

        [Event(2)]
        public void TestEvent2(string message) => WriteEvent(2, message);

        [Event(3)]
        public void TestEvent3(string message) => WriteEvent(3, message);

        [Event(4)]
        public void TestEvent4(string message) => WriteEvent(4, message);

        [Event(5)]
        public void TestEvent5(string message) => WriteEvent(5, message);

        [Event(6)]
        public void TestEvent6(string message) => WriteEvent(6, message);
    }

    public class TestEventListener : EventListener
    {
        private readonly List<string> _events = new List<string>();
        private int _eventCount;

        protected override void OnEventSourceCreated(EventSource eventSource)
        {
            if (eventSource is MultipleListenerEventSource)
            {
                EnableEvents(eventSource, EventLevel.Verbose);
            }
        }

        protected override void OnEventWritten(EventWrittenEventArgs eventData)
        {

        }

        public int EventCount => _eventCount;

        public void Reset()
        {
            _eventCount = 0;
            lock (_events)
            {
                _events.Clear();
            }
        }
    }

    [SimpleJob]
    public class MultipleListenerBenchmark
    {
        private List<TestEventListener> _listeners = new List<TestEventListener>();

        // use 10 listeners
        [Params(10)]
        public int ListenerCount { get; set; }

        [GlobalSetup]
        public void Setup()
        {
            foreach (var listener in _listeners)
            {
                listener?.Dispose();
            }
            _listeners.Clear();

            for (int i = 0; i < ListenerCount; i++)
            {
                _listeners.Add(new TestEventListener());
            }
        }

        [GlobalCleanup]
        public void Cleanup()
        {
            foreach (var listener in _listeners)
            {
                listener?.Dispose();
            }
            _listeners.Clear();
        }

        [Benchmark]
        public void Write600Events()
        {
            for (int i = 0; i < 100; i++)
            {
                var eventSource = MultipleListenerEventSource.Log;
                eventSource.TestEvent1("Test message 1");
                eventSource.TestEvent2("Test message 2");
                eventSource.TestEvent3("Test message 3");
                eventSource.TestEvent4("Test message 4");
                eventSource.TestEvent5("Test message 5");
                eventSource.TestEvent6("Test message 6");
            }
        }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            if (args.Length > 0 && args[0] == "benchmark")
            {
                var summary = BenchmarkRunner.Run<MultipleListenerBenchmark>();
            }
            else
            {
                Console.WriteLine("Testing with 10 listeners...");

                var listeners = new List<TestEventListener>();
                for (int i = 0; i < 10; i++)
                {
                    listeners.Add(new TestEventListener());
                }

                var eventSource = MultipleListenerEventSource.Log;

                for (int i = 0; i < 100; i++)
                {
                    eventSource.TestEvent1($"Message {i}");
                    eventSource.TestEvent2($"Message {i}");
                }

                Console.WriteLine($"EventSource enabled: {eventSource.IsEnabled()}");
                Console.WriteLine($"Events written to {listeners.Count} listeners");

                foreach (var listener in listeners)
                {
                    Console.WriteLine($"Listener received {listener.EventCount} events");
                    listener.Dispose();
                }
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Higher Memory Usage for the EventSource Class
3 participants