os.linux: Handle cpu field in statedump events
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.core / src / org / eclipse / tracecompass / internal / analysis / os / linux / core / kernel / handlers / StateDumpHandler.java
CommitLineData
c8f45ad2
MK
1/*******************************************************************************
2 * Copyright (c) 2015 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 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers;
14
15import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
b054b99a 17import org.eclipse.jdt.annotation.Nullable;
0f7a12d3
AM
18import org.eclipse.tracecompass.analysis.os.linux.core.kernel.LinuxValues;
19import org.eclipse.tracecompass.analysis.os.linux.core.kernel.StateValues;
c8f45ad2 20import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelAnalysisEventLayout;
f69045e2 21import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.Attributes;
c8f45ad2
MK
22import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
23import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
24import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
25import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
26import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
27import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
28
29/**
30 * LTTng Specific state dump event handler
31 */
32public class StateDumpHandler extends KernelEventHandler {
33
34 /**
35 * Constructor
36 *
37 * @param layout
38 * event layout
39 */
40 public StateDumpHandler(IKernelAnalysisEventLayout layout) {
41 super(layout);
42 }
43
44 @Override
45 public void handleEvent(ITmfStateSystemBuilder ss, ITmfEvent event) throws AttributeNotFoundException {
46 ITmfEventField content = event.getContent();
b054b99a 47 Integer eventCpu = KernelEventHandlerUtils.getCpu(event);
c8f45ad2
MK
48 int tid = ((Long) content.getField("tid").getValue()).intValue(); //$NON-NLS-1$
49 int pid = ((Long) content.getField("pid").getValue()).intValue(); //$NON-NLS-1$
50 int ppid = ((Long) content.getField("ppid").getValue()).intValue(); //$NON-NLS-1$
51 int status = ((Long) content.getField("status").getValue()).intValue(); //$NON-NLS-1$
52 String name = checkNotNull((String) content.getField("name").getValue()); //$NON-NLS-1$
b054b99a
AM
53 /* Only present in LTTng 2.10+ */
54 @Nullable Long cpuField = content.getFieldValue(Long.class, "cpu"); //$NON-NLS-1$
c8f45ad2
MK
55 /*
56 * "mode" could be interesting too, but it doesn't seem to be populated
57 * with anything relevant for now.
58 */
59
b054b99a 60 String threadAttributeName = Attributes.buildThreadAttributeName(tid, eventCpu);
8a0bbebf
MJ
61 if (threadAttributeName == null) {
62 return;
63 }
64
65 int curThreadNode = ss.getQuarkRelativeAndAdd(KernelEventHandlerUtils.getNodeThreads(ss), threadAttributeName);
c8f45ad2
MK
66 long timestamp = KernelEventHandlerUtils.getTimestamp(event);
67 /* Set the process' name */
68 setProcessName(ss, name, curThreadNode, timestamp);
69
70 /* Set the process' PPID */
71 setPpid(ss, tid, pid, ppid, curThreadNode, timestamp);
72
73 /* Set the process' status */
b054b99a 74 setStatus(ss, status, curThreadNode, cpuField, timestamp);
c8f45ad2
MK
75 }
76
b054b99a 77 private static void setStatus(ITmfStateSystemBuilder ss, int status, int curThreadNode, @Nullable Long cpu, long timestamp) {
c8f45ad2 78 ITmfStateValue value;
d3cc952f 79 if (ss.queryOngoingState(curThreadNode).isNull()) {
c8f45ad2
MK
80 switch (status) {
81 case LinuxValues.STATEDUMP_PROCESS_STATUS_WAIT_CPU:
82 value = StateValues.PROCESS_STATUS_WAIT_FOR_CPU_VALUE;
b054b99a 83 setRunQueue(ss, curThreadNode, cpu, timestamp);
c8f45ad2
MK
84 break;
85 case LinuxValues.STATEDUMP_PROCESS_STATUS_WAIT:
86 /*
87 * We have no information on what the process is waiting on
88 * (unlike a sched_switch for example), so we will use the
89 * WAIT_UNKNOWN state instead of the "normal" WAIT_BLOCKED
90 * state.
91 */
92 value = StateValues.PROCESS_STATUS_WAIT_UNKNOWN_VALUE;
93 break;
94 default:
95 value = StateValues.PROCESS_STATUS_UNKNOWN_VALUE;
96 }
d3cc952f 97 ss.modifyAttribute(timestamp, value, curThreadNode);
c8f45ad2
MK
98 }
99 }
100
b054b99a
AM
101 private static void setRunQueue(ITmfStateSystemBuilder ss, int curThreadNode, @Nullable Long cpu, long timestamp) {
102 if (cpu != null) {
103 int quark = ss.getQuarkRelativeAndAdd(curThreadNode, Attributes.CURRENT_CPU_RQ);
104 ITmfStateValue value = TmfStateValue.newValueInt(cpu.intValue());
105 ss.modifyAttribute(timestamp, value, quark);
106 }
107 }
108
5ae5a500 109 private static void setPpid(ITmfStateSystemBuilder ss, int tid, int pid, int ppid, int curThreadNode, long timestamp) {
c8f45ad2
MK
110 ITmfStateValue value;
111 int quark;
112 quark = ss.getQuarkRelativeAndAdd(curThreadNode, Attributes.PPID);
113 if (ss.queryOngoingState(quark).isNull()) {
114 if (pid == tid) {
115 /* We have a process. Use the 'PPID' field. */
116 value = TmfStateValue.newValueInt(ppid);
117 } else {
118 /* We have a thread, use the 'PID' field for the parent. */
119 value = TmfStateValue.newValueInt(pid);
120 }
121 ss.modifyAttribute(timestamp, value, quark);
122 }
123 }
124
5ae5a500 125 private static void setProcessName(ITmfStateSystemBuilder ss, String name, int curThreadNode, long timestamp) {
c8f45ad2
MK
126 ITmfStateValue value;
127 int quark = ss.getQuarkRelativeAndAdd(curThreadNode, Attributes.EXEC_NAME);
128 if (ss.queryOngoingState(quark).isNull()) {
129 /* If the value didn't exist previously, set it */
130 value = TmfStateValue.newValueString(name);
131 ss.modifyAttribute(timestamp, value, quark);
132 }
133 }
134}
This page took 0.042127 seconds and 5 git commands to generate.