Print this page
4719 Common patchset for jdk1.7 support preparation
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/dtrace/test/tst/common/java_api/src/TestDrop.java
+++ new/usr/src/cmd/dtrace/test/tst/common/java_api/src/TestDrop.java
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 *
26 26 * ident "%Z%%M% %I% %E% SMI"
27 27 */
28 28 import java.util.*;
29 29 import java.util.concurrent.atomic.*;
30 30 import org.opensolaris.os.dtrace.*;
31 31
32 32 /**
33 33 * Regression test for 6521523 aggregation drops can hang the Java
34 34 * DTrace API.
35 35 */
36 36 public class TestDrop {
37 37 static final String PROGRAM =
38 38 "fbt:genunix::entry { @[execname, pid] = count(); }";
39 39
40 40 static AtomicLong consumerThreadID = new AtomicLong();
41 41 static AtomicLong getAggregateThreadID = new AtomicLong();
42 42 static AtomicBoolean done = new AtomicBoolean();
43 43 static int seconds;
44 44
45 45 private static void
46 46 startTimer()
47 47 {
48 48 if (seconds <= 0) {
49 49 return;
50 50 }
51 51
52 52 final Timer timer = new Timer();
53 53 timer.schedule(new TimerTask() {
54 54 public void run() {
55 55 done.set(true);
↓ open down ↓ |
55 lines elided |
↑ open up ↑ |
56 56 timer.cancel();
57 57 }
58 58 }, seconds * 1000L);
59 59 }
60 60
61 61 private static void
62 62 sampleAggregate(Consumer consumer) throws DTraceException
63 63 {
64 64 while (consumer.isRunning() && !done.get()) {
65 65 try {
66 - Thread.currentThread().sleep(50);
66 + Thread.sleep(50);
67 67 } catch (InterruptedException e) {
68 68 }
69 69
70 70 consumer.getAggregate(Collections. <String> emptySet());
71 71 }
72 72 }
73 73
74 74 private static void
75 75 startAggregateThread(final Consumer consumer)
76 76 {
77 77 Runnable aggregateSampler = new Runnable() {
78 78 public void run() {
79 79 Thread t = Thread.currentThread();
80 80 getAggregateThreadID.set(t.getId());
81 81 Throwable x = null;
82 82 try {
83 83 sampleAggregate(consumer);
84 84 } catch (Throwable e) {
85 85 x = e;
86 86 }
87 87
88 88 if (Thread.holdsLock(LocalConsumer.class)) {
89 89 if (x != null) {
90 90 x.printStackTrace();
91 91 }
92 92 System.out.println("Lock held");
93 93 System.exit(1);
94 94 } else {
95 95 System.out.println("Lock released");
96 96 consumer.close(); // blocks if lock held
97 97 }
98 98 }
99 99 };
100 100
101 101 Thread t = new Thread(aggregateSampler, "Aggregate Sampler");
102 102 t.start();
103 103 }
104 104
105 105 static void
106 106 usage()
107 107 {
108 108 System.err.println("usage: java TestDrop [ seconds ]");
109 109 System.exit(2);
110 110 }
111 111
112 112 public static void
113 113 main(String[] args)
114 114 {
115 115 if (args.length == 1) {
116 116 try {
117 117 seconds = Integer.parseInt(args[0]);
118 118 } catch (NumberFormatException e) {
119 119 usage();
120 120 }
121 121 } else if (args.length > 1) {
122 122 usage();
123 123 }
124 124
125 125 final Consumer consumer = new LocalConsumer() {
126 126 protected Thread createThread() {
127 127 Runnable worker = new Runnable() {
128 128 public void run() {
129 129 Thread t = Thread.currentThread();
130 130 consumerThreadID.set(t.getId());
131 131 work();
132 132 }
133 133 };
134 134 Thread t = new Thread(worker);
135 135 return t;
136 136 }
137 137 };
138 138
139 139 consumer.addConsumerListener(new ConsumerAdapter() {
140 140 public void consumerStarted(ConsumerEvent e) {
141 141 startAggregateThread(consumer);
142 142 startTimer();
143 143 }
144 144 public void dataDropped(DropEvent e) throws ConsumerException {
145 145 Thread t = Thread.currentThread();
146 146 if (t.getId() == getAggregateThreadID.get()) {
147 147 Drop drop = e.getDrop();
148 148 throw new ConsumerException(drop.getDefaultMessage(),
149 149 drop);
150 150 }
151 151 }
152 152 });
153 153
154 154 try {
155 155 consumer.open();
156 156 consumer.setOption(Option.aggsize, Option.kb(1));
157 157 consumer.setOption(Option.aggrate, Option.millis(101));
158 158 consumer.compile(PROGRAM);
159 159 consumer.enable();
160 160 consumer.go(new ExceptionHandler() {
161 161 public void handleException(Throwable e) {
162 162 e.printStackTrace();
163 163 }
164 164 });
165 165 } catch (DTraceException e) {
166 166 e.printStackTrace();
167 167 }
168 168 }
169 169 }
↓ open down ↓ |
93 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX