|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.Diagnostics.Tracing; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Tracing.UserEvents.Tests.Common; |
| 11 | +using Microsoft.Diagnostics.Tracing; |
| 12 | + |
| 13 | +namespace Tracing.UserEvents.Tests.MultiThread |
| 14 | +{ |
| 15 | + [EventSource(Name = "DemoMultiThread")] |
| 16 | + public sealed class MultiThreadEventSource : EventSource |
| 17 | + { |
| 18 | + public static readonly MultiThreadEventSource Log = new MultiThreadEventSource(); |
| 19 | + |
| 20 | + private MultiThreadEventSource() {} |
| 21 | + |
| 22 | + [Event(1, Level = EventLevel.Informational)] |
| 23 | + public void WorkerEvent(int workerId) |
| 24 | + { |
| 25 | + WriteEvent(1, workerId); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + public static class MultiThread |
| 30 | + { |
| 31 | + private const int WorkerCount = 4; |
| 32 | + |
| 33 | + public static void MultiThreadTracee() |
| 34 | + { |
| 35 | + Task[] tasks = new Task[WorkerCount]; |
| 36 | + |
| 37 | + for (int i = 0; i < WorkerCount; i++) |
| 38 | + { |
| 39 | + int workerId = i; |
| 40 | + tasks[i] = Task.Run(() => |
| 41 | + { |
| 42 | + MultiThreadEventSource.Log.WorkerEvent(workerId); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + Task.WaitAll(tasks); |
| 47 | + } |
| 48 | + |
| 49 | + private static readonly Func<EventPipeEventSource, bool> s_traceValidator = source => |
| 50 | + { |
| 51 | + HashSet<int> seenWorkers = new HashSet<int>(); |
| 52 | + |
| 53 | + source.Dynamic.All += (TraceEvent e) => |
| 54 | + { |
| 55 | + if (!string.Equals(e.ProviderName, "DemoMultiThread", StringComparison.Ordinal)) |
| 56 | + { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (e.EventName is not "WorkerEvent") |
| 61 | + { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + int workerId = -1; |
| 66 | + try |
| 67 | + { |
| 68 | + workerId = (int)(e.PayloadByName("workerId") ?? -1); |
| 69 | + } |
| 70 | + catch (Exception ex) |
| 71 | + { |
| 72 | + Console.Error.WriteLine($"Exception while reading workerId payload: {ex}"); |
| 73 | + } |
| 74 | + |
| 75 | + if (workerId >= 0) |
| 76 | + { |
| 77 | + seenWorkers.Add(workerId); |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + source.Process(); |
| 82 | + |
| 83 | + for (int i = 0; i < WorkerCount; i++) |
| 84 | + { |
| 85 | + if (!seenWorkers.Contains(i)) |
| 86 | + { |
| 87 | + Console.Error.WriteLine($"Did not observe event for worker {i}."); |
| 88 | + return false; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return true; |
| 93 | + }; |
| 94 | + |
| 95 | + public static int Main(string[] args) |
| 96 | + { |
| 97 | + if (args.Length > 0 && args[0].Equals("tracee", StringComparison.OrdinalIgnoreCase)) |
| 98 | + { |
| 99 | + MultiThreadTracee(); |
| 100 | + return 0; |
| 101 | + } |
| 102 | + |
| 103 | + return UserEventsTestRunner.Run("multithread", typeof(MultiThread).Assembly.Location, s_traceValidator); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments