Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dd4adbf8fc773489c8da685931f641beac9885e7 (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
/*******************************************************************************
 * Copyright (c) 2004, 2016 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.internal.oprofile.core.daemon;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;

import org.eclipse.linuxtools.internal.oprofile.core.OprofileCorePlugin;
import org.eclipse.linuxtools.internal.oprofile.core.linux.LinuxOpxmlProvider.OpInfoRunner;
import org.eclipse.linuxtools.internal.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> {
		@Override
		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> {
		@Override
		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();

		OpInfoRunner opxml = (OpInfoRunner) OprofileCorePlugin.getDefault().getOpxmlProvider().info(info);
		boolean ret = opxml.run0(null);
		if (!ret) {
			info = null;
		}

		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) {
		this.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 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