1
1
/*
2
- * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2019, 2021 Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
24
24
*/
25
25
package org .graalvm .visualvm .jfr .views .socketio ;
26
26
27
- import java .text .NumberFormat ;
28
27
import java .time .Duration ;
29
28
import java .util .ArrayList ;
30
29
import java .util .List ;
45
44
*/
46
45
abstract class SocketIONode extends CCTNode {
47
46
47
+ private static final String UNKNOWN = "<unknown>" ;
48
48
private static final SocketIONode [] NO_NODES = new SocketIONode [0 ];
49
49
50
50
private final SocketIONode parent ;
51
51
private final List <SocketIONode > children ;
52
52
53
- final String name ;
53
+ final String key ;
54
+ String name ;
54
55
final Icon icon ;
55
56
56
57
long countR , countW = 0 ;
57
58
long bytesR , bytesW = 0 ;
58
59
Duration durationR , durationRMax , durationW , durationWMax ;
59
60
60
61
61
- SocketIONode (String name , Icon icon , SocketIONode parent , List <SocketIONode > children ) {
62
+ SocketIONode (String key , String name , Icon icon , SocketIONode parent , List <SocketIONode > children ) {
63
+ this .key = key ;
62
64
this .parent = parent ;
63
65
this .children = children ;
64
66
@@ -90,10 +92,10 @@ final void processWrite(Duration duration, long bytes) {
90
92
}
91
93
92
94
93
- SocketIONode getChild (String name ) {
95
+ SocketIONode getChild (String key ) {
94
96
if (children != null )
95
97
for (SocketIONode child : children )
96
- if (Objects .equals (name , child .name ))
98
+ if (Objects .equals (key , child .key ))
97
99
return child ;
98
100
return null ;
99
101
}
@@ -141,28 +143,34 @@ protected void removeAllChildren() {
141
143
142
144
@ Override
143
145
public int hashCode () {
144
- return name .hashCode ();
146
+ return key .hashCode ();
145
147
}
146
148
147
149
@ Override
148
150
public boolean equals (Object o ) {
149
151
if (!(o instanceof SocketIONode )) return false ;
150
- return Objects .equals (name , ((SocketIONode )o ).name );
152
+ return Objects .equals (key , ((SocketIONode )o ).key );
151
153
}
152
154
153
155
@ Override
154
156
public String toString () {
155
157
return name ;
156
158
}
159
+
160
+ private void setName (String newName ) {
161
+ if (name == null || newName .length () > name .length ()) {
162
+ name = newName ;
163
+ }
164
+ }
157
165
158
166
159
167
static final class Address extends SocketIONode {
160
168
161
169
private static final String IMAGE_PATH = "org/graalvm/visualvm/jfr/resources/host.png" ; // NOI18N
162
170
private static final Icon ICON = new ImageIcon (ImageUtilities .loadImage (IMAGE_PATH , true ));
163
171
164
- Address (String name , SocketIONode parent , boolean terminal ) {
165
- super (name , ICON , parent , terminal ? null : new ArrayList ());
172
+ Address (String key , String name , SocketIONode parent , boolean terminal ) {
173
+ super (key , name , ICON , parent , terminal ? null : new ArrayList ());
166
174
}
167
175
168
176
}
@@ -173,7 +181,7 @@ static final class Port extends SocketIONode {
173
181
private static final Icon ICON = new ImageIcon (ImageUtilities .loadImage (IMAGE_PATH , true ));
174
182
175
183
Port (String name , SocketIONode parent , boolean terminal ) {
176
- super (name , ICON , parent , terminal ? null : new ArrayList ());
184
+ super (name , name , ICON , parent , terminal ? null : new ArrayList ());
177
185
}
178
186
179
187
}
@@ -182,7 +190,7 @@ static final class Port extends SocketIONode {
182
190
static final class Thread extends SocketIONode {
183
191
184
192
Thread (String name , SocketIONode parent , boolean terminal ) {
185
- super (name , Icons .getIcon (ProfilerIcons .THREAD ), parent , terminal ? null : new ArrayList ());
193
+ super (name , name , Icons .getIcon (ProfilerIcons .THREAD ), parent , terminal ? null : new ArrayList ());
186
194
}
187
195
188
196
}
@@ -199,7 +207,7 @@ static final class Root extends SocketIONode implements JFREventVisitor {
199
207
}
200
208
201
209
Root (SocketIOViewSupport .Aggregation primary , SocketIOViewSupport .Aggregation secondary ) {
202
- super (null , null , null , primary == null && secondary == null ? null : new ArrayList ());
210
+ super (null , null , null , null , primary == null && secondary == null ? null : new ArrayList ());
203
211
204
212
this .primary = primary ;
205
213
this .secondary = SocketIOViewSupport .Aggregation .NONE .equals (secondary ) ? null : secondary ;
@@ -214,40 +222,43 @@ public boolean visit(String typeName, JFREvent event) {
214
222
else rw = null ;
215
223
216
224
if (rw != null ) {
225
+ String primaryKey = getKey (primary , event );
217
226
String primaryName = getName (primary , event );
218
- if (primaryName == null ) primaryName = "<unknown>" ;
227
+ if (primaryKey == null ) primaryKey = UNKNOWN ;
228
+ if (primaryName == null ) primaryName = UNKNOWN ;
219
229
220
- SocketIONode primaryNode = getChild (primaryName );
230
+ SocketIONode primaryNode = getChild (primaryKey );
221
231
if (primaryNode == null ) {
222
- primaryNode = createNode (primaryName , primary , this , secondary == null );
232
+ primaryNode = createNode (primaryKey , primaryName , primary , this , secondary == null );
223
233
addChild (primaryNode );
224
234
}
225
235
226
236
if (secondary != null ) {
237
+ String secondaryKey = getKey (secondary , event );
227
238
String secondaryName = getName (secondary , event );
228
- if (secondaryName == null ) secondaryName = "<unknown>" ;
229
-
239
+ if (secondaryKey == null ) secondaryKey = UNKNOWN ;
240
+ if ( secondaryName == null ) secondaryName = UNKNOWN ;
230
241
231
- SocketIONode secondaryNode = primaryNode .getChild (secondaryName );
242
+ SocketIONode secondaryNode = primaryNode .getChild (secondaryKey );
232
243
if (secondaryNode == null ) {
233
- secondaryNode = createNode (secondaryName , secondary , primaryNode , true );
244
+ secondaryNode = createNode (secondaryKey , secondaryName , secondary , primaryNode , true );
234
245
primaryNode .addChild (secondaryNode );
235
246
}
236
247
237
- processEvent (secondaryNode , event , rw );
248
+ processEvent (secondaryNode , secondaryName , event , rw );
238
249
} else {
239
- processEvent (primaryNode , event , rw );
250
+ processEvent (primaryNode , primaryName , event , rw );
240
251
}
241
252
}
242
253
243
254
return false ;
244
255
}
245
256
246
-
247
- private static void processEvent (SocketIONode node , JFREvent event , Boolean rw ) {
257
+ private static void processEvent (SocketIONode node , String name , JFREvent event , Boolean rw ) {
248
258
try {
249
- if (Boolean .FALSE .equals (rw )) node .processRead (event .getDuration ("eventDuration" ), event .getLong ("bytesRead" )); // NOI18N
250
- else node .processWrite (event .getDuration ("eventDuration" ), event .getLong ("bytesWritten" )); // NOI18N
259
+ node .setName (name );
260
+ if (Boolean .FALSE .equals (rw )) node .processRead (getDuration (event ), event .getLong ("bytesRead" )); // NOI18N
261
+ else node .processWrite (getDuration (event ), event .getLong ("bytesWritten" )); // NOI18N
251
262
} catch (JFRPropertyNotAvailableException e ) {
252
263
System .err .println (">>> " + e );
253
264
}
@@ -265,33 +276,56 @@ public boolean equals(Object o) {
265
276
}
266
277
267
278
268
- private static String getName (SocketIOViewSupport .Aggregation aggregation , JFREvent event ) {
279
+ private static String getKey (SocketIOViewSupport .Aggregation aggregation , JFREvent event ) {
269
280
try {
270
281
if (SocketIOViewSupport .Aggregation .ADDRESS .equals (aggregation )) {
271
- String address = event .getString ("address" ); // NOI18N
272
- String host = event .getString ("host" ); // NOI18N
273
- return host == null || host .trim ().isEmpty () ? address : address + " (" + host + ")" ; // NOI18N
282
+ return getAddress (event ); // NOI18N
274
283
}
275
284
if (SocketIOViewSupport .Aggregation .PORT .equals (aggregation )) return getPort (event );
276
285
if (SocketIOViewSupport .Aggregation .ADDRESS_PORT .equals (aggregation )) {
277
- String address = event .getString ("address" ); // NOI18N
278
- String host = event .getString ("host" ); // NOI18N
279
- if (host != null && !host .trim ().isEmpty ()) address = address + " (" + host + ")" ; // NOI18N
286
+ String address = getAddress (event ); // NOI18N
280
287
return address + " : " + getPort (event ); // NOI18N
281
288
}
282
289
if (SocketIOViewSupport .Aggregation .THREAD .equals (aggregation )) return event .getThread ("eventThread" ).getName ();
283
290
} catch (JFRPropertyNotAvailableException e ) {}
284
291
return null ;
285
292
}
293
+
294
+ private static Duration getDuration (JFREvent event ) throws JFRPropertyNotAvailableException {
295
+ return event .getDuration ("eventDuration" );
296
+ }
286
297
298
+ private static String getAddress (JFREvent event ) throws JFRPropertyNotAvailableException {
299
+ return event .getString ("address" );
300
+ }
301
+
302
+ private static String getName (SocketIOViewSupport .Aggregation aggregation , JFREvent event ) {
303
+ try {
304
+ if (SocketIOViewSupport .Aggregation .ADDRESS .equals (aggregation )) return getFullAddress (event );
305
+ if (SocketIOViewSupport .Aggregation .PORT .equals (aggregation )) return getPort (event );
306
+ if (SocketIOViewSupport .Aggregation .ADDRESS_PORT .equals (aggregation )) {
307
+ return getFullAddress (event ) + " : " + getPort (event ); // NOI18N
308
+ }
309
+ if (SocketIOViewSupport .Aggregation .THREAD .equals (aggregation )) return event .getThread ("eventThread" ).getName ();
310
+ } catch (JFRPropertyNotAvailableException e ) {}
311
+ return null ;
312
+ }
313
+
287
314
private static String getPort (JFREvent event ) throws JFRPropertyNotAvailableException {
288
- return NumberFormat .getIntegerInstance ().format (event .getInt ("port" )); // NOI18N
315
+ return String .valueOf (event .getInt ("port" )); // NOI18N
316
+ }
317
+
318
+ private static String getFullAddress (JFREvent event ) throws JFRPropertyNotAvailableException {
319
+ String address = getAddress (event ); // NOI18N
320
+ String host = event .getString ("host" ); // NOI18N
321
+ if (host != null && !host .trim ().isEmpty () && !host .equals (address )) address = address + " (" + host + ")" ; // NOI18N
322
+ return address ;
289
323
}
290
324
291
- private SocketIONode createNode (String name , SocketIOViewSupport .Aggregation aggregation , SocketIONode parent , boolean terminal ) {
292
- if (SocketIOViewSupport .Aggregation .ADDRESS .equals (aggregation )) return new SocketIONode .Address (name , parent , terminal );
325
+ private SocketIONode createNode (String key , String name , SocketIOViewSupport .Aggregation aggregation , SocketIONode parent , boolean terminal ) {
326
+ if (SocketIOViewSupport .Aggregation .ADDRESS .equals (aggregation )) return new SocketIONode .Address (key , name , parent , terminal );
293
327
if (SocketIOViewSupport .Aggregation .PORT .equals (aggregation )) return new SocketIONode .Port (name , parent , terminal );
294
- if (SocketIOViewSupport .Aggregation .ADDRESS_PORT .equals (aggregation )) return new SocketIONode .Address (name , parent , terminal );
328
+ if (SocketIOViewSupport .Aggregation .ADDRESS_PORT .equals (aggregation )) return new SocketIONode .Address (key , name , parent , terminal );
295
329
if (SocketIOViewSupport .Aggregation .THREAD .equals (aggregation )) return new SocketIONode .Thread (name , parent , terminal );
296
330
return null ;
297
331
}
0 commit comments