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