public ProcfsBasedProcessTree getProcessTree ( ) { if ( pid != - 1 ) { // Get the list of processes List < Integer > processList = getProcessList ( ) ; Map < Integer , ProcessInfo > allProcessInfo = new HashMap < Integer , ProcessInfo > ( ) ; // cache the processTree to get the age for processes Map < Integer , ProcessInfo > oldProcs = new HashMap < Integer , ProcessInfo > ( processTree ) ; processTree . clear ( ) ; ProcessInfo me = null ; for ( Integer proc : processList ) { // Get information for each process ProcessInfo pInfo = new ProcessInfo ( proc ) ; if ( constructProcessInfo ( pInfo , procfsDir ) != null ) { allProcessInfo . put ( proc , pInfo ) ; if ( proc . equals ( this . pid ) ) { me = pInfo ; // cache 'me' processTree . put ( proc , pInfo ) ; } } } if ( me == null ) { return this ; } // Add each process to its parent. for ( Map . Entry < Integer , ProcessInfo > entry : allProcessInfo . entrySet ( ) ) { Integer pID = entry . getKey ( ) ; if ( pID != 1 ) { ProcessInfo pInfo = entry . getValue ( ) ; ProcessInfo parentPInfo = allProcessInfo . get ( pInfo . getPpid ( ) ) ; if ( parentPInfo != null ) { parentPInfo . addChild ( pInfo ) ; } } } // now start constructing the process-tree LinkedList < ProcessInfo > pInfoQueue = new LinkedList < ProcessInfo > ( ) ; pInfoQueue . addAll ( me . getChildren ( ) ) ; while ( ! pInfoQueue . isEmpty ( ) ) { ProcessInfo pInfo = pInfoQueue . remove ( ) ; if ( ! processTree . containsKey ( pInfo . getPid ( ) ) ) { processTree . put ( pInfo . getPid ( ) , pInfo ) ; } pInfoQueue . addAll ( pInfo . getChildren ( ) ) ; } // update age values and compute the number of jiffies since last update for ( Map . Entry < Integer , ProcessInfo > procs : processTree . entrySet ( ) ) { ProcessInfo oldInfo = oldProcs . get ( procs . getKey ( ) ) ; if ( procs . getValue ( ) != null ) { procs . getValue ( ) . updateJiffy ( oldInfo ) ; if ( oldInfo != null ) { procs . getValue ( ) . updateAge ( oldInfo ) ; } } } if ( LOG . isDebugEnabled ( ) ) { // Log.debug the ProcfsBasedProcessTree LOG . debug ( this . toString ( ) ) ; } } return this ; }
Get the process - tree with latest state . If the root - process is not alive an empty tree will be returned .