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