1 17 package org.apache.excalibur.util.system; 18 19 import java.io.BufferedReader ; 20 import java.io.FileReader ; 21 import java.util.Properties ; 22 import java.util.StringTokenizer ; 23 24 import org.apache.excalibur.util.CPUParser; 25 26 33 public final class Linux implements CPUParser 34 { 35 private final int m_processors; 36 private final String m_cpuInfo; 37 38 public Linux() 39 { 40 int procs = 1; 41 String info = ""; 42 43 try 44 { 45 BufferedReader reader = new BufferedReader ( new FileReader ( "/proc/cpuinfo" ) ); 46 procs = 0; 47 48 Properties props = new Properties (); 49 String line = null; 50 51 while( ( line = reader.readLine() ) != null ) 52 { 53 String [] args = split( line, ":\t" ); 54 55 if( args.length > 1 ) 56 { 57 props.setProperty( args[ 0 ].trim(), args[ 1 ].trim() ); 58 if( args[ 0 ].trim().equals( "processor" ) ) 59 { 60 procs++; 61 } 62 } 63 } 64 65 StringBuffer buf = new StringBuffer (); 66 buf.append( props.getProperty( "model name" ) ); 67 buf.append( " Family " ); 68 buf.append( props.getProperty( "cpu family" ) ); 69 buf.append( " Model " ); 70 buf.append( props.getProperty( "model" ) ); 71 buf.append( " Stepping " ); 72 buf.append( props.getProperty( "stepping" ) ); 73 buf.append( ", " ); 74 buf.append( props.getProperty( "vendor_id" ) ); 75 76 info = buf.toString(); 77 } 78 catch( Exception e ) 79 { 80 procs = 1; 81 e.printStackTrace(); 82 } 83 84 m_processors = procs; 85 m_cpuInfo = info; 86 } 87 88 91 public int numProcessors() 92 { 93 return m_processors; 94 } 95 96 102 public String cpuInfo() 103 { 104 return m_cpuInfo; 105 } 106 107 114 private static final String [] split( final String string, final String onToken ) 115 { 116 final StringTokenizer tokenizer = new StringTokenizer ( string, onToken ); 117 final String [] result = new String [ tokenizer.countTokens() ]; 118 119 for( int i = 0; i < result.length; i++ ) 120 { 121 result[ i ] = tokenizer.nextToken(); 122 } 123 124 return result; 125 } 126 } 127 128 | Popular Tags |