19
19
20
20
package org .elasticsearch .monitor .jvm ;
21
21
22
+ import org .elasticsearch .common .Booleans ;
22
23
import org .elasticsearch .common .collect .Iterators ;
23
24
import org .elasticsearch .common .io .stream .StreamInput ;
24
25
import org .elasticsearch .common .io .stream .StreamOutput ;
42
43
*/
43
44
public class JvmStats implements Streamable , Serializable , ToXContent {
44
45
46
+ private static boolean enableLastGc ;
47
+
48
+ public static boolean isLastGcEnabled () {
49
+ return enableLastGc ;
50
+ }
51
+
45
52
private final static RuntimeMXBean runtimeMXBean ;
46
53
private final static MemoryMXBean memoryMXBean ;
47
54
private final static ThreadMXBean threadMXBean ;
48
55
49
- private static boolean sunGc ;
50
56
private static Method getLastGcInfoMethod ;
51
57
private static Method getMemoryUsageBeforeGcMethod ;
52
58
private static Method getMemoryUsageAfterGcMethod ;
@@ -59,28 +65,32 @@ public class JvmStats implements Streamable, Serializable, ToXContent {
59
65
memoryMXBean = ManagementFactory .getMemoryMXBean ();
60
66
threadMXBean = ManagementFactory .getThreadMXBean ();
61
67
62
- try {
63
- Class sunGcClass = Class .forName ("com.sun.management.GarbageCollectorMXBean" );
64
- Class gcInfoClass = Class .forName ("com.sun.management.GcInfo" );
65
-
66
- getLastGcInfoMethod = sunGcClass .getDeclaredMethod ("getLastGcInfo" );
67
- getLastGcInfoMethod .setAccessible (true );
68
-
69
- getMemoryUsageBeforeGcMethod = gcInfoClass .getDeclaredMethod ("getMemoryUsageBeforeGc" );
70
- getMemoryUsageBeforeGcMethod .setAccessible (true );
71
- getMemoryUsageAfterGcMethod = gcInfoClass .getDeclaredMethod ("getMemoryUsageAfterGc" );
72
- getMemoryUsageAfterGcMethod .setAccessible (true );
73
- getStartTimeMethod = gcInfoClass .getDeclaredMethod ("getStartTime" );
74
- getStartTimeMethod .setAccessible (true );
75
- getEndTimeMethod = gcInfoClass .getDeclaredMethod ("getEndTime" );
76
- getEndTimeMethod .setAccessible (true );
77
- getDurationMethod = gcInfoClass .getDeclaredMethod ("getDuration" );
78
- getDurationMethod .setAccessible (true );
79
-
80
- sunGc = true ;
81
- } catch (Throwable ex ) {
82
- sunGc = false ;
68
+ boolean enableLastGc = Booleans .parseBoolean (System .getProperty ("monitory.jvm.enable_last_gc" ), false );
69
+ if (enableLastGc ) {
70
+ try {
71
+ Class sunGcClass = Class .forName ("com.sun.management.GarbageCollectorMXBean" );
72
+ Class gcInfoClass = Class .forName ("com.sun.management.GcInfo" );
73
+
74
+ getLastGcInfoMethod = sunGcClass .getDeclaredMethod ("getLastGcInfo" );
75
+ getLastGcInfoMethod .setAccessible (true );
76
+
77
+ getMemoryUsageBeforeGcMethod = gcInfoClass .getDeclaredMethod ("getMemoryUsageBeforeGc" );
78
+ getMemoryUsageBeforeGcMethod .setAccessible (true );
79
+ getMemoryUsageAfterGcMethod = gcInfoClass .getDeclaredMethod ("getMemoryUsageAfterGc" );
80
+ getMemoryUsageAfterGcMethod .setAccessible (true );
81
+ getStartTimeMethod = gcInfoClass .getDeclaredMethod ("getStartTime" );
82
+ getStartTimeMethod .setAccessible (true );
83
+ getEndTimeMethod = gcInfoClass .getDeclaredMethod ("getEndTime" );
84
+ getEndTimeMethod .setAccessible (true );
85
+ getDurationMethod = gcInfoClass .getDeclaredMethod ("getDuration" );
86
+ getDurationMethod .setAccessible (true );
87
+
88
+ } catch (Throwable ex ) {
89
+ enableLastGc = false ;
90
+ }
83
91
}
92
+
93
+ JvmStats .enableLastGc = false ;
84
94
}
85
95
86
96
public static JvmStats jvmStats () {
@@ -106,7 +116,7 @@ public static JvmStats jvmStats() {
106
116
stats .gc .collectors [i ].name = gcMxBean .getName ();
107
117
stats .gc .collectors [i ].collectionCount = gcMxBean .getCollectionCount ();
108
118
stats .gc .collectors [i ].collectionTime = gcMxBean .getCollectionTime ();
109
- if (sunGc ) {
119
+ if (enableLastGc ) {
110
120
try {
111
121
Object lastGcInfo = getLastGcInfoMethod .invoke (gcMxBean );
112
122
if (lastGcInfo != null ) {
0 commit comments