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 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * Sun Microsystems, Inc. confidential
|
---|
10 | * All rights reserved
|
---|
11 | */
|
---|
12 | import com.sun.xml.ws.commons.virtualbox.*;
|
---|
13 | import java.util.*;
|
---|
14 | import javax.xml.ws.Holder;
|
---|
15 | import org.virtualbox.*;
|
---|
16 |
|
---|
17 | class PerformanceData
|
---|
18 | {
|
---|
19 | public String name;
|
---|
20 | public IUnknown object;
|
---|
21 | public String unit;
|
---|
22 | public Long scale;
|
---|
23 | public Long sequenceNumber;
|
---|
24 | public List<Long> samples;
|
---|
25 |
|
---|
26 | public String getFormattedSamples()
|
---|
27 | {
|
---|
28 | String out = "[";
|
---|
29 | String separator = "";
|
---|
30 |
|
---|
31 | if (scale != 1)
|
---|
32 | {
|
---|
33 | for (Long sample : samples)
|
---|
34 | {
|
---|
35 | out += separator + (sample.doubleValue() / scale) + " " + unit;
|
---|
36 | separator = ", ";
|
---|
37 | }
|
---|
38 | }
|
---|
39 | else
|
---|
40 | {
|
---|
41 | for (Long sample : samples)
|
---|
42 | {
|
---|
43 | out += separator + sample.toString() + " " + unit;
|
---|
44 | separator = ", ";
|
---|
45 | }
|
---|
46 | }
|
---|
47 | out += "]";
|
---|
48 | return out;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | class PerformanceCollector
|
---|
53 | {
|
---|
54 | private IVirtualBox _vbox;
|
---|
55 | private IPerformanceCollector _collector;
|
---|
56 |
|
---|
57 | public PerformanceCollector(IVirtualBox vbox)
|
---|
58 | {
|
---|
59 | _vbox = vbox;
|
---|
60 | _collector = vbox.getPerformanceCollector();
|
---|
61 | }
|
---|
62 |
|
---|
63 | public void cleanup()
|
---|
64 | {
|
---|
65 | _collector.releaseRemote();
|
---|
66 | }
|
---|
67 |
|
---|
68 | public List<IPerformanceMetric> setup(List<String> metricNames, List<IUnknown> objects, Long period, Long samples)
|
---|
69 | {
|
---|
70 | return _collector.setupMetrics(metricNames, objects, period, samples);
|
---|
71 | }
|
---|
72 |
|
---|
73 | public List<IPerformanceMetric> enable(List<String> metricNames, List<IUnknown> objects)
|
---|
74 | {
|
---|
75 | return _collector.enableMetrics(metricNames, objects);
|
---|
76 | }
|
---|
77 |
|
---|
78 | public List<IPerformanceMetric> disable(List<String> metricNames, List<IUnknown> objects)
|
---|
79 | {
|
---|
80 | return _collector.disableMetrics(metricNames, objects);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public List<PerformanceData> query(List<String> filterMetrics, List<IUnknown> filterObjects)
|
---|
84 | {
|
---|
85 | Holder<List<String>> names = new Holder<List<String>>();
|
---|
86 | Holder<List<IUnknown>> objects = new Holder<List<IUnknown>>();
|
---|
87 | Holder<List<String>> units = new Holder<List<String>>();
|
---|
88 | Holder<List<Long>> scales = new Holder<List<Long>>();
|
---|
89 | Holder<List<Long>> sequenceNumbers = new Holder<List<Long>>();
|
---|
90 | Holder<List<Long>> indices = new Holder<List<Long>>();
|
---|
91 | Holder<List<Long>> lengths = new Holder<List<Long>>();
|
---|
92 | List<Integer> values =
|
---|
93 | _collector.queryMetricsData(filterMetrics, filterObjects,
|
---|
94 | names, objects, units, scales, sequenceNumbers, indices, lengths);
|
---|
95 | List<PerformanceData> data = new ArrayList<PerformanceData>(names.value.size());
|
---|
96 | for (int i = 0; i < names.value.size(); i++)
|
---|
97 | {
|
---|
98 | PerformanceData singleMetricData = new PerformanceData();
|
---|
99 | singleMetricData.name = names.value.get(i);
|
---|
100 | singleMetricData.object = objects.value.get(i);
|
---|
101 | singleMetricData.unit = units.value.get(i);
|
---|
102 | singleMetricData.scale = scales.value.get(i);
|
---|
103 | singleMetricData.sequenceNumber = sequenceNumbers.value.get(i);
|
---|
104 | List<Long> samples = new ArrayList<Long>(lengths.value.get(i).intValue());
|
---|
105 | for (int j = 0; j < lengths.value.get(i); j++)
|
---|
106 | {
|
---|
107 | samples.add(values.get(indices.value.get(i).intValue() + j).longValue());
|
---|
108 | }
|
---|
109 | singleMetricData.samples = samples;
|
---|
110 | data.add(singleMetricData);
|
---|
111 | }
|
---|
112 |
|
---|
113 | return data;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | public class metrictest implements Runnable
|
---|
118 | {
|
---|
119 | IVirtualBox vbox;
|
---|
120 | IWebsessionManager mgr;
|
---|
121 | PerformanceCollector perf;
|
---|
122 |
|
---|
123 | public metrictest()
|
---|
124 | {
|
---|
125 | vbox = VirtualBox.connect("http://localhost:18083/");
|
---|
126 | mgr = new IWebsessionManager(vbox.getRemoteWSPort());
|
---|
127 | System.out.println("Initialized connection to VirtualBox version " + vbox.getVersion());
|
---|
128 | perf = new PerformanceCollector(vbox);
|
---|
129 | }
|
---|
130 |
|
---|
131 | private String getObjectName(IUnknown object)
|
---|
132 | {
|
---|
133 | try
|
---|
134 | {
|
---|
135 | String machineName = object.getRemoteWSPort().iMachineGetName(object.getRef());
|
---|
136 | return machineName;
|
---|
137 | } catch (Exception e)
|
---|
138 | {
|
---|
139 | }
|
---|
140 | return new String("host");
|
---|
141 | }
|
---|
142 |
|
---|
143 | public void setup()
|
---|
144 | {
|
---|
145 | perf.setup(Arrays.asList(new String[]{"*"}),
|
---|
146 | new ArrayList<IUnknown>(),
|
---|
147 | new Long(1), new Long(5));
|
---|
148 | }
|
---|
149 |
|
---|
150 | public void collect()
|
---|
151 | {
|
---|
152 | try
|
---|
153 | {
|
---|
154 | List<IUnknown> allObjects = new ArrayList<IUnknown>();
|
---|
155 | List<PerformanceData> metricData = perf.query(Arrays.asList(new String[]{"*"}),
|
---|
156 | allObjects);
|
---|
157 | for (PerformanceData md : metricData)
|
---|
158 | {
|
---|
159 | System.out.println("(" + getObjectName(md.object) + ") " +
|
---|
160 | md.name + " " + md.getFormattedSamples());
|
---|
161 | }
|
---|
162 | }
|
---|
163 | catch (Exception e)
|
---|
164 | {
|
---|
165 | e.printStackTrace();
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | public void run()
|
---|
170 | {
|
---|
171 | // Clean up
|
---|
172 | try
|
---|
173 | {
|
---|
174 | if (perf != null)
|
---|
175 | {
|
---|
176 | perf.cleanup();
|
---|
177 | perf = null;
|
---|
178 | }
|
---|
179 | if (vbox != null)
|
---|
180 | {
|
---|
181 | mgr.logoff(vbox);
|
---|
182 | vbox = null;
|
---|
183 | mgr = null;
|
---|
184 | System.out.println("Logged off.");
|
---|
185 | }
|
---|
186 | }
|
---|
187 | catch (Exception e)
|
---|
188 | {
|
---|
189 | e.printStackTrace();
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | public static void main(String[] args) throws InterruptedException
|
---|
194 | {
|
---|
195 | metrictest c = new metrictest();
|
---|
196 | // Add a shutdown handle to clean up
|
---|
197 | Runtime.getRuntime().addShutdownHook(new Thread(c));
|
---|
198 | // Start metric collection
|
---|
199 | c.setup();
|
---|
200 | // Obtain and print out stats continuosly until ctrl-C is pressed
|
---|
201 | while (true)
|
---|
202 | {
|
---|
203 | Thread.sleep(1000); // Sleep for a second
|
---|
204 | c.collect();
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|