1 | /*
|
---|
2 | * Sample of performance API usage, written in Java.
|
---|
3 | *
|
---|
4 | * Don't forget to run VBOX webserver
|
---|
5 | * with 'vboxwebsrv -t 1000' command, to calm down watchdog thread.
|
---|
6 | *
|
---|
7 | * Copyright (C) 2008-2009 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * The following license applies to this file only:
|
---|
10 | *
|
---|
11 | * Permission is hereby granted, free of charge, to any person
|
---|
12 | * obtaining a copy of this software and associated documentation
|
---|
13 | * files (the "Software"), to deal in the Software without
|
---|
14 | * restriction, including without limitation the rights to use,
|
---|
15 | * copy, modify, merge, publish, distribute, sublicense, and/or
|
---|
16 | * sell copies of the Software, and to permit persons to whom the
|
---|
17 | * Software is furnished to do so, subject to the following conditions:
|
---|
18 | *
|
---|
19 | * The above copyright notice and this permission notice shall be
|
---|
20 | * included in all copies or substantial portions of the Software.
|
---|
21 | *
|
---|
22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
23 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
24 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
25 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
26 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
27 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
28 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
29 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
30 | */
|
---|
31 | import com.sun.xml.ws.commons.virtualbox{VBOX_API_SUFFIX}.*;
|
---|
32 | import org.virtualbox{VBOX_API_SUFFIX}.*;
|
---|
33 |
|
---|
34 | import java.util.*;
|
---|
35 | import javax.xml.ws.Holder;
|
---|
36 |
|
---|
37 | class PerformanceData
|
---|
38 | {
|
---|
39 | public String name;
|
---|
40 | public IUnknown object;
|
---|
41 | public String unit;
|
---|
42 | public Long scale;
|
---|
43 | public Long sequenceNumber;
|
---|
44 | public List<Long> samples;
|
---|
45 |
|
---|
46 | public String getFormattedSamples()
|
---|
47 | {
|
---|
48 | String out = "[";
|
---|
49 | String separator = "";
|
---|
50 |
|
---|
51 | if (scale != 1)
|
---|
52 | {
|
---|
53 | for (Long sample : samples)
|
---|
54 | {
|
---|
55 | out += separator + (sample.doubleValue() / scale) + " " + unit;
|
---|
56 | separator = ", ";
|
---|
57 | }
|
---|
58 | }
|
---|
59 | else
|
---|
60 | {
|
---|
61 | for (Long sample : samples)
|
---|
62 | {
|
---|
63 | out += separator + sample.toString() + " " + unit;
|
---|
64 | separator = ", ";
|
---|
65 | }
|
---|
66 | }
|
---|
67 | out += "]";
|
---|
68 | return out;
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | class PerformanceCollector
|
---|
73 | {
|
---|
74 | private IVirtualBox _vbox;
|
---|
75 | private IPerformanceCollector _collector;
|
---|
76 |
|
---|
77 | public PerformanceCollector(IVirtualBox vbox)
|
---|
78 | {
|
---|
79 | _vbox = vbox;
|
---|
80 | _collector = vbox.getPerformanceCollector();
|
---|
81 | }
|
---|
82 |
|
---|
83 | public void cleanup()
|
---|
84 | {
|
---|
85 | _collector.releaseRemote();
|
---|
86 | }
|
---|
87 |
|
---|
88 | public List<IPerformanceMetric> setup(List<String> metricNames, List<IUnknown> objects, Long period, Long samples)
|
---|
89 | {
|
---|
90 | return _collector.setupMetrics(metricNames, objects, period, samples);
|
---|
91 | }
|
---|
92 |
|
---|
93 | public List<IPerformanceMetric> enable(List<String> metricNames, List<IUnknown> objects)
|
---|
94 | {
|
---|
95 | return _collector.enableMetrics(metricNames, objects);
|
---|
96 | }
|
---|
97 |
|
---|
98 | public List<IPerformanceMetric> disable(List<String> metricNames, List<IUnknown> objects)
|
---|
99 | {
|
---|
100 | return _collector.disableMetrics(metricNames, objects);
|
---|
101 | }
|
---|
102 |
|
---|
103 | public List<PerformanceData> query(List<String> filterMetrics, List<IUnknown> filterObjects)
|
---|
104 | {
|
---|
105 | Holder<List<String>> names = new Holder<List<String>>();
|
---|
106 | Holder<List<IUnknown>> objects = new Holder<List<IUnknown>>();
|
---|
107 | Holder<List<String>> units = new Holder<List<String>>();
|
---|
108 | Holder<List<Long>> scales = new Holder<List<Long>>();
|
---|
109 | Holder<List<Long>> sequenceNumbers = new Holder<List<Long>>();
|
---|
110 | Holder<List<Long>> indices = new Holder<List<Long>>();
|
---|
111 | Holder<List<Long>> lengths = new Holder<List<Long>>();
|
---|
112 | List<Integer> values =
|
---|
113 | _collector.queryMetricsData(filterMetrics, filterObjects,
|
---|
114 | names, objects, units, scales, sequenceNumbers, indices, lengths);
|
---|
115 | List<PerformanceData> data = new ArrayList<PerformanceData>(names.value.size());
|
---|
116 | for (int i = 0; i < names.value.size(); i++)
|
---|
117 | {
|
---|
118 | PerformanceData singleMetricData = new PerformanceData();
|
---|
119 | singleMetricData.name = names.value.get(i);
|
---|
120 | singleMetricData.object = objects.value.get(i);
|
---|
121 | singleMetricData.unit = units.value.get(i);
|
---|
122 | singleMetricData.scale = scales.value.get(i);
|
---|
123 | singleMetricData.sequenceNumber = sequenceNumbers.value.get(i);
|
---|
124 | List<Long> samples = new ArrayList<Long>(lengths.value.get(i).intValue());
|
---|
125 | for (int j = 0; j < lengths.value.get(i); j++)
|
---|
126 | {
|
---|
127 | samples.add(values.get(indices.value.get(i).intValue() + j).longValue());
|
---|
128 | }
|
---|
129 | singleMetricData.samples = samples;
|
---|
130 | data.add(singleMetricData);
|
---|
131 | }
|
---|
132 |
|
---|
133 | return data;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | public class metrictest implements Runnable
|
---|
138 | {
|
---|
139 | IVirtualBox vbox;
|
---|
140 | IWebsessionManager mgr;
|
---|
141 | PerformanceCollector perf;
|
---|
142 |
|
---|
143 | public metrictest()
|
---|
144 | {
|
---|
145 | vbox = VirtualBox.connect("http://localhost:18083/");
|
---|
146 | mgr = new IWebsessionManager(vbox.getRemoteWSPort());
|
---|
147 | System.out.println("Initialized connection to VirtualBox version " + vbox.getVersion());
|
---|
148 | perf = new PerformanceCollector(vbox);
|
---|
149 | }
|
---|
150 |
|
---|
151 | private String getObjectName(IUnknown object)
|
---|
152 | {
|
---|
153 | try
|
---|
154 | {
|
---|
155 | String machineName = object.getRemoteWSPort().iMachineGetName(object.getRef());
|
---|
156 | return machineName;
|
---|
157 | } catch (Exception e)
|
---|
158 | {
|
---|
159 | }
|
---|
160 | return new String("host");
|
---|
161 | }
|
---|
162 |
|
---|
163 | public void setup()
|
---|
164 | {
|
---|
165 | perf.setup(Arrays.asList(new String[]{"*"}),
|
---|
166 | new ArrayList<IUnknown>(),
|
---|
167 | new Long(1), new Long(5));
|
---|
168 | }
|
---|
169 |
|
---|
170 | public void collect()
|
---|
171 | {
|
---|
172 | try
|
---|
173 | {
|
---|
174 | List<IUnknown> allObjects = new ArrayList<IUnknown>();
|
---|
175 | List<PerformanceData> metricData = perf.query(Arrays.asList(new String[]{"*"}),
|
---|
176 | allObjects);
|
---|
177 | for (PerformanceData md : metricData)
|
---|
178 | {
|
---|
179 | System.out.println("(" + getObjectName(md.object) + ") " +
|
---|
180 | md.name + " " + md.getFormattedSamples());
|
---|
181 | }
|
---|
182 | }
|
---|
183 | catch (Exception e)
|
---|
184 | {
|
---|
185 | e.printStackTrace();
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | public void run()
|
---|
190 | {
|
---|
191 | // Clean up
|
---|
192 | try
|
---|
193 | {
|
---|
194 | if (perf != null)
|
---|
195 | {
|
---|
196 | perf.cleanup();
|
---|
197 | perf = null;
|
---|
198 | }
|
---|
199 | if (vbox != null)
|
---|
200 | {
|
---|
201 | mgr.logoff(vbox);
|
---|
202 | vbox = null;
|
---|
203 | mgr = null;
|
---|
204 | System.out.println("Logged off.");
|
---|
205 | }
|
---|
206 | }
|
---|
207 | catch (Exception e)
|
---|
208 | {
|
---|
209 | e.printStackTrace();
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | public static void main(String[] args) throws InterruptedException
|
---|
214 | {
|
---|
215 | metrictest c = new metrictest();
|
---|
216 | // Add a shutdown handle to clean up
|
---|
217 | Runtime.getRuntime().addShutdownHook(new Thread(c));
|
---|
218 | // Start metric collection
|
---|
219 | c.setup();
|
---|
220 | // Obtain and print out stats continuosly until ctrl-C is pressed
|
---|
221 | while (true)
|
---|
222 | {
|
---|
223 | Thread.sleep(1000); // Sleep for a second
|
---|
224 | c.collect();
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|