| 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.lttng.scope.lttng.kernel.core.analysis.os.handlers.internal; |
| 14 | |
| 15 | import static java.util.Objects.requireNonNull; |
| 16 | |
| 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; |
| 24 | |
| 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; |
| 30 | |
| 31 | /** |
| 32 | * LTTng Specific state dump event handler |
| 33 | */ |
| 34 | public class StateDumpHandler extends KernelEventHandler { |
| 35 | |
| 36 | /** |
| 37 | * Constructor |
| 38 | * |
| 39 | * @param layout |
| 40 | * event layout |
| 41 | */ |
| 42 | public StateDumpHandler(ILttngKernelEventLayout layout) { |
| 43 | super(layout); |
| 44 | } |
| 45 | |
| 46 | @Override |
| 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$ |
| 57 | /* |
| 58 | * "mode" could be interesting too, but it doesn't seem to be populated |
| 59 | * with anything relevant for now. |
| 60 | */ |
| 61 | |
| 62 | String threadAttributeName = Attributes.buildThreadAttributeName(tid, eventCpu); |
| 63 | if (threadAttributeName == null) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 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); |
| 71 | |
| 72 | /* Set the process' PPID */ |
| 73 | setPpid(ss, tid, pid, ppid, curThreadNode, timestamp); |
| 74 | |
| 75 | /* Set the process' status */ |
| 76 | setStatus(ss, status, curThreadNode, cpuField, timestamp); |
| 77 | } |
| 78 | |
| 79 | private static void setStatus(ITmfStateSystemBuilder ss, int status, int curThreadNode, @Nullable Long cpu, long timestamp) { |
| 80 | ITmfStateValue value; |
| 81 | if (ss.queryOngoingState(curThreadNode).isNull()) { |
| 82 | switch (status) { |
| 83 | case LinuxValues.STATEDUMP_PROCESS_STATUS_WAIT_CPU: |
| 84 | value = StateValues.PROCESS_STATUS_WAIT_FOR_CPU_VALUE; |
| 85 | setRunQueue(ss, curThreadNode, cpu, timestamp); |
| 86 | break; |
| 87 | case LinuxValues.STATEDUMP_PROCESS_STATUS_WAIT: |
| 88 | /* |
| 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 |
| 92 | * state. |
| 93 | */ |
| 94 | value = StateValues.PROCESS_STATUS_WAIT_UNKNOWN_VALUE; |
| 95 | break; |
| 96 | default: |
| 97 | value = StateValues.PROCESS_STATUS_UNKNOWN_VALUE; |
| 98 | } |
| 99 | ss.modifyAttribute(timestamp, value, curThreadNode); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private static void setRunQueue(ITmfStateSystemBuilder ss, int curThreadNode, @Nullable Long cpu, long timestamp) { |
| 104 | if (cpu != null) { |
| 105 | int quark = ss.getQuarkRelativeAndAdd(curThreadNode, Attributes.CURRENT_CPU_RQ); |
| 106 | ITmfStateValue value = TmfStateValue.newValueInt(cpu.intValue()); |
| 107 | ss.modifyAttribute(timestamp, value, quark); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private static void setPpid(ITmfStateSystemBuilder ss, int tid, int pid, int ppid, int curThreadNode, long timestamp) |
| 112 | throws StateValueTypeException, AttributeNotFoundException { |
| 113 | ITmfStateValue value; |
| 114 | int quark; |
| 115 | quark = ss.getQuarkRelativeAndAdd(curThreadNode, Attributes.PPID); |
| 116 | if (ss.queryOngoingState(quark).isNull()) { |
| 117 | if (pid == tid) { |
| 118 | /* We have a process. Use the 'PPID' field. */ |
| 119 | value = TmfStateValue.newValueInt(ppid); |
| 120 | } else { |
| 121 | /* We have a thread, use the 'PID' field for the parent. */ |
| 122 | value = TmfStateValue.newValueInt(pid); |
| 123 | } |
| 124 | ss.modifyAttribute(timestamp, value, quark); |
| 125 | } |
| 126 | } |
| 127 | |
| 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); |
| 136 | } |
| 137 | } |
| 138 | } |