/
Auditor.java
800 lines (706 loc) · 28 KB
/
Auditor.java
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*
* Copyright 2015 The Portico Project
*
* This file is part of portico.
*
* portico is free software; you can redistribute it and/or modify
* it under the terms of the Common Developer and Distribution License (CDDL)
* as published by Sun Microsystems. For more information see the LICENSE file.
*
* Use of this software is strictly AT YOUR OWN RISK!!!
* If something bad happens you do not have permission to come crying to me.
* (that goes for your lawyer as well)
*
*/
package org.portico.bindings.jgroups;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.FileAppender;
import org.jgroups.Version;
import org.portico.lrc.LRC;
import org.portico.lrc.LRCState;
import org.portico.lrc.PorticoConstants;
import org.portico.lrc.compat.JConfigurationException;
import org.portico.lrc.management.Federate;
import org.portico.lrc.model.OCInstance;
import org.portico.lrc.model.ObjectModel;
import org.portico.lrc.services.object.msg.DeleteObject;
import org.portico.lrc.services.object.msg.DiscoverObject;
import org.portico.lrc.services.object.msg.SendInteraction;
import org.portico.lrc.services.object.msg.UpdateAttributes;
import org.portico.utils.messaging.PorticoMessage;
/**
* This class is used to log metadata about incoming/outgoing messages so that
* they can be audited later. This information is typically used to support decisions
* about configuration tweaks for performance tuning and to get an insight into
* how information is flowing through a federation.
*
* The class wraps a simple Log4j logger that writes its information to a separate
* file from the main log so that is can be audited and followed without the additional
* noise of all the other goings on inside the LRC.
*
* By default the Auditor is turned off, requiring enabling in the RID file.
*/
public class Auditor
{
//----------------------------------------------------------
// STATIC VARIABLES
//----------------------------------------------------------
//----------------------------------------------------------
// INSTANCE VARIABLES
//----------------------------------------------------------
private boolean recording;
private Logger logger;
private FileAppender appender; // keep a ref so we can close out safely
private String receivedFormat;
private String sentFormat;
private String federationName;
private String federateName;
private LRC lrc;
private LRCState lrcState;
// counters and metrics
private Map<String,MessageMetrics> metrics;
// filters to restrict what we log
private List<String> directionFilters;
private List<String> messageFilters;
private List<String> fomtypeFilters;
//----------------------------------------------------------
// CONSTRUCTORS
//----------------------------------------------------------
public Auditor()
{
this.recording = false;
this.logger = null;
this.federationName = "";
this.federateName = "";
this.lrc = null; // set in startAuditing()
this.lrcState = null; // set in startAuditing()
String timeFormat = "%1tH:%<tM:%<tS.%<tL";
this.receivedFormat = "received "+timeFormat+" %30s %5d from %-15s %s";
this.sentFormat = " sent "+timeFormat+" %30s %5d %-20s %s";
// counters and metrics
this.metrics = new HashMap<String,MessageMetrics>();
// filters
this.directionFilters = new ArrayList<String>();
this.messageFilters = new ArrayList<String>();
this.fomtypeFilters = new ArrayList<String>();
}
//----------------------------------------------------------
// INSTANCE METHODS
//----------------------------------------------------------
/**
* The local federate just sent the given message. When serialized, the message was the
* given size in bytes.
*
* @param message The message that was sent
* @param size The serialized size of the message
*/
public void sent( PorticoMessage message, int size )
{
if( !recording )
return;
// does this need to be filtered out?
if( shouldFilter(message,"sent") )
return;
// who are we sending this to?
String federateName = getFederateName( message.getTargetFederate() );
if( federateName.startsWith("unknown") )
federateName = " ";
else
federateName = " to "+federateName+" ";
// log our message
String type = message.getClass().getSimpleName();
String entry = String.format( sentFormat,
new Date(System.currentTimeMillis()),
type,
size,
federateName,
getMessageSpecificString(message) );
logger.info( entry );
// log some metrics
MessageMetrics counter = metrics.get( type );
if( counter == null )
{
counter = new MessageMetrics( type );
metrics.put( type, counter );
}
counter.totalSent += 1;
counter.totalSentSize += size;
// store FOM class specific information
MessageMetrics fomSpecific = getFomSpecificMetrics( message );
if( fomSpecific != null )
{
fomSpecific.totalSent += 1;
fomSpecific.totalSentSize += size;
}
}
/**
* The local federate just received the given message. When received on the wire, the
* message was the given size in bytes.
*
* @param message The message that was received
* @param size The serialized size of the message
*/
public void received( PorticoMessage message, int size )
{
if( !recording )
return;
// does this need to be filtered out?
if( shouldFilter(message,"received") )
return;
// discard our own messages as they're not coming from the network
if( message.getSourceFederate() == lrc.getState().getFederateHandle() )
return;
// log our entry
String federateName = getFederateName( message.getSourceFederate() );
String type = message.getClass().getSimpleName();
String entry = String.format( receivedFormat,
new Date(System.currentTimeMillis()),
type,
size,
federateName,
getMessageSpecificString(message) );
logger.info( entry );
// log some metrics
MessageMetrics counter = metrics.get( type );
if( counter == null )
{
counter = new MessageMetrics( type );
metrics.put( type, counter );
}
counter.totalReceived += 1;
counter.totalReceivedSize += size;
// store FOM class specific information
MessageMetrics fomSpecific = getFomSpecificMetrics( message );
if( fomSpecific != null )
{
fomSpecific.totalReceived += 1;
fomSpecific.totalReceivedSize += size;
}
}
///////////////////////////////////////////////////////////////////////////////
/////////////////////////// Portico Data Methods ////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/**
* For the federate with the given handle, get a printable name. Value returned
* is in the format "federateName(handle)". If the federate is not known, "unknown"
* is returned for the name.
*/
private String getFederateName( int federateHandle )
{
Federate source = lrcState.getKnownFederate( federateHandle );
if( source != null )
return source.getFederateName()+"("+federateHandle+")";
else
return "unknown("+federateHandle+")";
}
/**
* Inspect the given PorticoMessage and if it is one of the specified types (create, update
* delete, interaction) then generate some additional information to print and return it. If
* the message isn't one of these then an empty string is returned signalling that no extra
* information should be printed.
*/
private String getMessageSpecificString( PorticoMessage message )
{
if( message instanceof UpdateAttributes )
{
// //
// Reflection Message //
// //
UpdateAttributes update = (UpdateAttributes)message;
// To get the class name we have to get the object and ask it.
// In rare cases we might now have received & processed the discover call
// before an update comes in, so the object may not exist in the store yet
int objectHandle = update.getObjectId();
OCInstance object = getObject( objectHandle );
if( object != null )
{
String className = getClassName( object.getRegisteredClassHandle() );
String objectName = object.getName()+"("+objectHandle+")";
// return in form "className, objectName(handle), 7 attributes"
return className+", "+objectName+", "+update.getAttributes().size()+" attributes";
}
else
{
return "UnknownClass, UnknownObject, "+update.getAttributes().size()+" attributes";
}
}
else if( message instanceof SendInteraction )
{
// //
// Send Interaction //
// //
SendInteraction interaction = (SendInteraction)message;
// get the class name
int interactionHandle = interaction.getInteractionId();
String className = getFom().getInteractionClass(interactionHandle).getLocalName();
// return in form "className, 7 parameters"
return className+", "+interaction.getParameters().size()+" parameters";
}
else if( message instanceof DiscoverObject )
{
// //
// Discover Message //
// //
DiscoverObject discover = (DiscoverObject)message;
String className = getFom().getObjectClass(discover.getClassHandle()).getLocalName();
String objectName = discover.getObjectName()+"("+discover.getObjectHandle()+")";
return className+", "+objectName;
}
else if( message instanceof DeleteObject )
{
DeleteObject delete = (DeleteObject)message;
// to get the class name we have to get the object, and from it get the class
int objectHandle = delete.getObjectHandle();
OCInstance object = getObject( objectHandle );
if( object == null )
return "Unknown";
String className = getClassName( object.getRegisteredClassHandle() );
// now get the object name
String objectName = object.getName()+"("+objectHandle+")";
// return in form "className, objectName(handle)"
return className+", "+objectName;
}
else
{
return "";
}
}
/**
* Find and fetch the MessageMetrics for the specific FOM class within the metrics for the
* type of the given message. For example, find the MessageMetrics representing the Lifeform
* class from within the metrics object representing update messages.
*
* If this class isn't one we're interested in FOM-specific metrics for (create, refelct,
* delete, interaction), then return null.
*/
private MessageMetrics getFomSpecificMetrics( PorticoMessage message )
{
// get the metrics object for the message type (reflect, interaction, etc...)
// if this isn't here, we have no specific metrics so return null
MessageMetrics parent = metrics.get( message.getClass().getSimpleName() );
if( parent == null )
return null;
String className = getClassName( message );
if( className.equals("Unknown") == false )
return parent.getOrAdd( className );
else
return null;
}
/**
* We can't cache this at startup because we start auditing during the join call before
* things are fully set up.As such, we have to ask for it each time. It's cool - we won't g
* et a request until after join finishes and by then the code below won't return null.
*/
private ObjectModel getFom()
{
return lrcState.getFOM();
}
/**
* For the given handle, get the associated {@link OCInstance} data from the repository.
* This may return <code>null</code> if it comes in before the federate has had a chance
* to process the discovery call.
*/
private final OCInstance getObject( int objectHandle )
{
return lrcState.getRepository().getDiscoveredOrUndiscovered( objectHandle );
}
/**
* Get the name of the Object Class represented by the given handle. This will return only
* the local portion of the class, not its fully qualified name
*/
private final String getClassName( int classHandle )
{
return lrcState.getFOM().getObjectClass(classHandle).getLocalName();
}
/** Get appropriate object/interaction class name for this message type, or return "Unknown" */
private final String getClassName( PorticoMessage message )
{
if( message instanceof UpdateAttributes )
{
int objectHandle = ((UpdateAttributes)message).getObjectId();
OCInstance object = getObject( objectHandle );
if( object != null )
return getClassName( object.getRegisteredClassHandle() );
}
else if( message instanceof SendInteraction )
{
int classHandle = ((SendInteraction)message).getInteractionId();
return getFom().getInteractionClass(classHandle).getLocalName();
}
else if( message instanceof DiscoverObject )
{
int classHandle = ((DiscoverObject)message).getClassHandle();
return getFom().getObjectClass(classHandle).getLocalName();
}
else if( message instanceof DeleteObject )
{
OCInstance object = getObject( ((DeleteObject)message).getObjectHandle() );
return getClassName( object.getRegisteredClassHandle() );
}
return "Unknown";
}
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// Filter Methods ///////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
* @return True if we should filter out this message - false otherwise
*/
private boolean shouldFilter( PorticoMessage message, String direction )
{
// The filter lists contain information about which types should be let through,
// so if something isn't in the list, it shouldn't get through. If the list is
// empty it means we have no filters, so let everything through
// check the direction
if( directionFilters.isEmpty() == false )
{
if( this.directionFilters.contains(direction) == false )
return true; // FILTER IT!
}
// check the message type
if( messageFilters.isEmpty() == false )
{
String type = message.getClass().getSimpleName();
if( this.messageFilters.contains(type) == false )
return true; // FILTER IT!
}
if( fomtypeFilters.isEmpty() == false )
{
String fomtype = getClassName(message);
if( this.fomtypeFilters.contains(fomtype) == false )
return true; // FILTER IT!
}
// let it through
return false;
}
/** Populate our filter list from configuration */
private void populateFilters()
{
this.directionFilters = JGroupsProperties.getAuditorDirectionFilters();
this.messageFilters = JGroupsProperties.getAuditorMessageFilters();
this.fomtypeFilters = JGroupsProperties.getAuditorFomtypeFilters();
// if "all" is set, clear the filter
if( shouldDisableFilter(directionFilters) )
directionFilters.clear();
if( shouldDisableFilter(messageFilters) )
messageFilters.clear();
if( shouldDisableFilter(fomtypeFilters) )
fomtypeFilters.clear();
}
/** If the list contains "all", true is returned indicating we shouldn't filter anything */
private boolean shouldDisableFilter( List<String> list )
{
for( String temp : list )
{
if( temp.equals("all") )
return true;
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////// Lifecycle Methods /////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/**
* @return True if the auditor is currently recording content. False if it is not.
*/
public final boolean isRecording()
{
return this.recording;
}
/**
* Enable the auditor and start collecting metadata. At this point in time the log file
* will be created under "logs/audit-[federateName].log".
*
* @param federationName Used in an initial log message
* @param federateName Used in the audit file name
* @param lrc Used to convert federate names to handles for incoming/outgoing messages
*/
public void startAuditing( String federationName, String federateName, LRC lrc )
throws JConfigurationException
{
if( this.recording )
return;
this.federationName = federationName;
this.federateName = federateName;
this.lrc = lrc;
this.lrcState = this.lrc.getState();
turnLoggerOn();
this.recording = true;
// get our filters
populateFilters();
// log a startup message with some useful information
logger.info( "Starting Audit log for federate ["+federateName+"] in federation ["+
federationName+"]" );
logger.info( "Portico "+PorticoConstants.RTI_VERSION + " - JGroups "+Version.description );
logger.info( "Active Filters:" );
logger.info( " direction: "+directionFilters );
logger.info( " message: "+messageFilters );
logger.info( " fomtype: "+fomtypeFilters );
logger.info( "" );
}
/**
* Create and configure out logger and file appender so that we can start recording.
*/
private void turnLoggerOn() throws JConfigurationException
{
try
{
// log file is in logs/audit-federateName.log
String logdir = System.getProperty( PorticoConstants.PROPERTY_LOG_DIR, "logs" );
String logfile = logdir +"/audit-"+this.federateName+".log";
// create the appender for the logger
String pattern = new String( "%m%n" );
PatternLayout layout = new PatternLayout( pattern );
this.appender = new FileAppender( layout, logfile, true );
// attach the appender
this.logger = Logger.getLogger( "auditor" );
this.logger.addAppender( appender );
}
catch( IOException ioex )
{
throw new JConfigurationException( "error configuring auditor log: "+ioex.getMessage(), ioex );
}
}
/**
* The federation execution is now over - wrap up the auditing by printing a final summary
* and closing out the file.
*/
public void stopAuditing()
{
if( this.recording == false )
return;
this.federationName = "";
this.federateName = "";
this.lrc = null;
this.recording = false;
// print the execution summary before we leave
logSummary();
// turn the logger off
this.logger.removeAppender( appender );
this.appender.close();
// clear the filter lists
this.directionFilters.clear();
this.messageFilters.clear();
this.fomtypeFilters.clear();
}
///////////////////////////////////////////////////////////////////////////////
////////////////////////// Summary Logging Methods //////////////////////////
///////////////////////////////////////////////////////////////////////////////
/**
* Log the captured event information showing how many times each message type was captured
* in both the sending and receiving directions, along with the total and average size of
* these messages.
*/
private void logSummary()
{
// get some totals
long sentCount = 0;
long sentSize = 0;
long receivedCount = 0;
long receivedSize = 0;
for( String message : metrics.keySet() )
{
MessageMetrics temp = metrics.get( message );
sentCount += temp.totalSent;
sentSize += temp.totalSentSize;
receivedCount += temp.totalReceived;
receivedSize += temp.totalReceivedSize;
}
// print a report
String finishTime = String.format( "%1tH:%<tM:%<tS.%<tL", new Date(System.currentTimeMillis()) );
String sentString = String.format( "%6d (%s)", sentCount, getSizeString(sentSize) );
String receivedString = String.format( "%6d (%s)", receivedCount, getSizeString(receivedSize) );
logger.info( "" );
logger.info( "==============================================" );
logger.info( " Execution Summary" );
logger.info( "==============================================" );
logger.info( " Finish Time: "+finishTime );
logger.info( " Total Sent: "+sentString );
logger.info( " Total Received: "+receivedString );
logger.info( "" );
// events table
logger.info(" |-----------------------------|-------------------------------|-------------------------------|" );
logger.info(" | | Sent | Received |" );
logger.info(" | |---------------------------------------------------------------|" );
logger.info(" | Message Name | Count | Size | Avg | Count | Size | Avg |" );
logger.info(" |-----------------------------|-------------------------------|-------------------------------|" );
List<MessageMetrics> ordered = new ArrayList<MessageMetrics>( metrics.values() );
Collections.sort( ordered );
for( MessageMetrics temp : ordered )
{
String sentTotal = temp.totalSent == 0 ? "" : ""+temp.totalSent;
String recvTotal = temp.totalReceived == 0 ? "" : ""+temp.totalReceived;
String line = String.format( " | %27s | %7s | %9s |%8s | %7s | %9s |%8s |",
temp.messageClass,
sentTotal,
getSizeString(temp.totalSentSize),
getSizeString(temp.getSentSizeAvg()),
recvTotal,
getSizeString(temp.totalReceivedSize),
getSizeString(temp.getReceivedSizeAvg()) );
logger.info( line );
}
// print the totals
long sentAverage = sentCount == 0 ? 0 : sentSize/sentCount;
long receivedAverage = receivedCount == 0 ? 0 : receivedSize/receivedCount;
logger.info(" |-----------------------------|-------------------------------|-------------------------------|" );
logger.info( String.format(" | Total | %7d | %9s |%8s | %7d | %9s |%8s |",
sentCount,
getSizeString(sentSize),
getSizeString(sentAverage),
receivedCount,
getSizeString(receivedSize),
getSizeString(receivedAverage)) );
logger.info(" |-----------------------------|-------------------------------|-------------------------------|" );
logger.info( "" );
logger.info( "" );
logFomSpecificSummary();
}
/**
* The standard summary logs information broken down by Portico message types. These relate
* to API-level actions like Discover, Reflect, etc... The Auditor also records FOM specific
* information for major events so that it can tell you, for example, how many objects of a
* particular class were registered. This method prints that as a separate summary table.
*/
private void logFomSpecificSummary()
{
// events table
logger.info( " FOM Specific Summary" );
logger.info( "" );
logger.info(" |-----------------------------|-------------------------------|-------------------------------|" );
logger.info(" | | Sent | Received |" );
logger.info(" | |---------------------------------------------------------------|" );
logger.info(" | Message Name | Count | Size | Avg | Count | Size | Avg |" );
logger.info(" |-----------------------------|-------------------------------|-------------------------------|" );
// loop through all the message types and print out any FOM specific data they have
List<MessageMetrics> ordered = new ArrayList<MessageMetrics>( metrics.values() );
Collections.sort( ordered );
for( MessageMetrics type : ordered )
{
if( type.hasFomSpecificData() == false )
continue;
String sentTotal = type.totalSent == 0 ? "" : ""+type.totalSent;
String recvTotal = type.totalReceived == 0 ? "" : ""+type.totalReceived;
String line = String.format( " | %-27s | %7s | %9s |%8s | %7s | %9s |%8s |",
type.messageClass,
sentTotal,
getSizeString(type.totalSentSize),
getSizeString(type.getSentSizeAvg()),
recvTotal,
getSizeString(type.totalReceivedSize),
getSizeString(type.getReceivedSizeAvg()) );
logger.info( line );
// print the FOM specific breakdown
List<MessageMetrics> fomtypes = new ArrayList<MessageMetrics>( type.fomspecific.values() );
Collections.sort( fomtypes );
for( MessageMetrics fomtype : fomtypes )
{
sentTotal = fomtype.totalSent == 0 ? "" : ""+fomtype.totalSent;
recvTotal = fomtype.totalReceived == 0 ? "" : ""+fomtype.totalReceived;
line = String.format( " | %27s | %7s | %9s |%8s | %7s | %9s |%8s |",
fomtype.messageClass,
sentTotal,
getSizeString(fomtype.totalSentSize),
getSizeString(fomtype.getSentSizeAvg()),
recvTotal,
getSizeString(fomtype.totalReceivedSize),
getSizeString(fomtype.getReceivedSizeAvg()) );
logger.info( line );
}
logger.info(" |-----------------------------|-------------------------------|-------------------------------|" );
}
// a bit of breathing room at the end
logger.info( "" );
logger.info( "" );
}
/**
* Convert the given size (in bytes) to a more human readable string. Returned values
* will be in the form: "16B", "16KB", "16MB", "16GB".
*/
private String getSizeString( double size )
{
if( size == 0 )
return "";
double totalkb = size / 1024;
double totalmb = totalkb / 1024;
double totalgb = totalmb / 1024;
if( totalgb > 1.0 )
return String.format("%5.1f GB", totalgb );
else if( totalmb > 1.0 )
return String.format("%5.1f MB", totalmb );
else if( totalkb > 1.0 )
return String.format("%5.1f KB", totalkb );
else
return String.format("%5d B ", (int)size );
}
//----------------------------------------------------------
// STATIC METHODS
//----------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
//////////////////// Private Inner Class: Message Metrics ////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
* Used to store information about the messages sent and received by the federate.
* This information is stored per message class.
*/
private class MessageMetrics implements Comparable<MessageMetrics>
{
public String messageClass;
public long totalSent;
public long totalSentSize; // fine up to 8 exabytes
public long totalReceived;
public long totalReceivedSize;
// We use this to record FOM-specific metrics broken down by object/interaction
// class name (rather than java type). A type that stores a set of itself? Oh god.
// This is only used for create/reflect/delete/interaction events
public Map<String,MessageMetrics> fomspecific;
public MessageMetrics( String className )
{
this.messageClass = className;
// create this all the time just to avoid NPE problems
this.fomspecific = new HashMap<String,MessageMetrics>();
}
public boolean hasFomSpecificData()
{
return this.fomspecific.isEmpty() == false;
}
/** Get the FOM-specific MessageMetrics information for the given FOM class. If there
is no record of it for this type of message, add one and return it */
public MessageMetrics getOrAdd( String fomClass )
{
MessageMetrics specific = fomspecific.get( fomClass );
if( specific == null )
{
specific = new MessageMetrics( fomClass );
fomspecific.put( fomClass, specific );
}
return specific;
}
public double getSentSizeAvg()
{
return totalSent == 0 ? 0 : totalSentSize/totalSent;
}
public double getReceivedSizeAvg()
{
return totalReceived == 0 ? 0 : totalReceivedSize/totalReceived;
}
public int compareTo( MessageMetrics other )
{
// this comparision is reverse - as we want the large values at the front of the list
long ours = totalSent + totalReceived;
long theirs = other.totalSent + totalReceived;
if( ours > theirs )
return -1;
else if( ours < theirs )
return 1;
else return 0;
}
}
}