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