Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 573643ecfd100aedbe4183781db89c33c577fd24 (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
/*******************************************************************************
 * Copyright (c) 2004, 2008, 2009 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:
 *    Keith Seitz <keiths@redhat.com> - initial API and implementation
 *    Kent Sebastian <ksebasti@redhat.com>
 *******************************************************************************/ 

package org.eclipse.linuxtools.oprofile.core.daemon;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;

import org.eclipse.linuxtools.oprofile.core.OprofileCorePlugin;
import org.eclipse.linuxtools.oprofile.core.OpxmlException;
import org.eclipse.linuxtools.oprofile.core.linux.LinuxOpxmlProvider.OpInfoRunner;
import org.eclipse.linuxtools.oprofile.core.opxml.info.DefaultsProcessor;


/**
 * A class to hold generic information about Oprofile.
 */
public class OpInfo {
	// Oprofile defaults
	public static final String DEFAULT_SAMPLE_DIR = DefaultsProcessor.SAMPLE_DIR;
	public static final String DEFAULT_LOCK_FILE = DefaultsProcessor.LOCK_FILE;
	public static final String DEFAULT_LOG_FILE = DefaultsProcessor.LOG_FILE;
	public static final String DEFAULT_DUMP_STATUS = DefaultsProcessor.DUMP_STATUS;
	
	// A comparator class used when sorting events
	// (sorting by event name)
	private static class SortEventComparator implements Comparator<OpEvent> {
		public int compare(OpEvent o1, OpEvent o2) {
			return o1.getText().compareTo(o2.getText());
		}
	}

	// A comparator class used when searching events
	// (searching by event name)
	private static class SearchEventComparator implements Comparator<Object> {
		public int compare(Object a, Object b) {
			String astr, bstr;
			if (a instanceof String) {
				astr = (String) a;
				bstr = ((OpEvent) b).getText();
			} else {
				astr = ((OpEvent) a).getText();
				bstr = (String) b;
			}
			return astr.compareTo(bstr);
		}
	}
	
	// The number of counters supported by this configuration
	private int _nrCounters;
	
	// A HashMap of Oprofile defaults
	private HashMap<String,String> _defaults;
	
	// The permanent list of events indexed by counter
	private OpEvent[][] _eventList;
	
	// The CPU frequency of this CPU in MHz
	private double _cpuSpeed;
	
	// Whether or not oprofile is running in timer mode
	private boolean _timerMode;
	
	/**
	 * Return all of Oprofile's generic information.
	 * @return a class containing the information
	 */
	public static OpInfo getInfo() {		
		// Run opmxl and get the static information
		OpInfo info = new OpInfo();

		try {
			OpInfoRunner opxml = (OpInfoRunner) OprofileCorePlugin.getDefault().getOpxmlProvider().info(info);
			boolean ret = opxml.run0(null);
			if (ret == false) 
				info = null;
		} catch (InvocationTargetException e) {
		} catch (InterruptedException e) {
		} catch (OpxmlException e) {
			OprofileCorePlugin.showErrorDialog("opxmlProvider", e); //$NON-NLS-1$
		}
		
		return info;
	}

	/**
	 * Sets the number of counters allowed by Oprofile. This method is called
	 * after this object is contstructed, while opxml is run (the first tag output
	 * is num-counters).
	 * Only called from XML parsers.
	 * @param ctrs the number of counters
	 */
	public void _setNrCounters(int ctrs) {
		_nrCounters = ctrs;
		
		// Allocate room for event lists for the counters
		_eventList = new OpEvent[_nrCounters][];
	}

	/**
	 * Set the CPU frequency (in MHz).
	 * Only called from the XML parsers.
	 * @param freq the frequency
	 */
	public void _setCPUSpeed(double freq) {
		_cpuSpeed = freq;
	}

	/**
	 * Sets the defaults associated with this configuration of Oprofile.
	 * Only called from XML parsers.
	 * @param map the <code>HashMap</code> containing the defaults
	 */
	public void _setDefaults(HashMap<String,String> map) {
		_defaults = map;
	}

	/**
	 * Adds the events of the counter counterNum into the list of all events.
	 * Note they are sorted here.
	 * Only called from XML parsers.
	 * @param counterNum the counter with the events
	 * @param events an array of OpEvent events belonging to this counter
	 */
	public void _setEvents(int counterNum, OpEvent[] events) {
		if (counterNum < _eventList.length) {
			_eventList[counterNum] = events;
			Arrays.sort(_eventList[counterNum], new SortEventComparator());
		}
	}
	
	/**
	 * Sets whether or not oprofile is operating in timer mode.
	 * Only called from XML parsers.
	 * @param timerMode true if oprofile is in timer mode, false if not
	 */
	public void _setTimerMode(boolean timerMode) {
		_timerMode = timerMode;
	}

	/**
	 * Returns the number of counters allowed by Oprofile
	 * @return the number of counters
	 */
	public int getNrCounters() {
		return _nrCounters;
	}
		
	/**
	 * Returns the CPU's speed in MHz
	 * @return the speed
	 */
	public double getCPUSpeed() {
		return _cpuSpeed;
	}
	
	/**
	 * Returns the requested default. Valid defaults are <code>DEFAULT_DUMP_STATUS</code>,
	 * <code>DEFAULT_LOCK_FILE</code>, <code>DEFAULT_LOG_FILE</code>, and
	 * <code>DEFAULT_SAMPLE_DIR</code>.
	 * @param what which default to return
	 * @return the requested default or <code>null</code> if not known
	 */
	public String getDefault(String what) {
		return (String) _defaults.get(what);
	}

	/**
	 * Returns an array of events valid for the given counter number.
	 * @param num the counter number
	 * @return an array of valid events
	 */ 
	public OpEvent[] getEvents(int num) {
		if (num >= 0 && num < _eventList.length)
			return _eventList[num];
		
		return new OpEvent[0];
	}
	
	/**
	 * Returns whether or not oprofile is operating in timer mode.
	 * @return a boolean, true if in timer mode, false if not
	 */
	public boolean getTimerMode() {
		return _timerMode;
	}
	
	/**
	 * Searches the for the event with the given name
	 * @param name the name of the event (e.g., CPU_CLK_UNHALTED)
	 * @return the event or <code>null</code> if not found
	 */
	public OpEvent findEvent(String name) {
		// Search through all counters
		for (int counter = 0; counter < getNrCounters(); ++counter) {
			int idx = Arrays.binarySearch(getEvents(counter), name, new SearchEventComparator());
			if (idx >= 0)
				return _eventList[counter][idx];
		}
		
		return null;
	}
}

Back to the top