e324337cf8b3f1b3c9f54be7edf5d4f896897721
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.core / src / org / eclipse / tracecompass / analysis / os / linux / core / latency / SystemCallLatencyAnalysis.java
1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 EfficiOS Inc., Ericsson
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
10 package org.eclipse.tracecompass.analysis.os.linux.core.latency;
11
12 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14 import java.io.IOException;
15 import java.io.ObjectInputStream;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.Comparator;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.function.Function;
22 import java.util.stream.Collectors;
23
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.core.runtime.NullProgressMonitor;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelTidAspect;
29 import org.eclipse.tracecompass.analysis.os.linux.core.tid.TidAnalysisModule;
30 import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelAnalysisEventLayout;
31 import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelTrace;
32 import org.eclipse.tracecompass.analysis.timing.core.segmentstore.AbstractSegmentStoreAnalysisEventBasedModule;
33 import org.eclipse.tracecompass.segmentstore.core.ISegment;
34 import org.eclipse.tracecompass.segmentstore.core.ISegmentStore;
35 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
36 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
37 import org.eclipse.tracecompass.tmf.core.segment.ISegmentAspect;
38 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
39
40 import com.google.common.collect.ImmutableList;
41 import com.google.common.collect.ImmutableSet;
42
43 /**
44 * @author Alexandre Montplaisir
45 * @since 2.0
46 */
47 public class SystemCallLatencyAnalysis extends AbstractSegmentStoreAnalysisEventBasedModule {
48
49 /**
50 * The ID of this analysis
51 */
52 public static final String ID = "org.eclipse.tracecompass.analysis.os.linux.latency.syscall"; //$NON-NLS-1$
53
54 private static final String DATA_FILENAME = "latency-analysis.dat"; //$NON-NLS-1$
55
56 private static final Collection<ISegmentAspect> BASE_ASPECTS =
57 ImmutableList.of(SyscallNameAspect.INSTANCE);
58
59 @Override
60 public String getId() {
61 return ID;
62 }
63
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
77 @Override
78 public Iterable<ISegmentAspect> getSegmentAspects() {
79 return BASE_ASPECTS;
80 }
81
82 @Override
83 public @NonNull String getDataFileName() {
84 return DATA_FILENAME;
85 }
86
87 @Override
88 public AbstractSegmentStoreAnalysisRequest createAnalysisRequest(ISegmentStore<ISegment> syscalls) {
89 return new SyscallLatencyAnalysisRequest(syscalls);
90 }
91
92 @Override
93 protected Object[] readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
94 return checkNotNull((Object[]) ois.readObject());
95 }
96
97 private class SyscallLatencyAnalysisRequest extends AbstractSegmentStoreAnalysisRequest {
98
99 private final Map<Integer, SystemCall.InitialInfo> fOngoingSystemCalls = new HashMap<>();
100 private @Nullable IKernelAnalysisEventLayout fLayout;
101 private final IProgressMonitor fMonitor = new NullProgressMonitor();
102
103 public SyscallLatencyAnalysisRequest(ISegmentStore<ISegment> syscalls) {
104 super(syscalls);
105 }
106
107 @Override
108 public void handleData(final ITmfEvent event) {
109 super.handleData(event);
110 IKernelAnalysisEventLayout layout = fLayout;
111 if (layout == null) {
112 IKernelTrace trace = (IKernelTrace) event.getTrace();
113 layout = trace.getKernelEventLayout();
114 fLayout = layout;
115 }
116 final String eventName = event.getType().getName();
117
118 if (eventName.startsWith(layout.eventSyscallEntryPrefix()) ||
119 eventName.startsWith(layout.eventCompatSyscallEntryPrefix())) {
120 /* This is a system call entry event */
121
122 Integer tid;
123 try {
124 tid = KernelTidAspect.INSTANCE.resolve(event, true, fMonitor);
125 } catch (InterruptedException e) {
126 return;
127 }
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();
136 String syscallName = eventName.substring(layout.eventSyscallEntryPrefix().length());
137
138 Map<String, String> args = event.getContent().getFieldNames().stream()
139 .collect(Collectors.toMap(Function.identity(),
140 input -> checkNotNull(event.getContent().getField(input).toString())));
141
142 SystemCall.InitialInfo newSysCall = new SystemCall.InitialInfo(startTime, checkNotNull(syscallName), checkNotNull(args));
143 fOngoingSystemCalls.put(tid, newSysCall);
144
145 } else if (eventName.startsWith(layout.eventSyscallExitPrefix())) {
146 /* This is a system call exit event */
147
148 Integer tid;
149 try {
150 tid = KernelTidAspect.INSTANCE.resolve(event, true, fMonitor);
151 } catch (InterruptedException e) {
152 return;
153 }
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);
170 getSegmentStore().add(syscall);
171 }
172 }
173
174 @Override
175 public void handleCompleted() {
176 fOngoingSystemCalls.clear();
177 super.handleCompleted();
178 }
179
180 @Override
181 public void handleCancel() {
182 fMonitor.setCanceled(true);
183 super.handleCancel();
184 }
185 }
186
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
201 public @Nullable Comparator<?> getComparator() {
202 return null;
203 }
204 @Override
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
213 }
This page took 0.040889 seconds and 4 git commands to generate.