1*23b5c241Stomee /*
2*23b5c241Stomee  * CDDL HEADER START
3*23b5c241Stomee  *
4*23b5c241Stomee  * The contents of this file are subject to the terms of the
5*23b5c241Stomee  * Common Development and Distribution License (the "License").
6*23b5c241Stomee  * You may not use this file except in compliance with the License.
7*23b5c241Stomee  *
8*23b5c241Stomee  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*23b5c241Stomee  * or http://www.opensolaris.org/os/licensing.
10*23b5c241Stomee  * See the License for the specific language governing permissions
11*23b5c241Stomee  * and limitations under the License.
12*23b5c241Stomee  *
13*23b5c241Stomee  * When distributing Covered Code, include this CDDL HEADER in each
14*23b5c241Stomee  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*23b5c241Stomee  * If applicable, add the following below this CDDL HEADER, with the
16*23b5c241Stomee  * fields enclosed by brackets "[]" replaced with your own identifying
17*23b5c241Stomee  * information: Portions Copyright [yyyy] [name of copyright owner]
18*23b5c241Stomee  *
19*23b5c241Stomee  * CDDL HEADER END
20*23b5c241Stomee  */
21*23b5c241Stomee 
22*23b5c241Stomee /*
23*23b5c241Stomee  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*23b5c241Stomee  * Use is subject to license terms.
25*23b5c241Stomee  *
26*23b5c241Stomee  * ident	"%Z%%M%	%I%	%E% SMI"
27*23b5c241Stomee  */
28*23b5c241Stomee 
29*23b5c241Stomee import java.io.File;
30*23b5c241Stomee import java.io.IOException;
31*23b5c241Stomee import java.util.List;
32*23b5c241Stomee import org.opensolaris.os.dtrace.*;
33*23b5c241Stomee 
34*23b5c241Stomee /**
35*23b5c241Stomee  * Regression for multi-aggregation printa() corner cases.
36*23b5c241Stomee  */
37*23b5c241Stomee public class TestMultiAggPrinta {
38*23b5c241Stomee     static int exitStatus;
39*23b5c241Stomee 
40*23b5c241Stomee     // Gets a string representation of the given PrintaRecord minus the
41*23b5c241Stomee     // timestamp of the aggregate snapshot, so that the output is
42*23b5c241Stomee     // comparable across multiple test runs.
43*23b5c241Stomee     static String
printaRecordString(PrintaRecord rec)44*23b5c241Stomee     printaRecordString(PrintaRecord rec)
45*23b5c241Stomee     {
46*23b5c241Stomee 	StringBuffer buf = new StringBuffer();
47*23b5c241Stomee 	buf.append(PrintaRecord.class.getName());
48*23b5c241Stomee 	buf.append("[aggregations = ");
49*23b5c241Stomee 	buf.append(rec.getAggregations());
50*23b5c241Stomee 	buf.append(", formattedStrings = ");
51*23b5c241Stomee 	buf.append(rec.getFormattedStrings());
52*23b5c241Stomee 	buf.append(", tuples = ");
53*23b5c241Stomee 	buf.append(rec.getTuples());
54*23b5c241Stomee 	buf.append(", output = ");
55*23b5c241Stomee 	buf.append(rec.getOutput());
56*23b5c241Stomee 	buf.append(']');
57*23b5c241Stomee 	return buf.toString();
58*23b5c241Stomee     }
59*23b5c241Stomee 
60*23b5c241Stomee     static String
probeDataString(ProbeData data)61*23b5c241Stomee     probeDataString(ProbeData data)
62*23b5c241Stomee     {
63*23b5c241Stomee 	StringBuffer buf = new StringBuffer();
64*23b5c241Stomee 	buf.append(ProbeData.class.getName());
65*23b5c241Stomee 	buf.append("[epid = ");
66*23b5c241Stomee 	buf.append(data.getEnabledProbeID());
67*23b5c241Stomee 	// Do not include cpu, since it can change across test runs
68*23b5c241Stomee 	buf.append(", enabledProbeDescription = ");
69*23b5c241Stomee 	buf.append(data.getEnabledProbeDescription());
70*23b5c241Stomee 	buf.append(", flow = ");
71*23b5c241Stomee 	buf.append(data.getFlow());
72*23b5c241Stomee 	buf.append(", records = ");
73*23b5c241Stomee 
74*23b5c241Stomee 	List <Record> records = data.getRecords();
75*23b5c241Stomee 	Record record;
76*23b5c241Stomee 	Object value;
77*23b5c241Stomee 	buf.append('[');
78*23b5c241Stomee 	for (int i = 0; i < records.size(); ++i) {
79*23b5c241Stomee 	    if (i > 0) {
80*23b5c241Stomee 		buf.append(", ");
81*23b5c241Stomee 	    }
82*23b5c241Stomee 	    record = records.get(i);
83*23b5c241Stomee 	    if (record instanceof ValueRecord) {
84*23b5c241Stomee 		value = ((ValueRecord)record).getValue();
85*23b5c241Stomee 		if (value instanceof String) {
86*23b5c241Stomee 		    buf.append("\"");
87*23b5c241Stomee 		    buf.append((String)value);
88*23b5c241Stomee 		    buf.append("\"");
89*23b5c241Stomee 		} else {
90*23b5c241Stomee 		    buf.append(record);
91*23b5c241Stomee 		}
92*23b5c241Stomee 	    } else if (record instanceof PrintaRecord) {
93*23b5c241Stomee 		PrintaRecord printa = (PrintaRecord)record;
94*23b5c241Stomee 		buf.append(printaRecordString(printa));
95*23b5c241Stomee 	    } else {
96*23b5c241Stomee 		buf.append(record);
97*23b5c241Stomee 	    }
98*23b5c241Stomee 	}
99*23b5c241Stomee 	buf.append(']');
100*23b5c241Stomee 	return buf.toString();
101*23b5c241Stomee     }
102*23b5c241Stomee 
103*23b5c241Stomee     public static void
main(String[] args)104*23b5c241Stomee     main(String[] args)
105*23b5c241Stomee     {
106*23b5c241Stomee 	if (args.length != 1) {
107*23b5c241Stomee 	    System.err.println("usage: java TestMultiAggPrinta <script>");
108*23b5c241Stomee 	    System.exit(2);
109*23b5c241Stomee 	}
110*23b5c241Stomee 
111*23b5c241Stomee 	final Consumer consumer = new LocalConsumer();
112*23b5c241Stomee 	consumer.addConsumerListener(new ConsumerAdapter() {
113*23b5c241Stomee 	    public void dataReceived(DataEvent e) {
114*23b5c241Stomee 		ProbeData data = e.getProbeData();
115*23b5c241Stomee 		List <Record> records = data.getRecords();
116*23b5c241Stomee 		for (Record r : records) {
117*23b5c241Stomee 		    if (r instanceof ExitRecord) {
118*23b5c241Stomee 			ExitRecord exitRecord = (ExitRecord)r;
119*23b5c241Stomee 			exitStatus = exitRecord.getStatus();
120*23b5c241Stomee 		    }
121*23b5c241Stomee 		}
122*23b5c241Stomee 		System.out.println(probeDataString(e.getProbeData()));
123*23b5c241Stomee 	    }
124*23b5c241Stomee 	    public void consumerStopped(ConsumerEvent e) {
125*23b5c241Stomee 		consumer.close();
126*23b5c241Stomee 		System.exit(exitStatus);
127*23b5c241Stomee 	    }
128*23b5c241Stomee 	});
129*23b5c241Stomee 
130*23b5c241Stomee 	File file = new File(args[0]);
131*23b5c241Stomee 	try {
132*23b5c241Stomee 	    consumer.open();
133*23b5c241Stomee 	    consumer.compile(file);
134*23b5c241Stomee 	    consumer.enable();
135*23b5c241Stomee 	    consumer.go();
136*23b5c241Stomee 	} catch (DTraceException e) {
137*23b5c241Stomee 	    e.printStackTrace();
138*23b5c241Stomee 	    System.exit(1);
139*23b5c241Stomee 	} catch (IOException e) {
140*23b5c241Stomee 	    e.printStackTrace();
141*23b5c241Stomee 	    System.exit(1);
142*23b5c241Stomee 	}
143*23b5c241Stomee     }
144*23b5c241Stomee }
145