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

package org.eclipse.linuxtools.internal.oprofile.launch.configuration;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.linuxtools.internal.oprofile.core.Oprofile;
import org.eclipse.linuxtools.internal.oprofile.core.daemon.OpEvent;
import org.eclipse.linuxtools.internal.oprofile.core.daemon.OpUnitMask;
import org.eclipse.linuxtools.internal.oprofile.core.daemon.OprofileDaemonEvent;
import org.eclipse.linuxtools.internal.oprofile.launch.OprofileLaunchMessages;
import org.eclipse.linuxtools.internal.oprofile.launch.OprofileLaunchPlugin;

/**
 * This class represents an oprofile runtime configuration of a counter. It is
 * used to construct arguments for launching op_start on the host. It
 * simply wraps OprofileDaemonEvent.
 */
public class OprofileCounter {
    private static final String COUNTER_STRING = OprofileLaunchMessages.getString("oprofileCounter.counterString"); //$NON-NLS-1$

    // The counter number
    private int number;

    // Is this counter enabled?
    private boolean _enabled;

    // The event(s) to collect on this counter
    private OprofileDaemonEvent[] daemonEvent;

    // List of valid events on this counter
    private OpEvent[] eventList = null;

    /**
     * Constructor for OprofileCounter.
     * @param nr    the counter number
     */
    public OprofileCounter(int nr) {
        this(nr, Oprofile.getEvents(nr));
    }

    /**
     * Constructor for OprofileCounter.
     * @param nr the counter number
     * @param events the given events for counter number <code>nr</code>
     */
    public OprofileCounter(int nr, OpEvent[] events) {
        number = nr;
        _enabled = false;
        eventList = events;
        daemonEvent = new OprofileDaemonEvent [] {new OprofileDaemonEvent()};
    }

    /**
     * Constructs all of the counters in  the given launch configuration.
     * @param config the launch configuration
     * @return an array of all counters
     */
    public static OprofileCounter[] getCounters(ILaunchConfiguration config) {
        OprofileCounter[] ctrs = new OprofileCounter[Oprofile.getNumberOfCounters()];
        for (int i = 0; i < ctrs.length; i++)
        {
            ctrs[i] = new OprofileCounter(i);
            if (config != null)
                ctrs[i].loadConfiguration(config);
        }

        return ctrs;
    }

    /**
     * Method setEnabled.
     * @param enabled    whether to set this counter as enabled
     */
    public void setEnabled(boolean enabled) {
        this._enabled = enabled;
    }

    /**
     * Method setEvent.
     * @param event    the event for this counter
     */
    public void setEvents(OpEvent [] events) {
        OprofileDaemonEvent [] newDaemonEvent = new OprofileDaemonEvent[events.length];
        for (int i = 0; i < events.length; i++) {
            if (i > daemonEvent.length - 1) {
                OprofileDaemonEvent de = new OprofileDaemonEvent();
                de.setEvent(events[i]);
                de.setResetCount(daemonEvent[0].getResetCount());
                newDaemonEvent[i] = de;
            } else {
                daemonEvent[i].setEvent(events[i]);
                newDaemonEvent[i] = daemonEvent[i];
            }
        }
        daemonEvent = newDaemonEvent;
    }

    /**
     * Method setProfileKernel.
     * @param profileKernel    whether this counter should count kernel events
     */
    public void setProfileKernel(boolean profileKernel) {
        for (int i = 0; i < daemonEvent.length; i++) {
            daemonEvent[i].setProfileKernel(profileKernel);
        }
    }

    /**
     * Method setProfileUser.
     * @param profileUser    whether this counter should count user events
     */
    public void setProfileUser(boolean profileUser) {
        for (int i = 0; i < daemonEvent.length; i++) {
            daemonEvent[i].setProfileUser(profileUser);
        }
    }

    /**
     * Method setCount.
     * @param count    the number of events between samples for this counter
     */
    public void setCount(int count) {
        for (int i = 0; i < daemonEvent.length; i++) {
            daemonEvent[i].setResetCount(count);
        }
    }

    /**
     * Saves this counter's configuration into the specified launch
     * configuration.
     * @param config    the launch configuration
     */
    public void saveConfiguration(ILaunchConfigurationWorkingCopy config) {
        config.setAttribute(OprofileLaunchPlugin.attrConterEnabled(number), _enabled);
        config.setAttribute(OprofileLaunchPlugin.attrNumberOfEvents(number), daemonEvent.length);

        for (int i = 0; i < daemonEvent.length; i++) {
            if (daemonEvent[i].getEvent() != null) {
                config.setAttribute(OprofileLaunchPlugin.attrConterEvent(number, i), daemonEvent[i].getEvent().getText());
                config.setAttribute(OprofileLaunchPlugin.attrCounterUnitMask(number), daemonEvent[i].getEvent().getUnitMask().getMaskValue());
                config.setAttribute(OprofileLaunchPlugin.attrCounterUnitMaskName(number), daemonEvent[i].getEvent().getUnitMask().getMaskName());
            }
            config.setAttribute(OprofileLaunchPlugin.attrCounterProfileKernel(number), daemonEvent[i].getProfileKernel());
            config.setAttribute(OprofileLaunchPlugin.attrCounterProfileUser(number), daemonEvent[i].getProfileUser());
            config.setAttribute(OprofileLaunchPlugin.attrCounterCount(number), daemonEvent[i].getResetCount());
        }
    }

    /**
     * Loads a counter configuration from the specified launch configuration.
     * @param config    the launch configuration
     */
    public void loadConfiguration(ILaunchConfiguration config) {
        try {
            _enabled = config.getAttribute(OprofileLaunchPlugin.attrConterEnabled(number), false);
            int numEvents = config.getAttribute(OprofileLaunchPlugin.attrNumberOfEvents(number), 1);
            daemonEvent = new OprofileDaemonEvent[numEvents];

            for (int i = 0; i < numEvents; i++) {
                String str = config.getAttribute(OprofileLaunchPlugin.attrConterEvent(number, i), ""); //$NON-NLS-1$
                int maskValue = config.getAttribute(OprofileLaunchPlugin.attrCounterUnitMask(number), OpUnitMask.SET_DEFAULT_MASK);
                String maskName = config.getAttribute(OprofileLaunchPlugin.attrCounterUnitMaskName(number), ""); //$NON-NLS-1$

                daemonEvent[i] = new OprofileDaemonEvent();
                daemonEvent[i].setEvent(eventFromString(str));

                if (daemonEvent[i].getEvent() == null) {
                    continue;
                }

                daemonEvent[i].getEvent().getUnitMask().setMaskValue(maskValue);
                daemonEvent[i].getEvent().getUnitMask().setMaskName(maskName);
                daemonEvent[i].setProfileKernel(config.getAttribute(OprofileLaunchPlugin.attrCounterProfileKernel(number), false));
                daemonEvent[i].setProfileUser(config.getAttribute(OprofileLaunchPlugin.attrCounterProfileUser(number), false));

                daemonEvent[i].setResetCount(config.getAttribute(OprofileLaunchPlugin.attrCounterCount(number), OprofileDaemonEvent.COUNT_UNINITIALIZED));
            }
        } catch (CoreException ce) {

        }
    }

    public OpUnitMask getUnitMask() {
        OpEvent event = daemonEvent[0].getEvent();

        if (event != null) {
            return event.getUnitMask();
        } else {
            return null;
        }
    }

    /**
     * Returns a textual label for this counter (used by UI)
     * @return the label to use in widgets referring to this counter
     */
    public String getText() {
        return MessageFormat.format(COUNTER_STRING, Integer.valueOf(number));
    }

    /**
     * Method getNumber.
     * @return the counter's number
     */
    public int getNumber() {
        return number;
    }

    /**
     * Method getEnabled.
     * @return whether this counter is enabled
     */
    public boolean getEnabled() {
        return _enabled;
    }

    /**
     * Method getEvent.
     * @return the event for this counter
     */
    public OpEvent [] getEvents() {
        List<OpEvent> res = new ArrayList<> ();
        for (OprofileDaemonEvent de : daemonEvent) {
            res.add(de.getEvent());
        }
        return res.toArray(new OpEvent[0]);
    }

    /**
     * Method getProfileKernel.
     * @return whether this counter is counting kernel events
     */
    public boolean getProfileKernel() {
        return daemonEvent[0].getProfileKernel();
    }

    /**
     * Method getProfileUser.
     * @return whether this counter is counting user events
     */
    public boolean getProfileUser() {
        return daemonEvent[0].getProfileUser();
    }

    /**
     * Method getCount.
     * @return the number of events between samples for this counter
     */
    public int getCount() {
        return daemonEvent[0].getResetCount();
    }

    /**
     * Method getValidEvents.
     * @return an array of all events that this counter can monitor
     */
    public OpEvent[] getValidEvents() {
        return eventList;
    }

    /**
     * Gets the daemon event configuration for this counter.
     * <B>Not</B> valid if this counter is not enabled!
     * @return the OprofileDaemonEvent
     */
    public OprofileDaemonEvent [] getDaemonEvents() {
        return daemonEvent;
    }

    // Returns the event with the same label as the parameter STR
    private OpEvent eventFromString(String str) {
        if (eventList != null) {
            for (int i = 0; i < eventList.length; i++) {
                if (eventList[i].getText().equals(str))
                    return eventList[i];
            }
        }

        return null;
    }
}

Back to the top