Skip to content

Commit 115a234

Browse files
author
mwatson
committed
Change to high precision timer
1 parent 9cb9ef4 commit 115a234

File tree

5 files changed

+70
-12
lines changed

5 files changed

+70
-12
lines changed

Src/StackifyLib/Internal/Logs/LogQueue.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,17 @@ public void QueueLogMessage(Models.LogMsg msg)
8484
}
8585

8686

87-
//try
88-
//{
89-
90-
// if (string.IsNullOrEmpty(msg.Th))
91-
// {
92-
// msg.Th = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString();
93-
// }
94-
//}
95-
//catch
96-
//{
97-
//}
87+
try
88+
{
89+
90+
if (string.IsNullOrEmpty(msg.Th))
91+
{
92+
msg.Th = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString();
93+
}
94+
}
95+
catch
96+
{
97+
}
9898

9999
#if NET45 || NET40
100100
try

Src/StackifyLib/Models/LogMsgGroup.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Runtime.Serialization;
66
using System.Text;
77
using Newtonsoft.Json;
8+
using StackifyLib.Utils;
89

910
namespace StackifyLib.Models
1011
{
@@ -51,7 +52,10 @@ public class LogMsg
5152

5253
public LogMsg()
5354
{
54-
EpochMs = (long)DateTime.UtcNow.Subtract(_Epoch).TotalMilliseconds;
55+
EpochMs = (long)HighPrecisionTime.UtcNow.Subtract(_Epoch).TotalMilliseconds;
56+
57+
//Switched to high precision timer
58+
//EpochMs = (long)DateTime.UtcNow.Subtract(_Epoch).TotalMilliseconds;
5559
UploadErrors = 0;
5660
}
5761

Src/StackifyLib/ProfileTracer.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ public static void SetOperationName(string operationName)
9090

9191
}
9292

93+
[Obsolete("Just used for testing", false)]
94+
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
95+
public static void NoOp()
96+
{
97+
98+
}
99+
100+
93101

94102
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
95103
public static void TraceString(string logMsg)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.InteropServices;
5+
using System.Threading.Tasks;
6+
7+
namespace StackifyLib.Utils
8+
{
9+
public static class HighPrecisionTime
10+
{
11+
public static bool IsAvailable { get; private set; }
12+
13+
[DllImport("Kernel32.dll", CallingConvention = CallingConvention.Winapi)]
14+
private static extern void GetSystemTimePreciseAsFileTime(out long filetime);
15+
16+
public static DateTime UtcNow
17+
{
18+
get
19+
{
20+
if (!IsAvailable)
21+
{
22+
return DateTime.UtcNow;
23+
// throw new InvalidOperationException("High resolution clock isn't available.");
24+
}
25+
long filetime;
26+
GetSystemTimePreciseAsFileTime(out filetime);
27+
return DateTime.FromFileTimeUtc(filetime);
28+
}
29+
}
30+
31+
static HighPrecisionTime()
32+
{
33+
try
34+
{
35+
long filetime;
36+
GetSystemTimePreciseAsFileTime(out filetime);
37+
IsAvailable = true;
38+
}
39+
catch
40+
{
41+
IsAvailable = false;
42+
}
43+
}
44+
}
45+
}

Src/StackifyLib/Utils/PrefixOrAPM.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ internal static ProfilerType GetProfilerType()
5757
switch (process?.ProcessName?.ToLower().Replace(".vshost", ""))
5858
{
5959
case "devdashservice":
60+
case "stackifytracerservice":
6061
case "stackifytracernotifier":
6162
case "devdashtestconsole":
6263
_LastProfilerType = ProfilerType.Prefix;

0 commit comments

Comments
 (0)