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