4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 import org.opensolaris.os.dtrace.*;
31 * Assert getAggregate() can explicitly specify the anonymous aggregation.
33 public class TestGetAggregate {
34 static final String programString =
35 "profile:::tick-50ms" +
41 static final String ANONYMOUS_AGGREGATION = "";
42 static final int TICK = 50;
43 static final int EXPECTED_TICKS = 3;
44 static final int INTERVALS = 4;
47 testIncluded(Consumer consumer, String ... aggregationNames)
48 throws DTraceException, InterruptedException
51 Set <String> included = new HashSet <String> ();
54 for (String name : aggregationNames) {
58 // Wait up to a full second to obtain aggregate data. Without a
59 // time limit, we'll loop forever if no aggregation was
60 // successfully included.
63 aggregate = consumer.getAggregate(included, null);
64 } while (aggregate.asMap().isEmpty() && n++ < (1000 / TICK));
66 for (String name : included) {
67 if (aggregate.getAggregation(name) == null) {
68 throw new IllegalStateException("@" + name +
69 " was explicitly included but did not appear " +
73 for (Aggregation a : aggregate.getAggregations()) {
74 if (!included.contains(a.getName())) {
75 throw new IllegalStateException("@" + a.getName() +
76 " was not explicitly included but appeared " +
77 "in the aggregate anyway");
81 if (!consumer.isRunning()) {
82 throw new IllegalStateException("consumer exited");
87 testCleared(Consumer consumer, String ... aggregationNames)
88 throws DTraceException, InterruptedException
91 AggregationRecord rec;
95 Map <String, Long> firstValues = new HashMap <String, Long> ();
96 Set <String> cleared = new HashSet <String> ();
98 for (String name : aggregationNames) {
104 aggregate = consumer.getAggregate(null, cleared);
105 } while (aggregate.asMap().isEmpty() && n++ < (1000 / TICK));
109 Thread.sleep(TICK * EXPECTED_TICKS);
110 aggregate = consumer.getAggregate(null, cleared);
112 for (Aggregation a : aggregate.getAggregations()) {
113 if (!firstValues.containsKey(a.getName())) {
114 rec = a.getRecord(Tuple.EMPTY);
115 value = rec.getValue().getValue().longValue();
116 firstValues.put(a.getName(), value);
119 } while (consumer.isRunning() && n++ < INTERVALS);
121 for (Aggregation a : aggregate.getAggregations()) {
122 rec = a.getRecord(Tuple.EMPTY);
123 value = rec.getValue().getValue().longValue();
124 firstValue = firstValues.get(a.getName());
126 if (cleared.contains(a.getName())) {
127 // last value should be about the same as first value
128 if (value > (firstValue * 2)) {
129 throw new IllegalStateException(
130 "@" + a.getName() + " should have " +
131 "been cleared but instead grew from " +
132 firstValue + " to " + value);
135 // last value should be about (INTERVALS * firstValue)
136 if (value < (firstValue * 2)) {
137 throw new IllegalStateException(
138 "@" + a.getName() + " should have " +
139 "accumulated a running total but " +
140 "instead went from " +
141 firstValue + " to " + value);
146 if (!consumer.isRunning()) {
147 throw new IllegalStateException("consumer exited");
151 static Integer includedStatus;
152 static Integer clearedStatus;
157 final Consumer consumer = new LocalConsumer();
158 consumer.addConsumerListener(new ConsumerAdapter() {
159 public void consumerStarted(ConsumerEvent e) {
160 new Thread(new Runnable() {
163 testIncluded(consumer, ANONYMOUS_AGGREGATION);
165 } catch (Exception e) {
178 consumer.setOption(Option.aggrate, Option.millis(TICK));
179 consumer.compile(programString);
182 } catch (Exception e) {
191 final Consumer consumer = new LocalConsumer();
192 consumer.addConsumerListener(new ConsumerAdapter() {
193 public void consumerStarted(ConsumerEvent e) {
194 new Thread(new Runnable() {
197 testCleared(consumer, ANONYMOUS_AGGREGATION);
199 } catch (Exception e) {
212 consumer.setOption(Option.aggrate, Option.millis(TICK));
213 consumer.compile(programString);
216 } catch (Exception e) {
230 } catch (InterruptedException e) {
233 } while (includedStatus == null);
240 } catch (InterruptedException e) {
243 } while (clearedStatus == null);
245 if (includedStatus != 0 || clearedStatus != 0) {
246 System.out.println("Failure");
250 System.out.println("Success");