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
|
/*
* SSSync, a Simple and Stupid Synchronizer for data with multi-valued attributes
* Copyright (C) 2014 Ludovic Pouzenc <ludovic@pouzenc.fr>
*
* This file is part of SSSync.
*
* SSSync is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SSSync is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SSSync. If not, see <http://www.gnu.org/licenses/>
*/
package utils;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.RuntimeMXBean;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
/**
* TODO javadoc
*
* @author lpouzenc
*/
public class JVMStatsDumper {
private static final Logger logger = Logger.getLogger(JVMStatsDumper.class.getName());
public static void logGCStats() {
// Skip all string construction if will not print this stuff
if ( logger.getLevel().isGreaterOrEqual(Level.INFO) ) { return; }
long totalGarbageCollections = 0;
long garbageCollectionTime = 0;
final String gcDumpHeader="Dumping Garbage Collector statistics\n" +
"+--------------------+-----------------------------+\n" +
"+ GC Name + Count + Time (ms) +\n" +
"+--------------------+--------------+--------------+\n";
StringBuilder sb = new StringBuilder(1024);
sb.append(gcDumpHeader);
for(GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
long count = gc.getCollectionCount();
long time = gc.getCollectionTime();
sb.append(String.format("+ %18s + %,12d + %,12d +%n", gc.getName(), count, time));
if(count >= 0) totalGarbageCollections += count;
if(time >= 0) garbageCollectionTime += time;
}
sb.append("+ + + +\n");
sb.append(String.format("+ %18s + %,12d + %,12d +%n",
"Total", totalGarbageCollections, garbageCollectionTime
));
sb.append("+--------------------+--------------+--------------+\n");
sb.append("JVM arguments : ");
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
for ( String arg : runtimeMxBean.getInputArguments() ) {
sb.append(arg + " ");
}
logger.debug(sb);
}
/**
* Helper function to log the current memory usage
*/
public static void logMemoryUsage() {
// Skip all string construction if will not print this stuff
if ( logger.getLevel().isGreaterOrEqual(Level.INFO) ) { return; }
final String memDumpHeader="Dumping memory statistics\n" +
"+--------------------------------------------------------------------------------+\n" +
"+ + Current (kio) + Peak (kio) +\n" +
"+ Pool +-----------------------------------------------------------+\n" +
"+ + Used + Committed + Used + Committed +\n" +
"+--------------------+--------------+--------------+--------------+--------------+\n";
StringBuilder sb = new StringBuilder(1024);
sb.append(memDumpHeader);
for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
MemoryUsage peak = pool.getPeakUsage();
MemoryUsage curr = pool.getUsage();
sb.append(String.format("+ %18s + %,12d + %,12d + %,12d + %,12d +%n",
pool.getName(),curr.getUsed()/1024, curr.getCommitted()/1024, peak.getUsed()/1024, peak.getCommitted()/1024
));
pool.resetPeakUsage(); //XXX Maybe this is not a global action and is useless on a temporary object used once
}
sb.append("+--------------------+--------------+--------------+--------------+--------------+\n");
logger.debug(sb);
}
}
|