1 /*******************************************************************************
2 * Copyright (c) 2015 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
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
13 package org
.lttng
.scope
.lttng
.kernel
.core
.analysis
.os
.handlers
.internal
;
15 import static java
.util
.Objects
.requireNonNull
;
17 import org
.eclipse
.jdt
.annotation
.Nullable
;
18 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
19 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
20 import org
.lttng
.scope
.lttng
.kernel
.core
.analysis
.os
.Attributes
;
21 import org
.lttng
.scope
.lttng
.kernel
.core
.analysis
.os
.LinuxValues
;
22 import org
.lttng
.scope
.lttng
.kernel
.core
.analysis
.os
.StateValues
;
23 import org
.lttng
.scope
.lttng
.kernel
.core
.trace
.layout
.ILttngKernelEventLayout
;
25 import ca
.polymtl
.dorsal
.libdelorean
.ITmfStateSystemBuilder
;
26 import ca
.polymtl
.dorsal
.libdelorean
.exceptions
.AttributeNotFoundException
;
27 import ca
.polymtl
.dorsal
.libdelorean
.exceptions
.StateValueTypeException
;
28 import ca
.polymtl
.dorsal
.libdelorean
.statevalue
.ITmfStateValue
;
29 import ca
.polymtl
.dorsal
.libdelorean
.statevalue
.TmfStateValue
;
32 * LTTng Specific state dump event handler
34 public class StateDumpHandler
extends KernelEventHandler
{
42 public StateDumpHandler(ILttngKernelEventLayout layout
) {
47 public void handleEvent(ITmfStateSystemBuilder ss
, ITmfEvent event
) throws AttributeNotFoundException
{
48 ITmfEventField content
= event
.getContent();
49 Integer eventCpu
= KernelEventHandlerUtils
.getCpu(event
);
50 int tid
= ((Long
) content
.getField("tid").getValue()).intValue(); //$NON-NLS-1$
51 int pid
= ((Long
) content
.getField("pid").getValue()).intValue(); //$NON-NLS-1$
52 int ppid
= ((Long
) content
.getField("ppid").getValue()).intValue(); //$NON-NLS-1$
53 int status
= ((Long
) content
.getField("status").getValue()).intValue(); //$NON-NLS-1$
54 String name
= requireNonNull((String
) content
.getField("name").getValue()); //$NON-NLS-1$
55 /* Only present in LTTng 2.10+ */
56 @Nullable Long cpuField
= content
.getFieldValue(Long
.class, "cpu"); //$NON-NLS-1$
58 * "mode" could be interesting too, but it doesn't seem to be populated
59 * with anything relevant for now.
62 String threadAttributeName
= Attributes
.buildThreadAttributeName(tid
, eventCpu
);
63 if (threadAttributeName
== null) {
67 int curThreadNode
= ss
.getQuarkRelativeAndAdd(KernelEventHandlerUtils
.getNodeThreads(ss
), threadAttributeName
);
68 long timestamp
= KernelEventHandlerUtils
.getTimestamp(event
);
69 /* Set the process' name */
70 setProcessName(ss
, name
, curThreadNode
, timestamp
);
72 /* Set the process' PPID */
73 setPpid(ss
, tid
, pid
, ppid
, curThreadNode
, timestamp
);
75 /* Set the process' status */
76 setStatus(ss
, status
, curThreadNode
, cpuField
, timestamp
);
79 private static void setStatus(ITmfStateSystemBuilder ss
, int status
, int curThreadNode
, @Nullable Long cpu
, long timestamp
) {
81 if (ss
.queryOngoingState(curThreadNode
).isNull()) {
83 case LinuxValues
.STATEDUMP_PROCESS_STATUS_WAIT_CPU
:
84 value
= StateValues
.PROCESS_STATUS_WAIT_FOR_CPU_VALUE
;
85 setRunQueue(ss
, curThreadNode
, cpu
, timestamp
);
87 case LinuxValues
.STATEDUMP_PROCESS_STATUS_WAIT
:
89 * We have no information on what the process is waiting on
90 * (unlike a sched_switch for example), so we will use the
91 * WAIT_UNKNOWN state instead of the "normal" WAIT_BLOCKED
94 value
= StateValues
.PROCESS_STATUS_WAIT_UNKNOWN_VALUE
;
97 value
= StateValues
.PROCESS_STATUS_UNKNOWN_VALUE
;
99 ss
.modifyAttribute(timestamp
, value
, curThreadNode
);
103 private static void setRunQueue(ITmfStateSystemBuilder ss
, int curThreadNode
, @Nullable Long cpu
, long timestamp
) {
105 int quark
= ss
.getQuarkRelativeAndAdd(curThreadNode
, Attributes
.CURRENT_CPU_RQ
);
106 ITmfStateValue value
= TmfStateValue
.newValueInt(cpu
.intValue());
107 ss
.modifyAttribute(timestamp
, value
, quark
);
111 private static void setPpid(ITmfStateSystemBuilder ss
, int tid
, int pid
, int ppid
, int curThreadNode
, long timestamp
)
112 throws StateValueTypeException
, AttributeNotFoundException
{
113 ITmfStateValue value
;
115 quark
= ss
.getQuarkRelativeAndAdd(curThreadNode
, Attributes
.PPID
);
116 if (ss
.queryOngoingState(quark
).isNull()) {
118 /* We have a process. Use the 'PPID' field. */
119 value
= TmfStateValue
.newValueInt(ppid
);
121 /* We have a thread, use the 'PID' field for the parent. */
122 value
= TmfStateValue
.newValueInt(pid
);
124 ss
.modifyAttribute(timestamp
, value
, quark
);
128 private static void setProcessName(ITmfStateSystemBuilder ss
, String name
, int curThreadNode
, long timestamp
)
129 throws StateValueTypeException
, AttributeNotFoundException
{
130 ITmfStateValue value
;
131 int quark
= ss
.getQuarkRelativeAndAdd(curThreadNode
, Attributes
.EXEC_NAME
);
132 if (ss
.queryOngoingState(quark
).isNull()) {
133 /* If the value didn't exist previously, set it */
134 value
= TmfStateValue
.newValueString(name
);
135 ss
.modifyAttribute(timestamp
, value
, quark
);