KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > probe > cpu > Insert


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.cpu;
25
26 import org.objectweb.clif.supervisor.api.ClifException;
27 import org.objectweb.clif.probe.util.AbstractDumbInsert;
28 import org.objectweb.clif.storage.api.ProbeEvent;
29 import org.objectweb.lewys.probe.Probe;
30 import org.objectweb.lewys.common.exceptions.NoResourceToProbeException;
31
32
33 /**
34  *
35  * @author Bruno Dillenseger
36  */

37 public class Insert extends AbstractDumbInsert
38 {
39     private static final short OS_UNDEF = -1;
40     private static final short OS_LINUX = 0;
41     private static final short OS_WINDOWS = 1;
42
43
44     private Probe probe;
45     private int[] resourceIds;
46     private short os = OS_UNDEF;
47     private long[] previousValues = null;
48
49
50     public Insert()
51         throws ClifException
52     {
53         try
54         {
55             if (System.getProperty("os.name").equalsIgnoreCase("linux"))
56             {
57                 probe = new org.objectweb.lewys.probe.linux.CpuProbe();
58                 resourceIds = new int[] { 0, 2, 3 }; // user ticks, kernel ticks, idle ticks
59
os = OS_LINUX;
60             }
61             else if (System.getProperty("os.name").startsWith("Windows"))
62             {
63                 probe = new org.objectweb.lewys.probe.windows.CpuProbe();
64                 resourceIds = new int[] { 1, 2, 0 }; // user ticks, priviledged ticks, all ticks
65
os = OS_WINDOWS;
66             }
67             else
68             {
69                 throw new ClifException("No CPU probe available for " + System.getProperty("os.name"));
70             }
71         }
72         catch (NoResourceToProbeException ex)
73         {
74             throw new ClifException("Can't set CPU probe", ex);
75         }
76     }
77
78
79     public ProbeEvent doProbe()
80     {
81         CPUEvent event = null;
82         try
83         {
84             long[] values = probe.getValue(resourceIds);
85             switch(os)
86             {
87                 case OS_LINUX:
88                     values[2] += values[0] + values[1]; // all ticks = user ticks + kernel ticks + idle ticks
89
break;
90             }
91             if (previousValues == null)
92             {
93                 previousValues = values;
94             }
95             else
96             {
97                 long[] result = new long[3];
98                 long delta = values[2] - previousValues[2];
99                 if (delta != 0)
100                 {
101                     result[1] = 100 * (values[0] - previousValues[0]);
102                     result[2] = 100 * (values[1] - previousValues[1]);
103                     result[0] = (result[1] + result[2]) / delta;
104                     result[1] /= delta;
105                     result[2] /= delta;
106                     if (result[0] <= 100 && result[1] <= 100 && result[2] <= 100)
107                     {
108                         previousValues = values;
109                         event = new CPUEvent(
110                         System.currentTimeMillis(),
111                             probeId,
112                             result);
113                     }
114                 }
115             }
116         }
117         catch (Exception JavaDoc ex)
118         {
119             ex.printStackTrace(System.err);
120         }
121         return event;
122     }
123 }
124
Popular Tags