Commit | Line | Data |
---|---|---|
e693075d | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2014, 2015 École Polytechnique de Montréal |
e693075d FR |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * François Rajotte - Initial API and implementation | |
11 | * Geneviève Bastien - Revision of the initial implementation | |
12 | *******************************************************************************/ | |
13 | ||
e363eae1 | 14 | package org.eclipse.tracecompass.analysis.os.linux.core.cpuusage; |
e693075d | 15 | |
d0c7e4ba AM |
16 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
17 | ||
e693075d FR |
18 | import java.util.HashMap; |
19 | import java.util.Map; | |
20 | ||
e363eae1 | 21 | import org.eclipse.jdt.annotation.Nullable; |
0f7a12d3 | 22 | import org.eclipse.tracecompass.analysis.os.linux.core.kernel.Attributes; |
e363eae1 AM |
23 | import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelAnalysisEventLayout; |
24 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.Activator; | |
d0c7e4ba | 25 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder; |
2241331e | 26 | import org.eclipse.tracecompass.statesystem.core.StateSystemBuilderUtils; |
e894a508 | 27 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; |
2bdf0193 AM |
28 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
29 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; | |
1786026d | 30 | import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect; |
2bdf0193 AM |
31 | import org.eclipse.tracecompass.tmf.core.statesystem.AbstractTmfStateProvider; |
32 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
1786026d | 33 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils; |
e693075d FR |
34 | |
35 | /** | |
36 | * Creates a state system with the total time spent on CPU for each thread and | |
37 | * for each CPU from a kernel trace. | |
38 | * | |
39 | * This state system in itself keeps the total time on CPU since last time the | |
40 | * process was scheduled out. The state system queries will only be accurate | |
41 | * when the process is not in a running state. To have exact CPU usage when | |
42 | * running, this state system needs to be used along the LTTng Kernel analysis. | |
43 | * | |
44 | * It requires only the 'sched_switch' events enabled on the trace. | |
45 | * | |
46 | * @author François Rajotte | |
e693075d | 47 | */ |
e363eae1 | 48 | public class KernelCpuUsageStateProvider extends AbstractTmfStateProvider { |
e693075d | 49 | |
96811390 | 50 | private static final int VERSION = 2; |
e693075d FR |
51 | |
52 | /* For each CPU, maps the last time a thread was scheduled in */ | |
1786026d | 53 | private final Map<Integer, Long> fLastStartTimes = new HashMap<>(); |
e693075d | 54 | private final long fTraceStart; |
e363eae1 | 55 | private final IKernelAnalysisEventLayout fLayout; |
e693075d FR |
56 | |
57 | /** | |
58 | * Constructor | |
59 | * | |
60 | * @param trace | |
61 | * The trace from which to get the CPU usage | |
7411cd67 AM |
62 | * @param layout |
63 | * The event layout to use for this state provider. | |
e693075d | 64 | */ |
e363eae1 | 65 | public KernelCpuUsageStateProvider(ITmfTrace trace, IKernelAnalysisEventLayout layout) { |
e2bcc8a5 | 66 | super(trace, "Kernel CPU usage"); //$NON-NLS-1$ |
e693075d | 67 | fTraceStart = trace.getStartTime().getValue(); |
7411cd67 | 68 | fLayout = layout; |
e693075d FR |
69 | } |
70 | ||
71 | // ------------------------------------------------------------------------ | |
72 | // ITmfStateProvider | |
73 | // ------------------------------------------------------------------------ | |
74 | ||
75 | @Override | |
76 | public int getVersion() { | |
77 | return VERSION; | |
78 | } | |
79 | ||
80 | @Override | |
e363eae1 AM |
81 | public KernelCpuUsageStateProvider getNewInstance() { |
82 | return new KernelCpuUsageStateProvider(this.getTrace(), this.fLayout); | |
e693075d FR |
83 | } |
84 | ||
85 | @Override | |
e363eae1 AM |
86 | protected void eventHandle(@Nullable ITmfEvent event) { |
87 | if (event == null) { | |
88 | return; | |
89 | } | |
578716e6 | 90 | final String eventName = event.getName(); |
e693075d | 91 | |
7411cd67 | 92 | if (eventName.equals(fLayout.eventSchedSwitch())) { |
b3867ecc MAL |
93 | Integer cpu = TmfTraceUtils.resolveIntEventAspectOfClassForEvent(event.getTrace(), TmfCpuAspect.class, event); |
94 | if (cpu == null) { | |
1786026d GB |
95 | /* We couldn't find any CPU information, ignore this event */ |
96 | return; | |
97 | } | |
98 | ||
e693075d FR |
99 | /* |
100 | * Fields: string prev_comm, int32 prev_tid, int32 prev_prio, int64 | |
101 | * prev_state, string next_comm, int32 next_tid, int32 next_prio | |
102 | */ | |
e693075d FR |
103 | ITmfEventField content = event.getContent(); |
104 | long ts = event.getTimestamp().getValue(); | |
e693075d | 105 | |
7411cd67 | 106 | Long prevTid = (Long) content.getField(fLayout.fieldPrevTid()).getValue(); |
e693075d FR |
107 | |
108 | try { | |
e363eae1 AM |
109 | final ITmfStateSystemBuilder ss = checkNotNull(getStateSystemBuilder()); |
110 | ||
d0c7e4ba | 111 | Integer currentCPUNode = ss.getQuarkRelativeAndAdd(getNodeCPUs(ss), cpu.toString()); |
e693075d FR |
112 | |
113 | /* | |
114 | * This quark contains the value of the cumulative time spent on | |
115 | * the source CPU by the currently running thread | |
116 | */ | |
117 | Integer cumulativeTimeQuark = ss.getQuarkRelativeAndAdd(currentCPUNode, prevTid.toString()); | |
118 | Long startTime = fLastStartTimes.get(cpu); | |
119 | /* | |
120 | * If start time is null, we haven't seen the start of the | |
121 | * process, so we assume beginning of the trace | |
122 | */ | |
123 | if (startTime == null) { | |
124 | startTime = fTraceStart; | |
125 | } | |
126 | ||
c74fb445 AM |
127 | /* |
128 | * Modify cumulative time for this CPU/TID combo: The total time | |
129 | * changes when the process is scheduled out. Nothing happens | |
130 | * when the process is scheduled in. | |
131 | */ | |
2241331e | 132 | StateSystemBuilderUtils.incrementAttributeLong(ss, ts, cumulativeTimeQuark, ts - startTime); |
c74fb445 | 133 | |
c74fb445 AM |
134 | fLastStartTimes.put(cpu, ts); |
135 | ||
e693075d FR |
136 | } catch (AttributeNotFoundException e) { |
137 | Activator.getDefault().logError("Attribute not found in LttngKernelCpuStateProvider", e); //$NON-NLS-1$ | |
138 | } | |
139 | ||
140 | } | |
141 | } | |
142 | ||
143 | /* Shortcut for the "current CPU" attribute node */ | |
d0c7e4ba AM |
144 | private static int getNodeCPUs(ITmfStateSystemBuilder ssb) { |
145 | return ssb.getQuarkAbsoluteAndAdd(Attributes.CPUS); | |
e693075d FR |
146 | } |
147 | ||
148 | } |