1 /*******************************************************************************
2 * Copyright (c) 2015 EfficiOS Inc., Ericsson
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.latency
;
12 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
14 import java
.io
.IOException
;
15 import java
.io
.ObjectInputStream
;
16 import java
.util
.Collection
;
17 import java
.util
.Comparator
;
18 import java
.util
.HashMap
;
20 import java
.util
.function
.Function
;
21 import java
.util
.stream
.Collectors
;
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
;
27 import org
.eclipse
.tracecompass
.analysis
.timing
.core
.segmentstore
.AbstractSegmentStoreAnalysisModule
;
28 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegment
;
29 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegmentStore
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.segment
.ISegmentAspect
;
33 import com
.google
.common
.collect
.ImmutableList
;
36 * @author Alexandre Montplaisir
39 public class LatencyAnalysis
extends AbstractSegmentStoreAnalysisModule
{
42 * The ID of this analysis
44 public static final String ID
= "org.eclipse.tracecompass.analysis.os.linux.latency"; //$NON-NLS-1$
46 private static final String DATA_FILENAME
= "latency-analysis.dat"; //$NON-NLS-1$
48 private static final Collection
<ISegmentAspect
> BASE_ASPECTS
=
49 checkNotNull(ImmutableList
.of(SyscallNameAspect
.INSTANCE
));
52 public String
getId() {
57 public Iterable
<ISegmentAspect
> getSegmentAspects() {
62 public String
getDataFileName() {
67 public AbstractSegmentStoreAnalysisRequest
createAnalysisRequest(ISegmentStore
<ISegment
> syscalls
) {
68 return new SyscallLatencyAnalysisRequest(syscalls
);
72 protected Object
[] readObject(ObjectInputStream ois
) throws ClassNotFoundException
, IOException
{
73 return checkNotNull((Object
[]) ois
.readObject());
76 private static class SyscallLatencyAnalysisRequest
extends AbstractSegmentStoreAnalysisRequest
{
78 private final Map
<Integer
, SystemCall
.InitialInfo
> fOngoingSystemCalls
= new HashMap
<>();
79 private @Nullable IKernelAnalysisEventLayout fLayout
;
81 public SyscallLatencyAnalysisRequest(ISegmentStore
<ISegment
> syscalls
) {
86 public void handleData(final ITmfEvent event
) {
87 super.handleData(event
);
88 IKernelAnalysisEventLayout layout
= fLayout
;
90 IKernelTrace trace
= checkNotNull((IKernelTrace
) event
.getTrace());
91 layout
= trace
.getKernelEventLayout();
94 final String eventName
= event
.getType().getName();
96 if (eventName
.startsWith(layout
.eventSyscallEntryPrefix()) ||
97 eventName
.startsWith(layout
.eventCompatSyscallEntryPrefix())) {
98 /* This is a system call entry event */
100 Integer tid
= KernelTidAspect
.INSTANCE
.resolve(event
);
102 // no information on this event/trace ?
106 /* Record the event's data into the intial system call info */
107 // String syscallName = fLayout.getSyscallNameFromEvent(event);
108 long startTime
= event
.getTimestamp().getValue();
109 String syscallName
= eventName
.substring(layout
.eventSyscallEntryPrefix().length());
111 Map
<String
, String
> args
= event
.getContent().getFieldNames().stream()
112 .collect(Collectors
.toMap(Function
.identity(),
113 input
-> checkNotNull(event
.getContent().getField(input
).getValue().toString())));
115 SystemCall
.InitialInfo newSysCall
= new SystemCall
.InitialInfo(startTime
, checkNotNull(syscallName
), checkNotNull(args
));
116 fOngoingSystemCalls
.put(tid
, newSysCall
);
118 } else if (eventName
.startsWith(layout
.eventSyscallExitPrefix())) {
119 /* This is a system call exit event */
121 Integer tid
= KernelTidAspect
.INSTANCE
.resolve(event
);
126 SystemCall
.InitialInfo info
= fOngoingSystemCalls
.remove(tid
);
129 * We have not seen the entry event corresponding to this
130 * exit (lost event, or before start of trace).
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
);
138 getSegmentStore().add(syscall
);
143 public void handleCompleted() {
144 fOngoingSystemCalls
.clear();
148 private static class SyscallNameAspect
implements ISegmentAspect
{
149 public static final ISegmentAspect INSTANCE
= new SyscallNameAspect();
151 private SyscallNameAspect() { }
154 public String
getHelpText() {
155 return checkNotNull(Messages
.SegmentAspectHelpText_SystemCall
);
158 public String
getName() {
159 return checkNotNull(Messages
.SegmentAspectName_SystemCall
);
162 public @Nullable Comparator
<?
> getComparator() {
166 public @Nullable String
resolve(ISegment segment
) {
167 if (segment
instanceof SystemCall
) {
168 return ((SystemCall
) segment
).getName();