OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015-2016, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.IO; |
| 36 using System.Threading; |
| 37 using Grpc.Core.Internal; |
| 38 |
| 39 namespace Grpc.Core.Profiling |
| 40 { |
| 41 internal static class Profilers |
| 42 { |
| 43 static readonly NopProfiler DefaultProfiler = new NopProfiler(); |
| 44 static readonly ThreadLocal<IProfiler> profilers = new ThreadLocal<IProf
iler>(); |
| 45 |
| 46 public static IProfiler ForCurrentThread() |
| 47 { |
| 48 return profilers.Value ?? DefaultProfiler; |
| 49 } |
| 50 |
| 51 public static void SetForCurrentThread(IProfiler profiler) |
| 52 { |
| 53 profilers.Value = profiler; |
| 54 } |
| 55 |
| 56 public static ProfilerScope NewScope(this IProfiler profiler, string tag
) |
| 57 { |
| 58 return new ProfilerScope(profiler, tag); |
| 59 } |
| 60 } |
| 61 |
| 62 internal class NopProfiler : IProfiler |
| 63 { |
| 64 public void Begin(string tag) |
| 65 { |
| 66 } |
| 67 |
| 68 public void End(string tag) |
| 69 { |
| 70 } |
| 71 |
| 72 public void Mark(string tag) |
| 73 { |
| 74 } |
| 75 } |
| 76 |
| 77 // Profiler using Timespec.PreciseNow |
| 78 internal class BasicProfiler : IProfiler |
| 79 { |
| 80 ProfilerEntry[] entries; |
| 81 int count; |
| 82 |
| 83 public BasicProfiler() : this(1024*1024) |
| 84 { |
| 85 } |
| 86 |
| 87 public BasicProfiler(int capacity) |
| 88 { |
| 89 this.entries = new ProfilerEntry[capacity]; |
| 90 } |
| 91 |
| 92 public void Begin(string tag) |
| 93 { |
| 94 AddEntry(new ProfilerEntry(Timespec.PreciseNow, ProfilerEntry.Type.B
EGIN, tag)); |
| 95 } |
| 96 |
| 97 public void End(string tag) |
| 98 { |
| 99 AddEntry(new ProfilerEntry(Timespec.PreciseNow, ProfilerEntry.Type.E
ND, tag)); |
| 100 } |
| 101 |
| 102 public void Mark(string tag) |
| 103 { |
| 104 AddEntry(new ProfilerEntry(Timespec.PreciseNow, ProfilerEntry.Type.M
ARK, tag)); |
| 105 } |
| 106 |
| 107 public void Reset() |
| 108 { |
| 109 count = 0; |
| 110 } |
| 111 |
| 112 public void Dump(string filepath) |
| 113 { |
| 114 using (var stream = File.CreateText(filepath)) |
| 115 { |
| 116 Dump(stream); |
| 117 } |
| 118 } |
| 119 |
| 120 public void Dump(TextWriter stream) |
| 121 { |
| 122 for (int i = 0; i < count; i++) |
| 123 { |
| 124 var entry = entries[i]; |
| 125 stream.WriteLine(entry.ToString()); |
| 126 } |
| 127 } |
| 128 |
| 129 // NOT THREADSAFE! |
| 130 void AddEntry(ProfilerEntry entry) { |
| 131 entries[count++] = entry; |
| 132 } |
| 133 } |
| 134 } |
OLD | NEW |