forked from mbebenita/j2me.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
instrument.js
159 lines (130 loc) · 4.98 KB
/
instrument.js
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
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set shiftwidth=4 tabstop=4 autoindent cindent expandtab: */
'use strict';
var Instrument = {
enter: {},
exit: {},
profiling: false,
profile: null,
getKey: function(methodInfo) {
return methodInfo.classInfo.className + "." + methodInfo.name + "." + methodInfo.signature;
},
callEnterHooks: function(methodInfo, caller, callee) {
var key = this.getKey(methodInfo);
if (Instrument.enter[key]) {
Instrument.enter[key](caller, callee);
}
if (this.profiling) {
var now = performance.now();
if (caller.profileData && caller.profileData.then) {
caller.profileData.cost += now - caller.profileData.then;
caller.profileData.then = null;
}
callee.profileData = {
cost: 0,
then: now,
};
}
},
callExitHooks: function(methodInfo, caller, callee) {
var key = this.getKey(methodInfo);
if (this.profiling) {
var now = performance.now();
if (callee.profileData) {
if (callee.profileData.then) {
callee.profileData.cost += now - callee.profileData.then;
callee.profileData.then = null;
}
var methodProfileData = this.profile[key] || (this.profile[key] = { count: 0, cost: 0 });
methodProfileData.count++;
methodProfileData.cost += callee.profileData.cost;
}
if (caller.profileData) {
caller.profileData.then = now;
}
}
if (Instrument.exit[key]) {
Instrument.exit[key](caller, callee);
}
},
callPauseHooks: function(frame) {
if (this.profiling && frame.profileData && frame.profileData.then) {
frame.profileData.cost += performance.now() - frame.profileData.then;
frame.profileData.then = null;
}
},
callResumeHooks: function(frame) {
if (this.profiling && frame.profileData) {
frame.profileData.then = performance.now();
}
},
startProfile: function() {
this.profile = {};
this.profiling = true;
},
stopProfile: function() {
var methods = [];
for (var key in this.profile) {
methods.push({
key: key,
count: this.profile[key].count,
cost: this.profile[key].cost,
});
}
methods.sort(function(a, b) { return b.cost - a.cost });
console.log("Profile:");
methods.forEach(function(method) {
console.log(Math.round(method.cost) + "ms " + method.count + " " + method.key);
});
this.profiling = false;
},
measure: function(Alt, ctx, methodInfo) {
if (this.profiling) {
var then = performance.now();
Alt.invoke(ctx, methodInfo);
var key = this.getKey(methodInfo);
var methodProfileData = this.profile[key] || (this.profile[key] = { count: 0, cost: 0 });
methodProfileData.count++;
methodProfileData.cost += performance.now() - then;
} else {
Alt.invoke(ctx, methodInfo);
}
},
};
Instrument.enter["com/sun/midp/ssl/SSLStreamConnection.<init>.(Ljava/lang/String;ILjava/io/InputStream;Ljava/io/OutputStream;Lcom/sun/midp/pki/CertStore;)V"] = function(caller, callee) {
var _this = caller.stack.read(6), port = caller.stack.read(4), host = util.fromJavaString(caller.stack.read(5));
_this.logBuffer = "SSLStreamConnection to " + host + ":" + port + ":\n";
};
Instrument.enter["com/sun/midp/ssl/Out.write.(I)V"] = function(caller, callee) {
var _this = caller.stack.read(3);
var connection = _this.class.getField("I.ssc.Lcom/sun/midp/ssl/SSLStreamConnection;").get(_this);
connection.logBuffer += String.fromCharCode(callee.stack.read(1));
};
Instrument.enter["com/sun/midp/ssl/Out.write.([BII)V"] = function(caller, callee) {
var len = caller.stack.read(1), off = caller.stack.read(2), b = caller.stack.read(3), _this = caller.stack.read(4);
var connection = _this.class.getField("I.ssc.Lcom/sun/midp/ssl/SSLStreamConnection;").get(_this);
var range = b.subarray(off, off + len);
for (var i = 0; i < range.length; i++) {
connection.logBuffer += String.fromCharCode(range[i] & 0xff);
}
};
Instrument.exit["com/sun/midp/ssl/In.read.()I"] = function(caller, callee) {
var _this = caller.stack.read(3);
var connection = _this.class.getField("I.ssc.Lcom/sun/midp/ssl/SSLStreamConnection;").get(_this);
connection.logBuffer += String.fromCharCode(callee.stack.read(1));
};
Instrument.exit["com/sun/midp/ssl/In.read.([BII)I"] = function(caller, callee) {
var len = caller.stack.read(4), off = caller.stack.read(5), b = caller.stack.read(6), _this = caller.stack.read(7);
var connection = _this.class.getField("I.ssc.Lcom/sun/midp/ssl/SSLStreamConnection;").get(_this);
var range = b.subarray(off, off + len);
for (var i = 0; i < range.length; i++) {
connection.logBuffer += String.fromCharCode(range[i] & 0xff);
}
};
Instrument.enter["com/sun/midp/ssl/SSLStreamConnection.close.()V"] = function(caller, callee) {
var _this = caller.stack.read(1);
if ("logBuffer" in _this) {
console.log(_this.logBuffer);
delete _this.logBuffer;
}
};