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