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
.state
.model
;
14 import java
.util
.Stack
;
16 import org
.eclipse
.linuxtools
.lttng
.TraceDebug
;
17 import org
.eclipse
.linuxtools
.lttng
.state
.StateStrings
;
18 import org
.eclipse
.linuxtools
.lttng
.state
.StateStrings
.ExecutionMode
;
19 import org
.eclipse
.linuxtools
.lttng
.state
.StateStrings
.ExecutionSubMode
;
20 import org
.eclipse
.linuxtools
.lttng
.state
.StateStrings
.ProcessStatus
;
23 * <b>LttngProcessState</b>
28 public class LttngProcessState
implements Cloneable
{
29 // ========================================================================
31 // =======================================================================
32 private Long cpu
= null;
33 private Long pid
= null;
34 private Long tgid
= null;
35 private String name
= null;
36 private Long creation_time
= null;
37 private String brand
= null;
38 private StateStrings
.ProcessType type
= null;
39 private Long current_function
= null;
40 private Long ppid
= null;
41 private Long insertion_time
= null;
42 private String pid_time
= null;
43 private Long free_events
= null;
44 private LttngExecutionState state
= null; // top of stack
45 private Stack
<LttngExecutionState
> execution_stack
= new Stack
<LttngExecutionState
>();
46 private Stack
<Long
> user_stack
= new Stack
<Long
>(); // user space
48 private String userTrace
= null; /* Associated file trace */
49 private Long target_pid
= null; /* target PID of the current event. */
50 private String trace_id
= null;
52 // ========================================================================
54 // =======================================================================
55 public LttngProcessState(Long startTime
, String traceId
) {
59 this.name
= StateStrings
.ProcessStatus
.LTTV_STATE_UNNAMED
.getInName();
60 this.insertion_time
= startTime
;
61 this.trace_id
= traceId
;
65 public LttngProcessState(Long cpu
, Long pid
, Long tgid
,
66 String name
, Long startTime
, String traceId
) {
71 this.insertion_time
= startTime
;
72 this.trace_id
= traceId
;
76 // ========================================================================
78 // =======================================================================
80 this.brand
= StateStrings
.LTTV_STATE_UNBRANDED
;
81 this.type
= StateStrings
.ProcessType
.LTTV_STATE_USER_THREAD
;
82 this.current_function
= 0L;
84 this.creation_time
= 0L;
85 this.free_events
= 0L;
88 LttngExecutionState es
= new LttngExecutionState();
89 es
.setExec_mode(ExecutionMode
.LTTV_STATE_USER_MODE
);
90 es
.setExec_submode(ExecutionSubMode
.LTTV_STATE_SUBMODE_NONE
.getInName());
91 es
.setEntry_Time(this.insertion_time
);
92 es
.setChange_Time(this.insertion_time
);
93 es
.setCum_cpu_time(0L);
94 es
.setProc_status(ProcessStatus
.LTTV_STATE_RUN
);
95 this.execution_stack
.push(es
);
97 //This second entry is needed when processes are created via a Fork event.
98 es
= new LttngExecutionState();
99 es
.setExec_mode(ExecutionMode
.LTTV_STATE_SYSCALL
);
101 .setExec_submode(ExecutionSubMode
.LTTV_STATE_SUBMODE_NONE
103 es
.setEntry_Time(this.insertion_time
);
104 es
.setChange_Time(this.insertion_time
);
105 es
.setCum_cpu_time(0L);
106 es
.setProc_status(ProcessStatus
.LTTV_STATE_WAIT_FORK
);
107 this.execution_stack
.push(es
);
109 // point state to the top of the stack
114 @SuppressWarnings("unchecked")
115 public LttngProcessState
clone() {
116 LttngProcessState newState
= null;
119 newState
= (LttngProcessState
)super.clone();
122 // Basic type in java are immutable!
123 // Thus, using assignment ("=") on basic type is CORRECT,
124 // but we should ALWAYS use "new" or "clone()" on "non basic" type
125 newState
.cpu
= this.cpu
;
126 newState
.pid
= this.pid
;
127 newState
.tgid
= this.tgid
;
128 newState
.name
= this.name
;
129 newState
.brand
= this.brand
;
130 newState
.type
= this.type
;
131 newState
.current_function
= this.current_function
;
132 newState
.ppid
= this.ppid
;
133 newState
.pid_time
= this.pid_time
;
134 newState
.free_events
= this.free_events
;
135 newState
.userTrace
= this.userTrace
;
136 newState
.target_pid
= this.target_pid
;
137 newState
.trace_id
= this.trace_id
;
138 newState
.creation_time
= this.creation_time
;
139 newState
.insertion_time
= this.insertion_time
;
141 // Call clone on our own object is safe as Long it implements Clonable
142 newState
.state
= (LttngExecutionState
)this.state
.clone();
144 // Clone should work correctly for all stack object that contain basic java object (String, Long, etc...)
145 newState
.user_stack
= (Stack
<Long
>)this.user_stack
.clone();
148 // This is worst case : Stack that contain user defined object. We have to unstack it and clone every object in a new stack!
149 // Why does java does not call clone() for every object in the stack it clone? It would probably be too useful...
150 newState
.execution_stack
= new Stack
<LttngExecutionState
>();
152 // Work stack we will use to "pop" item
153 Stack
<LttngExecutionState
> tmpStack
= new Stack
<LttngExecutionState
>();
155 // First, we pop every ExecutionState, and insert a CLONED copy into our new cloned stack
156 while ( this.execution_stack
.empty() == false ) {
157 // Save a copy of the original reference
158 tmpStack
.push(this.execution_stack
.peek());
159 // Push a CLONED copy into the new stack while poping it from the original stack
160 newState
.execution_stack
.push( this.execution_stack
.pop().clone() );
163 // Second, we reinsert back our content into the original stack
164 while ( tmpStack
.empty() == false ) {
165 // Pop the cloned copy and push it back into the original stack
166 this.execution_stack
.push( tmpStack
.pop() );
169 catch ( CloneNotSupportedException e
) {
170 System
.out
.println("Cloning failed with : " + e
.getMessage() );
177 // ========================================================================
179 // =======================================================================
183 public Long
getPid() {
191 public void setPid(Long pid
) {
198 public Long
getTgid() {
206 public void setTgid(Long tgid
) {
213 public Long
getPpid() {
221 public void setPpid(Long ppid
) {
227 * When the parent pid is known, the creation time is also known and
234 public void setPpid(Long ppid
, Long creationTime
) {
239 if (creationTime
!= null) {
240 setCreation_time(creationTime
);
245 * @return the creation_time
247 public Long
getCreation_time() {
248 return creation_time
;
252 * @param creationTime
253 * the creation_time to set
255 public void setCreation_time(Long creationTime
) {
256 if ( (creationTime
!= null) && (pid
!= null) ) {
257 creation_time
= creationTime
;
258 StringBuilder sb
= new StringBuilder(this.pid
.toString() + "-"
259 + creationTime
.toString());
260 this.pid_time
= sb
.toString();
265 * @return the insertion_time
267 public Long
getInsertion_time() {
268 return insertion_time
;
272 * @param insertionTime
273 * the insertion_time to set
275 public void setInsertion_time(Long insertionTime
) {
276 insertion_time
= insertionTime
;
282 public String
getName() {
290 public void setName(String name
) {
297 public String
getBrand() {
305 public void setBrand(String brand
) {
310 * @return the prid_time
312 public String
getPid_time() {
319 public Long
getCpu() {
327 public void setCpu(Long cpu
) {
332 * @return the current_function
334 public Long
getCurrent_function() {
335 return current_function
;
339 * @param currentFunction
340 * the current_function to set
342 public void setCurrent_function(Long currentFunction
) {
343 current_function
= currentFunction
;
347 * @return the target_pid
349 public Long
getTarget_pid() {
355 * the target_pid to set
357 public void setTarget_pid(Long targetPid
) {
358 target_pid
= targetPid
;
361 public String
getTrace_id() {
365 public void setTrace_id(String traceId
) {
370 * @return the free_events
372 public Long
getFree_events() {
378 * the free_events to set
380 public void setFree_events(Long freeEvents
) {
381 free_events
= freeEvents
;
385 * increment the nuber of free events
387 public void incrementFree_events() {
394 public LttngExecutionState
getState() {
402 public void setState(LttngExecutionState state
) {
409 public StateStrings
.ProcessType
getType() {
417 public void setType(StateStrings
.ProcessType type
) {
422 * @return the userTrace
424 public String
getUserTrace() {
430 * the userTrace to set
432 public void setUserTrace(String userTrace
) {
433 this.userTrace
= userTrace
;
437 public void clearUserStack() {
441 public void pushToUserStack(Long newState
) {
442 user_stack
.push(newState
);
445 public Long
popFromUserStack() {
446 if (user_stack
.size() <= 1) {
447 TraceDebug
.debug("Removing last item from user stack is not allowed! (popFromUserStack)");
451 return user_stack
.pop();
455 public Long
peekFromUserStack() {
456 return user_stack
.peek();
461 public void clearExecutionStack() {
462 execution_stack
.clear();
465 public void pushToExecutionStack(LttngExecutionState newState
) {
466 execution_stack
.push(newState
);
470 public LttngExecutionState
popFromExecutionStack() {
471 if (execution_stack
.size() <= 1) {
472 TraceDebug
.debug("Removing last item from execution stack is not allowed! (popFromExecutionStack)");
476 LttngExecutionState popedState
= execution_stack
.pop();
477 // adjust current state to the new top
478 setState(peekFromExecutionStack());
483 public LttngExecutionState
peekFromExecutionStack() {
484 return execution_stack
.peek();
487 public LttngExecutionState
getFirstElementFromExecutionStack() {
488 return execution_stack
.firstElement();
492 @SuppressWarnings("nls")
493 public String
toString() {
494 String stateSt
= state
.toString();
495 String eStackSt
= execution_stack
.toString();
497 return "[LttngProcessState: " + "cpu=" + cpu
+ ",pid=" + pid
+ ",tgid=" + tgid
+ ",name=" + name
+ ",ctime=" + creation_time
+
498 ",brand=" + brand
+ ",type=" + type
+ ",cfunc=" + current_function
+ ",ppid=" + ppid
+ ",itime=" + insertion_time
+ ",ptime=" + pid_time
+
499 ",fevents=" + free_events
+ ",state=" + stateSt
+ ",estack=" + eStackSt
+ ",ustack=" + user_stack
+ ",utrace=" + userTrace
+
500 ",tpid=" + target_pid
+ ",trace=" + trace_id
+ "]";