| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012 Ericsson |
| 3 | * Copyright (c) 2010, 2011 École Polytechnique de Montréal |
| 4 | * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com> |
| 5 | * |
| 6 | * All rights reserved. This program and the accompanying materials are |
| 7 | * made available under the terms of the Eclipse Public License v1.0 which |
| 8 | * accompanies this distribution, and is available at |
| 9 | * http://www.eclipse.org/legal/epl-v10.html |
| 10 | * |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.internal.lttng2.kernel.core.stateprovider; |
| 14 | |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.HashMap; |
| 17 | import java.util.List; |
| 18 | import java.util.concurrent.BlockingQueue; |
| 19 | |
| 20 | import org.eclipse.linuxtools.internal.lttng2.kernel.core.Attributes; |
| 21 | import org.eclipse.linuxtools.internal.lttng2.kernel.core.LttngStrings; |
| 22 | import org.eclipse.linuxtools.internal.lttng2.kernel.core.StateValues; |
| 23 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent; |
| 24 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; |
| 25 | import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException; |
| 26 | import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException; |
| 27 | import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException; |
| 28 | import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemBuilder; |
| 29 | import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue; |
| 30 | import org.eclipse.linuxtools.tmf.core.statevalue.TmfStateValue; |
| 31 | |
| 32 | /** |
| 33 | * This is the reference "state provider" for LTTng 2.0 kernel traces. |
| 34 | * |
| 35 | * @author alexmont |
| 36 | * |
| 37 | */ |
| 38 | class CtfKernelHandler implements Runnable { |
| 39 | |
| 40 | private final BlockingQueue<CtfTmfEvent> inQueue; |
| 41 | private IStateSystemBuilder ss; |
| 42 | |
| 43 | private CtfTmfEvent currentEvent; |
| 44 | |
| 45 | /* |
| 46 | * We can keep handles to some Attribute Nodes so these don't need to be |
| 47 | * re-found (re-hashed Strings etc.) every new event |
| 48 | */ |
| 49 | List<Integer> currentCPUNodes; |
| 50 | List<Integer> currentThreadNodes; |
| 51 | |
| 52 | /* Event names HashMap. TODO: This can be discarded once we move to Java 7 */ |
| 53 | private final HashMap<String, Integer> knownEventNames; |
| 54 | |
| 55 | /* Common locations in the attribute tree */ |
| 56 | private int cpusNode = -1; |
| 57 | private int threadsNode = -1; |
| 58 | private int irqsNode = -1; |
| 59 | private int softIrqsNode = -1; |
| 60 | |
| 61 | CtfKernelHandler(BlockingQueue<CtfTmfEvent> eventsQueue) { |
| 62 | assert (eventsQueue != null); |
| 63 | this.inQueue = eventsQueue; |
| 64 | currentCPUNodes = new ArrayList<Integer>(); |
| 65 | currentThreadNodes = new ArrayList<Integer>(); |
| 66 | |
| 67 | knownEventNames = fillEventNames(); |
| 68 | } |
| 69 | |
| 70 | void assignStateSystem(IStateSystemBuilder targetSS) { |
| 71 | this.ss = targetSS; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void run() { |
| 76 | if (ss == null) { |
| 77 | System.err.println("Cannot run event manager without assigning a target state system first!"); //$NON-NLS-1$ |
| 78 | return; |
| 79 | } |
| 80 | CtfTmfEvent event; |
| 81 | setupCommonLocations(); |
| 82 | |
| 83 | try { |
| 84 | event = inQueue.take(); |
| 85 | while (event.getTimestampValue() != -1) { |
| 86 | processEvent(event); |
| 87 | event = inQueue.take(); |
| 88 | } |
| 89 | /* We've received the last event, clean up */ |
| 90 | closeStateSystem(); |
| 91 | return; |
| 92 | } catch (InterruptedException e) { |
| 93 | /* We've been interrupted abnormally */ |
| 94 | System.out.println("Event handler interrupted!"); //$NON-NLS-1$ |
| 95 | e.printStackTrace(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private void closeStateSystem() { |
| 100 | /* Close the History system, if there is one */ |
| 101 | if (currentEvent == null) { |
| 102 | return; |
| 103 | } |
| 104 | try { |
| 105 | ss.closeHistory(currentEvent.getTimestamp().getValue()); |
| 106 | } catch (TimeRangeException e) { |
| 107 | /* |
| 108 | * Since we're using currentEvent.getTimestamp, this shouldn't |
| 109 | * cause any problem |
| 110 | */ |
| 111 | e.printStackTrace(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private void processEvent(CtfTmfEvent event) { |
| 116 | currentEvent = event; |
| 117 | ITmfEventField content = event.getContent(); |
| 118 | String eventName = event.getEventName(); |
| 119 | |
| 120 | long ts = event.getTimestamp().getValue(); |
| 121 | int quark; |
| 122 | ITmfStateValue value; |
| 123 | Integer eventCpu = event.getCPU(); |
| 124 | Integer currentCPUNode, currentThreadNode; |
| 125 | |
| 126 | /* Adjust the current nodes Vectors if we see a new CPU in an event */ |
| 127 | if (eventCpu >= currentCPUNodes.size()) { |
| 128 | /* We need to add this node to the vector */ |
| 129 | for (Integer i = currentCPUNodes.size(); i < eventCpu + 1; i++) { |
| 130 | quark = ss.getQuarkRelativeAndAdd(cpusNode, i.toString()); |
| 131 | currentCPUNodes.add(quark); |
| 132 | |
| 133 | quark = ss.getQuarkRelativeAndAdd(threadsNode, Attributes.UNKNOWN); |
| 134 | currentThreadNodes.add(quark); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | currentCPUNode = currentCPUNodes.get(eventCpu); |
| 139 | currentThreadNode = currentThreadNodes.get(eventCpu); |
| 140 | assert (currentCPUNode != null); |
| 141 | assert (currentThreadNode != null); |
| 142 | |
| 143 | try { |
| 144 | /* |
| 145 | * Feed event to the history system if it's known to cause a state |
| 146 | * transition. |
| 147 | */ |
| 148 | switch (getEventIndex(eventName)) { |
| 149 | |
| 150 | case 1: // "exit_syscall": |
| 151 | /* Fields: int64 ret */ |
| 152 | { |
| 153 | /* Clear the current system call on the process */ |
| 154 | quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.SYSTEM_CALL); |
| 155 | value = TmfStateValue.nullValue(); |
| 156 | ss.modifyAttribute(ts, value, quark); |
| 157 | |
| 158 | /* Put the process' status back to user mode */ |
| 159 | quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.STATUS); |
| 160 | value = TmfStateValue.newValueInt(StateValues.PROCESS_STATUS_RUN_USERMODE); |
| 161 | ss.modifyAttribute(ts, value, quark); |
| 162 | |
| 163 | /* Put the CPU's status back to user mode */ |
| 164 | quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.STATUS); |
| 165 | value = TmfStateValue.newValueInt(StateValues.CPU_STATUS_RUN_USERMODE); |
| 166 | ss.modifyAttribute(ts, value, quark); |
| 167 | } |
| 168 | break; |
| 169 | |
| 170 | case 2: // "irq_handler_entry": |
| 171 | /* Fields: int32 irq, string name */ |
| 172 | { |
| 173 | Integer irqId = ((Long) content.getField(LttngStrings.IRQ).getValue()).intValue(); |
| 174 | |
| 175 | /* Mark this IRQ as active in the resource tree. |
| 176 | * The state value = the CPU on which this IRQ is sitting */ |
| 177 | quark = ss.getQuarkRelativeAndAdd(irqsNode, irqId.toString()); |
| 178 | value = TmfStateValue.newValueInt(event.getCPU()); |
| 179 | ss.modifyAttribute(ts, value, quark); |
| 180 | |
| 181 | /* Change the status of the running process to interrupted */ |
| 182 | quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.STATUS); |
| 183 | value = TmfStateValue.newValueInt(StateValues.PROCESS_STATUS_INTERRUPTED); |
| 184 | ss.modifyAttribute(ts, value, quark); |
| 185 | \r |
| 186 | /* Change the status of the CPU to interrupted */\r |
| 187 | quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.STATUS);\r |
| 188 | value = TmfStateValue.newValueInt(StateValues.CPU_STATUS_IRQ);\r |
| 189 | ss.modifyAttribute(ts, value, quark);\r |
| 190 | } |
| 191 | break; |
| 192 | |
| 193 | case 3: // "irq_handler_exit": |
| 194 | /* Fields: int32 irq, int32 ret */ |
| 195 | { |
| 196 | Integer irqId = ((Long) content.getField(LttngStrings.IRQ).getValue()).intValue(); |
| 197 | |
| 198 | /* Put this IRQ back to inactive in the resource tree */ |
| 199 | quark = ss.getQuarkRelativeAndAdd(irqsNode, irqId.toString()); |
| 200 | value = TmfStateValue.nullValue(); |
| 201 | ss.modifyAttribute(ts, value, quark); |
| 202 | |
| 203 | /* Set the previous process back to running */ |
| 204 | setProcessToRunning(ts, currentThreadNode); |
| 205 | \r |
| 206 | /* Set the CPU status back to running or "idle" */\r |
| 207 | cpuExitInterrupt(ts, currentCPUNode, currentThreadNode);\r |
| 208 | } |
| 209 | break; |
| 210 | |
| 211 | case 4: // "softirq_entry": |
| 212 | /* Fields: int32 vec */ |
| 213 | { |
| 214 | Integer softIrqId = ((Long) content.getField(LttngStrings.VEC).getValue()).intValue(); |
| 215 | |
| 216 | /* Mark this SoftIRQ as active in the resource tree. |
| 217 | * The state value = the CPU on which this SoftIRQ is processed */ |
| 218 | quark = ss.getQuarkRelativeAndAdd(softIrqsNode, softIrqId.toString()); |
| 219 | value = TmfStateValue.newValueInt(event.getCPU()); |
| 220 | ss.modifyAttribute(ts, value, quark); |
| 221 | |
| 222 | /* Change the status of the running process to interrupted */ |
| 223 | quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.STATUS); |
| 224 | value = TmfStateValue.newValueInt(StateValues.PROCESS_STATUS_INTERRUPTED); |
| 225 | ss.modifyAttribute(ts, value, quark); |
| 226 | \r |
| 227 | /* Change the status of the CPU to interrupted */\r |
| 228 | quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.STATUS);\r |
| 229 | value = TmfStateValue.newValueInt(StateValues.CPU_STATUS_SOFTIRQ);\r |
| 230 | ss.modifyAttribute(ts, value, quark);\r |
| 231 | } |
| 232 | break; |
| 233 | |
| 234 | case 5: // "softirq_exit": |
| 235 | /* Fields: int32 vec */ |
| 236 | { |
| 237 | Integer softIrqId = ((Long) content.getField(LttngStrings.VEC).getValue()).intValue(); |
| 238 | |
| 239 | /* Put this SoftIRQ back to inactive (= -1) in the resource tree */ |
| 240 | quark = ss.getQuarkRelativeAndAdd(softIrqsNode, softIrqId.toString()); |
| 241 | value = TmfStateValue.nullValue(); |
| 242 | ss.modifyAttribute(ts, value, quark); |
| 243 | |
| 244 | /* Set the previous process back to running */ |
| 245 | setProcessToRunning(ts, currentThreadNode); |
| 246 | \r |
| 247 | /* Set the CPU status back to "busy" or "idle" */\r |
| 248 | cpuExitInterrupt(ts, currentCPUNode, currentThreadNode);\r |
| 249 | } |
| 250 | break; |
| 251 | |
| 252 | case 6: // "softirq_raise": |
| 253 | /* Fields: int32 vec */ |
| 254 | { |
| 255 | Integer softIrqId = ((Long) content.getField(LttngStrings.VEC).getValue()).intValue(); |
| 256 | |
| 257 | /* Mark this SoftIRQ as *raised* in the resource tree. |
| 258 | * State value = -2 */ |
| 259 | quark = ss.getQuarkRelativeAndAdd(softIrqsNode, softIrqId.toString()); |
| 260 | value = TmfStateValue.newValueInt(StateValues.SOFT_IRQ_RAISED); |
| 261 | ss.modifyAttribute(ts, value, quark); |
| 262 | } |
| 263 | break; |
| 264 | |
| 265 | case 7: // "sched_switch": |
| 266 | /* |
| 267 | * Fields: string prev_comm, int32 prev_tid, int32 prev_prio, int64 prev_state, |
| 268 | * string next_comm, int32 next_tid, int32 next_prio |
| 269 | */ |
| 270 | { |
| 271 | Integer prevTid = ((Long) content.getField(LttngStrings.PREV_TID).getValue()).intValue(); |
| 272 | //Long prevState = (Long) content.getField(LttngStrings.PREV_STATE).getValue(); |
| 273 | String nextProcessName = (String) content.getField(LttngStrings.NEXT_COMM).getValue(); |
| 274 | Integer nextTid = ((Long) content.getField(LttngStrings.NEXT_TID).getValue()).intValue(); |
| 275 | |
| 276 | Integer formerThreadNode = ss.getQuarkRelativeAndAdd(threadsNode, prevTid.toString()); |
| 277 | Integer newCurrentThreadNode = ss.getQuarkRelativeAndAdd(threadsNode, nextTid.toString()); |
| 278 | |
| 279 | /* Update the currentThreadNodes pointer */ |
| 280 | currentThreadNodes.set(eventCpu, newCurrentThreadNode); |
| 281 | |
| 282 | /* Set the status of the process that got scheduled out. */ |
| 283 | quark = ss.getQuarkRelativeAndAdd(formerThreadNode, Attributes.STATUS); |
| 284 | value = TmfStateValue.newValueInt(StateValues.PROCESS_STATUS_WAIT); |
| 285 | ss.modifyAttribute(ts, value, quark); |
| 286 | |
| 287 | /* Set the status of the new scheduled process */ |
| 288 | setProcessToRunning(ts, newCurrentThreadNode); |
| 289 | |
| 290 | /* Set the exec name of the new process */ |
| 291 | quark = ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.EXEC_NAME); |
| 292 | value = TmfStateValue.newValueString(nextProcessName); |
| 293 | ss.modifyAttribute(ts, value, quark); |
| 294 | |
| 295 | /* |
| 296 | * Check if we need to set the syscall state and the PPID of |
| 297 | * the new process (in case we haven't seen this process before) |
| 298 | */ |
| 299 | quark = ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.SYSTEM_CALL); |
| 300 | if (quark == ss.getNbAttributes()) { /* Did we just add this attribute? */ |
| 301 | value = TmfStateValue.nullValue(); |
| 302 | ss.modifyAttribute(ts, value, quark); |
| 303 | } |
| 304 | quark = ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.PPID); |
| 305 | if (quark == ss.getNbAttributes()) { |
| 306 | value = TmfStateValue.nullValue(); |
| 307 | ss.modifyAttribute(ts, value, quark); |
| 308 | } |
| 309 | |
| 310 | /* Set the current scheduled process on the relevant CPU */ |
| 311 | quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.CURRENT_THREAD); |
| 312 | value = TmfStateValue.newValueInt(nextTid); |
| 313 | ss.modifyAttribute(ts, value, quark); |
| 314 | \r |
| 315 | /* Set the status of the CPU itself */ |
| 316 | if (nextTid > 0) { |
| 317 | /* Check if the entering process is in kernel or user mode */ |
| 318 | quark = ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.SYSTEM_CALL); |
| 319 | if (ss.queryOngoingState(quark).isNull()) { |
| 320 | value = TmfStateValue.newValueInt(StateValues.CPU_STATUS_RUN_USERMODE); |
| 321 | } else { |
| 322 | value = TmfStateValue.newValueInt(StateValues.CPU_STATUS_RUN_SYSCALL); |
| 323 | }\r |
| 324 | } else {\r |
| 325 | value = TmfStateValue.newValueInt(StateValues.CPU_STATUS_IDLE);\r |
| 326 | } |
| 327 | quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.STATUS); |
| 328 | ss.modifyAttribute(ts, value, quark);\r |
| 329 | } |
| 330 | break; |
| 331 | |
| 332 | case 8: // "sched_process_fork": |
| 333 | /* Fields: string parent_comm, int32 parent_tid, |
| 334 | * string child_comm, int32 child_tid */ |
| 335 | { |
| 336 | // String parentProcessName = (String) event.getFieldValue("parent_comm"); |
| 337 | String childProcessName = (String) content.getField(LttngStrings.CHILD_COMM).getValue(); |
| 338 | // assert ( parentProcessName.equals(childProcessName) ); |
| 339 | |
| 340 | Integer parentTid = ((Long) content.getField(LttngStrings.PARENT_TID).getValue()).intValue(); |
| 341 | Integer childTid = ((Long) content.getField(LttngStrings.CHILD_TID).getValue()).intValue(); |
| 342 | |
| 343 | Integer parentTidNode = ss.getQuarkRelativeAndAdd(threadsNode, parentTid.toString()); |
| 344 | Integer childTidNode = ss.getQuarkRelativeAndAdd(threadsNode, childTid.toString()); |
| 345 | |
| 346 | /* Assign the PPID to the new process */ |
| 347 | quark = ss.getQuarkRelativeAndAdd(childTidNode, Attributes.PPID); |
| 348 | value = TmfStateValue.newValueInt(parentTid); |
| 349 | ss.modifyAttribute(ts, value, quark); |
| 350 | |
| 351 | /* Set the new process' exec_name */ |
| 352 | quark = ss.getQuarkRelativeAndAdd(childTidNode, Attributes.EXEC_NAME); |
| 353 | value = TmfStateValue.newValueString(childProcessName); |
| 354 | ss.modifyAttribute(ts, value, quark); |
| 355 | |
| 356 | /* Set the new process' status */ |
| 357 | quark = ss.getQuarkRelativeAndAdd(childTidNode, Attributes.STATUS); |
| 358 | value = TmfStateValue.newValueInt(StateValues.PROCESS_STATUS_WAIT); |
| 359 | ss.modifyAttribute(ts, value, quark); |
| 360 | |
| 361 | /* Set the process' syscall name, to be the same as the parent's */ |
| 362 | quark = ss.getQuarkRelativeAndAdd(parentTidNode, Attributes.SYSTEM_CALL); |
| 363 | value = ss.queryOngoingState(quark); |
| 364 | quark = ss.getQuarkRelativeAndAdd(childTidNode, Attributes.SYSTEM_CALL); |
| 365 | ss.modifyAttribute(ts, value, quark); |
| 366 | } |
| 367 | break; |
| 368 | |
| 369 | case 9: // "sched_process_exit": |
| 370 | /* Fields: string comm, int32 tid, int32 prio */ |
| 371 | break; |
| 372 | |
| 373 | case 10: // "sched_process_free": |
| 374 | /* Fields: string comm, int32 tid, int32 prio */ |
| 375 | /* |
| 376 | * A sched_process_free will always happen after the sched_switch |
| 377 | * that will remove the process from the cpu for the last time. So |
| 378 | * this is when we should delete everything wrt to the process. |
| 379 | */ |
| 380 | { |
| 381 | Integer tid = ((Long) content.getField(LttngStrings.TID).getValue()).intValue(); |
| 382 | /* |
| 383 | * Remove the process and all its sub-attributes from the |
| 384 | * current state |
| 385 | */ |
| 386 | quark = ss.getQuarkRelativeAndAdd(threadsNode, tid.toString()); |
| 387 | ss.removeAttribute(ts, quark); |
| 388 | } |
| 389 | break; |
| 390 | |
| 391 | // FIXME In CTF it's as "syscall_exec". Will have to be adapted. |
| 392 | // case LTT_EVENT_EXEC: |
| 393 | // filename = new String((byte[]) event.getField(0)); |
| 394 | // |
| 395 | // /* Change the Exec_name of the process */ |
| 396 | // quark = ss.getQuarkRelativePath(true, currentThreadNode, |
| 397 | // "Exec_name"); |
| 398 | // ss.modifyAttribute(ts, filename, quark); |
| 399 | // break; |
| 400 | |
| 401 | default: |
| 402 | /* Other event types not covered by the main switch */ |
| 403 | { |
| 404 | if (eventName.startsWith(LttngStrings.SYSCALL_PREFIX) |
| 405 | || eventName.startsWith(LttngStrings.COMPAT_SYSCALL_PREFIX)) { |
| 406 | /* |
| 407 | * This is a replacement for the old sys_enter event. Now |
| 408 | * syscall names are listed into the event type |
| 409 | */ |
| 410 | |
| 411 | /* Assign the new system call to the process */ |
| 412 | quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.SYSTEM_CALL); |
| 413 | value = TmfStateValue.newValueString(eventName); |
| 414 | ss.modifyAttribute(ts, value, quark); |
| 415 | |
| 416 | /* Put the process in system call mode */ |
| 417 | quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.STATUS); |
| 418 | value = TmfStateValue.newValueInt(StateValues.PROCESS_STATUS_RUN_SYSCALL); |
| 419 | ss.modifyAttribute(ts, value, quark); |
| 420 | |
| 421 | /* Put the CPU in system call (kernel) mode */ |
| 422 | quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.STATUS); |
| 423 | value = TmfStateValue.newValueInt(StateValues.CPU_STATUS_RUN_SYSCALL); |
| 424 | ss.modifyAttribute(ts, value, quark); |
| 425 | } |
| 426 | } |
| 427 | break; |
| 428 | } // End of big switch |
| 429 | |
| 430 | /* |
| 431 | * Statistics |
| 432 | */ |
| 433 | |
| 434 | /* Number of events of each type, globally */ |
| 435 | // quark = ss.getQuarkAbsoluteAndAdd(Attributes.STATISTICS, |
| 436 | // Attributes.EVENT_TYPES, eventName); |
| 437 | // ss.incrementAttribute(ts, quark); |
| 438 | |
| 439 | /* Number of events per CPU */ |
| 440 | // quark = ss.getQuarkRelativeAndAdd(currentCPUNode, |
| 441 | // Attributes.STATISTICS, Attributes.EVENT_TYPES, eventName); |
| 442 | // ss.incrementAttribute(ts, quark); |
| 443 | |
| 444 | /* Number of events per process */ |
| 445 | // quark = ss.getQuarkRelativeAndAdd(currentThreadNode, |
| 446 | // Attributes.STATISTICS, Attributes.EVENT_TYPES, eventName); |
| 447 | // ss.incrementAttribute(ts, quark); |
| 448 | |
| 449 | } catch (AttributeNotFoundException ae) { |
| 450 | /* |
| 451 | * This would indicate a problem with the logic of the manager here, |
| 452 | * so it shouldn't happen. |
| 453 | */ |
| 454 | ae.printStackTrace(); |
| 455 | |
| 456 | } catch (TimeRangeException tre) { |
| 457 | /* |
| 458 | * This would happen if the events in the trace aren't ordered |
| 459 | * chronologically, which should never be the case ... |
| 460 | */ |
| 461 | System.err.println("TimeRangeExcpetion caught in the state system's event manager."); //$NON-NLS-1$ |
| 462 | System.err.println("Are the events in the trace correctly ordered?"); //$NON-NLS-1$ |
| 463 | tre.printStackTrace(); |
| 464 | |
| 465 | } catch (StateValueTypeException sve) { |
| 466 | /* |
| 467 | * This would happen if we were trying to push/pop attributes not of |
| 468 | * type integer. Which, once again, should never happen. |
| 469 | */ |
| 470 | sve.printStackTrace(); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | private void setupCommonLocations() { |
| 475 | cpusNode = ss.getQuarkAbsoluteAndAdd(Attributes.CPUS); |
| 476 | threadsNode = ss.getQuarkAbsoluteAndAdd(Attributes.THREADS); |
| 477 | irqsNode = ss.getQuarkAbsoluteAndAdd(Attributes.RESOURCES, Attributes.IRQS); |
| 478 | softIrqsNode = ss.getQuarkAbsoluteAndAdd(Attributes.RESOURCES, Attributes.SOFT_IRQS); |
| 479 | } |
| 480 | |
| 481 | private static HashMap<String, Integer> fillEventNames() { |
| 482 | /* |
| 483 | * TODO Replace with straight strings in the switch/case once we move to |
| 484 | * Java 7 |
| 485 | */ |
| 486 | HashMap<String, Integer> map = new HashMap<String, Integer>(); |
| 487 | |
| 488 | map.put(LttngStrings.EXIT_SYSCALL, 1); |
| 489 | map.put(LttngStrings.IRQ_HANDLER_ENTRY, 2); |
| 490 | map.put(LttngStrings.IRQ_HANDLER_EXIT, 3); |
| 491 | map.put(LttngStrings.SOFTIRQ_ENTRY, 4); |
| 492 | map.put(LttngStrings.SOFTIRQ_EXIT, 5); |
| 493 | map.put(LttngStrings.SOFTIRQ_RAISE, 6); |
| 494 | map.put(LttngStrings.SCHED_SWITCH, 7); |
| 495 | map.put(LttngStrings.SCHED_PROCESS_FORK, 8); |
| 496 | map.put(LttngStrings.SCHED_PROCESS_EXIT, 9); |
| 497 | map.put(LttngStrings.SCHED_PROCESS_FREE, 10); |
| 498 | |
| 499 | return map; |
| 500 | } |
| 501 | |
| 502 | private int getEventIndex(String eventName) { |
| 503 | Integer ret = knownEventNames.get(eventName); |
| 504 | return (ret != null) ? ret : -1; |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * When we want to set a process back to a "running" state, first check |
| 509 | * its current System_call attribute. If there is a system call active, we |
| 510 | * put the process back in the syscall state. If not, we put it back in |
| 511 | * user mode state. |
| 512 | */ |
| 513 | private void setProcessToRunning(long ts, int currentThreadNode) |
| 514 | throws AttributeNotFoundException, TimeRangeException, |
| 515 | StateValueTypeException { |
| 516 | int quark; |
| 517 | ITmfStateValue value; |
| 518 | |
| 519 | quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.SYSTEM_CALL); |
| 520 | if (ss.queryOngoingState(quark).isNull()) { |
| 521 | /* We were in user mode before the interruption */ |
| 522 | value = TmfStateValue.newValueInt(StateValues.PROCESS_STATUS_RUN_USERMODE); |
| 523 | } else { |
| 524 | /* We were previously in kernel mode */ |
| 525 | value = TmfStateValue.newValueInt(StateValues.PROCESS_STATUS_RUN_SYSCALL); |
| 526 | } |
| 527 | quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.STATUS); |
| 528 | ss.modifyAttribute(ts, value, quark); |
| 529 | } |
| 530 | \r |
| 531 | /**\r |
| 532 | * Similar logic as above, but to set the CPU's status when it's coming out\r |
| 533 | * of an interruption.\r |
| 534 | * @throws AttributeNotFoundException \r |
| 535 | * @throws StateValueTypeException \r |
| 536 | * @throws TimeRangeException \r |
| 537 | */\r |
| 538 | private void cpuExitInterrupt(long ts, int currentCpuNode, int currentThreadNode)\r |
| 539 | throws StateValueTypeException, AttributeNotFoundException,\r |
| 540 | TimeRangeException {\r |
| 541 | int quark;\r |
| 542 | ITmfStateValue value;\r |
| 543 | \r |
| 544 | quark = ss.getQuarkRelativeAndAdd(currentCpuNode, Attributes.CURRENT_THREAD);\r |
| 545 | if (ss.queryOngoingState(quark).unboxInt() > 0) {\r |
| 546 | /* There was a process on the CPU */ |
| 547 | quark = ss.getQuarkRelative(currentThreadNode, Attributes.SYSTEM_CALL); |
| 548 | if (ss.queryOngoingState(quark).isNull()) { |
| 549 | /* That process was in user mode */ |
| 550 | value = TmfStateValue.newValueInt(StateValues.CPU_STATUS_RUN_USERMODE); |
| 551 | } else { |
| 552 | /* That process was in a system call */ |
| 553 | value = TmfStateValue.newValueInt(StateValues.CPU_STATUS_RUN_SYSCALL); |
| 554 | } |
| 555 | } else {\r |
| 556 | /* There was no real process scheduled, CPU was idle */\r |
| 557 | value = TmfStateValue.newValueInt(StateValues.CPU_STATUS_IDLE);\r |
| 558 | }\r |
| 559 | quark = ss.getQuarkRelativeAndAdd(currentCpuNode, Attributes.STATUS);\r |
| 560 | ss.modifyAttribute(ts, value, quark);\r |
| 561 | }\r |
| 562 | } |