-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAgent.java
More file actions
504 lines (359 loc) · 11.9 KB
/
Agent.java
File metadata and controls
504 lines (359 loc) · 11.9 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package com.stackimpact.agent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.util.concurrent.atomic.AtomicBoolean;
import com.stackimpact.agent.profilers.*;
import com.stackimpact.agent.reporters.*;
public class Agent {
public final static String VERSION = "1.0.6";
public final static String SAAS_DASHBOARD_ADDRESS = "https://agent-api.stackimpact.com";
private static Agent instance;
private long runTS;
private String runID;
private APIRequest apiRequest;
private ConfigLoader configLoader;
private MessageQueue messageQueue;
private ProfileReporter cpuReporter;
private ProfileReporter lockReporter;
private ProcessReporter processReporter;
private boolean isAgentStarted = false;
private boolean isAgentDestroyed = false;
private boolean isAgentEnabled = false;
private boolean isAutoProfilingMode = true;
private boolean isProfilingDisabled = false;
private AtomicBoolean profilerLock = new AtomicBoolean(false);
private AtomicBoolean spanLock = new AtomicBoolean(false);
private String dashboardAddress;
private String agentKey;
private String appName;
private String appVersion;
private String appEnvironment;
private String hostName;
private boolean isCPUProfilerDisabled = false;
private boolean isAllocationProfilerDisabled = false;
private boolean isLockProfilerDisabled = false;
private boolean isDebugMode = false;
private boolean isAgentFramesEnabled = false;
private Thread shutdownHook;
private Agent() {
}
public static Agent getInstance() {
if (instance != null) {
return instance;
}
instance = new Agent();
return instance;
}
public long getRunTS() {
return runTS;
}
public String getRunID() {
return runID;
}
public APIRequest getAPIRequest() {
return apiRequest;
}
public ConfigLoader getConfigLoader() {
return configLoader;
}
public MessageQueue getMessageQueue() {
return messageQueue;
}
public ProcessReporter getProcessReporter() {
return processReporter;
}
public ProfileReporter getCPUReporter() {
return cpuReporter;
}
public ProfileReporter getLockReporter() {
return lockReporter;
}
public boolean isAgentStarted() {
return isAgentStarted;
}
public boolean isAgentDestroyed() {
return isAgentStarted;
}
public boolean isAgentEnabled() {
return isAgentEnabled;
}
public void setAgentEnabled(boolean isAgentEnabled) {
this.isAgentEnabled = isAgentEnabled;
}
public boolean isAutoProfilingMode() {
return isAutoProfilingMode;
}
public void setAutoProfilingMode(boolean isAutoProfilingMode) {
this.isAutoProfilingMode = isAutoProfilingMode;
}
public boolean isProfilingDisabled() {
return isProfilingDisabled;
}
public void setProfilingDisabled(boolean isProfilingDisabled) {
this.isProfilingDisabled = isProfilingDisabled;
}
public AtomicBoolean getProfilerLock() {
return profilerLock;
}
public AtomicBoolean getSpanLock() {
return spanLock;
}
public String getDashboardAddress() {
return dashboardAddress;
}
public void setDashboardAddress(String dashboardAddress) {
this.dashboardAddress = dashboardAddress;
}
public String getAgentKey() {
return agentKey;
}
public void setAgentKey(String agentKey) {
this.agentKey = agentKey;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getAppVersion() {
return appVersion;
}
public void setAppVersion(String appVersion) {
this.appVersion = appVersion;
}
public String getAppEnvironment() {
return appEnvironment;
}
public void setAppEnvironment(String appEnvironment) {
this.appEnvironment = appEnvironment;
}
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public boolean isCPUProfilerDisabled() {
return this.isCPUProfilerDisabled;
}
public void setCPUProfilerDisabled(boolean isCPUProfilerDisabled) {
this.isCPUProfilerDisabled = isCPUProfilerDisabled;
}
public boolean isAllocationProfilerDisabled() {
return this.isAllocationProfilerDisabled;
}
public void setAllocationProfilerDisabled(boolean isAllocationProfilerDisabled) {
this.isAllocationProfilerDisabled = isAllocationProfilerDisabled;
}
public boolean isLockProfilerDisabled() {
return this.isLockProfilerDisabled;
}
public void setLockProfilerDisabled(boolean isLockProfilerDisabled) {
this.isLockProfilerDisabled = isLockProfilerDisabled;
}
public boolean isDebugMode() {
return isDebugMode;
}
public void setDebugMode(boolean isDebugMode) {
this.isDebugMode = isDebugMode;
}
public boolean isAgentFramesEnabled() {
return this.isAgentFramesEnabled;
}
public void setAgentFramesEnabled(boolean isAgentFramesEnabled) {
this.isAgentFramesEnabled = isAgentFramesEnabled;
}
public synchronized void start(String agentKey, String appName) {
if (isAgentStarted) {
logInfo("The agent has already been started.");
return;
}
try {
if (AgentUtils.getJavaVersion()[0] < 7) {
logError("Java versions older than 7 are not supported.");
return;
}
}
catch (Exception ex) {
logException(ex);
return;
}
try {
runTS = AgentUtils.timestamp();
runID = AgentUtils.generateUUID();
}
catch(Exception ex) {
logException(ex);
return;
}
if (agentKey == null || appName == null) {
logError("Invalid initialization parameters");
return;
}
if(dashboardAddress == null) {
dashboardAddress = SAAS_DASHBOARD_ADDRESS;
}
this.agentKey = agentKey;
this.appName = appName;
if (hostName == null) {
try {
hostName = InetAddress.getLocalHost().getHostName();
}
catch(Exception ex) {
hostName = "unknown";
logException(ex);
}
}
try {
loadAgentLib();
if (!attach(isDebugMode)) {
logError("Attaching JVMTI agent failed.");
return;
}
}
catch (Exception ex) {
logException(ex);
return;
}
apiRequest = new APIRequest(this);
configLoader = new ConfigLoader(this);
messageQueue = new MessageQueue(this);
processReporter = new ProcessReporter(this);
ProfilerConfig cpuProfilerConfig = new ProfilerConfig();
cpuProfilerConfig.logPrefix = "CPU profiler";
cpuProfilerConfig.maxProfileDuration = 10 * 1000;
cpuProfilerConfig.maxSpanDuration = 2 * 1000;
cpuProfilerConfig.maxSpanCount = 30;
cpuProfilerConfig.spanInterval = 16 * 1000;
cpuProfilerConfig.reportInterval = 120 * 1000;
cpuReporter = new ProfileReporter(this, new CPUProfiler(this), cpuProfilerConfig);
cpuReporter.setup();
ProfilerConfig lockProfilerConfig = new ProfilerConfig();
lockProfilerConfig.logPrefix = "Lock profiler";
lockProfilerConfig.maxProfileDuration = 10 * 1000;
lockProfilerConfig.maxSpanDuration = 2 * 1000;
lockProfilerConfig.maxSpanCount = 30;
lockProfilerConfig.spanInterval = 16 * 1000;
lockProfilerConfig.reportInterval = 120 * 1000;
lockReporter = new ProfileReporter(this, new LockProfiler(this), lockProfilerConfig);
lockReporter.setup();
configLoader.start();
messageQueue.start();
shutdownHook = new Thread() {
public void run() {
try {
Agent.this.destroy();
}
catch (Exception ex) {
Agent.this.logException(ex);
}
}
};
Runtime.getRuntime().addShutdownHook(shutdownHook);
isAgentStarted = true;
logInfo("Agent started.");
}
private native boolean attach(boolean isDebugMode);
public void destroy() {
if (!isAgentStarted) {
logInfo("The agent has not been started.");
return;
}
if (isAgentDestroyed) {
logInfo("The agent has already been destroyed.");
return;
}
try {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
}
catch(Exception ex) {
}
cpuReporter.stop();
lockReporter.stop();
processReporter.stop();
configLoader.stop();
messageQueue.stop();
cpuReporter.destroy();
lockReporter.destroy();
isAgentStarted = false;
logInfo("Agent destroyed.");
}
private String extractResourceFromJar(String name) throws Exception {
String filePath = AgentUtils.getSystemTempDir() + File.separator + name;
logInfo("Extracting JVMTI agent to " + filePath + ".");
File file = new File(filePath);
if (file.exists()) {
logInfo("Found extracted JVMTI agent.");
return filePath;
}
InputStream in = getClass().getResourceAsStream(File.separator + name);
if (in == null) {
throw new Exception("Resource not found in JAR: " + name);
}
OutputStream out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
out.close();
in.close();
file.setExecutable(true, true);
return filePath;
}
private void loadAgentLib() throws Exception {
String agentTempDir = null;
String os = AgentUtils.getOSTag();
if (os == null) {
throw new Exception("Cannot identify the OS.");
}
String agentLibName =
"libstackimpact-" + VERSION + "-" + os + "-x64" +
(os != "win" ? ".so" : ".dll");
String agentLibPath = extractResourceFromJar(agentLibName);
logInfo("Loading JVM TI agent: " + agentLibPath);
System.load(agentLibPath);
}
public ProfileSpan profile() {
ProfileSpan span = new ProfileSpan(this);
span.start();
return span;
}
public void logMessage(String level, String message) {
if (!isDebugMode) {
return;
}
StringBuffer buf = new StringBuffer();
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.S");
buf.append("[");
buf.append(sdf.format(cal.getTime()));
buf.append("] ");
buf.append("StackImpact ");
buf.append(VERSION);
buf.append(": ");
buf.append(level);
buf.append(": ");
buf.append(message);
System.out.println(buf.toString());
}
public void logInfo(String message) {
logMessage("INFO", message);
}
public void logError(String message) {
logMessage("ERROR", message);
}
public void logException(Throwable t) {
logMessage("EXCEPTION", t.getMessage());
if (isDebugMode) {
t.printStackTrace();
}
}
}