4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.OutputStreamWriter;
8 import java.io.PrintWriter;
9 import java.net.InetAddress;
10 import java.util.ArrayList;
12 import org.hyperic.sigar.CpuInfo;
13 import org.hyperic.sigar.Mem;
14 import org.hyperic.sigar.Sigar;
15 import org.hyperic.sigar.SigarException;
17 import com.thoughtworks.xstream.XStream;
18 import com.thoughtworks.xstream.io.xml.DomDriver;
23 * Class providing some tools to the library
24 * @author Séastien Miquée
28 // public static String getOS()
30 // return System.getProperty( "os.name" ) ;
35 * Creation of the representation of the node in the Mapping point of view. It needs
36 * some information about the computing node, which will be exploited by mapping
38 * @return A node from the Mapping library
40 public static GNode createGNode()
42 GNode n = new GNode() ;
49 // InputStreamReader ipsr ;
50 // BufferedReader br ;
54 // String fichier_cpu = "" ;
55 // String fichier_mem = "" ;
58 // if( getOS().equals( "Linux" ) )
60 // fichier_cpu = "/proc/cpuinfo" ;
61 // fichier_mem = "/proc/meminfo" ;
63 // /* Lecture des informations processeur */
65 // ips = new FileInputStream( fichier_cpu ) ;
66 // ipsr = new InputStreamReader( ips ) ;
67 // br = new BufferedReader( ipsr ) ;
70 // while( ( ligne = br.readLine() ) != null )
72 // if( ligne.contains( "processor" ) )
77 // if( ligne.contains("cpu MHz") )
79 // String tab[] = new String[2] ;
81 // tab = ligne.split( ":" ) ;
83 // frequence = ( int ) Double.parseDouble( tab[1] ) ;
92 // } catch( Exception e ) {
93 // System.out.println( e.toString() ) ;
97 // /* Lecture des informations mémoire */
99 // ips = new FileInputStream( fichier_mem ) ;
100 // ipsr = new InputStreamReader( ips ) ;
101 // br = new BufferedReader( ipsr ) ;
103 // while( ( ligne = br.readLine() ) != null )
105 // if( ligne.contains("MemTotal") )
107 // String tab[] = new String[2] ;
109 // tab = ligne.split( ":" ) ;
110 // ligne = tab[1].trim() ;
111 // tab = ligne.split( " " ) ;
112 // memoire = ( int ) Double.parseDouble( tab[0] ) ;
121 // } catch( Exception e ) {
122 // System.out.println( e.toString() ) ;
127 /* Creating the hardware information retriever */
128 Sigar sigar = new Sigar() ;
130 /* CPU information */
131 CpuInfo cpuinfo[] = null ;
133 cpuinfo = sigar.getCpuInfoList();
134 } catch (SigarException e) {
135 System.err.println( "Unable to retrieve CPU information !" ) ;
141 for( int i = 0 ; i < cpuinfo.length ; i++ )
144 tmp+= cpuinfo[i].getMhz() ;
147 /* The frequency is the average of cores frequencies */
148 frequency = tmp / nbCore ;
151 /* Memory information */
154 mem = sigar.getMem() ;
155 } catch (SigarException e) {
156 System.err.println( "Unable to retrieve memory information !" ) ;
161 memory = (int)mem.getFree()/1000 ;
166 InetAddress addr = InetAddress.getLocalHost() ;
167 name = new String( addr.getCanonicalHostName() ) ;
168 } catch( final Exception e ) {
169 System.err.println( "Unalbe to retrieve host name !" ) ;
175 /* Updating node information */
176 n.setFrequency( frequency ) ;
177 n.setMemory( memory ) ;
178 n.setNb_cores( nbCore ) ;
185 * Creation of the representation of the grid, according to clusters into sites.
186 * This representation may only fit on Grid'5000 like architectures (with computing
187 * nodes name based on the following pattern |cluster_name|id_of_node_into_cluster|.|site_of_cluster|.|organisation|.*|).
188 * @param _an the list of computing nodes connected
189 * @return the grid's architecture
191 public static Grid createGridG5k( ArrayList<GNode> _an )
193 Grid gr = new Grid() ;
198 String nom, cluster, site ;
201 ArrayList<Cluster> clusters = new ArrayList<Cluster>() ;
203 for( int i = 0 ; i < _an.size() ; i++ )
205 /* Variables edition */
206 String tab[] = new String[ 5 ] ;
208 temp = _an.get( i ).getName().trim().replace('.', '!' ) ;
210 tab = temp.split( "!" ) ;
215 tab = nom.split( "-" ) ;
223 //System.out.println( nom + " dans " + cluster + " du site " + site ) ;
226 /* Insertion du noeud dans son cluster */
227 boolean trouve = false ;
229 for( int j = 0 ; j < clusters.size() ; j++ )
231 if( clusters.get( j ).getName().equals( cluster ) && clusters.get( j ).getSite().equals( site ))
233 _an.get( i ).setInCluster( true ) ;
234 _an.get( i ).setSite( clusters.get( j ).getSite() ) ;
235 _an.get( i ).setCluster( clusters.get( j ).getName() ) ;
236 clusters.get( j ).addGNode( _an.get( i ) ) ;
245 Cluster cl = new Cluster() ;
247 cl.setName( cluster ) ;
250 _an.get( i ).setInCluster( true ) ;
251 _an.get( i ).setSite( site ) ;
252 _an.get( i ).setCluster( cluster ) ;
254 cl.addGNode( _an.get( i ) ) ;
260 gr.addClusters( clusters ) ;
269 * Write the Grid object in an XML file.
270 * @param _gl Grid graph to be write
271 * @param _file File's name
272 * @param _path File's path
274 public static void writeGrid( Grid _gl, String _file, String _path )
276 if ( ! _file.equals( "" ) && ! _file.endsWith( ".xml" ) )
278 _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
281 if( ! _file.equals( "" ) )
285 if( _path.length() != 0 )
287 path = _path+"/"+_file ;
289 path = new String( "./" + _file ) ;
292 XStream xstream = new XStream( new DomDriver() ) ;
294 String xml = xstream.toXML( _gl ) ;
296 PrintWriter ecrivain = null ;
299 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
301 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
302 ecrivain.println( xml ) ;
305 } catch( Exception e ) {
306 System.err.println( "\nError during the write opération !\n" ) ;
307 e.printStackTrace() ;
313 * Write an application Graph in a file.
314 * @param _gr Application Graph to be write
315 * @param _file File's name
316 * @param _path File's path
318 public static void writeGraph( Graph _gr, String _file, String _path )
320 if ( ! _file.equals( "" ) && ! _file.endsWith( ".xml" ) )
322 _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
325 if( ! _file.equals( "" ) )
329 if( _path.length() != 0 )
331 path = _path+"/"+_file ;
333 path = new String( "./" + _file ) ;
336 XStream xstream = new XStream( new DomDriver() ) ;
338 String xml = xstream.toXML( _gr ) ;
340 PrintWriter ecrivain = null ;
343 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
345 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
346 ecrivain.println( xml ) ;
349 } catch( Exception e ) {
350 System.err.println( "\nError during the write opération !\n" ) ;
351 e.printStackTrace() ;
358 * Read an application Graph from a file.
359 * @param _file File's name
360 * @return The application Graph read
362 public static Graph readGraph( String _file )
364 if ( _file.equals( "" ) || ! _file.endsWith( ".xml" ) )
366 System.err.println( "Bad file !\n" ) ;
372 XStream xstream = new XStream( new DomDriver() ) ;
375 gr = (Graph) xstream.fromXML( new FileInputStream( _file ) ) ;
376 } catch( FileNotFoundException e ) {
377 System.err.println( "File not found !\n" ) ;
380 } catch( ClassCastException e ) {
381 System.err.println( "The file does not contain a Graph" ) ;
382 e.printStackTrace() ;
391 * Read a Grid graph from a file.
392 * @param _file File's name
393 * @return The Grid graph read
395 public static Grid readGrid( String _file )
397 if ( _file.equals( "" ) || ! _file.endsWith( ".xml" ) )
399 System.err.println( "Bad file !\n" ) ;
405 XStream xstream = new XStream( new DomDriver() ) ;
408 gr = (Grid) xstream.fromXML( new FileInputStream( _file ) ) ;
409 } catch( FileNotFoundException e ) {
410 System.err.println( "File not found !\n" ) ;
413 } catch( ClassCastException e ) {
414 System.err.println( "The file does not contain a Grid" ) ;
415 e.printStackTrace() ;
425 /** La programmation est un art, respectons ceux qui la pratiquent !! **/