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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 package org.opensolaris.os.dtrace;
27 
28 import java.util.*;
29 
30 /**
31  * Implementation detail used by {@link Consumer#getAggregate()}.
32  * Package level access.
33  */
34 class AggregateSpec {
35     private Set <String> includedAggregationNames;
36     private Set <String> clearedAggregationNames;
37 
AggregateSpec()38     AggregateSpec()
39     {
40 	includedAggregationNames = new HashSet <String> ();
41 	clearedAggregationNames = new HashSet <String> ();
42     }
43 
44     public boolean
isIncludeByDefault()45     isIncludeByDefault()
46     {
47 	return (includedAggregationNames == null);
48     }
49 
50     public boolean
isClearByDefault()51     isClearByDefault()
52     {
53 	return (clearedAggregationNames == null);
54     }
55 
56     public void
setIncludeByDefault(boolean include)57     setIncludeByDefault(boolean include)
58     {
59 	if (include) {
60 	    includedAggregationNames = null;
61 	} else if (includedAggregationNames == null) {
62 	    includedAggregationNames = new HashSet <String> ();
63 	}
64     }
65 
66     public void
setClearByDefault(boolean clear)67     setClearByDefault(boolean clear)
68     {
69 	if (clear) {
70 	    clearedAggregationNames = null;
71 	} else if (clearedAggregationNames == null) {
72 	    clearedAggregationNames = new HashSet <String> ();
73 	}
74     }
75 
76     /**
77      * Specifies which aggregations to include in an aggregate snapshot.
78      * If none are specified, all aggregations are included.  A snapshot
79      * is read-consistent across all included aggregations.
80      *
81      * @see Consumer#getAggregate(AggregateSpec spec)
82      */
83     public void
addIncludedAggregationName(String name)84     addIncludedAggregationName(String name)
85     {
86 	if (includedAggregationNames == null) {
87 	    includedAggregationNames = new HashSet <String> ();
88 	}
89 	includedAggregationNames.add(
90 		Aggregate.filterUnnamedAggregationName(name));
91     }
92 
93     /**
94      * Specifies which aggregations to clear after snapping the
95      * aggregate.  If none are specified, no aggregations are cleared.
96      * <p>
97      * Aggregations are cleared immediately after they are snapped
98      * before any more data can be accumulated in order to prevent loss
99      * of data between snapshots.
100      *
101      * @see Consumer#getAggregate(AggregateSpec spec)
102      */
103     public void
addClearedAggregationName(String name)104     addClearedAggregationName(String name)
105     {
106 	if (clearedAggregationNames == null) {
107 	    clearedAggregationNames = new HashSet <String> ();
108 	}
109 	clearedAggregationNames.add(
110 		Aggregate.filterUnnamedAggregationName(name));
111     }
112 
113     public Set <String>
getIncludedAggregationNames()114     getIncludedAggregationNames()
115     {
116 	if (includedAggregationNames == null) {
117 	    return Collections. <String> emptySet();
118 	}
119 	return Collections. <String> unmodifiableSet(includedAggregationNames);
120     }
121 
122     public Set <String>
getClearedAggregationNames()123     getClearedAggregationNames()
124     {
125 	if (clearedAggregationNames == null) {
126 	    return Collections. <String> emptySet();
127 	}
128 	return Collections. <String> unmodifiableSet(clearedAggregationNames);
129     }
130 
131     // Called by native code
132     public boolean
isIncluded(String aggregationName)133     isIncluded(String aggregationName)
134     {
135 	return ((includedAggregationNames == null) ||
136 		includedAggregationNames.contains(
137 		Aggregate.filterUnnamedAggregationName(aggregationName)));
138     }
139 
140     // Called by native code
141     public boolean
isCleared(String aggregationName)142     isCleared(String aggregationName)
143     {
144 	return ((clearedAggregationNames == null) ||
145 		clearedAggregationNames.contains(
146 		Aggregate.filterUnnamedAggregationName(aggregationName)));
147     }
148 
149     public String
toString()150     toString()
151     {
152 	StringBuilder buf = new StringBuilder();
153 	buf.append(AggregateSpec.class.getName());
154 	buf.append("[includedAggregationNames = ");
155 	buf.append(Arrays.toString(getIncludedAggregationNames().toArray()));
156 	buf.append(", clearedAggregationNames = ");
157 	buf.append(Arrays.toString(getClearedAggregationNames().toArray()));
158 	buf.append(", includeByDefault = ");
159 	buf.append(isIncludeByDefault());
160 	buf.append(", clearByDefault = ");
161 	buf.append(isClearByDefault());
162 	buf.append(']');
163 	return buf.toString();
164     }
165 }
166