001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.jvmstat.perfdata.monitor.v1_0;
027:
028: import sun.management.counter.Units;
029: import sun.management.counter.Variability;
030: import sun.jvmstat.monitor.*;
031: import sun.jvmstat.perfdata.monitor.*;
032: import java.util.*;
033: import java.util.regex.*;
034: import java.nio.*;
035:
036: /**
037: * The concrete implementation of version 1.0 of the HotSpot PerfData
038: * Instrumentation buffer. This class is responsible for parsing the
039: * instrumentation memory and constructing the necessary objects to
040: * represent and access the instrumentation objects contained in the
041: * memory buffer.
042: *
043: * @author Brian Doherty
044: * @version 1.9, 05/09/07
045: * @since 1.5
046: * @see AbstractPerfDataBuffer
047: */
048: public class PerfDataBuffer extends PerfDataBufferImpl {
049:
050: private static final boolean DEBUG = false;
051: private static final int syncWaitMs = Integer.getInteger(
052: "sun.jvmstat.perdata.syncWaitMs", 5000);
053: private static final ArrayList EMPTY_LIST = new ArrayList(0);
054:
055: /*
056: * the following constants must be kept in sync with struct
057: * PerfDataEntry in perfMemory.hpp
058: */
059: private final static int PERFDATA_ENTRYLENGTH_OFFSET = 0;
060: private final static int PERFDATA_ENTRYLENGTH_SIZE = 4; // sizeof(int)
061: private final static int PERFDATA_NAMELENGTH_OFFSET = 4;
062: private final static int PERFDATA_NAMELENGTH_SIZE = 4; // sizeof(int)
063: private final static int PERFDATA_VECTORLENGTH_OFFSET = 8;
064: private final static int PERFDATA_VECTORLENGTH_SIZE = 4; // sizeof(int)
065: private final static int PERFDATA_DATATYPE_OFFSET = 12;
066: private final static int PERFDATA_DATATYPE_SIZE = 1; // sizeof(byte)
067: private final static int PERFDATA_FLAGS_OFFSET = 13;
068: private final static int PERFDATA_FLAGS_SIZE = 1; // sizeof(byte)
069: private final static int PERFDATA_DATAUNITS_OFFSET = 14;
070: private final static int PERFDATA_DATAUNITS_SIZE = 1; // sizeof(byte)
071: private final static int PERFDATA_DATAATTR_OFFSET = 15;
072: private final static int PERFDATA_DATAATTR_SIZE = 1; // sizeof(byte)
073: private final static int PERFDATA_NAME_OFFSET = 16;
074:
075: PerfDataBufferPrologue prologue;
076: int nextEntry;
077: int pollForEntry;
078: int perfDataItem;
079: long lastModificationTime;
080: int lastUsed;
081: IntegerMonitor overflow;
082: ArrayList<Monitor> insertedMonitors;
083:
084: /**
085: * Construct a PerfDataBufferImpl instance.
086: * <p>
087: * This class is dynamically loaded by
088: * {@link AbstractPerfDataBuffer#createPerfDataBuffer}, and this
089: * constructor is called to instantiate the instance.
090: *
091: * @param buffer the buffer containing the instrumentation data
092: * @param lvmid the Local Java Virtual Machine Identifier for this
093: * instrumentation buffer.
094: */
095: public PerfDataBuffer(ByteBuffer buffer, int lvmid)
096: throws MonitorException {
097: super (buffer, lvmid);
098: prologue = new PerfDataBufferPrologue(buffer);
099: this .buffer.order(prologue.getByteOrder());
100: }
101:
102: /**
103: * {@inheritDoc}
104: */
105: protected void buildMonitorMap(Map<String, Monitor> map)
106: throws MonitorException {
107: assert Thread.holdsLock(this );
108:
109: // start at the beginning of the buffer
110: buffer.rewind();
111:
112: // create pseudo monitors
113: buildPseudoMonitors(map);
114:
115: // position buffer to start of the data section
116: buffer.position(prologue.getSize());
117: nextEntry = buffer.position();
118: perfDataItem = 0;
119:
120: int used = prologue.getUsed();
121: long modificationTime = prologue.getModificationTimeStamp();
122:
123: Monitor m = getNextMonitorEntry();
124: while (m != null) {
125: map.put(m.getName(), m);
126: m = getNextMonitorEntry();
127: }
128:
129: /*
130: * set the last modification data. These are set to the values
131: * recorded before parsing the data structure. This allows the
132: * the data structure to be modified while the Map is being built.
133: * The Map may contain more entries than indicated based on the
134: * time stamp, but this is handled by ignoring duplicate entries
135: * when the Map is updated in getNewMonitors().
136: */
137: lastUsed = used;
138: lastModificationTime = modificationTime;
139:
140: // synchronize with the target jvm
141: synchWithTarget(map);
142:
143: // work around 1.4.2 counter inititization bugs
144: kludge(map);
145:
146: insertedMonitors = new ArrayList<Monitor>(map.values());
147: }
148:
149: /**
150: * {@inheritDoc}
151: */
152: protected void getNewMonitors(Map<String, Monitor> map)
153: throws MonitorException {
154: assert Thread.holdsLock(this );
155:
156: int used = prologue.getUsed();
157: long modificationTime = prologue.getModificationTimeStamp();
158:
159: if ((used > lastUsed)
160: || (lastModificationTime > modificationTime)) {
161:
162: lastUsed = used;
163: lastModificationTime = modificationTime;
164:
165: Monitor monitor = getNextMonitorEntry();
166: while (monitor != null) {
167: String name = monitor.getName();
168:
169: // guard against duplicate entries
170: if (!map.containsKey(name)) {
171: map.put(name, monitor);
172:
173: /*
174: * insertedMonitors is null when called from pollFor()
175: * via buildMonitorMap(). Since we update insertedMonitors
176: * at the end of buildMonitorMap(), it's ok to skip the
177: * add here.
178: */
179: if (insertedMonitors != null) {
180: insertedMonitors.add(monitor);
181: }
182: }
183: monitor = getNextMonitorEntry();
184: }
185: }
186: }
187:
188: /**
189: * {@inheritDoc}
190: */
191: protected MonitorStatus getMonitorStatus(Map<String, Monitor> map)
192: throws MonitorException {
193: assert Thread.holdsLock(this );
194: assert insertedMonitors != null;
195:
196: // load any new monitors
197: getNewMonitors(map);
198:
199: // current implementation doesn't support deletion or reuse of entries
200: ArrayList removed = EMPTY_LIST;
201: ArrayList inserted = insertedMonitors;
202:
203: insertedMonitors = new ArrayList<Monitor>();
204: return new MonitorStatus(inserted, removed);
205: }
206:
207: /**
208: * Build the pseudo monitors used to map the prolog data into counters.
209: */
210: protected void buildPseudoMonitors(Map<String, Monitor> map) {
211: Monitor monitor = null;
212: String name = null;
213: IntBuffer ib = null;
214:
215: name = PerfDataBufferPrologue.PERFDATA_MAJOR_NAME;
216: ib = prologue.majorVersionBuffer();
217: monitor = new PerfIntegerMonitor(name, Units.NONE,
218: Variability.CONSTANT, false, ib);
219: map.put(name, monitor);
220:
221: name = PerfDataBufferPrologue.PERFDATA_MINOR_NAME;
222: ib = prologue.minorVersionBuffer();
223: monitor = new PerfIntegerMonitor(name, Units.NONE,
224: Variability.CONSTANT, false, ib);
225: map.put(name, monitor);
226:
227: name = PerfDataBufferPrologue.PERFDATA_BUFFER_SIZE_NAME;
228: ib = prologue.sizeBuffer();
229: monitor = new PerfIntegerMonitor(name, Units.BYTES,
230: Variability.MONOTONIC, false, ib);
231: map.put(name, monitor);
232:
233: name = PerfDataBufferPrologue.PERFDATA_BUFFER_USED_NAME;
234: ib = prologue.usedBuffer();
235: monitor = new PerfIntegerMonitor(name, Units.BYTES,
236: Variability.MONOTONIC, false, ib);
237: map.put(name, monitor);
238:
239: name = PerfDataBufferPrologue.PERFDATA_OVERFLOW_NAME;
240: ib = prologue.overflowBuffer();
241: monitor = new PerfIntegerMonitor(name, Units.BYTES,
242: Variability.MONOTONIC, false, ib);
243: map.put(name, monitor);
244: this .overflow = (IntegerMonitor) monitor;
245:
246: name = PerfDataBufferPrologue.PERFDATA_MODTIMESTAMP_NAME;
247: LongBuffer lb = prologue.modificationTimeStampBuffer();
248: monitor = new PerfLongMonitor(name, Units.TICKS,
249: Variability.MONOTONIC, false, lb);
250: map.put(name, monitor);
251: }
252:
253: /**
254: * Method to provide a gross level of synchronization with the
255: * target monitored jvm.
256: *
257: * gross synchronization works by polling for the hotspot.rt.hrt.ticks
258: * counter, which is the last counter created by the StatSampler
259: * initialization code. The counter is updated when the watcher thread
260: * starts scheduling tasks, which is the last thing done in vm
261: * initialization.
262: */
263: protected void synchWithTarget(Map<String, Monitor> map)
264: throws MonitorException {
265: /*
266: * synch must happen with syncWaitMs from now. Default is 5 seconds,
267: * which is reasonabally generous and should provide for extreme
268: * situations like startup delays due to allocation of large ISM heaps.
269: */
270: long timeLimit = System.currentTimeMillis() + syncWaitMs;
271:
272: String name = "hotspot.rt.hrt.ticks";
273: LongMonitor ticks = (LongMonitor) pollFor(map, name, timeLimit);
274:
275: /*
276: * loop waiting for the ticks counter to be non zero. This is
277: * an indication that the jvm is initialized.
278: */
279: log("synchWithTarget: " + lvmid + " ");
280: while (ticks.longValue() == 0) {
281: log(".");
282:
283: try {
284: Thread.sleep(20);
285: } catch (InterruptedException e) {
286: }
287:
288: if (System.currentTimeMillis() > timeLimit) {
289: lognl("failed: " + lvmid);
290: throw new MonitorException(
291: "Could Not Synchronize with target");
292: }
293: }
294: lognl("success: " + lvmid);
295: }
296:
297: /**
298: * Method to poll the instrumentation memory for a counter with
299: * the given name. The polling period is bounded by the timeLimit
300: * argument.
301: */
302: protected Monitor pollFor(Map<String, Monitor> map, String name,
303: long timeLimit) throws MonitorException {
304: Monitor monitor = null;
305:
306: log("polling for: " + lvmid + "," + name + " ");
307:
308: pollForEntry = nextEntry;
309: while ((monitor = map.get(name)) == null) {
310: log(".");
311:
312: try {
313: Thread.sleep(20);
314: } catch (InterruptedException e) {
315: }
316:
317: long t = System.currentTimeMillis();
318: if ((t > timeLimit) || (overflow.intValue() > 0)) {
319: lognl("failed: " + lvmid + "," + name);
320: dumpAll(map, lvmid);
321: throw new MonitorException(
322: "Could not find expected counter");
323: }
324:
325: getNewMonitors(map);
326: }
327: lognl("success: " + lvmid + "," + name);
328: return monitor;
329: }
330:
331: /**
332: * method to make adjustments for known counter problems. This
333: * method depends on the availability of certain counters, which
334: * is generally guaranteed by the synchWithTarget() method.
335: */
336: protected void kludge(Map<String, Monitor> map) {
337: if (Boolean.getBoolean("sun.jvmstat.perfdata.disableKludge")) {
338: // bypass all kludges
339: return;
340: }
341:
342: String name = "java.vm.version";
343: StringMonitor jvm_version = (StringMonitor) map.get(name);
344: if (jvm_version == null) {
345: jvm_version = (StringMonitor) findByAlias(name);
346: }
347:
348: name = "java.vm.name";
349: StringMonitor jvm_name = (StringMonitor) map.get(name);
350: if (jvm_name == null) {
351: jvm_name = (StringMonitor) findByAlias(name);
352: }
353:
354: name = "hotspot.vm.args";
355: StringMonitor args = (StringMonitor) map.get(name);
356: if (args == null) {
357: args = (StringMonitor) findByAlias(name);
358: }
359:
360: assert ((jvm_name != null) && (jvm_version != null) && (args != null));
361:
362: if (jvm_name.stringValue().indexOf("HotSpot") >= 0) {
363: if (jvm_version.stringValue().startsWith("1.4.2")) {
364: kludgeMantis(map, args);
365: }
366: }
367: }
368:
369: /**
370: * method to repair the 1.4.2 parallel scavenge counters that are
371: * incorrectly initialized by the JVM when UseAdaptiveSizePolicy
372: * is set. This bug couldn't be fixed for 1.4.2 FCS due to putback
373: * restrictions.
374: */
375: private void kludgeMantis(Map<String, Monitor> map,
376: StringMonitor args) {
377: /*
378: * the HotSpot 1.4.2 JVM with the +UseParallelGC option along
379: * with its default +UseAdaptiveSizePolicy option has a bug with
380: * the initialization of the sizes of the eden and survivor spaces.
381: * See bugid 4890736.
382: *
383: * note - use explicit 1.4.2 counter names here - don't update
384: * to latest counter names or attempt to find aliases.
385: */
386:
387: String cname = "hotspot.gc.collector.0.name";
388: StringMonitor collector = (StringMonitor) map.get(cname);
389:
390: if (collector.stringValue().compareTo("PSScavenge") == 0) {
391: boolean adaptiveSizePolicy = true;
392:
393: /*
394: * HotSpot processes the -XX:Flags/.hotspotrc arguments prior to
395: * processing the command line arguments. This allows the command
396: * line arguments to override any defaults set in .hotspotrc
397: */
398: cname = "hotspot.vm.flags";
399: StringMonitor flags = (StringMonitor) map.get(cname);
400: String allArgs = flags.stringValue() + " "
401: + args.stringValue();
402:
403: /*
404: * ignore the -XX: prefix as it only applies to the arguments
405: * passed from the command line (i.e. the invocation api).
406: * arguments passed through .hotspotrc omit the -XX: prefix.
407: */
408: int ahi = allArgs.lastIndexOf("+AggressiveHeap");
409: int aspi = allArgs.lastIndexOf("-UseAdaptiveSizePolicy");
410:
411: if (ahi != -1) {
412: /*
413: * +AggressiveHeap was set, check if -UseAdaptiveSizePolicy
414: * is set after +AggressiveHeap.
415: */
416: //
417: if ((aspi != -1) && (aspi > ahi)) {
418: adaptiveSizePolicy = false;
419: }
420: } else {
421: /*
422: * +AggressiveHeap not set, must be +UseParallelGC. The
423: * relative position of -UseAdaptiveSizePolicy is not
424: * important in this case, as it will override the
425: * UseParallelGC default (+UseAdaptiveSizePolicy) if it
426: * appears anywhere in the JVM arguments.
427: */
428: if (aspi != -1) {
429: adaptiveSizePolicy = false;
430: }
431: }
432:
433: if (adaptiveSizePolicy) {
434: // adjust the buggy AdaptiveSizePolicy size counters.
435:
436: // first remove the real counters.
437: String eden_size = "hotspot.gc.generation.0.space.0.size";
438: String s0_size = "hotspot.gc.generation.0.space.1.size";
439: String s1_size = "hotspot.gc.generation.0.space.2.size";
440: map.remove(eden_size);
441: map.remove(s0_size);
442: map.remove(s1_size);
443:
444: // get the maximum new generation size
445: String new_max_name = "hotspot.gc.generation.0.capacity.max";
446: LongMonitor new_max = (LongMonitor) map
447: .get(new_max_name);
448:
449: /*
450: * replace the real counters with pseudo counters that are
451: * initialized to to the correct values. The maximum size of
452: * the eden and survivor spaces are supposed to be:
453: * max_eden_size = new_size - (2*alignment).
454: * max_survivor_size = new_size - (2*alignment).
455: * since we don't know the alignment value used, and because
456: * of other parallel scavenge bugs that result in oversized
457: * spaces, we just set the maximum size of each space to the
458: * full new gen size.
459: */
460: Monitor monitor = null;
461:
462: LongBuffer lb = LongBuffer.allocate(1);
463: lb.put(new_max.longValue());
464: monitor = new PerfLongMonitor(eden_size, Units.BYTES,
465: Variability.CONSTANT, false, lb);
466: map.put(eden_size, monitor);
467:
468: monitor = new PerfLongMonitor(s0_size, Units.BYTES,
469: Variability.CONSTANT, false, lb);
470: map.put(s0_size, monitor);
471:
472: monitor = new PerfLongMonitor(s1_size, Units.BYTES,
473: Variability.CONSTANT, false, lb);
474: map.put(s1_size, monitor);
475: }
476: }
477: }
478:
479: /**
480: * method to extract the next monitor entry from the instrumentation memory.
481: * assumes that nextEntry is the offset into the byte array
482: * at which to start the search for the next entry. method leaves
483: * next entry pointing to the next entry or to the end of data.
484: */
485: protected Monitor getNextMonitorEntry() throws MonitorException {
486: Monitor monitor = null;
487:
488: // entries are always 4 byte aligned.
489: if ((nextEntry % 4) != 0) {
490: throw new MonitorStructureException(
491: "Entry index not properly aligned: " + nextEntry);
492: }
493:
494: // protect against a corrupted shared memory region.
495: if ((nextEntry < 0) || (nextEntry > buffer.limit())) {
496: throw new MonitorStructureException(
497: "Entry index out of bounds: nextEntry = "
498: + nextEntry + ", limit = " + buffer.limit());
499: }
500:
501: // check for the end of the buffer
502: if (nextEntry == buffer.limit()) {
503: lognl("getNextMonitorEntry():"
504: + " nextEntry == buffer.limit(): returning");
505: return null;
506: }
507:
508: buffer.position(nextEntry);
509:
510: int entryStart = buffer.position();
511: int entryLength = buffer.getInt();
512:
513: // check for valid entry length
514: if ((entryLength < 0) || (entryLength > buffer.limit())) {
515: throw new MonitorStructureException(
516: "Invalid entry length: entryLength = "
517: + entryLength);
518: }
519:
520: // check if last entry occurs before the eof.
521: if ((entryStart + entryLength) > buffer.limit()) {
522: throw new MonitorStructureException(
523: "Entry extends beyond end of buffer: "
524: + " entryStart = " + entryStart
525: + " entryLength = " + entryLength
526: + " buffer limit = " + buffer.limit());
527: }
528:
529: if (entryLength == 0) {
530: // end of data
531: return null;
532: }
533:
534: int nameLength = buffer.getInt();
535: int vectorLength = buffer.getInt();
536: byte dataType = buffer.get();
537: byte flags = buffer.get();
538: Units u = Units.toUnits(buffer.get());
539: Variability v = Variability.toVariability(buffer.get());
540: boolean supported = (flags & 0x01) != 0;
541:
542: // defend against corrupt entries
543: if ((nameLength <= 0) || (nameLength > entryLength)) {
544: throw new MonitorStructureException(
545: "Invalid Monitor name length: " + nameLength);
546: }
547:
548: if ((vectorLength < 0) || (vectorLength > entryLength)) {
549: throw new MonitorStructureException(
550: "Invalid Monitor vector length: " + vectorLength);
551: }
552:
553: // read in the perfData item name, casting bytes to chars. skip the
554: // null terminator
555: //
556: byte[] nameBytes = new byte[nameLength - 1];
557: for (int i = 0; i < nameLength - 1; i++) {
558: nameBytes[i] = buffer.get();
559: }
560:
561: // convert name into a String
562: String name = new String(nameBytes, 0, nameLength - 1);
563:
564: if (v == Variability.INVALID) {
565: throw new MonitorDataException(
566: "Invalid variability attribute:"
567: + " entry index = " + perfDataItem
568: + " name = " + name);
569: }
570: if (u == Units.INVALID) {
571: throw new MonitorDataException("Invalid units attribute: "
572: + " entry index = " + perfDataItem + " name = "
573: + name);
574: }
575:
576: int offset;
577: if (vectorLength == 0) {
578: // scalar Types
579: if (dataType == BasicType.LONG.intValue()) {
580: offset = entryStart + entryLength - 8; /* 8 = sizeof(long) */
581: buffer.position(offset);
582: LongBuffer lb = buffer.asLongBuffer();
583: lb.limit(1);
584: monitor = new PerfLongMonitor(name, u, v, supported, lb);
585: perfDataItem++;
586: } else {
587: // bad data types.
588: throw new MonitorTypeException("Invalid Monitor type:"
589: + " entry index = " + perfDataItem + " name = "
590: + name + " type = " + dataType);
591: }
592: } else {
593: // vector types
594: if (dataType == BasicType.BYTE.intValue()) {
595: if (u != Units.STRING) {
596: // only byte arrays of type STRING are currently supported
597: throw new MonitorTypeException(
598: "Invalid Monitor type:" + " entry index = "
599: + perfDataItem + " name = " + name
600: + " type = " + dataType);
601: }
602:
603: offset = entryStart + PERFDATA_NAME_OFFSET + nameLength;
604: buffer.position(offset);
605: ByteBuffer bb = buffer.slice();
606: bb.limit(vectorLength);
607: bb.position(0);
608:
609: if (v == Variability.CONSTANT) {
610: monitor = new PerfStringConstantMonitor(name,
611: supported, bb);
612: } else if (v == Variability.VARIABLE) {
613: monitor = new PerfStringVariableMonitor(name,
614: supported, bb, vectorLength - 1);
615: } else {
616: // Monotonically increasing byte arrays are not supported
617: throw new MonitorDataException(
618: "Invalid variability attribute:"
619: + " entry index = " + perfDataItem
620: + " name = " + name
621: + " variability = " + v);
622: }
623: perfDataItem++;
624: } else {
625: // bad data types.
626: throw new MonitorTypeException("Invalid Monitor type:"
627: + " entry index = " + perfDataItem + " name = "
628: + name + " type = " + dataType);
629: }
630: }
631:
632: // setup index to next entry for next iteration of the loop.
633: nextEntry = entryStart + entryLength;
634: return monitor;
635: }
636:
637: /**
638: * Method to dump debugging information
639: */
640: private void dumpAll(Map map, int lvmid) {
641: if (DEBUG) {
642: Set keys = map.keySet();
643:
644: System.err.println("Dump for " + lvmid);
645: int j = 0;
646: for (Iterator i = keys.iterator(); i.hasNext(); j++) {
647: Monitor monitor = (Monitor) map.get(i.next());
648: System.err.println(j + "\t" + monitor.getName() + "="
649: + monitor.getValue());
650: }
651: System.err.println("nextEntry = " + nextEntry
652: + " pollForEntry = " + pollForEntry);
653: System.err.println("Buffer info:");
654: System.err.println("buffer = " + buffer);
655: }
656: }
657:
658: private void lognl(String s) {
659: if (DEBUG) {
660: System.err.println(s);
661: }
662: }
663:
664: private void log(String s) {
665: if (DEBUG) {
666: System.err.print(s);
667: }
668: }
669: }
|