Commit | Line | Data |
---|---|---|
7b79ee46 | 1 | /******************************************************************************* |
65a4afc0 | 2 | * Copyright (c) 2015, 2016 EfficiOS Inc., Ericsson |
7b79ee46 FLN |
3 | * |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | *******************************************************************************/ | |
9 | ||
660d4ed9 | 10 | package org.eclipse.tracecompass.internal.analysis.os.linux.core.latency; |
7b79ee46 FLN |
11 | |
12 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; | |
13 | ||
14 | import java.io.IOException; | |
15 | import java.io.ObjectInputStream; | |
18c18ee0 | 16 | import java.util.Collection; |
09f15ee9 | 17 | import java.util.Collections; |
6ad9d1cb | 18 | import java.util.Comparator; |
7b79ee46 | 19 | import java.util.HashMap; |
7b79ee46 | 20 | import java.util.Map; |
bbadfd0a AM |
21 | import java.util.function.Function; |
22 | import java.util.stream.Collectors; | |
7b79ee46 | 23 | |
7858428e GB |
24 | import org.eclipse.core.runtime.IProgressMonitor; |
25 | import org.eclipse.core.runtime.NullProgressMonitor; | |
07b705e8 | 26 | import org.eclipse.jdt.annotation.NonNull; |
7b79ee46 | 27 | import org.eclipse.jdt.annotation.Nullable; |
0f7a12d3 | 28 | import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelTidAspect; |
09f15ee9 | 29 | import org.eclipse.tracecompass.analysis.os.linux.core.tid.TidAnalysisModule; |
7b79ee46 FLN |
30 | import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelAnalysisEventLayout; |
31 | import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelTrace; | |
65a4afc0 | 32 | import org.eclipse.tracecompass.analysis.timing.core.segmentstore.AbstractSegmentStoreAnalysisEventBasedModule; |
7b79ee46 FLN |
33 | import org.eclipse.tracecompass.segmentstore.core.ISegment; |
34 | import org.eclipse.tracecompass.segmentstore.core.ISegmentStore; | |
09f15ee9 | 35 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; |
7b79ee46 | 36 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
18c18ee0 | 37 | import org.eclipse.tracecompass.tmf.core.segment.ISegmentAspect; |
09f15ee9 | 38 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
7b79ee46 | 39 | |
18c18ee0 | 40 | import com.google.common.collect.ImmutableList; |
09f15ee9 | 41 | import com.google.common.collect.ImmutableSet; |
7b79ee46 FLN |
42 | |
43 | /** | |
44 | * @author Alexandre Montplaisir | |
fb3a499b | 45 | * @since 2.0 |
7b79ee46 | 46 | */ |
65a4afc0 | 47 | public class SystemCallLatencyAnalysis extends AbstractSegmentStoreAnalysisEventBasedModule { |
7b79ee46 FLN |
48 | |
49 | /** | |
50 | * The ID of this analysis | |
51 | */ | |
aa65365a | 52 | public static final String ID = "org.eclipse.tracecompass.analysis.os.linux.latency.syscall"; //$NON-NLS-1$ |
7b79ee46 FLN |
53 | |
54 | private static final String DATA_FILENAME = "latency-analysis.dat"; //$NON-NLS-1$ | |
55 | ||
18c18ee0 | 56 | private static final Collection<ISegmentAspect> BASE_ASPECTS = |
0e4f957e | 57 | ImmutableList.of(SyscallNameAspect.INSTANCE); |
18c18ee0 | 58 | |
7b79ee46 FLN |
59 | @Override |
60 | public String getId() { | |
61 | return ID; | |
62 | } | |
63 | ||
09f15ee9 GB |
64 | @Override |
65 | protected Iterable<IAnalysisModule> getDependentAnalyses() { | |
66 | ITmfTrace trace = getTrace(); | |
67 | if (trace == null) { | |
68 | throw new IllegalStateException(); | |
69 | } | |
70 | IAnalysisModule module = trace.getAnalysisModule(TidAnalysisModule.ID); | |
71 | if (module == null) { | |
72 | return Collections.EMPTY_SET; | |
73 | } | |
74 | return ImmutableSet.of(module); | |
75 | } | |
76 | ||
18c18ee0 BH |
77 | @Override |
78 | public Iterable<ISegmentAspect> getSegmentAspects() { | |
79 | return BASE_ASPECTS; | |
80 | } | |
81 | ||
7b79ee46 | 82 | @Override |
07b705e8 | 83 | public @NonNull String getDataFileName() { |
152630e0 | 84 | return DATA_FILENAME; |
7b79ee46 FLN |
85 | } |
86 | ||
87 | @Override | |
152630e0 BH |
88 | public AbstractSegmentStoreAnalysisRequest createAnalysisRequest(ISegmentStore<ISegment> syscalls) { |
89 | return new SyscallLatencyAnalysisRequest(syscalls); | |
7b79ee46 FLN |
90 | } |
91 | ||
152630e0 BH |
92 | @Override |
93 | protected Object[] readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { | |
94 | return checkNotNull((Object[]) ois.readObject()); | |
7b79ee46 FLN |
95 | } |
96 | ||
09f15ee9 | 97 | private class SyscallLatencyAnalysisRequest extends AbstractSegmentStoreAnalysisRequest { |
7b79ee46 | 98 | |
7b79ee46 | 99 | private final Map<Integer, SystemCall.InitialInfo> fOngoingSystemCalls = new HashMap<>(); |
152630e0 | 100 | private @Nullable IKernelAnalysisEventLayout fLayout; |
7858428e | 101 | private final IProgressMonitor fMonitor = new NullProgressMonitor(); |
7b79ee46 | 102 | |
152630e0 BH |
103 | public SyscallLatencyAnalysisRequest(ISegmentStore<ISegment> syscalls) { |
104 | super(syscalls); | |
7b79ee46 FLN |
105 | } |
106 | ||
107 | @Override | |
108 | public void handleData(final ITmfEvent event) { | |
109 | super.handleData(event); | |
152630e0 BH |
110 | IKernelAnalysisEventLayout layout = fLayout; |
111 | if (layout == null) { | |
0e4f957e | 112 | IKernelTrace trace = (IKernelTrace) event.getTrace(); |
152630e0 BH |
113 | layout = trace.getKernelEventLayout(); |
114 | fLayout = layout; | |
115 | } | |
7b79ee46 FLN |
116 | final String eventName = event.getType().getName(); |
117 | ||
152630e0 BH |
118 | if (eventName.startsWith(layout.eventSyscallEntryPrefix()) || |
119 | eventName.startsWith(layout.eventCompatSyscallEntryPrefix())) { | |
7b79ee46 FLN |
120 | /* This is a system call entry event */ |
121 | ||
7858428e GB |
122 | Integer tid; |
123 | try { | |
124 | tid = KernelTidAspect.INSTANCE.resolve(event, true, fMonitor); | |
125 | } catch (InterruptedException e) { | |
126 | return; | |
127 | } | |
7b79ee46 FLN |
128 | if (tid == null) { |
129 | // no information on this event/trace ? | |
130 | return; | |
131 | } | |
132 | ||
133 | /* Record the event's data into the intial system call info */ | |
134 | // String syscallName = fLayout.getSyscallNameFromEvent(event); | |
135 | long startTime = event.getTimestamp().getValue(); | |
152630e0 | 136 | String syscallName = eventName.substring(layout.eventSyscallEntryPrefix().length()); |
bbadfd0a AM |
137 | |
138 | Map<String, String> args = event.getContent().getFieldNames().stream() | |
139 | .collect(Collectors.toMap(Function.identity(), | |
cd655e55 | 140 | input -> checkNotNull(event.getContent().getField(input).toString()))); |
bbadfd0a AM |
141 | |
142 | SystemCall.InitialInfo newSysCall = new SystemCall.InitialInfo(startTime, checkNotNull(syscallName), checkNotNull(args)); | |
7b79ee46 FLN |
143 | fOngoingSystemCalls.put(tid, newSysCall); |
144 | ||
152630e0 | 145 | } else if (eventName.startsWith(layout.eventSyscallExitPrefix())) { |
7b79ee46 FLN |
146 | /* This is a system call exit event */ |
147 | ||
7858428e GB |
148 | Integer tid; |
149 | try { | |
150 | tid = KernelTidAspect.INSTANCE.resolve(event, true, fMonitor); | |
151 | } catch (InterruptedException e) { | |
152 | return; | |
153 | } | |
7b79ee46 FLN |
154 | if (tid == null) { |
155 | return; | |
156 | } | |
157 | ||
158 | SystemCall.InitialInfo info = fOngoingSystemCalls.remove(tid); | |
159 | if (info == null) { | |
160 | /* | |
161 | * We have not seen the entry event corresponding to this | |
162 | * exit (lost event, or before start of trace). | |
163 | */ | |
164 | return; | |
165 | } | |
166 | ||
167 | long endTime = event.getTimestamp().getValue(); | |
168 | int ret = ((Long) event.getContent().getField("ret").getValue()).intValue(); //$NON-NLS-1$ | |
169 | ISegment syscall = new SystemCall(info, endTime, ret); | |
152630e0 | 170 | getSegmentStore().add(syscall); |
7b79ee46 FLN |
171 | } |
172 | } | |
173 | ||
174 | @Override | |
175 | public void handleCompleted() { | |
176 | fOngoingSystemCalls.clear(); | |
7858428e GB |
177 | super.handleCompleted(); |
178 | } | |
179 | ||
180 | @Override | |
181 | public void handleCancel() { | |
182 | fMonitor.setCanceled(true); | |
183 | super.handleCancel(); | |
7b79ee46 FLN |
184 | } |
185 | } | |
186 | ||
18c18ee0 BH |
187 | private static class SyscallNameAspect implements ISegmentAspect { |
188 | public static final ISegmentAspect INSTANCE = new SyscallNameAspect(); | |
189 | ||
190 | private SyscallNameAspect() { } | |
191 | ||
192 | @Override | |
193 | public String getHelpText() { | |
194 | return checkNotNull(Messages.SegmentAspectHelpText_SystemCall); | |
195 | } | |
196 | @Override | |
197 | public String getName() { | |
198 | return checkNotNull(Messages.SegmentAspectName_SystemCall); | |
199 | } | |
200 | @Override | |
6ad9d1cb BH |
201 | public @Nullable Comparator<?> getComparator() { |
202 | return null; | |
203 | } | |
204 | @Override | |
18c18ee0 BH |
205 | public @Nullable String resolve(ISegment segment) { |
206 | if (segment instanceof SystemCall) { | |
207 | return ((SystemCall) segment).getName(); | |
208 | } | |
209 | return EMPTY_STRING; | |
210 | } | |
211 | } | |
212 | ||
7b79ee46 | 213 | } |