Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/management/GarbageCollectorImpl.java
38827 views
1
/*
2
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.management;
27
28
import com.sun.management.GarbageCollectorMXBean;
29
import com.sun.management.GarbageCollectionNotificationInfo;
30
import java.lang.management.ManagementFactory;
31
import java.lang.management.MemoryPoolMXBean;
32
import java.lang.management.MemoryUsage;
33
34
import com.sun.management.GcInfo;
35
import javax.management.openmbean.CompositeData;
36
import javax.management.MBeanInfo;
37
import javax.management.MBeanAttributeInfo;
38
import javax.management.ObjectName;
39
import javax.management.MBeanNotificationInfo;
40
import javax.management.Notification;
41
import javax.management.NotificationFilter;
42
import javax.management.NotificationListener;
43
import javax.management.ListenerNotFoundException;
44
45
import java.util.List;
46
import java.util.ListIterator;
47
import java.util.Map;
48
49
/**
50
* Implementation class for the garbage collector.
51
* Standard and committed hotspot-specific metrics if any.
52
*
53
* ManagementFactory.getGarbageCollectorMXBeans() returns a list
54
* of instances of this class.
55
*/
56
class GarbageCollectorImpl extends MemoryManagerImpl
57
implements GarbageCollectorMXBean {
58
59
GarbageCollectorImpl(String name) {
60
super(name);
61
}
62
63
public native long getCollectionCount();
64
public native long getCollectionTime();
65
66
67
// The memory pools are static and won't be changed.
68
// TODO: If the hotspot implementation begins to have pools
69
// dynamically created and removed, this needs to be modified.
70
private String[] poolNames = null;
71
synchronized String[] getAllPoolNames() {
72
if (poolNames == null) {
73
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
74
poolNames = new String[pools.size()];
75
int i = 0;
76
for (MemoryPoolMXBean m : pools) {
77
poolNames[i++] = m.getName();
78
}
79
}
80
return poolNames;
81
}
82
83
// Sun JDK extension
84
private GcInfoBuilder gcInfoBuilder;
85
86
private synchronized GcInfoBuilder getGcInfoBuilder() {
87
if(gcInfoBuilder == null) {
88
gcInfoBuilder = new GcInfoBuilder(this, getAllPoolNames());
89
}
90
return gcInfoBuilder;
91
}
92
93
public GcInfo getLastGcInfo() {
94
GcInfo info = getGcInfoBuilder().getLastGcInfo();
95
return info;
96
}
97
98
private final static String notifName =
99
"javax.management.Notification";
100
101
private final static String[] gcNotifTypes = {
102
GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION
103
};
104
105
@Override
106
public MBeanNotificationInfo[] getNotificationInfo() {
107
return new MBeanNotificationInfo[]{
108
new MBeanNotificationInfo(gcNotifTypes,
109
notifName,
110
"GC Notification")
111
};
112
}
113
114
private static long seqNumber = 0;
115
private static long getNextSeqNumber() {
116
return ++seqNumber;
117
}
118
119
void createGCNotification(long timestamp,
120
String gcName,
121
String gcAction,
122
String gcCause,
123
GcInfo gcInfo) {
124
125
if (!hasListeners()) {
126
return;
127
}
128
129
Notification notif = new Notification(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION,
130
getObjectName(),
131
getNextSeqNumber(),
132
timestamp,
133
gcName);
134
GarbageCollectionNotificationInfo info =
135
new GarbageCollectionNotificationInfo(gcName,
136
gcAction,
137
gcCause,
138
gcInfo);
139
140
CompositeData cd =
141
GarbageCollectionNotifInfoCompositeData.toCompositeData(info);
142
notif.setUserData(cd);
143
sendNotification(notif);
144
}
145
146
public synchronized void addNotificationListener(NotificationListener listener,
147
NotificationFilter filter,
148
Object handback)
149
{
150
boolean before = hasListeners();
151
super.addNotificationListener(listener, filter, handback);
152
boolean after = hasListeners();
153
if (!before && after) {
154
setNotificationEnabled(this, true);
155
}
156
}
157
158
public synchronized void removeNotificationListener(NotificationListener listener)
159
throws ListenerNotFoundException {
160
boolean before = hasListeners();
161
super.removeNotificationListener(listener);
162
boolean after = hasListeners();
163
if (before && !after) {
164
setNotificationEnabled(this,false);
165
}
166
}
167
168
public synchronized void removeNotificationListener(NotificationListener listener,
169
NotificationFilter filter,
170
Object handback)
171
throws ListenerNotFoundException
172
{
173
boolean before = hasListeners();
174
super.removeNotificationListener(listener,filter,handback);
175
boolean after = hasListeners();
176
if (before && !after) {
177
setNotificationEnabled(this,false);
178
}
179
}
180
181
public ObjectName getObjectName() {
182
return Util.newObjectName(ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE, getName());
183
}
184
185
native void setNotificationEnabled(GarbageCollectorMXBean gc,
186
boolean enabled);
187
188
}
189
190