KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > probe > util > AbstractDumbInsert


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2004 France Telecom R&D
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * CLIF $Name: $
20 *
21 * Contact: clif@objectweb.org
22 */

23
24 package org.objectweb.clif.probe.util;
25
26 import org.objectweb.clif.server.api.BladeControl;
27 import org.objectweb.clif.server.api.BladeInsertResponse;
28 import org.objectweb.clif.datacollector.api.DataCollectorWrite;
29 import org.objectweb.clif.supervisor.api.ClifException;
30 import org.objectweb.clif.storage.api.ProbeEvent;
31 import org.objectweb.fractal.api.control.BindingController;
32 import org.objectweb.fractal.api.control.LifeCycleController;
33 import java.io.Serializable JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35
36
37 /**
38  * This abstract implementation of a blade insert component manages a single thread,
39  * doing a periodic task (probing) with a given period (in milliseconds) and during
40  * an given actual time. It provides a "dumb" interpretation of the blade lifecycle
41  * with regard to the ActivityControl interface, inasmuch as it actually probes from
42  * its initialization time to its stop time, regardless of start(), suspend() and
43  * resume() orders.
44  * @author Bruno Dillenseger
45  */

46 abstract public class AbstractDumbInsert
47     implements
48         BladeControl,
49         BindingController,
50         LifeCycleController,
51         Runnable JavaDoc
52 {
53     static private final String JavaDoc[] interfaceNames = new String JavaDoc[] {
54         DataCollectorWrite.DATA_COLLECTOR_WRITE,
55         BladeInsertResponse.BLADE_INSERT_RESPONSE };
56     protected String JavaDoc probeId;
57     protected Object JavaDoc activity_lock = new Object JavaDoc();
58     protected BladeInsertResponse bir;
59     protected Object JavaDoc sr_lock = new Object JavaDoc();
60     protected DataCollectorWrite dcw;
61     protected Object JavaDoc dc_lock = new Object JavaDoc();
62     protected long arg_period_ms = 0;
63     protected long arg_duration_ms = 0;
64     protected long baseTime_ms = 0;
65     protected volatile boolean started;
66     protected volatile boolean stopped;
67     protected volatile boolean suspended;
68     protected volatile boolean terminated;
69     private String JavaDoc fcState = LifeCycleController.STOPPED;
70     protected Thread JavaDoc poller;
71
72
73     /**
74      * Override this method to perform your measure and return it
75      * @return an probe event holding the measure
76      */

77     abstract protected ProbeEvent doProbe();
78
79
80     ///////////////////////////////////
81
// interface LifeCycleController //
82
///////////////////////////////////
83

84
85     public void startFc()
86     {
87         fcState = LifeCycleController.STARTED;
88     }
89
90
91     public void stopFc()
92     {
93         fcState = LifeCycleController.STOPPED;
94     }
95
96
97     public String JavaDoc getFcState()
98     {
99         return fcState;
100     }
101
102
103     ///////////////////////////////////////////////////
104
// implementation of interface BindingController //
105
///////////////////////////////////////////////////
106

107
108     public Object JavaDoc lookupFc(String JavaDoc clientItfName)
109     {
110         if (clientItfName.equals(DataCollectorWrite.DATA_COLLECTOR_WRITE))
111         {
112             return dcw;
113         }
114         else if (clientItfName.equals(BladeInsertResponse.BLADE_INSERT_RESPONSE))
115         {
116             return bir;
117         }
118         else
119         {
120             return null;
121         }
122     }
123
124
125     public void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
126     {
127         if (clientItfName.equals(DataCollectorWrite.DATA_COLLECTOR_WRITE))
128         {
129             synchronized (dc_lock)
130             {
131                 dcw = (DataCollectorWrite) serverItf;
132             }
133         }
134         else if (clientItfName.equals(BladeInsertResponse.BLADE_INSERT_RESPONSE))
135         {
136             synchronized (sr_lock)
137             {
138                 bir = (BladeInsertResponse) serverItf;
139             }
140         }
141     }
142
143
144     public void unbindFc(String JavaDoc clientItfName)
145     {
146         if (clientItfName.equals(DataCollectorWrite.DATA_COLLECTOR_WRITE))
147         {
148             synchronized (dc_lock)
149             {
150                 dcw = null;
151             }
152         }
153         else if (clientItfName.equals(BladeInsertResponse.BLADE_INSERT_RESPONSE))
154         {
155             synchronized (sr_lock)
156             {
157                 bir = null;
158             }
159         }
160     }
161
162
163     public String JavaDoc[] listFc()
164     {
165         return interfaceNames;
166     }
167
168
169     //////////////////////////////////////////////
170
// implementation of interface BladeControl //
171
//////////////////////////////////////////////
172

173
174     /**
175      * @param testId unique identifier of the new test
176      */

177     public void init(Serializable JavaDoc testId)
178     {
179         started = false;
180         stopped = false;
181         suspended = false;
182         terminated = false;
183         poller = new Thread JavaDoc(this, "memory probe");
184         poller.start();
185     }
186
187
188     public void start()
189     {
190         started = true;
191         baseTime_ms = System.currentTimeMillis();
192     }
193
194
195     public void stop()
196     {
197         stopped = true;
198         poller.interrupt();
199         synchronized (activity_lock)
200         {
201             if (started && ! terminated)
202             {
203                 try
204                 {
205                     activity_lock.wait();
206                 }
207                 catch (InterruptedException JavaDoc ex)
208                 {
209                 }
210             }
211         }
212     }
213
214
215     public void suspend()
216     {
217         suspended = true;
218         baseTime_ms = System.currentTimeMillis() - baseTime_ms;
219     }
220
221
222     public void resume()
223     {
224         baseTime_ms = System.currentTimeMillis() - baseTime_ms;
225         suspended = false;
226     }
227
228
229     public void join()
230     {
231         synchronized (activity_lock)
232         {
233             if (!terminated)
234             {
235                 try
236                 {
237                     activity_lock.wait();
238                 }
239                 catch (InterruptedException JavaDoc ex)
240                 {
241                 }
242             }
243         }
244     }
245
246
247     /**
248      * Sets number of threads and test duration parameters
249      * @param arg should hold 2 integer parameters (separated with usual separators) setting
250      * (1) the observation polling period in ms, and (2) the test duration in seconds.
251      */

252     public void setArgument(String JavaDoc arg)
253         throws ClifException
254     {
255         StringTokenizer JavaDoc parser = new StringTokenizer JavaDoc(arg);
256         try
257         {
258             arg_period_ms = Integer.parseInt(parser.nextToken());
259             arg_duration_ms = Integer.parseInt(parser.nextToken()) * 1000;
260         }
261         catch (Exception JavaDoc ex)
262         {
263             throw new ClifException("probe expects 2 arguments: <period_ms> <duration_s>");
264         }
265     }
266
267
268     /**
269      * Sets this scenario's unique identifier
270      */

271     public void setId(String JavaDoc id)
272     {
273         probeId = id;
274     }
275
276
277     /**
278      * @return the scenario/blade identifier
279      */

280      public String JavaDoc getId()
281     {
282         return probeId;
283     }
284
285
286     public void run()
287     {
288         while (
289             ! started
290             || (
291                 ! stopped
292                 &&
293                 (suspended
294                  || arg_duration_ms > System.currentTimeMillis() - baseTime_ms)))
295         {
296             try
297             {
298                 dcw.add(doProbe());
299             }
300             catch (Exception JavaDoc ex)
301             {
302             }
303             try
304             {
305                 long time = System.currentTimeMillis();
306                 if (!started || suspended || arg_duration_ms > time - baseTime_ms + arg_period_ms)
307                 {
308                     Thread.currentThread().sleep(arg_period_ms);
309                 }
310                 else
311                 {
312                     Thread.currentThread().sleep(
313                         arg_duration_ms
314                         - time + baseTime_ms);
315                 }
316             }
317             catch (InterruptedException JavaDoc ex)
318             {
319             }
320         }
321         synchronized (activity_lock)
322         {
323             terminated = true;
324             if (! stopped)
325             {
326                 bir.completed();
327             }
328             else
329             {
330                 activity_lock.notify();
331             }
332         }
333     }
334 }
335
Popular Tags