IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
profiler.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2002 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25using System.Collections.Generic;
26using System.Diagnostics;
27
28namespace IKVM.Runtime
29{
30
31 sealed class Profiler
32 {
33
34 private static Profiler instance = new Profiler();
35 private static Dictionary<string, Entry> counters = new Dictionary<string, Entry>();
36 [ThreadStatic]
37 private static Stack<Entry> stack;
38
39 private sealed class Entry
40 {
41 internal long Time;
42 internal long Count;
43 }
44
45 ~Profiler()
46 {
47 // get rid off the warning that "instance" is unused
48 instance.Equals(null);
49 Console.Error.WriteLine("{0,-40}{1,10}{2,12}", "Event", "Count", "Time (ms)");
50 Console.Error.WriteLine("{0,-40}{1,10}{2,12}", "-----", "-----", "---------");
51 long totalTime = 0;
52 foreach (KeyValuePair<string, Entry> e in counters)
53 {
54 Entry entry = e.Value;
55 if (entry.Time == 0)
56 {
57 Console.Error.WriteLine("{0,-40}{1,10}", e.Key, entry.Count);
58 }
59 else
60 {
61 totalTime += entry.Time / 10000;
62 Console.Error.WriteLine("{0,-40}{1,10}{2,12}", e.Key, entry.Count, entry.Time / 10000);
63 }
64 }
65 Console.Error.WriteLine("{0,-40}{1,10}{2,12}", "", "", "---------");
66 Console.Error.WriteLine("{0,-40}{1,10}{2,12}", "", "", totalTime);
67 }
68
69 [Conditional("PROFILE")]
70 internal static void Enter(string name)
71 {
72 long ticks = DateTime.Now.Ticks;
73 lock (counters)
74 {
75 if (stack == null)
76 {
77 stack = new Stack<Entry>();
78 }
79 if (stack.Count > 0)
80 {
81 stack.Peek().Time += ticks;
82 }
83 Entry e;
84 if (!counters.TryGetValue(name, out e))
85 {
86 e = new Entry();
87 counters[name] = e;
88 }
89 e.Count++;
90 e.Time -= ticks;
91 stack.Push(e);
92 }
93 }
94
95 [Conditional("PROFILE")]
96 internal static void Leave(string name)
97 {
98 long ticks = DateTime.Now.Ticks;
99 stack.Pop();
100 lock (counters)
101 {
102 Entry e = counters[name];
103 e.Time += ticks;
104 if (stack.Count > 0)
105 {
106 ((Entry)stack.Peek()).Time -= ticks;
107 }
108 }
109 }
110
111 [Conditional("PROFILE")]
112 internal static void Count(string name)
113 {
114 lock (counters)
115 {
116 Entry e;
117 if (!counters.TryGetValue(name, out e))
118 {
119 e = new Entry();
120 counters[name] = e;
121 }
122 e.Count++;
123 }
124 }
125 }
126
127}