1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Alvaro Sanchez-Leon (alvsan09@gmail.com) - Initial API and implementation
11 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.lttng
.core
.state
.model
;
14 import java
.util
.Stack
;
16 import org
.eclipse
.linuxtools
.lttng
.core
.LttngConstants
;
17 import org
.eclipse
.linuxtools
.lttng
.core
.TraceDebug
;
18 import org
.eclipse
.linuxtools
.lttng
.core
.state
.StateStrings
;
19 import org
.eclipse
.linuxtools
.lttng
.core
.state
.StateStrings
.ExecutionMode
;
20 import org
.eclipse
.linuxtools
.lttng
.core
.state
.StateStrings
.ExecutionSubMode
;
21 import org
.eclipse
.linuxtools
.lttng
.core
.state
.StateStrings
.ProcessStatus
;
24 * <b>LttngProcessState</b>
29 public class LttngProcessState
implements Cloneable
{
30 // ========================================================================
32 // =======================================================================
33 private Long cpu
= null;
34 private Long pid
= null;
35 private Long tgid
= null;
36 private String name
= null;
37 private Long creation_time
= null;
38 private String brand
= null;
39 private StateStrings
.ProcessType type
= null;
40 private Long current_function
= null;
41 private Long ppid
= null;
42 private Long insertion_time
= null;
43 private String pid_time
= null;
44 private Long free_events
= null;
45 private LttngExecutionState state
= null; // top of stack
46 private Stack
<LttngExecutionState
> execution_stack
= new Stack
<LttngExecutionState
>();
47 private Stack
<Long
> user_stack
= new Stack
<Long
>(); // user space
49 private String userTrace
= null; /* Associated file trace */
50 private Long target_pid
= null; /* target PID of the current event. */
51 private String trace_id
= null;
53 // ========================================================================
55 // =======================================================================
56 public LttngProcessState(Long startTime
, String traceId
) {
60 this.name
= StateStrings
.ProcessStatus
.LTTV_STATE_UNNAMED
.getInName();
61 this.insertion_time
= startTime
;
62 this.trace_id
= traceId
;
66 public LttngProcessState(Long cpu
, Long pid
, Long tgid
,
67 String name
, Long startTime
, String traceId
) {
72 this.insertion_time
= startTime
;
73 this.trace_id
= traceId
;
77 // ========================================================================
79 // =======================================================================
81 this.brand
= StateStrings
.LTTV_STATE_UNBRANDED
;
82 this.type
= StateStrings
.ProcessType
.LTTV_STATE_USER_THREAD
;
83 this.current_function
= 0L;
85 this.creation_time
= 0L;
86 this.free_events
= 0L;
89 LttngExecutionState es
= new LttngExecutionState();
90 es
.setExec_mode(ExecutionMode
.LTTV_STATE_USER_MODE
);
91 es
.setExec_submode(ExecutionSubMode
.LTTV_STATE_SUBMODE_NONE
.getInName());
92 // Note: For statistics performance improvement a integer representation of the submode is used
93 // as well as a bit mask is applied!
94 es
.setExec_submode_id(StateStrings
.ExecutionSubMode
.LTTV_STATE_SUBMODE_NONE
.ordinal() |
95 LttngConstants
.STATS_NONE_ID
);
96 es
.setEntry_Time(this.insertion_time
);
97 es
.setChange_Time(this.insertion_time
);
98 es
.setCum_cpu_time(0L);
99 es
.setProc_status(ProcessStatus
.LTTV_STATE_RUN
);
100 this.execution_stack
.push(es
);
102 //This second entry is needed when processes are created via a Fork event.
103 es
= new LttngExecutionState();
104 es
.setExec_mode(ExecutionMode
.LTTV_STATE_SYSCALL
);
105 es
.setExec_submode(ExecutionSubMode
.LTTV_STATE_SUBMODE_NONE
.getInName());
106 // Note: For statistics performance improvement a integer representation of the submode is used
107 // as well as a bit mask is applied!
108 es
.setExec_submode_id(StateStrings
.ExecutionSubMode
.LTTV_STATE_SUBMODE_NONE
.ordinal() |
109 LttngConstants
.STATS_NONE_ID
);
110 es
.setEntry_Time(this.insertion_time
);
111 es
.setChange_Time(this.insertion_time
);
112 es
.setCum_cpu_time(0L);
113 es
.setProc_status(ProcessStatus
.LTTV_STATE_WAIT_FORK
);
114 this.execution_stack
.push(es
);
116 // point state to the top of the stack
121 @SuppressWarnings("unchecked")
122 public LttngProcessState
clone() {
123 LttngProcessState newState
= null;
126 newState
= (LttngProcessState
)super.clone();
129 // Basic type in java are immutable!
130 // Thus, using assignment ("=") on basic type is CORRECT,
131 // but we should ALWAYS use "new" or "clone()" on "non basic" type
132 newState
.cpu
= this.cpu
;
133 newState
.pid
= this.pid
;
134 newState
.tgid
= this.tgid
;
135 newState
.name
= this.name
;
136 newState
.brand
= this.brand
;
137 newState
.type
= this.type
;
138 newState
.current_function
= this.current_function
;
139 newState
.ppid
= this.ppid
;
140 newState
.pid_time
= this.pid_time
;
141 newState
.free_events
= this.free_events
;
142 newState
.userTrace
= this.userTrace
;
143 newState
.target_pid
= this.target_pid
;
144 newState
.trace_id
= this.trace_id
;
145 newState
.creation_time
= this.creation_time
;
146 newState
.insertion_time
= this.insertion_time
;
148 // Call clone on our own object is safe as Long it implements Clonable
149 newState
.state
= (LttngExecutionState
)this.state
.clone();
151 // Clone should work correctly for all stack object that contain basic java object (String, Long, etc...)
152 newState
.user_stack
= (Stack
<Long
>)this.user_stack
.clone();
155 // This is worst case : Stack that contain user defined object. We have to unstack it and clone every object in a new stack!
156 // Why does java does not call clone() for every object in the stack it clone? It would probably be too useful...
157 newState
.execution_stack
= new Stack
<LttngExecutionState
>();
159 // Work stack we will use to "pop" item
160 Stack
<LttngExecutionState
> tmpStack
= new Stack
<LttngExecutionState
>();
162 // First, we pop every ExecutionState, and insert a CLONED copy into our new cloned stack
163 while ( this.execution_stack
.empty() == false ) {
164 // Save a copy of the original reference
165 tmpStack
.push(this.execution_stack
.peek());
166 // Push a CLONED copy into the new stack while poping it from the original stack
167 newState
.execution_stack
.push( this.execution_stack
.pop().clone() );
170 // Second, we reinsert back our content into the original stack
171 while ( tmpStack
.empty() == false ) {
172 // Pop the cloned copy and push it back into the original stack
173 this.execution_stack
.push( tmpStack
.pop() );
176 catch ( CloneNotSupportedException e
) {
177 System
.out
.println("Cloning failed with : " + e
.getMessage() ); //$NON-NLS-1$
184 // ========================================================================
186 // =======================================================================
190 public Long
getPid() {
198 public void setPid(Long pid
) {
205 public Long
getTgid() {
213 public void setTgid(Long tgid
) {
220 public Long
getPpid() {
228 public void setPpid(Long ppid
) {
234 * When the parent pid is known, the creation time is also known and
241 public void setPpid(Long ppid
, Long creationTime
) {
246 if (creationTime
!= null) {
247 setCreation_time(creationTime
);
252 * @return the creation_time
254 public Long
getCreation_time() {
255 return creation_time
;
259 * @param creationTime
260 * the creation_time to set
262 public void setCreation_time(Long creationTime
) {
263 if ( (creationTime
!= null) && (pid
!= null) ) {
264 creation_time
= creationTime
;
265 StringBuilder sb
= new StringBuilder(this.pid
.toString() + "-" //$NON-NLS-1$
266 + creationTime
.toString());
267 this.pid_time
= sb
.toString();
272 * @return the insertion_time
274 public Long
getInsertion_time() {
275 return insertion_time
;
279 * @param insertionTime
280 * the insertion_time to set
282 public void setInsertion_time(Long insertionTime
) {
283 insertion_time
= insertionTime
;
289 public String
getName() {
297 public void setName(String name
) {
304 public String
getBrand() {
312 public void setBrand(String brand
) {
317 * @return the prid_time
319 public String
getPid_time() {
326 public Long
getCpu() {
334 public void setCpu(Long cpu
) {
339 * @return the current_function
341 public Long
getCurrent_function() {
342 return current_function
;
346 * @param currentFunction
347 * the current_function to set
349 public void setCurrent_function(Long currentFunction
) {
350 current_function
= currentFunction
;
354 * @return the target_pid
356 public Long
getTarget_pid() {
362 * the target_pid to set
364 public void setTarget_pid(Long targetPid
) {
365 target_pid
= targetPid
;
368 public String
getTrace_id() {
372 public void setTrace_id(String traceId
) {
377 * @return the free_events
379 public Long
getFree_events() {
385 * the free_events to set
387 public void setFree_events(Long freeEvents
) {
388 free_events
= freeEvents
;
392 * increment the nuber of free events
394 public void incrementFree_events() {
401 public LttngExecutionState
getState() {
409 public void setState(LttngExecutionState state
) {
416 public StateStrings
.ProcessType
getType() {
424 public void setType(StateStrings
.ProcessType type
) {
429 * @return the userTrace
431 public String
getUserTrace() {
437 * the userTrace to set
439 public void setUserTrace(String userTrace
) {
440 this.userTrace
= userTrace
;
444 public void clearUserStack() {
448 public void pushToUserStack(Long newState
) {
449 user_stack
.push(newState
);
452 public Long
popFromUserStack() {
453 if (user_stack
.size() <= 1) {
454 TraceDebug
.debug("Removing last item from user stack is not allowed! (popFromUserStack)"); //$NON-NLS-1$
458 return user_stack
.pop();
462 public Long
peekFromUserStack() {
463 return user_stack
.peek();
468 public void clearExecutionStack() {
469 execution_stack
.clear();
472 public void pushToExecutionStack(LttngExecutionState newState
) {
473 execution_stack
.push(newState
);
477 public LttngExecutionState
popFromExecutionStack() {
478 if (execution_stack
.size() <= 1) {
479 TraceDebug
.debug("Removing last item from execution stack is not allowed! (popFromExecutionStack)"); //$NON-NLS-1$
483 LttngExecutionState popedState
= execution_stack
.pop();
484 // adjust current state to the new top
485 setState(peekFromExecutionStack());
490 public LttngExecutionState
peekFromExecutionStack() {
491 return execution_stack
.peek();
494 public LttngExecutionState
getFirstElementFromExecutionStack() {
495 return execution_stack
.firstElement();
499 @SuppressWarnings("nls")
500 public String
toString() {
501 String stateSt
= state
.toString();
502 String eStackSt
= execution_stack
.toString();
504 return "[LttngProcessState: " + "cpu=" + cpu
+ ",pid=" + pid
+ ",tgid=" + tgid
+ ",name=" + name
+ ",ctime=" + creation_time
+
505 ",brand=" + brand
+ ",type=" + type
+ ",cfunc=" + current_function
+ ",ppid=" + ppid
+ ",itime=" + insertion_time
+ ",ptime=" + pid_time
+
506 ",fevents=" + free_events
+ ",state=" + stateSt
+ ",estack=" + eStackSt
+ ",ustack=" + user_stack
+ ",utrace=" + userTrace
+
507 ",tpid=" + target_pid
+ ",trace=" + trace_id
+ "]";