lttng: make dead tasks return the state to null instead of blocked.
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.core / src / org / eclipse / tracecompass / internal / analysis / os / linux / core / kernelanalysis / KernelStateProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 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.tracecompass.internal.analysis.os.linux.core.kernelanalysis;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis.Attributes;
21 import org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis.StateValues;
22 import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelAnalysisEventLayout;
23 import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernelanalysis.LinuxValues;
24 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
25 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
26 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
27 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
28 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
29 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
30 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
31 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
32 import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect;
33 import org.eclipse.tracecompass.tmf.core.statesystem.AbstractTmfStateProvider;
34 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
35 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
36
37 import com.google.common.collect.ImmutableMap;
38
39 /**
40 * This is the state change input plugin for TMF's state system which handles
41 * the LTTng 2.0 kernel traces in CTF format.
42 *
43 * It uses the reference handler defined in CTFKernelHandler.java.
44 *
45 * @author alexmont
46 *
47 */
48 public class KernelStateProvider extends AbstractTmfStateProvider {
49
50 // ------------------------------------------------------------------------
51 // Static fields
52 // ------------------------------------------------------------------------
53
54 /**
55 * Version number of this state provider. Please bump this if you modify the
56 * contents of the generated state history in some way.
57 */
58 private static final int VERSION = 7;
59
60 private static final int IRQ_HANDLER_ENTRY_INDEX = 1;
61 private static final int IRQ_HANDLER_EXIT_INDEX = 2;
62 private static final int SOFT_IRQ_ENTRY_INDEX = 3;
63 private static final int SOFT_IRQ_EXIT_INDEX = 4;
64 private static final int SOFT_IRQ_RAISE_INDEX = 5;
65 private static final int SCHED_SWITCH_INDEX = 6;
66 private static final int SCHED_PROCESS_FORK_INDEX = 7;
67 private static final int SCHED_PROCESS_EXIT_INDEX = 8;
68 private static final int SCHED_PROCESS_FREE_INDEX = 9;
69 private static final int STATEDUMP_PROCESS_STATE_INDEX = 10;
70 private static final int SCHED_WAKEUP_INDEX = 11;
71 private static final int SCHED_PI_SETPRIO_INDEX = 12;
72
73
74 // ------------------------------------------------------------------------
75 // Fields
76 // ------------------------------------------------------------------------
77
78 private final Map<String, Integer> fEventNames;
79 private final IKernelAnalysisEventLayout fLayout;
80
81 // ------------------------------------------------------------------------
82 // Constructor
83 // ------------------------------------------------------------------------
84
85 /**
86 * Instantiate a new state provider plugin.
87 *
88 * @param trace
89 * The LTTng 2.0 kernel trace directory
90 * @param layout
91 * The event layout to use for this state provider. Usually
92 * depending on the tracer implementation.
93 */
94 public KernelStateProvider(ITmfTrace trace, IKernelAnalysisEventLayout layout) {
95 super(trace, "Kernel"); //$NON-NLS-1$
96 fLayout = layout;
97 fEventNames = buildEventNames(layout);
98 }
99
100 // ------------------------------------------------------------------------
101 // Event names management
102 // ------------------------------------------------------------------------
103
104 private static Map<String, Integer> buildEventNames(IKernelAnalysisEventLayout layout) {
105 ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder();
106
107 builder.put(layout.eventIrqHandlerEntry(), IRQ_HANDLER_ENTRY_INDEX);
108 builder.put(layout.eventIrqHandlerExit(), IRQ_HANDLER_EXIT_INDEX);
109 builder.put(layout.eventSoftIrqEntry(), SOFT_IRQ_ENTRY_INDEX);
110 builder.put(layout.eventSoftIrqExit(), SOFT_IRQ_EXIT_INDEX);
111 builder.put(layout.eventSoftIrqRaise(), SOFT_IRQ_RAISE_INDEX);
112 builder.put(layout.eventSchedSwitch(), SCHED_SWITCH_INDEX);
113 builder.put(layout.eventSchedPiSetprio(), SCHED_PI_SETPRIO_INDEX);
114 builder.put(layout.eventSchedProcessFork(), SCHED_PROCESS_FORK_INDEX);
115 builder.put(layout.eventSchedProcessExit(), SCHED_PROCESS_EXIT_INDEX);
116 builder.put(layout.eventSchedProcessFree(), SCHED_PROCESS_FREE_INDEX);
117
118 final String eventStatedumpProcessState = layout.eventStatedumpProcessState();
119 if (eventStatedumpProcessState != null) {
120 builder.put(eventStatedumpProcessState, STATEDUMP_PROCESS_STATE_INDEX);
121 }
122
123 for (String eventSchedWakeup : layout.eventsSchedWakeup()) {
124 builder.put(eventSchedWakeup, SCHED_WAKEUP_INDEX);
125 }
126
127 return checkNotNull(builder.build());
128 }
129
130 // ------------------------------------------------------------------------
131 // IStateChangeInput
132 // ------------------------------------------------------------------------
133
134 @Override
135 public int getVersion() {
136 return VERSION;
137 }
138
139 @Override
140 public void assignTargetStateSystem(ITmfStateSystemBuilder ssb) {
141 /* We can only set up the locations once the state system is assigned */
142 super.assignTargetStateSystem(ssb);
143 }
144
145 @Override
146 public KernelStateProvider getNewInstance() {
147 return new KernelStateProvider(this.getTrace(), fLayout);
148 }
149
150 @Override
151 protected void eventHandle(@Nullable ITmfEvent event) {
152 if (event == null) {
153 return;
154 }
155
156 Object cpuObj = TmfTraceUtils.resolveEventAspectOfClassForEvent(event.getTrace(), TmfCpuAspect.class, event);
157 if (cpuObj == null) {
158 /* We couldn't find any CPU information, ignore this event */
159 return;
160 }
161 Integer cpu = (Integer) cpuObj;
162
163 final String eventName = event.getName();
164 final long ts = event.getTimestamp().getValue();
165
166 try {
167 final ITmfStateSystemBuilder ss = checkNotNull(getStateSystemBuilder());
168
169 /* Shortcut for the "current CPU" attribute node */
170 final int currentCPUNode = ss.getQuarkRelativeAndAdd(getNodeCPUs(ss), cpu.toString());
171
172 /*
173 * Shortcut for the "current thread" attribute node. It requires
174 * querying the current CPU's current thread.
175 */
176 int quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.CURRENT_THREAD);
177 ITmfStateValue value = ss.queryOngoingState(quark);
178 int thread = value.isNull() ? -1 : value.unboxInt();
179 final int currentThreadNode = ss.getQuarkRelativeAndAdd(getNodeThreads(ss), String.valueOf(thread));
180
181 /*
182 * Feed event to the history system if it's known to cause a state
183 * transition.
184 */
185 Integer idx = fEventNames.get(eventName);
186 int intval = (idx == null ? -1 : idx.intValue());
187 switch (intval) {
188
189 case IRQ_HANDLER_ENTRY_INDEX:
190 {
191 Integer irqId = ((Long) event.getContent().getField(fLayout.fieldIrq()).getValue()).intValue();
192
193 /* Mark this IRQ as active in the resource tree.
194 * The state value = the CPU on which this IRQ is sitting */
195 quark = ss.getQuarkRelativeAndAdd(getNodeIRQs(ss), irqId.toString());
196 value = TmfStateValue.newValueInt(cpu.intValue());
197 ss.modifyAttribute(ts, value, quark);
198
199 /* Change the status of the running process to interrupted */
200 quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.STATUS);
201 value = StateValues.PROCESS_STATUS_INTERRUPTED_VALUE;
202 ss.modifyAttribute(ts, value, quark);
203
204 /* Change the status of the CPU to interrupted */
205 quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.STATUS);
206 value = StateValues.CPU_STATUS_IRQ_VALUE;
207 ss.modifyAttribute(ts, value, quark);
208 }
209 break;
210
211 case IRQ_HANDLER_EXIT_INDEX:
212 {
213 Integer irqId = ((Long) event.getContent().getField(fLayout.fieldIrq()).getValue()).intValue();
214
215 /* Put this IRQ back to inactive in the resource tree */
216 quark = ss.getQuarkRelativeAndAdd(getNodeIRQs(ss), irqId.toString());
217 value = TmfStateValue.nullValue();
218 ss.modifyAttribute(ts, value, quark);
219
220 /* Set the previous process back to running */
221 setProcessToRunning(ss, ts, currentThreadNode);
222
223 /* Set the CPU status back to running or "idle" */
224 cpuExitInterrupt(ss, ts, currentCPUNode, currentThreadNode);
225 }
226 break;
227
228 case SOFT_IRQ_ENTRY_INDEX:
229 {
230 Integer softIrqId = ((Long) event.getContent().getField(fLayout.fieldVec()).getValue()).intValue();
231
232 /* Mark this SoftIRQ as active in the resource tree.
233 * The state value = the CPU on which this SoftIRQ is processed */
234 quark = ss.getQuarkRelativeAndAdd(getNodeSoftIRQs(ss), softIrqId.toString());
235 value = TmfStateValue.newValueInt(cpu.intValue());
236 ss.modifyAttribute(ts, value, quark);
237
238 /* Change the status of the running process to interrupted */
239 quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.STATUS);
240 value = StateValues.PROCESS_STATUS_INTERRUPTED_VALUE;
241 ss.modifyAttribute(ts, value, quark);
242
243 /* Change the status of the CPU to interrupted */
244 quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.STATUS);
245 value = StateValues.CPU_STATUS_SOFTIRQ_VALUE;
246 ss.modifyAttribute(ts, value, quark);
247 }
248 break;
249
250 case SOFT_IRQ_EXIT_INDEX:
251 {
252 Integer softIrqId = ((Long) event.getContent().getField(fLayout.fieldVec()).getValue()).intValue();
253
254 /* Put this SoftIRQ back to inactive (= -1) in the resource tree */
255 quark = ss.getQuarkRelativeAndAdd(getNodeSoftIRQs(ss), softIrqId.toString());
256 value = TmfStateValue.nullValue();
257 ss.modifyAttribute(ts, value, quark);
258
259 /* Set the previous process back to running */
260 setProcessToRunning(ss, ts, currentThreadNode);
261
262 /* Set the CPU status back to "busy" or "idle" */
263 cpuExitInterrupt(ss, ts, currentCPUNode, currentThreadNode);
264 }
265 break;
266
267 case SOFT_IRQ_RAISE_INDEX:
268 /* Fields: int32 vec */
269 {
270 Integer softIrqId = ((Long) event.getContent().getField(fLayout.fieldVec()).getValue()).intValue();
271
272 /* Mark this SoftIRQ as *raised* in the resource tree.
273 * State value = -2 */
274 quark = ss.getQuarkRelativeAndAdd(getNodeSoftIRQs(ss), softIrqId.toString());
275 value = StateValues.SOFT_IRQ_RAISED_VALUE;
276 ss.modifyAttribute(ts, value, quark);
277 }
278 break;
279
280 case SCHED_SWITCH_INDEX:
281 {
282 ITmfEventField content = event.getContent();
283 Integer prevTid = ((Long) content.getField(fLayout.fieldPrevTid()).getValue()).intValue();
284 Long prevState = (Long) content.getField(fLayout.fieldPrevState()).getValue();
285 String nextProcessName = (String) content.getField(fLayout.fieldNextComm()).getValue();
286 Integer nextTid = ((Long) content.getField(fLayout.fieldNextTid()).getValue()).intValue();
287 Integer nextPrio = ((Long) content.getField(fLayout.fieldNextPrio()).getValue()).intValue();
288
289 Integer formerThreadNode = ss.getQuarkRelativeAndAdd(getNodeThreads(ss), prevTid.toString());
290 Integer newCurrentThreadNode = ss.getQuarkRelativeAndAdd(getNodeThreads(ss), nextTid.toString());
291
292 /* Set the status of the process that got scheduled out. */
293 quark = ss.getQuarkRelativeAndAdd(formerThreadNode, Attributes.STATUS);
294 if (prevState != 0) {
295 if (prevState == LinuxValues.TASK_STATE_DEAD) {
296 value = TmfStateValue.nullValue();
297 } else {
298 value = StateValues.PROCESS_STATUS_WAIT_BLOCKED_VALUE;
299 }
300 } else {
301 value = StateValues.PROCESS_STATUS_WAIT_FOR_CPU_VALUE;
302 }
303 ss.modifyAttribute(ts, value, quark);
304
305 /* Set the status of the new scheduled process */
306 setProcessToRunning(ss, ts, newCurrentThreadNode);
307
308 /* Set the exec name of the new process */
309 quark = ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.EXEC_NAME);
310 value = TmfStateValue.newValueString(nextProcessName);
311 ss.modifyAttribute(ts, value, quark);
312
313 /* Set the current prio for the new process */
314 quark = ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.PRIO);
315 value = TmfStateValue.newValueInt(nextPrio);
316 ss.modifyAttribute(ts, value, quark);
317
318 /* Make sure the PPID and system_call sub-attributes exist */
319 ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.SYSTEM_CALL);
320 ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.PPID);
321
322 /* Set the current scheduled process on the relevant CPU */
323 quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.CURRENT_THREAD);
324 value = TmfStateValue.newValueInt(nextTid);
325 ss.modifyAttribute(ts, value, quark);
326
327 /* Set the status of the CPU itself */
328 if (nextTid > 0) {
329 /* Check if the entering process is in kernel or user mode */
330 quark = ss.getQuarkRelativeAndAdd(newCurrentThreadNode, Attributes.SYSTEM_CALL);
331 if (ss.queryOngoingState(quark).isNull()) {
332 value = StateValues.CPU_STATUS_RUN_USERMODE_VALUE;
333 } else {
334 value = StateValues.CPU_STATUS_RUN_SYSCALL_VALUE;
335 }
336 } else {
337 value = StateValues.CPU_STATUS_IDLE_VALUE;
338 }
339 quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.STATUS);
340 ss.modifyAttribute(ts, value, quark);
341 }
342 break;
343
344 case SCHED_PI_SETPRIO_INDEX:
345 {
346 ITmfEventField content = event.getContent();
347 Integer tid = ((Long) content.getField(fLayout.fieldTid()).getValue()).intValue();
348 Integer prio = ((Long) content.getField(fLayout.fieldNewPrio()).getValue()).intValue();
349
350 Integer updateThreadNode = ss.getQuarkRelativeAndAdd(getNodeThreads(ss), tid.toString());
351
352 /* Set the current prio for the new process */
353 quark = ss.getQuarkRelativeAndAdd(updateThreadNode, Attributes.PRIO);
354 value = TmfStateValue.newValueInt(prio);
355 ss.modifyAttribute(ts, value, quark);
356 }
357 break;
358
359 case SCHED_PROCESS_FORK_INDEX:
360 {
361 ITmfEventField content = event.getContent();
362 // String parentProcessName = (String) event.getFieldValue("parent_comm");
363 String childProcessName = (String) content.getField(fLayout.fieldChildComm()).getValue();
364 // assert ( parentProcessName.equals(childProcessName) );
365
366 Integer parentTid = ((Long) content.getField(fLayout.fieldParentTid()).getValue()).intValue();
367 Integer childTid = ((Long) content.getField(fLayout.fieldChildTid()).getValue()).intValue();
368
369 Integer parentTidNode = ss.getQuarkRelativeAndAdd(getNodeThreads(ss), parentTid.toString());
370 Integer childTidNode = ss.getQuarkRelativeAndAdd(getNodeThreads(ss), childTid.toString());
371
372 /* Assign the PPID to the new process */
373 quark = ss.getQuarkRelativeAndAdd(childTidNode, Attributes.PPID);
374 value = TmfStateValue.newValueInt(parentTid);
375 ss.modifyAttribute(ts, value, quark);
376
377 /* Set the new process' exec_name */
378 quark = ss.getQuarkRelativeAndAdd(childTidNode, Attributes.EXEC_NAME);
379 value = TmfStateValue.newValueString(childProcessName);
380 ss.modifyAttribute(ts, value, quark);
381
382 /* Set the new process' status */
383 quark = ss.getQuarkRelativeAndAdd(childTidNode, Attributes.STATUS);
384 value = StateValues.PROCESS_STATUS_WAIT_FOR_CPU_VALUE;
385 ss.modifyAttribute(ts, value, quark);
386
387 /* Set the process' syscall name, to be the same as the parent's */
388 quark = ss.getQuarkRelativeAndAdd(parentTidNode, Attributes.SYSTEM_CALL);
389 value = ss.queryOngoingState(quark);
390 if (value.isNull()) {
391 /*
392 * Maybe we were missing info about the parent? At least we
393 * will set the child right. Let's suppose "sys_clone".
394 */
395 value = TmfStateValue.newValueString(fLayout.eventSyscallEntryPrefix() + IKernelAnalysisEventLayout.INITIAL_SYSCALL_NAME);
396 }
397 quark = ss.getQuarkRelativeAndAdd(childTidNode, Attributes.SYSTEM_CALL);
398 ss.modifyAttribute(ts, value, quark);
399 }
400 break;
401
402 case SCHED_PROCESS_EXIT_INDEX:
403 break;
404
405 case SCHED_PROCESS_FREE_INDEX:
406 {
407 Integer tid = ((Long) event.getContent().getField(fLayout.fieldTid()).getValue()).intValue();
408 /*
409 * Remove the process and all its sub-attributes from the
410 * current state
411 */
412 quark = ss.getQuarkRelativeAndAdd(getNodeThreads(ss), tid.toString());
413 ss.removeAttribute(ts, quark);
414 }
415 break;
416
417 case STATEDUMP_PROCESS_STATE_INDEX:
418 /* LTTng-specific */
419 {
420 ITmfEventField content = event.getContent();
421 int tid = ((Long) content.getField("tid").getValue()).intValue(); //$NON-NLS-1$
422 int pid = ((Long) content.getField("pid").getValue()).intValue(); //$NON-NLS-1$
423 int ppid = ((Long) content.getField("ppid").getValue()).intValue(); //$NON-NLS-1$
424 int status = ((Long) content.getField("status").getValue()).intValue(); //$NON-NLS-1$
425 String name = (String) content.getField("name").getValue(); //$NON-NLS-1$
426 /*
427 * "mode" could be interesting too, but it doesn't seem to be
428 * populated with anything relevant for now.
429 */
430
431 int curThreadNode = ss.getQuarkRelativeAndAdd(getNodeThreads(ss), String.valueOf(tid));
432
433 /* Set the process' name */
434 quark = ss.getQuarkRelativeAndAdd(curThreadNode, Attributes.EXEC_NAME);
435 if (ss.queryOngoingState(quark).isNull()) {
436 /* If the value didn't exist previously, set it */
437 value = TmfStateValue.newValueString(name);
438 ss.modifyAttribute(ts, value, quark);
439 }
440
441 /* Set the process' PPID */
442 quark = ss.getQuarkRelativeAndAdd(curThreadNode, Attributes.PPID);
443 if (ss.queryOngoingState(quark).isNull()) {
444 if (pid == tid) {
445 /* We have a process. Use the 'PPID' field. */
446 value = TmfStateValue.newValueInt(ppid);
447 } else {
448 /* We have a thread, use the 'PID' field for the parent. */
449 value = TmfStateValue.newValueInt(pid);
450 }
451 ss.modifyAttribute(ts, value, quark);
452 }
453
454 /* Set the process' status */
455 quark = ss.getQuarkRelativeAndAdd(curThreadNode, Attributes.STATUS);
456 if (ss.queryOngoingState(quark).isNull()) {
457 switch (status) {
458 case LinuxValues.STATEDUMP_PROCESS_STATUS_WAIT_CPU:
459 value = StateValues.PROCESS_STATUS_WAIT_FOR_CPU_VALUE;
460 break;
461 case LinuxValues.STATEDUMP_PROCESS_STATUS_WAIT:
462 /*
463 * We have no information on what the process is waiting
464 * on (unlike a sched_switch for example), so we will
465 * use the WAIT_UNKNOWN state instead of the "normal"
466 * WAIT_BLOCKED state.
467 */
468 value = StateValues.PROCESS_STATUS_WAIT_UNKNOWN_VALUE;
469 break;
470 default:
471 value = StateValues.PROCESS_STATUS_UNKNOWN_VALUE;
472 }
473 ss.modifyAttribute(ts, value, quark);
474 }
475 }
476 break;
477
478 case SCHED_WAKEUP_INDEX:
479 {
480 final int tid = ((Long) event.getContent().getField(fLayout.fieldTid()).getValue()).intValue();
481 final int prio = ((Long) event.getContent().getField(fLayout.fieldPrio()).getValue()).intValue();
482 final int threadNode = ss.getQuarkRelativeAndAdd(getNodeThreads(ss), String.valueOf(tid));
483
484 /*
485 * The process indicated in the event's payload is now ready to
486 * run. Assign it to the "wait for cpu" state, but only if it
487 * was not already running.
488 */
489 quark = ss.getQuarkRelativeAndAdd(threadNode, Attributes.STATUS);
490 int status = ss.queryOngoingState(quark).unboxInt();
491
492 if (status != StateValues.PROCESS_STATUS_RUN_SYSCALL &&
493 status != StateValues.PROCESS_STATUS_RUN_USERMODE) {
494 value = StateValues.PROCESS_STATUS_WAIT_FOR_CPU_VALUE;
495 ss.modifyAttribute(ts, value, quark);
496 }
497
498 /*
499 * When a user changes a threads prio (e.g. with pthread_setschedparam),
500 * it shows in ftrace with a sched_wakeup.
501 */
502 quark = ss.getQuarkRelativeAndAdd(threadNode, Attributes.PRIO);
503 value = TmfStateValue.newValueInt(prio);
504 ss.modifyAttribute(ts, value, quark);
505 }
506 break;
507
508 default:
509 /* Other event types not covered by the main switch */
510 {
511 if (eventName.startsWith(fLayout.eventSyscallEntryPrefix())
512 || eventName.startsWith(fLayout.eventCompatSyscallEntryPrefix())) {
513
514 /* Assign the new system call to the process */
515 quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.SYSTEM_CALL);
516 value = TmfStateValue.newValueString(eventName);
517 ss.modifyAttribute(ts, value, quark);
518
519 /* Put the process in system call mode */
520 quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.STATUS);
521 value = StateValues.PROCESS_STATUS_RUN_SYSCALL_VALUE;
522 ss.modifyAttribute(ts, value, quark);
523
524 /* Put the CPU in system call (kernel) mode */
525 quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.STATUS);
526 value = StateValues.CPU_STATUS_RUN_SYSCALL_VALUE;
527 ss.modifyAttribute(ts, value, quark);
528
529 } else if (eventName.startsWith(fLayout.eventSyscallExitPrefix())) {
530
531 /* Clear the current system call on the process */
532 quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.SYSTEM_CALL);
533 value = TmfStateValue.nullValue();
534 ss.modifyAttribute(ts, value, quark);
535
536 /* Put the process' status back to user mode */
537 quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.STATUS);
538 value = StateValues.PROCESS_STATUS_RUN_USERMODE_VALUE;
539 ss.modifyAttribute(ts, value, quark);
540
541 /* Put the CPU's status back to user mode */
542 quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.STATUS);
543 value = StateValues.CPU_STATUS_RUN_USERMODE_VALUE;
544 ss.modifyAttribute(ts, value, quark);
545 }
546
547 }
548 break;
549 } // End of big switch
550
551 } catch (AttributeNotFoundException ae) {
552 /*
553 * This would indicate a problem with the logic of the manager here,
554 * so it shouldn't happen.
555 */
556 ae.printStackTrace();
557
558 } catch (TimeRangeException tre) {
559 /*
560 * This would happen if the events in the trace aren't ordered
561 * chronologically, which should never be the case ...
562 */
563 System.err.println("TimeRangeExcpetion caught in the state system's event manager."); //$NON-NLS-1$
564 System.err.println("Are the events in the trace correctly ordered?"); //$NON-NLS-1$
565 tre.printStackTrace();
566
567 } catch (StateValueTypeException sve) {
568 /*
569 * This would happen if we were trying to push/pop attributes not of
570 * type integer. Which, once again, should never happen.
571 */
572 sve.printStackTrace();
573 }
574 }
575
576 // ------------------------------------------------------------------------
577 // Convenience methods for commonly-used attribute tree locations
578 // ------------------------------------------------------------------------
579
580 private static int getNodeCPUs(ITmfStateSystemBuilder ssb) {
581 return ssb.getQuarkAbsoluteAndAdd(Attributes.CPUS);
582 }
583
584 private static int getNodeThreads(ITmfStateSystemBuilder ssb) {
585 return ssb.getQuarkAbsoluteAndAdd(Attributes.THREADS);
586 }
587
588 private static int getNodeIRQs(ITmfStateSystemBuilder ssb) {
589 return ssb.getQuarkAbsoluteAndAdd(Attributes.RESOURCES, Attributes.IRQS);
590 }
591
592 private static int getNodeSoftIRQs(ITmfStateSystemBuilder ssb) {
593 return ssb.getQuarkAbsoluteAndAdd(Attributes.RESOURCES, Attributes.SOFT_IRQS);
594 }
595
596 // ------------------------------------------------------------------------
597 // Advanced state-setting methods
598 // ------------------------------------------------------------------------
599
600 /**
601 * When we want to set a process back to a "running" state, first check
602 * its current System_call attribute. If there is a system call active, we
603 * put the process back in the syscall state. If not, we put it back in
604 * user mode state.
605 */
606 private static void setProcessToRunning(ITmfStateSystemBuilder ssb, long ts, int currentThreadNode)
607 throws AttributeNotFoundException, TimeRangeException,
608 StateValueTypeException {
609 int quark;
610 ITmfStateValue value;
611
612 quark = ssb.getQuarkRelativeAndAdd(currentThreadNode, Attributes.SYSTEM_CALL);
613 if (ssb.queryOngoingState(quark).isNull()) {
614 /* We were in user mode before the interruption */
615 value = StateValues.PROCESS_STATUS_RUN_USERMODE_VALUE;
616 } else {
617 /* We were previously in kernel mode */
618 value = StateValues.PROCESS_STATUS_RUN_SYSCALL_VALUE;
619 }
620 quark = ssb.getQuarkRelativeAndAdd(currentThreadNode, Attributes.STATUS);
621 ssb.modifyAttribute(ts, value, quark);
622 }
623
624 /**
625 * Similar logic as above, but to set the CPU's status when it's coming out
626 * of an interruption.
627 */
628 private static void cpuExitInterrupt(ITmfStateSystemBuilder ssb, long ts,
629 int currentCpuNode, int currentThreadNode)
630 throws StateValueTypeException, AttributeNotFoundException,
631 TimeRangeException {
632 int quark;
633 ITmfStateValue value;
634
635 quark = ssb.getQuarkRelativeAndAdd(currentCpuNode, Attributes.CURRENT_THREAD);
636 if (ssb.queryOngoingState(quark).unboxInt() > 0) {
637 /* There was a process on the CPU */
638 quark = ssb.getQuarkRelative(currentThreadNode, Attributes.SYSTEM_CALL);
639 if (ssb.queryOngoingState(quark).isNull()) {
640 /* That process was in user mode */
641 value = StateValues.CPU_STATUS_RUN_USERMODE_VALUE;
642 } else {
643 /* That process was in a system call */
644 value = StateValues.CPU_STATUS_RUN_SYSCALL_VALUE;
645 }
646 } else {
647 /* There was no real process scheduled, CPU was idle */
648 value = StateValues.CPU_STATUS_IDLE_VALUE;
649 }
650 quark = ssb.getQuarkRelativeAndAdd(currentCpuNode, Attributes.STATUS);
651 ssb.modifyAttribute(ts, value, quark);
652 }
653 }
This page took 0.048161 seconds and 6 git commands to generate.