Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3beb56b227f7d84988f04c505119ee63dcee7fd5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*******************************************************************************
 * Copyright (c) 2010, 2017 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Red Hat - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.internal.oprofile.core.opxml.info;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.linuxtools.internal.oprofile.core.Oprofile;
import org.eclipse.linuxtools.internal.oprofile.core.Oprofile.OprofileProject;
import org.eclipse.linuxtools.internal.oprofile.core.OprofileCorePlugin;
import org.eclipse.linuxtools.internal.oprofile.core.opxml.AbstractDataAdapter;
import org.eclipse.linuxtools.internal.oprofile.core.opxml.EventIdCache;
import org.eclipse.linuxtools.profiling.launch.IRemoteFileProxy;
import org.eclipse.linuxtools.profiling.launch.RemoteProxyManager;
import org.eclipse.linuxtools.tools.launch.core.factory.RuntimeProcessFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * This class takes the XML that is output from 'ophelp -X' for and uses that
 * data to modify it into the format expected by the SAX parser.
 */
public class InfoAdapter extends AbstractDataAdapter {

	public static final String HELP_EVENTS = "help_events"; //$NON-NLS-1$
	public static final String INFO = "info"; //$NON-NLS-1$

	public static final String DEFAULTS = "defaults"; //$NON-NLS-1$
	public static final String NUM_COUNTERS = "num-counters"; //$NON-NLS-1$
	public static final String CPU_FREQUENCY = "cpu-frequency"; //$NON-NLS-1$
	public static final String TIMER_MODE = "timer-mode"; //$NON-NLS-1$
	public static final String TIMER = "timer"; //$NON-NLS-1$

	public static final String EVENT_LIST = "event-list"; //$NON-NLS-1$
	public static final String COUNTER = "counter"; //$NON-NLS-1$

	public static final String EVENT = "event"; //$NON-NLS-1$
	public static final String EVENT_NAME = "event_name"; //$NON-NLS-1$
	public static final String NAME = "name"; //$NON-NLS-1$
	public static final String DESC = "desc"; //$NON-NLS-1$
	public static final String DESCRIPTION = "description"; //$NON-NLS-1$
	public static final String MIN_COUNT = "min_count"; //$NON-NLS-1$
	public static final String MINIMUM = "minimum"; //$NON-NLS-1$
	public static final String VALUE = "value"; //$NON-NLS-1$

	public static final String UNIT_MASKS = "unit_masks"; //$NON-NLS-1$
	public static final String UNITMASK = "unit-mask"; //$NON-NLS-1$
	public static final String DEFAULT = "default"; //$NON-NLS-1$
	public static final String TYPE = "type"; //$NON-NLS-1$

	public static final String UNIT_MASK = "unit_mask"; //$NON-NLS-1$
	public static final String MASK = "mask"; //$NON-NLS-1$

	public static final String SAMPLE_DIR = "sample-dir"; //$NON-NLS-1$
	public static final String LOCK_FILE = "lock-file"; //$NON-NLS-1$
	public static final String LOG_FILE = "log-file"; //$NON-NLS-1$
	public static final String DUMP_STATUS = "dump-status"; //$NON-NLS-1$

	public static final String CPUINFO = "/proc/cpuinfo"; //$NON-NLS-1$
	public static String DEV_OPROFILE = "/dev/oprofile/"; //$NON-NLS-1$
	public static String CPUTYPE = DEV_OPROFILE + "cpu_type"; //$NON-NLS-1$
	public static final String OP_SHARE = "/usr/share/oprofile/"; //$NON-NLS-1$
	public static final String EVENTS = "events"; //$NON-NLS-1$

	public static final String SAMPLE_DIR_VAL = "/var/lib/oprofile/samples/"; //$NON-NLS-1$
	public static final String LOCK_FILE_VAL = "/var/lib/oprofile/lock"; //$NON-NLS-1$
	public static final String LOG_FILE_VAL = "/var/lib/oprofile/samples/oprofiled.log"; //$NON-NLS-1$
	public static final String DUMP_STATUS_VAL = "/var/lib/oprofile/complete_dump"; //$NON-NLS-1$

	private Document newDoc; // the document we intend to build
	private Element oldRoot; // the root of the document with data from ophelp
	private Element newRoot; // the root of the document we intent to build
	private static IRemoteFileProxy proxy;

	private static boolean hasTimerSupport;

	public InfoAdapter() {
		try {
			if (hasTimerSupport()) {
				// In timer mode, we have no relevant XML generated by ophelp
				createDOM(null);
			} else {
				Process p = RuntimeProcessFactory.getFactory().exec("ophelp -X", Oprofile.OprofileProject.getProject()); //$NON-NLS-1$
				if (p != null) {
					InputStream is = p.getInputStream();
					createDOM(is);
				} else {
					createDOM(null);
				}
			}
		} catch (IOException e) {
			createDOM(null);
		}
	}

	/**
	 * @since 1.1
	 */
	public InfoAdapter(IFileStore resourceFile) {
		InputStream inputStream = null;
		try {
			inputStream = resourceFile.openInputStream(EFS.NONE, new NullProgressMonitor());
			createDOM(inputStream);
			setEventIdCacheDoc(oldRoot);
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Set up the DOM for later manipulation
	 * 
	 * @param is the InpuStream resulting from running the ophelp command. This will
	 *           be passed in as null for timer mode.
	 */
	private void createDOM(InputStream is) {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		try {
			builder = factory.newDocumentBuilder();
			if (is != null) {
				try {
					Document oldDoc = builder.parse(is);
					Element elem = (Element) oldDoc.getElementsByTagName(HELP_EVENTS).item(0);
					oldRoot = elem;
				} catch (SAXException | IOException e) {
					e.printStackTrace();
					CoreException opcontrolException = new CoreException(
							OprofileCorePlugin.createErrorStatus("ophelpRun", null)); //$NON-NLS-1$
					OprofileCorePlugin.showErrorDialog("opxmlSAXParseException", opcontrolException); //$NON-NLS-1$
				}
			} else {
				CoreException opcontrolException = new CoreException(
						OprofileCorePlugin.createErrorStatus("ophelpRun", null)); //$NON-NLS-1$
				OprofileCorePlugin.showErrorDialog("ophelpInputStream", opcontrolException); //$NON-NLS-1$
			}

			newDoc = builder.newDocument();
			try {
				newRoot = newDoc.createElement(INFO);
				newDoc.appendChild(newRoot);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (ParserConfigurationException e1) {
			e1.printStackTrace();
		}
	}

	@Override
	public void process() {
		if (getNrCounters() == -1) {
			Element numCountersTag = newDoc.createElement(NUM_COUNTERS);
			numCountersTag.setTextContent("error"); //$NON-NLS-1$
			newRoot.appendChild(numCountersTag);
			return;
		}
		createHeaders();
		if (!hasTimerSupport() && oldRoot != null) {
			createXML();
		}
	}

	private void createHeaders() {
		// number of counters
		String numCounters = String.valueOf(getNrCounters());
		Element numCountersTag = newDoc.createElement(NUM_COUNTERS);
		numCountersTag.setTextContent(String.valueOf(numCounters));
		newRoot.appendChild(numCountersTag);

		// cpu frequency
		int cpuFreq = getCPUFrequency();
		Element cpuFreqTag = newDoc.createElement(CPU_FREQUENCY);
		cpuFreqTag.setTextContent(String.valueOf(cpuFreq));
		newRoot.appendChild(cpuFreqTag);

		// file defaults
		Element defaultsTag = newDoc.createElement(DEFAULTS);

		Element sampleDirTag = newDoc.createElement(SAMPLE_DIR);
		sampleDirTag.setTextContent(SAMPLE_DIR_VAL);
		defaultsTag.appendChild(sampleDirTag);

		Element lockFileTag = newDoc.createElement(LOCK_FILE);
		lockFileTag.setTextContent(LOCK_FILE_VAL);
		defaultsTag.appendChild(lockFileTag);

		Element logFileTag = newDoc.createElement(LOG_FILE);
		logFileTag.setTextContent(LOG_FILE_VAL);
		defaultsTag.appendChild(logFileTag);

		Element dumpStatusTag = newDoc.createElement(DUMP_STATUS);
		dumpStatusTag.setTextContent(DUMP_STATUS_VAL);
		defaultsTag.appendChild(dumpStatusTag);

		newRoot.appendChild(defaultsTag);

		// timer mode
		Element timerModeTag = newDoc.createElement(TIMER_MODE);
		timerModeTag.setTextContent(String.valueOf(hasTimerSupport()));
		newRoot.appendChild(timerModeTag);
	}

	/**
	 * @since 3.0
	 */
	public static void setOprofileDir(String dir) {
		DEV_OPROFILE = dir;
		CPUTYPE = DEV_OPROFILE + "cpu_type"; //$NON-NLS-1$
	}

	/**
	 * Determine whether the cpu supports timer mode
	 * 
	 * @return true if it is true, and false otherwise
	 */
	public static boolean hasTimerSupport() {
		return hasTimerSupport;
	}

	/**
	 * Set whether the cpu supports timer mode
	 */
	public static void checkTimerSupport() {

		try {
			proxy = RemoteProxyManager.getInstance().getFileProxy(Oprofile.OprofileProject.getProject());
			IFileStore fileStore = proxy.getResource(CPUTYPE);
			if (fileStore.fetchInfo().exists()) {
				try (InputStream is = fileStore.openInputStream(EFS.NONE, new NullProgressMonitor());
						BufferedReader bi = new BufferedReader(new InputStreamReader(is))) {
					String cpuType = bi.readLine();
					if (cpuType.equals(TIMER)) {
						hasTimerSupport = true;
					} else {
						hasTimerSupport = false;
					}
				}
			}
		} catch (FileNotFoundException e) {
			hasTimerSupport = true;
		} catch (IOException e) {
			hasTimerSupport = true;
			e.printStackTrace();
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Get the system's cpu frequency
	 * 
	 * @return the system's cpu frequency
	 */
	private int getCPUFrequency() {
		int val = 0;
		try {
			proxy = RemoteProxyManager.getInstance().getFileProxy(Oprofile.OprofileProject.getProject());
			IFileStore fileStore = proxy.getResource(CPUINFO);
			if (fileStore.fetchInfo().exists()) {
				InputStream is = fileStore.openInputStream(EFS.NONE, new NullProgressMonitor());
				try (BufferedReader bi = new BufferedReader(new InputStreamReader(is))) {
					String line;
					while ((line = bi.readLine()) != null) {
						int index = line.indexOf(':');
						if (index != -1) {
							String substr;

							// x86/ia64/x86_64
							if (line.startsWith("cpu MHz")) { //$NON-NLS-1$
								substr = line.substring(index + 1).trim();
								return (int) Double.parseDouble(substr);
								// ppc/pc64
							} else if (line.startsWith("clock")) { //$NON-NLS-1$
								int MHzLoc = line.indexOf("MHz"); //$NON-NLS-1$
								substr = line.substring(index + 1, MHzLoc);
								return (int) Double.parseDouble(substr);
								// alpha
							} else if (line.startsWith("cycle frequency [Hz]")) { //$NON-NLS-1$
								substr = line.substring(index + 1).trim();
								return (int) (Double.parseDouble(substr) / 1E6);
								// sparc64
							} else if (line.startsWith("Cpu0ClkTck")) { //$NON-NLS-1$
								substr = line.substring(index + 1).trim();
								return (int) (Double.parseDouble(substr) / 1E6);
							}
						}
					}
				} catch (IOException | NumberFormatException e) {
					e.printStackTrace();
				}
			}
		} catch (CoreException e) {
			e.printStackTrace();
		}

		return val;
	}

	/**
	 * Get the number of counters for the system
	 * 
	 * @return the number of counters for the system
	 */
	private int getNrCounters() {
		/*
		 * TODO: Originally the number of counters for a given arch were hard-coded in a
		 * list. This method may not be entirely correct, although much simpler.
		 */

		/*
		 * Returning 1 for operf since it multiplexes the events through the counters
		 * and it is not possible to read data from opcontrol /dev dir if the opcontrol
		 * module was not initialized. TODO: Make possible to select more than one event
		 * in a tab.
		 */
		if ((OprofileProject.getProfilingBinary().equals(OprofileProject.OPERF_BINARY))
				|| (OprofileProject.getProfilingBinary().equals(OprofileProject.OCOUNT_BINARY))) {
			return 1;
		}
		try {
			proxy = RemoteProxyManager.getInstance().getFileProxy(Oprofile.OprofileProject.getProject());
		} catch (CoreException e) {
			e.printStackTrace();
		}
		final int MAXCPUS = Integer.MAX_VALUE;
		for (int i = 0; i < MAXCPUS; i++) {
			IFileStore fileStore = proxy.getResource(DEV_OPROFILE + i);
			if (!fileStore.fetchInfo().exists()) {
				return i;
			}
		}
		return -1;
	}

	private void createXML() {

		NodeList eventList = oldRoot.getElementsByTagName(EVENT);
		Element newEventList = newDoc.createElement(EVENT_LIST);

		for (int i = 0; i < eventList.getLength(); i++) {
			// get the event data
			Element event = (Element) eventList.item(i);
			String name = event.getAttribute(EVENT_NAME);
			String desc = event.getAttribute(DESC);
			String min_count = event.getAttribute(MIN_COUNT);

			// create the data for the new event
			Element newEventTag = newDoc.createElement(EVENT);
			Element nameTag = newDoc.createElement(NAME);
			nameTag.setTextContent(name);
			Element descTag = newDoc.createElement(DESCRIPTION);
			descTag.setTextContent(desc);
			Element minimumTag = newDoc.createElement(MINIMUM);
			minimumTag.setTextContent(min_count);

			newEventTag.appendChild(nameTag);
			newEventTag.appendChild(descTag);
			newEventTag.appendChild(minimumTag);

			Element unitMaskTag = (Element) event.getElementsByTagName(UNIT_MASKS).item(0);

			// check if there are any unit masks for this event
			if (unitMaskTag != null) {
				String defaultVal = unitMaskTag.getAttribute(DEFAULT);

				// Get the unit mask type (compatible with 1.0 and 1.1 ophelp
				// xml schemas)
				String type = EventIdCache.getInstance().getUnitMaskType(name);

				Element newUnitMaskTag = newDoc.createElement(UNITMASK);
				Element typeTag = newDoc.createElement(TYPE);
				typeTag.setTextContent(type);
				Element defaultValTag = newDoc.createElement(DEFAULT);
				defaultValTag.setTextContent(defaultVal);

				newUnitMaskTag.appendChild(typeTag);
				newUnitMaskTag.appendChild(defaultValTag);
				newEventTag.appendChild(newUnitMaskTag);

				NodeList unitMaskList = unitMaskTag.getElementsByTagName(UNIT_MASK);
				for (int j = 0; j < unitMaskList.getLength(); j++) {
					Element unitMask = (Element) unitMaskList.item(j);
					String maskVal = unitMask.getAttribute(MASK);
					String maskDesc = unitMask.getAttribute(DESC);
					String maskName = unitMask.getAttribute(NAME);

					Element newMask = newDoc.createElement(MASK);
					Element newVal = newDoc.createElement(VALUE);
					newVal.setTextContent(maskVal);
					Element newDesc = newDoc.createElement(DESCRIPTION);
					newDesc.setTextContent(maskDesc);
					Element newName = newDoc.createElement(NAME);
					newName.setTextContent(maskName);

					newMask.appendChild(newVal);
					newMask.appendChild(newDesc);
					newUnitMaskTag.appendChild(newMask);
				}
				// not unit mask for this event
			} else {
				String defaultVal = "0"; //$NON-NLS-1$
				String type = "mandatory"; //$NON-NLS-1$

				Element newUnitMaskTag = newDoc.createElement(UNITMASK);
				Element typeTag = newDoc.createElement(TYPE);
				typeTag.setTextContent(type);
				Element defaultValTag = newDoc.createElement(DEFAULT);
				defaultValTag.setTextContent(defaultVal);

				newUnitMaskTag.appendChild(typeTag);
				newUnitMaskTag.appendChild(defaultValTag);
				newEventTag.appendChild(newUnitMaskTag);

				Element newMask = newDoc.createElement(MASK);
				Element newVal = newDoc.createElement(VALUE);
				newVal.setTextContent("0"); //$NON-NLS-1$
				Element newDesc = newDoc.createElement(DESCRIPTION);
				newDesc.setTextContent("No unit mask"); //$NON-NLS-1$

				newMask.appendChild(newVal);
				newMask.appendChild(newDesc);
				newUnitMaskTag.appendChild(newMask);
			}

			newEventList.appendChild(newEventTag);
		}

		for (int i = 0; i < getNrCounters(); i++) {
			Element eventListTag = (Element) newEventList.cloneNode(true);
			eventListTag.setAttribute(COUNTER, String.valueOf(i));
			newRoot.appendChild(eventListTag);
		}
	}

	@Override
	public Document getDocument() {
		return newDoc;
	}

	/**
	 * @since 3.0
	 */
	private void setEventIdCacheDoc(Element elem) {
		EventIdCache.getInstance().setCacheDoc(elem);
	}
}

Back to the top