KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > util > system > Linux


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.util.system;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.FileReader JavaDoc;
21 import java.util.Properties JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 import org.apache.excalibur.util.CPUParser;
25
26 /**
27  * Parses the Linux environment--Uses the proc filesystem to determine all the
28  * CPU information.
29  *
30  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
31  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:29 $
32  */

33 public final class Linux implements CPUParser
34 {
35     private final int m_processors;
36     private final String JavaDoc m_cpuInfo;
37
38     public Linux()
39     {
40         int procs = 1;
41         String JavaDoc info = "";
42
43         try
44         {
45             BufferedReader JavaDoc reader = new BufferedReader JavaDoc( new FileReader JavaDoc( "/proc/cpuinfo" ) );
46             procs = 0;
47
48             Properties JavaDoc props = new Properties JavaDoc();
49             String JavaDoc line = null;
50
51             while( ( line = reader.readLine() ) != null )
52             {
53                 String JavaDoc[] 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 JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc e )
79         {
80             procs = 1;
81             e.printStackTrace();
82         }
83
84         m_processors = procs;
85         m_cpuInfo = info;
86     }
87
88     /**
89      * Return the number of processors available on the machine
90      */

91     public int numProcessors()
92     {
93         return m_processors;
94     }
95
96     /**
97      * Return the cpu info for the processors (assuming symetric multiprocessing
98      * which means that all CPUs are identical). The format is:
99      *
100      * ${arch} family ${family} Model ${model} Stepping ${stepping}, ${identifier}
101      */

102     public String JavaDoc cpuInfo()
103     {
104         return m_cpuInfo;
105     }
106
107     /**
108      * Splits the string on every token into an array of strings.
109      *
110      * @param string the string
111      * @param onToken the token
112      * @return the resultant array
113      */

114     private static final String JavaDoc[] split( final String JavaDoc string, final String JavaDoc onToken )
115     {
116         final StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc( string, onToken );
117         final String JavaDoc[] result = new String JavaDoc[ 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