Skip to content

Commit

Permalink
Nullable: System.Diagnostics.Tracing (dotnet/coreclr#24070)
Browse files Browse the repository at this point in the history
* Nullable: System.Diagnostics.Tracing

* apply feedback

* fix one more comment

Signed-off-by: dotnet-bot <[email protected]>
  • Loading branch information
krwq authored and dotnet-bot committed May 2, 2019
1 parent a7decab commit 3397f83
Show file tree
Hide file tree
Showing 42 changed files with 591 additions and 468 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System;
using System.Diagnostics;
using System.Threading;
Expand Down Expand Up @@ -99,7 +100,7 @@ public void OnStart(string providerName, string activityName, int task, ref Guid
// Check for recursion, and force-stop any activities if the activity already started.
if ((options & EventActivityOptions.Recursive) == 0)
{
ActivityInfo existingActivity = FindActiveActivity(fullActivityName, currentActivity);
ActivityInfo? existingActivity = FindActiveActivity(fullActivityName, currentActivity);
if (existingActivity != null)
{
OnStop(providerName, activityName, task, ref activityId);
Expand Down Expand Up @@ -154,13 +155,13 @@ public void OnStop(string providerName, string activityName, int task, ref Guid

for (; ; ) // This is a retry loop.
{
ActivityInfo currentActivity = m_current.Value;
ActivityInfo newCurrentActivity = null; // if we have seen any live activities (orphans), at he first one we have seen.
ActivityInfo? currentActivity = m_current.Value;
ActivityInfo? newCurrentActivity = null; // if we have seen any live activities (orphans), at he first one we have seen.

// Search to find the activity to stop in one pass. This insures that we don't let one mistake
// (stopping something that was not started) cause all active starts to be stopped
// By first finding the target start to stop we are more robust.
ActivityInfo activityToStop = FindActiveActivity(fullActivityName, currentActivity);
ActivityInfo? activityToStop = FindActiveActivity(fullActivityName, currentActivity);

// ignore stops where we can't find a start because we may have popped them previously.
if (activityToStop == null)
Expand All @@ -175,7 +176,7 @@ public void OnStop(string providerName, string activityName, int task, ref Guid
activityId = activityToStop.ActivityId;

// See if there are any orphans that need to be stopped.
ActivityInfo orphan = currentActivity;
ActivityInfo? orphan = currentActivity;
while (orphan != activityToStop && orphan != null)
{
if (orphan.m_stopped != 0) // Skip dead activities.
Expand Down Expand Up @@ -229,7 +230,7 @@ public void Enable()
// Catch the not Implemented
try
{
m_current = new AsyncLocal<ActivityInfo>(ActivityChanging);
m_current = new AsyncLocal<ActivityInfo?>(ActivityChanging);
}
catch (NotImplementedException) {
#if (!ES_BUILD_PCL && ! ES_BUILD_PN)
Expand All @@ -251,9 +252,9 @@ public void Enable()
/// <summary>
/// Searched for a active (nonstopped) activity with the given name. Returns null if not found.
/// </summary>
private ActivityInfo FindActiveActivity(string name, ActivityInfo startLocation)
private ActivityInfo? FindActiveActivity(string name, ActivityInfo? startLocation)
{
var activity = startLocation;
ActivityInfo? activity = startLocation;
while (activity != null)
{
if (name == activity.m_name && activity.m_stopped == 0)
Expand Down Expand Up @@ -293,7 +294,7 @@ private string NormalizeActivityName(string providerName, string activityName, i
/// </summary>
private class ActivityInfo
{
public ActivityInfo(string name, long uniqueId, ActivityInfo creator, Guid activityIDToRestore, EventActivityOptions options)
public ActivityInfo(string name, long uniqueId, ActivityInfo? creator, Guid activityIDToRestore, EventActivityOptions options)
{
m_name = name;
m_eventOptions = options;
Expand All @@ -314,10 +315,10 @@ public Guid ActivityId
}
}

public static string Path(ActivityInfo activityInfo)
public static string Path(ActivityInfo? activityInfo)
{
if (activityInfo == null)
return ("");
return "";
return Path(activityInfo.m_creator) + "/" + activityInfo.m_uniqueId.ToString();
}

Expand All @@ -326,7 +327,7 @@ public override string ToString()
return m_name + "(" + Path(this) + (m_stopped != 0 ? ",DEAD)" : ")");
}

public static string LiveActivities(ActivityInfo list)
public static string LiveActivities(ActivityInfo? list)
{
if (list == null)
return "";
Expand Down Expand Up @@ -400,7 +401,7 @@ private unsafe void CreateActivityPathGuid(out Guid idRet, out int activityPathG
private unsafe void CreateOverflowGuid(Guid* outPtr)
{
// Search backwards for an ancestor that has sufficient space to put the ID.
for (ActivityInfo ancestor = m_creator; ancestor != null; ancestor = ancestor.m_creator)
for (ActivityInfo? ancestor = m_creator; ancestor != null; ancestor = ancestor.m_creator)
{
if (ancestor.m_activityPathGuidOffset <= 10) // we need at least 2 bytes.
{
Expand Down Expand Up @@ -540,18 +541,18 @@ private static unsafe void WriteNibble(ref byte* ptr, byte* endPtr, uint value)
readonly internal EventActivityOptions m_eventOptions; // Options passed to start.
internal long m_lastChildID; // used to create a unique ID for my children activities
internal int m_stopped; // This work item has stopped
readonly internal ActivityInfo m_creator; // My parent (creator). Forms the Path() for the activity.
readonly internal ActivityInfo? m_creator; // My parent (creator). Forms the Path() for the activity.
readonly internal Guid m_activityIdToRestore; // The Guid to restore after a stop.
#endregion
}

// This callback is used to initialize the m_current AsyncLocal Variable.
// Its job is to keep the ETW Activity ID (part of thread local storage) in sync
// with m_current.ActivityID
void ActivityChanging(AsyncLocalValueChangedArgs<ActivityInfo> args)
void ActivityChanging(AsyncLocalValueChangedArgs<ActivityInfo?> args)
{
ActivityInfo cur = args.CurrentValue;
ActivityInfo prev = args.PreviousValue;
ActivityInfo? cur = args.CurrentValue;
ActivityInfo? prev = args.PreviousValue;

// Are we popping off a value? (we have a prev, and it creator is cur)
// Then check if we should use the GUID at the time of the start event
Expand Down Expand Up @@ -592,7 +593,7 @@ void ActivityChanging(AsyncLocalValueChangedArgs<ActivityInfo> args)
///
/// This variable points to a linked list that represents all Activities that have started but have not stopped.
/// </summary>
AsyncLocal<ActivityInfo> m_current;
AsyncLocal<ActivityInfo?>? m_current;
bool m_checkedForEnable;

// Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System;
using System.Diagnostics;
using System.Collections;
Expand Down Expand Up @@ -48,12 +49,14 @@ private void RegisterCommandCallback()
_eventSource.EventCommandExecuted += OnEventSourceCommand;
}

private void OnEventSourceCommand(object sender, EventCommandEventArgs e)
private void OnEventSourceCommand(object? sender, EventCommandEventArgs e)
{
if (e.Command == EventCommand.Enable || e.Command == EventCommand.Update)
{
string valueStr;
float value;
Debug.Assert(e.Arguments != null);

if (e.Arguments.TryGetValue("EventCounterIntervalSec", out valueStr) && float.TryParse(valueStr, out value))
{
// Recursion through EventSource callbacks possible. When we enable the timer
Expand All @@ -76,7 +79,7 @@ private void OnEventSourceCommand(object sender, EventCommandEventArgs e)
// We need eventCounters to 'attach' themselves to a particular EventSource.
// this table provides the mapping from EventSource -> CounterGroup
// which represents this 'attached' information.
private static WeakReference<CounterGroup>[] s_counterGroups;
private static WeakReference<CounterGroup>[]? s_counterGroups;
private static readonly object s_counterGroupsLock = new object();

private static void EnsureEventSourceIndexAvailable(int eventSourceIndex)
Expand All @@ -100,8 +103,9 @@ internal static CounterGroup GetCounterGroup(EventSource eventSource)
{
int eventSourceIndex = EventListener.EventSourceIndex(eventSource);
EnsureEventSourceIndexAvailable(eventSourceIndex);
Debug.Assert(s_counterGroups != null);
WeakReference<CounterGroup> weakRef = CounterGroup.s_counterGroups[eventSourceIndex];
CounterGroup ret = null;
CounterGroup? ret = null;
if (weakRef == null || !weakRef.TryGetTarget(out ret))
{
ret = new CounterGroup(eventSource);
Expand All @@ -117,7 +121,7 @@ internal static CounterGroup GetCounterGroup(EventSource eventSource)

private DateTime _timeStampSinceCollectionStarted;
private int _pollingIntervalInMilliseconds;
private Timer _pollingTimer;
private Timer? _pollingTimer;

private void DisposeTimer()
{
Expand Down Expand Up @@ -153,7 +157,7 @@ private void EnableTimer(float pollingIntervalInSeconds)
restoreFlow = true;
}

_pollingTimer = new Timer(s => ((CounterGroup)s).OnTimer(null), this, _pollingIntervalInMilliseconds, _pollingIntervalInMilliseconds);
_pollingTimer = new Timer(s => ((CounterGroup)s!).OnTimer(null), this, _pollingIntervalInMilliseconds, _pollingIntervalInMilliseconds);
}
finally
{
Expand All @@ -166,7 +170,7 @@ private void EnableTimer(float pollingIntervalInSeconds)
OnTimer(null);
}

private void OnTimer(object state)
private void OnTimer(object? state)
{
Debug.WriteLine("Timer fired at " + DateTime.UtcNow.ToString("mm.ss.ffffff"));
lock (this) // Lock the CounterGroup
Expand Down Expand Up @@ -227,4 +231,4 @@ private void OnTimer(Task t, object s)
#endregion // Timer Processing

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System;
using System.Diagnostics;
using System.Collections;
Expand All @@ -18,11 +19,11 @@ namespace System.Diagnostics.Tracing
#endif
{
[EventData]
internal class CounterPayload : IEnumerable<KeyValuePair<string, object>>
internal class CounterPayload : IEnumerable<KeyValuePair<string, object?>>
{
public string Name { get; set; }
public string? Name { get; set; }

public string DisplayName { get; set; }
public string? DisplayName { get; set; }

public double Mean { get; set; }

Expand All @@ -36,11 +37,11 @@ internal class CounterPayload : IEnumerable<KeyValuePair<string, object>>

public float IntervalSec { get; internal set; }

public string Metadata { get; set; }
public string? Metadata { get; set; }

#region Implementation of the IEnumerable interface

public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator()
{
return ForEnumeration.GetEnumerator();
}
Expand All @@ -50,45 +51,45 @@ IEnumerator IEnumerable.GetEnumerator()
return ForEnumeration.GetEnumerator();
}

private IEnumerable<KeyValuePair<string, object>> ForEnumeration
private IEnumerable<KeyValuePair<string, object?>> ForEnumeration
{
get
{
yield return new KeyValuePair<string, object>("Name", Name);
yield return new KeyValuePair<string, object>("DisplayName", DisplayName);
yield return new KeyValuePair<string, object>("Mean", Mean);
yield return new KeyValuePair<string, object>("StandardDeviation", StandardDeviation);
yield return new KeyValuePair<string, object>("Count", Count);
yield return new KeyValuePair<string, object>("Min", Min);
yield return new KeyValuePair<string, object>("Max", Max);
yield return new KeyValuePair<string, object>("IntervalSec", IntervalSec);
yield return new KeyValuePair<string, object>("Series", $"Interval={IntervalSec}");
yield return new KeyValuePair<string, object>("CounterType", "Mean");
yield return new KeyValuePair<string, object>("Metadata", Metadata);
yield return new KeyValuePair<string, object?>("Name", Name);
yield return new KeyValuePair<string, object?>("DisplayName", DisplayName);
yield return new KeyValuePair<string, object?>("Mean", Mean);
yield return new KeyValuePair<string, object?>("StandardDeviation", StandardDeviation);
yield return new KeyValuePair<string, object?>("Count", Count);
yield return new KeyValuePair<string, object?>("Min", Min);
yield return new KeyValuePair<string, object?>("Max", Max);
yield return new KeyValuePair<string, object?>("IntervalSec", IntervalSec);
yield return new KeyValuePair<string, object?>("Series", $"Interval={IntervalSec}");
yield return new KeyValuePair<string, object?>("CounterType", "Mean");
yield return new KeyValuePair<string, object?>("Metadata", Metadata);
}
}

#endregion // Implementation of the IEnumerable interface
}

[EventData]
internal class IncrementingCounterPayload : IEnumerable<KeyValuePair<string, object>>
internal class IncrementingCounterPayload : IEnumerable<KeyValuePair<string, object?>>
{
public string Name { get; set; }
public string? Name { get; set; }

public string DisplayName { get; set; }
public string? DisplayName { get; set; }

public string DisplayRateTimeScale { get; set; }
public string? DisplayRateTimeScale { get; set; }

public double Increment { get; set; }

public float IntervalSec { get; internal set; }

public string Metadata { get; set; }
public string? Metadata { get; set; }

#region Implementation of the IEnumerable interface

public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator()
{
return ForEnumeration.GetEnumerator();
}
Expand All @@ -98,18 +99,18 @@ IEnumerator IEnumerable.GetEnumerator()
return ForEnumeration.GetEnumerator();
}

private IEnumerable<KeyValuePair<string, object>> ForEnumeration
private IEnumerable<KeyValuePair<string, object?>> ForEnumeration
{
get
{
yield return new KeyValuePair<string, object>("Name", Name);
yield return new KeyValuePair<string, object>("DisplayName", DisplayName);
yield return new KeyValuePair<string, object>("DisplayRateTimeScale", DisplayRateTimeScale);
yield return new KeyValuePair<string, object>("Increment", Increment);
yield return new KeyValuePair<string, object>("IntervalSec", IntervalSec);
yield return new KeyValuePair<string, object>("Series", $"Interval={IntervalSec}");
yield return new KeyValuePair<string, object>("CounterType", "Sum");
yield return new KeyValuePair<string, object>("Metadata", Metadata);
yield return new KeyValuePair<string, object?>("Name", Name);
yield return new KeyValuePair<string, object?>("DisplayName", DisplayName);
yield return new KeyValuePair<string, object?>("DisplayRateTimeScale", DisplayRateTimeScale);
yield return new KeyValuePair<string, object?>("Increment", Increment);
yield return new KeyValuePair<string, object?>("IntervalSec", IntervalSec);
yield return new KeyValuePair<string, object?>("Series", $"Interval={IntervalSec}");
yield return new KeyValuePair<string, object?>("CounterType", "Sum");
yield return new KeyValuePair<string, object?>("Metadata", Metadata);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable enable
using System;
using System.Diagnostics;
using System.Collections;
Expand Down Expand Up @@ -59,7 +60,7 @@ public void Dispose()
if (_group != null)
{
_group.Remove(this);
_group = null;
_group = null!; // TODO-NULLABLE: should not be nulled out
}
}

Expand All @@ -75,7 +76,7 @@ public void AddMetadata(string key, string value)
}
}

public string DisplayName { get; set; }
public string? DisplayName { get; set; }

public string Name { get; }

Expand All @@ -84,7 +85,7 @@ public void AddMetadata(string key, string value)
#region private implementation

private CounterGroup _group;
private Dictionary<string, string> _metadata;
private Dictionary<string, string>? _metadata;

internal abstract void WritePayload(float intervalSec);

Expand Down
Loading

0 comments on commit 3397f83

Please sign in to comment.