/
timingutil.cs
191 lines (162 loc) · 4.35 KB
/
timingutil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
//using System.Collections;
using System.Threading;
using System.Collections.Generic;
// use TLS instead of threadInfoTable: Thread.AllocateNamedDataSlot("sleeptime");
// oops: some Members are not capitalized (hmm... coded to much perl in between) --> fit that
namespace Net.Exse.Timingutil
{
public class ThreadInfo
{
public Node root;
public Node currentNode;
}
public class Node
{
public List<Node> childs;
public ulong stop;
public ulong start;
public string nodetype,key;
public Object parentNode;
public Node(string p1,string p2)
{
childs = new List<Node>();
stop = 0;
start = 0;
parentNode = null;
key = p1;
nodetype = p2;
parentNode = null;
}
}
public sealed class Timingutil
{
private static ulong GetUnixTime()
{
return (ulong) ((DateTime.UtcNow.Ticks - 621355968000000000L ) / 10L);
}
private static Dictionary<int,ThreadInfo> threadInfoTable = new Dictionary<int,ThreadInfo>();
// private static SynchronizedKeyedCollection<int,ThreadInfo> threadInfoTable = new SynchronizedKeyedCollection<int,ThreadInfo>();
private static Object threadInfoTableLock = new Object();
public static void start(string name)
{
lock(threadInfoTableLock)
{
ThreadInfo threadInfo = (ThreadInfo)threadInfoTable[Thread.CurrentThread.GetHashCode()];
Node newNode = new Node(name,"timer");
newNode.parentNode = threadInfo.currentNode;
newNode.start = GetUnixTime();
threadInfo.currentNode.childs.Add(newNode);
threadInfo.currentNode = newNode;
}
//Console.Error.WriteLine("TM start:"+name);
}
public static void stop()
{
lock(threadInfoTableLock)
{
ThreadInfo threadInfo = (ThreadInfo)threadInfoTable[Thread.CurrentThread.GetHashCode()];
threadInfo.currentNode.stop = GetUnixTime();
threadInfo.currentNode = (Node)threadInfo.currentNode.parentNode;
}
//Console.Error.WriteLine("TM stop");
}
public static void info(string name)
{
lock(threadInfoTableLock)
{
ThreadInfo threadInfo = (ThreadInfo)threadInfoTable[Thread.CurrentThread.GetHashCode()];
Node newNode = new Node(name,"info");
threadInfo.currentNode.childs.Add(newNode);
}
}
public static void init()
{
ThreadInfo threadInfo = new ThreadInfo();
lock(threadInfoTableLock)
{
if(threadInfoTable.ContainsKey(Thread.CurrentThread.GetHashCode()))
{
threadInfoTable.Remove(Thread.CurrentThread.GetHashCode());
}
threadInfoTable.Add(Thread.CurrentThread.GetHashCode(),threadInfo);
}
threadInfo.root = new Node("root","timer");
threadInfo.root.start = GetUnixTime();
threadInfo.currentNode = threadInfo.root;
}
public static string dump()
{
string retval = "";
lock(threadInfoTableLock)
{
ThreadInfo threadInfo = (ThreadInfo)threadInfoTable[Thread.CurrentThread.GetHashCode()];
threadInfo.root.stop = GetUnixTime();
Dumper myDumper = new Dumper();
myDumper.dump(threadInfo.root,0,0);
retval = myDumper.retval;
threadInfoTable.Remove(Thread.CurrentThread.GetHashCode());
}
return retval;
}
public static Node objdump()
{
Node retval = null;
lock(threadInfoTableLock)
{
ThreadInfo threadInfo = (ThreadInfo)threadInfoTable[Thread.CurrentThread.GetHashCode()];
threadInfo.root.stop = GetUnixTime();
retval = threadInfo.root;
threadInfoTable.Remove(Thread.CurrentThread.GetHashCode());
}
return retval;
}
}
class Dumper
{
public string retval = "";
public void dump(Node node,int level,int levelmap)
{
{
string prefix = "";
int locallevelmap = levelmap;
for (int i = level; i > 0; i--)
{
if (locallevelmap >= Math.Pow(i,2))
{
locallevelmap -= (int)Math.Pow(i,2);
if (i == level)
{
prefix = " |-" + prefix;
}
else
{
prefix = " | " + prefix;
}
}
else
{
if (i == level)
{
prefix = " \\-" + prefix;
}
else
{
prefix = " " + prefix;
}
}
}
retval += prefix + node.key + (node.nodetype=="timer" ?" (" + (node.stop-node.start) + ")":"") + " \n";
}
for (int i = 0; i < node.childs.Count; i++)
{
int locallevelmap = levelmap;
if(i < node.childs.Count-1)
{
locallevelmap += (int)Math.Pow((level+1),2);
}
dump((Node)node.childs[i],level+1,locallevelmap);
}
}
}
}