2010-05-17 Francois Chouinard <fchouinard@gmail.com>
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.tests / src / org / eclipse / linuxtools / lttng / tests / state / handlers / AbsStateUpdate.java
1 package org.eclipse.linuxtools.lttng.tests.state.handlers;
2
3 import org.eclipse.linuxtools.lttng.TraceDebug;
4 import org.eclipse.linuxtools.lttng.event.LttngEvent;
5 import org.eclipse.linuxtools.lttng.event.LttngEventField;
6 import org.eclipse.linuxtools.lttng.state.StateStrings.Fields;
7 import org.eclipse.linuxtools.lttng.state.evProcessor.ILttngEventProcessor;
8 import org.eclipse.linuxtools.lttng.state.model.LttngExecutionState;
9 import org.eclipse.linuxtools.lttng.state.model.LttngTraceState;
10 import org.eclipse.linuxtools.tmf.event.TmfEventField;
11
12 public abstract class AbsStateUpdate implements ILttngEventProcessor {
13
14 // ========================================================================
15 // Data
16 // =======================================================================
17 protected static LttngExecutionState exState = null;
18 protected static Long pid = null;
19
20 // ========================================================================
21 // push and pop from stack
22 // =======================================================================
23
24 // protected static final Long ANY_CPU = 0L;
25
26
27 // protected void push_state(Long cpu, StateStrings.ExecutionMode execMode,
28 // String submode, TmfTimestamp eventTime, LttngTraceState traceSt) {
29 //
30 // LttngProcessState process = traceSt.getRunning_process().get(cpu);
31 // LttngExecutionState exe_state = new LttngExecutionState();
32 // exe_state.setExec_mode(execMode);
33 // exe_state.setExec_submode(submode);
34 // exe_state.setEntry_Time(eventTime);
35 // exe_state.setChage_Time(eventTime);
36 // exe_state.setCum_cpu_time(0L);
37 // exe_state.setProc_status(process.getState().getProc_status());
38 // process.setState(exe_state);
39 //
40 // Stack<LttngExecutionState> exe_state_stack = process
41 // .getExecution_stack();
42 // exe_state_stack.push(exe_state);
43 // }
44 //
45 // protected void pop_state(Long cpu, StateStrings.ExecutionMode execMode,
46 // LttngTraceState traceSt, TmfTimestamp eventTime) {
47 //
48 // LttngProcessState process = traceSt.getRunning_process().get(cpu);
49 //
50 // if (!process.getState().getExec_mode().equals(execMode)) {
51 // // Different execution mode
52 // TraceDebug.debug("Different Execution Mode type \n\tTime:"
53 // + eventTime.toString() + "\n\tprocess state has: \n\t"
54 // + process.getState().getExec_mode().toString()
55 // + "\n\twhen pop_int is:\n\t" + execMode.toString());
56 // return;
57 // }
58 //
59 // Stack<LttngExecutionState> exe_state_stack = process
60 // .getExecution_stack();
61 //
62 // if (exe_state_stack.size() <= 1) {
63 // TraceDebug
64 // .debug("Removing last item from execution stack is not allowed");
65 // }
66 //
67 // exe_state_stack.pop();
68 // process.setState(exe_state_stack.peek());
69 // process.getState().setChage_Time(eventTime);
70 // }
71 //
72 // protected void irq_push_mode(LttngIRQState irqst, IRQMode state) {
73 // irqst.getMode_stack().push(state);
74 // }
75 //
76 // protected void irq_set_base_mode(LttngIRQState irqst, IRQMode state) {
77 // Stack<IRQMode> irqModeStack = irqst.getMode_stack();
78 // irqModeStack.clear();
79 // irqModeStack.push(state);
80 // }
81 //
82 // protected void irq_pop_mode(LttngIRQState irqst) {
83 // Stack<IRQMode> irq_stack = irqst.getMode_stack();
84 // if (irq_stack.size() <= 1)
85 // irq_set_base_mode(irqst, IRQMode.LTTV_IRQ_UNKNOWN);
86 // else
87 // irq_stack.pop();
88 // }
89 //
90 // protected void cpu_push_mode(LTTngCPUState cpust, StateStrings.CpuMode state) {
91 // // The initialization (init) creates a LttngCPUState instance per
92 // // available cpu in the system
93 // Stack<CpuMode> cpuStack = cpust.getMode_stack();
94 // cpuStack.push(state);
95 // }
96 //
97 // protected void cpu_pop_mode(LTTngCPUState cpust) {
98 // if (cpust.getMode_stack().size() <= 1)
99 // cpu_set_base_mode(cpust, StateStrings.CpuMode.LTTV_CPU_UNKNOWN);
100 // else
101 // cpust.getMode_stack().pop();
102 // }
103 //
104 // /* clears the stack and sets the state passed as argument */
105 // protected void cpu_set_base_mode(LTTngCPUState cpust,
106 // StateStrings.CpuMode state) {
107 // Stack<CpuMode> cpuStack = cpust.getMode_stack();
108 // cpuStack.clear();
109 // cpuStack.push(state);
110 // }
111 //
112 // protected void bdev_pop_mode(LttngBdevState bdevst) {
113 // Stack<BdevMode> bdevModeStack = bdevst.getMode_stack();
114 // if (bdevModeStack.size() <= 1) {
115 // bdev_set_base_mode(bdevModeStack, BdevMode.LTTV_BDEV_UNKNOWN);
116 // } else {
117 // bdevModeStack.pop();
118 // }
119 //
120 // }
121 //
122 // protected void bdev_set_base_mode(Stack<BdevMode> bdevModeStack,
123 // BdevMode state) {
124 // bdevModeStack.clear();
125 // bdevModeStack.push(state);
126 // }
127 //
128 // /**
129 // * Push a new received function pointer to the user_stack
130 // *
131 // * @param traceSt
132 // * @param funcptr
133 // * @param cpu
134 // */
135 // protected void push_function(LttngTraceState traceSt, Long funcptr, Long cpu) {
136 // // Get the related process
137 // LttngProcessState process = traceSt.getRunning_process().get(cpu);
138 //
139 // // get the user_stack
140 // Stack<Long> user_stack = process.getUser_stack();
141 //
142 // // update stack
143 // user_stack.push(funcptr);
144 //
145 // // update the pointer to the current function on the corresponding
146 // // process
147 // process.setCurrent_function(funcptr);
148 // }
149 //
150 // protected void pop_function(LttngTraceState traceSt, LttngEvent trcEvent,
151 // Long funcptr) {
152 // Long cpu = trcEvent.getCpuId();
153 // // LttvTraceState *ts = (LttvTraceState*)tfs->parent.t_context;
154 // // LttvProcessState *process = ts->running_process[cpu];
155 // LttngProcessState process = traceSt.getRunning_process().get(cpu);
156 // Long curr_function = process.getCurrent_function();
157 // if (curr_function != null && curr_function != funcptr) {
158 // TraceDebug.debug("Different functions: " + funcptr + " current: "
159 // + curr_function + " time stamp: "
160 // + trcEvent.getTimestamp().toString());
161 //
162 // // g_info("Different functions (%lu.%09lu): ignore it\n",
163 // // tfs->parent.timestamp.tv_sec, tfs->parent.timestamp.tv_nsec);
164 // // g_info("process state has %" PRIu64 " when pop_function is %"
165 // // PRIu64
166 // // "\n",
167 // // process->current_function, funcptr);
168 // // g_info("{ %u, %u, %s, %s, %s }\n",
169 // // process->pid,
170 // // process->ppid,
171 // // g_quark_to_string(process->name),
172 // // g_quark_to_string(process->brand),
173 // // g_quark_to_string(process->state->s));
174 // return;
175 // }
176 //
177 // Stack<Long> user_stack = process.getUser_stack();
178 // if (user_stack.size() == 0) {
179 // TraceDebug
180 // .debug("Trying to pop last function in stack. Ignored. Time Stamp: "
181 // + trcEvent.getTimestamp());
182 // return;
183 // }
184 // user_stack.pop();
185 // process.setCurrent_function(user_stack.peek());
186 // }
187 //
188 // // ========================================================================
189 // // General methods
190 // // =======================================================================
191 // /**
192 // * protected method used when only one Field is expected with Type "Long" if
193 // * the number of fields is greater, the first field is returned and a
194 // * tracing message is sent Null is returned if the value could not be
195 // * extracted.
196 // *
197 // * @param trcEvent
198 // * @param traceSt
199 // * @param expectedNumFields
200 // * @return
201 // */
202 // protected Long getDField(LttngEvent trcEvent, LttngTraceState traceSt,
203 // Fields expectedField) {
204 // Long fieldVal = null;
205 // TmfEventField[] fields = trcEvent.getContent().getFields();
206 // String[] fieldLabels = trcEvent.getContent().getFormat().getLabels();
207 //
208 // // Only one field expected
209 // if (fields.length != 1 || fieldLabels.length != 1) {
210 // StringBuilder sb = new StringBuilder(
211 // "Unexpected number of fields received: " + fields.length
212 // + " for Event: " + trcEvent.getMarkerName() + "\n\t\tFields: ");
213 //
214 // for (TmfEventField field : fields) {
215 // sb.append(((LttngEventField)field).getName() + " ");
216 // }
217 //
218 // TraceDebug.debug(sb.toString());
219 // if (fields.length == 0) {
220 // return null;
221 // }
222 // }
223 //
224 // LttngEventField field = (LttngEventField) fields[0];
225 // String fieldname = field.getName();
226 // String expectedFieldName = expectedField.getInName();
227 // if (fieldname.equals(expectedFieldName)) {
228 // Object fieldObj = field.getValue();
229 // if (fieldObj instanceof Long) {
230 // // Expected value found
231 // fieldVal = (Long) field.getValue();
232 // } else {
233 // if (TraceDebug.isDEBUG()) {
234 // TraceDebug
235 // .debug("Unexpected field Type. Expected: Long, Received: "
236 // + fieldObj.getClass().getSimpleName());
237 // }
238 // return null;
239 // }
240 // } else {
241 // TraceDebug.debug("Unexpected field received: " + fieldname
242 // + " Expected: " + expectedFieldName);
243 // return null;
244 // }
245 //
246 // return fieldVal;
247 // }
248 //
249 /**
250 * protected method used when a Field is requested among several available
251 * fields and the expected type is Long
252 *
253 * @param trcEvent
254 * @param traceSt
255 * @param expectedNumFields
256 * @return
257 */
258 protected Long getAFieldLong(LttngEvent trcEvent, LttngTraceState traceSt,
259 Fields expectedField) {
260 Long fieldVal = null;
261 TmfEventField[] fields = trcEvent.getContent().getFields();
262
263 // At least one field expected
264 if (fields.length == 0) {
265 TraceDebug.debug("Unexpected number of fields received: "
266 + fields.length);
267 return null;
268 }
269
270 LttngEventField field;
271 String fieldname;
272 String expectedFieldName = expectedField.getInName();
273 for (int i = 0; i < fields.length; i++) {
274 field = (LttngEventField) fields[i];
275 fieldname = field.getId();
276 if (fieldname.equals(expectedFieldName)) {
277 Object fieldObj = field.getValue();
278 if (fieldObj instanceof Long) {
279 // Expected value found
280 fieldVal = (Long) field.getValue();
281 // if (expectedField == Fields.LTT_FIELD_TYPE) {
282 // TraceDebug.debug("Field Type value is: " + fieldVal);
283 // }
284 break;
285 } else {
286 if (TraceDebug.isDEBUG()) {
287 TraceDebug
288 .debug("Unexpected field Type. Expected: Long, Received: "
289 + fieldObj.getClass().getSimpleName());
290 }
291 return null;
292 }
293 }
294 }
295
296 if (fieldVal == null) {
297 if (TraceDebug.isDEBUG()) {
298 sendNoFieldFoundMsg(fields, expectedFieldName);
299 }
300 }
301 return fieldVal;
302 }
303
304 /**
305 * protected method used when a Field is requested among several available
306 * fields and the expected type is String
307 *
308 * @param trcEvent
309 * @param traceSt
310 * @param expectedNumFields
311 * @return
312 */
313 protected String getAFieldString(LttngEvent trcEvent,
314 LttngTraceState traceSt, Fields expectedField) {
315 String fieldVal = null;
316 TmfEventField[] fields = trcEvent.getContent().getFields();
317
318 // Only one field expected
319 if (fields.length == 0) {
320 TraceDebug.debug("Unexpected number of fields received: "
321 + fields.length);
322 return null;
323 }
324
325 LttngEventField field;
326 String fieldname;
327 String expectedFieldName = expectedField.getInName();
328 for (int i = 0; i < fields.length; i++) {
329 field = (LttngEventField) fields[i];
330 fieldname = field.getId();
331 if (fieldname.equals(expectedFieldName)) {
332 Object fieldObj = field.getValue();
333 if (fieldObj instanceof String) {
334 // Expected value found
335 fieldVal = (String) field.getValue();
336 break;
337 } else {
338 if (TraceDebug.isDEBUG()) {
339 TraceDebug
340 .debug("Unexpected field Type. Expected: String, Received: "
341 + fieldObj.getClass().getSimpleName());
342 }
343 return null;
344 }
345 }
346 }
347
348 if (fieldVal == null) {
349 if (TraceDebug.isDEBUG()) {
350 sendNoFieldFoundMsg(fields, expectedFieldName);
351 }
352 }
353 return fieldVal;
354 }
355
356 protected void sendNoFieldFoundMsg(TmfEventField[] fields,
357 String expectedFieldName) {
358 LttngEventField field;
359 StringBuilder sb = new StringBuilder("Field not found, requested: "
360 + expectedFieldName);
361 sb.append(" number of fields: " + fields.length + "Fields: ");
362 for (int i = 0; i < fields.length; i++) {
363 field = (LttngEventField) fields[i];
364 sb.append(field.getId() + " ");
365 }
366
367 TraceDebug.debug(sb.toString(), 5);
368 }
369
370 // // Adaption from MKDEV macro
371 // protected Long mkdev(Long major, Long minor) {
372 // Long result = null;
373 // if (major != null && minor != null) {
374 // result = (major << 20) | minor;
375 // }
376 // return result;
377 // }
378 //
379 // /*
380 // * FIXME : this function should be called when we receive an event telling
381 // * that release_task has been called in the kernel. In happens generally
382 // * when the parent waits for its child terminaison, but may also happen in
383 // * special cases in the child's exit : when the parent ignores its children
384 // * SIGCCHLD or has the flag SA_NOCLDWAIT. It can also happen when the child
385 // * is part of a killed thread group, but isn't the leader.
386 // */
387 // protected boolean exit_process(LttngTraceState ts, LttngProcessState process) {
388 // /*
389 // * Wait for both schedule with exit dead and process free to happen.
390 // * They can happen in any order.
391 // */
392 // process.incrementFree_events();
393 // if (process.getFree_events() < 2) {
394 // return false;
395 // }
396 //
397 // process.getExecution_stack().clear();
398 // process.getUser_stack().clear();
399 // ts.getProcesses().remove(process);
400 //
401 // return true;
402 // }
403 //
404 // // LttvProcessState *
405 // // lttv_state_create_process(LttvTraceState *tcs, LttvProcessState *parent,
406 // // guint cpu, guint pid, guint tgid, GQuark name, const LttTime *timestamp)
407 // // {
408 // // LttvProcessState *process = g_new(LttvProcessState, 1);
409 // //
410 // // LttvExecutionState *es;
411 // //
412 // // char buffer[128];
413 // //
414 // // process->pid = pid;
415 // // process->tgid = tgid;
416 // // process->cpu = cpu;
417 // // process->name = name;
418 // // process->brand = LTTV_STATE_UNBRANDED;
419 // // //process->last_cpu = tfs->cpu_name;
420 // // //process->last_cpu_index =
421 // // ltt_tracefile_num(((LttvTracefileContext*)tfs)->tf);
422 // // process->type = LTTV_STATE_USER_THREAD;
423 // // process->usertrace = ltt_state_usertrace_find(tcs, pid, timestamp);
424 // // process->current_function = 0; //function 0x0 by default.
425 // //
426 // // g_info("Process %u, core %p", process->pid, process);
427 // // g_hash_table_insert(tcs->processes, process, process);
428 // //
429 // // if(parent) {
430 // // process->ppid = parent->pid;
431 // // process->creation_time = *timestamp;
432 // // }
433 // //
434 // // /* No parent. This process exists but we are missing all information
435 // // about
436 // // its creation. The birth time is set to zero but we remember the time of
437 // // insertion */
438 // //
439 // // else {
440 // // process->ppid = 0;
441 // // process->creation_time = ltt_time_zero;
442 // // }
443 // //
444 // // process->insertion_time = *timestamp;
445 // // sprintf(buffer,"%d-%lu.%lu",pid, process->creation_time.tv_sec,
446 // // process->creation_time.tv_nsec);
447 // // process->pid_time = g_quark_from_string(buffer);
448 // // process->cpu = cpu;
449 // // process->free_events = 0;
450 // // //process->last_cpu = tfs->cpu_name;
451 // // //process->last_cpu_index =
452 // // ltt_tracefile_num(((LttvTracefileContext*)tfs)->tf);
453 // // process->execution_stack = g_array_sized_new(FALSE, FALSE,
454 // // sizeof(LttvExecutionState), PREALLOCATED_EXECUTION_STACK);
455 // // process->execution_stack = g_array_set_size(process->execution_stack, 2);
456 // // es = process->state = &g_array_index(process->execution_stack,
457 // // LttvExecutionState, 0);
458 // // es->t = LTTV_STATE_USER_MODE;
459 // // es->n = LTTV_STATE_SUBMODE_NONE;
460 // // es->entry = *timestamp;
461 // // //g_assert(timestamp->tv_sec != 0);
462 // // es->change = *timestamp;
463 // // es->cum_cpu_time = ltt_time_zero;
464 // // es->s = LTTV_STATE_RUN;
465 // //
466 // // es = process->state = &g_array_index(process->execution_stack,
467 // // LttvExecutionState, 1);
468 // // es->t = LTTV_STATE_SYSCALL;
469 // // es->n = LTTV_STATE_SUBMODE_NONE;
470 // // es->entry = *timestamp;
471 // // //g_assert(timestamp->tv_sec != 0);
472 // // es->change = *timestamp;
473 // // es->cum_cpu_time = ltt_time_zero;
474 // // es->s = LTTV_STATE_WAIT_FORK;
475 // //
476 // // /* Allocate an empty function call stack. If it's empty, use 0x0. */
477 // // process->user_stack = g_array_sized_new(FALSE, FALSE,
478 // // sizeof(guint64), 0);
479 // //
480 // // return process;
481 // // }
482 //
483 // /**
484 // * Find the process matching the given pid and cpu
485 // *
486 // * If cpu is 0, the cpu value is not matched and the selection is based on
487 // * pid value only
488 // *
489 // * @param ts
490 // * @param cpu
491 // * @param pid
492 // * @return
493 // */
494 // protected LttngProcessState lttv_state_find_process(LttngTraceState ts,
495 // Long cpu, Long pid) {
496 // // Define the return value
497 // LttngProcessState process = null;
498 //
499 // // Obtain the list of available processes
500 // List<LttngProcessState> processList = ts.getProcesses();
501 //
502 // // find the process matching pid and cpu,
503 // // TODO: This may need to be improved since the pid may be re-used and
504 // // the creation time may need to be considered
505 // for (LttngProcessState dprocess : processList) {
506 // if (dprocess.getPid() == pid) {
507 // if (dprocess.getCpu() == cpu || cpu == 0) {
508 // return process;
509 // }
510 // }
511 // }
512 //
513 // return process;
514 // }
515 //
516 // /**
517 // * @param ts
518 // * @param cpu
519 // * @param pid
520 // * @param timestamp
521 // * , Used when a new process is needed
522 // * @return
523 // */
524 // protected LttngProcessState lttv_state_find_process_or_create(
525 // LttngTraceState ts, Long cpu, Long pid, final TmfTimestamp timestamp) {
526 //
527 // LttngProcessState process = lttv_state_find_process(ts, cpu, pid);
528 // /* Put ltt_time_zero creation time for unexisting processes */
529 // if (process == null) {
530 // process = create_process(ts, cpu, pid, 0L, timestamp);
531 // }
532 // return process;
533 // }
534 //
535 // /**
536 // * @param traceSt
537 // * @param cpu
538 // * @param pid
539 // * @param tgid
540 // * @param timestamp
541 // * @return
542 // */
543 // protected LttngProcessState create_process(LttngTraceState traceSt,
544 // Long cpu, Long pid, Long tgid, final TmfTimestamp timestamp) {
545 // LttngProcessState process = create_process(traceSt, cpu, pid, tgid,
546 // ProcessStatus.LTTV_STATE_UNNAMED.getInName(), timestamp);
547 // return process;
548 // }
549 //
550 // /**
551 // * @param traceSt
552 // * @param cpu
553 // * @param pid
554 // * @param tgid
555 // * @param name
556 // * @param timestamp
557 // * @return
558 // */
559 // protected LttngProcessState create_process(LttngTraceState traceSt,
560 // Long cpu, Long pid, Long tgid, String name,
561 // final TmfTimestamp timestamp) {
562 // LttngProcessState process;
563 // process = new LttngProcessState(cpu, pid, tgid, name, timestamp);
564 // traceSt.getProcesses().add(process);
565 // return process;
566 // }
567
568 }
This page took 0.051428 seconds and 6 git commands to generate.