+package and.Mapping ;
+
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.net.InetAddress;
+import java.util.ArrayList;
+
+import org.hyperic.sigar.CpuInfo;
+import org.hyperic.sigar.Mem;
+import org.hyperic.sigar.Sigar;
+import org.hyperic.sigar.SigarException;
+
+import com.thoughtworks.xstream.XStream;
+import com.thoughtworks.xstream.io.xml.DomDriver;
+
+
+
+/**
+ * Class providing some tools to the library
+ * @author Séastien Miquée
+ */
+public class Utils
+{
+// public static String getOS()
+// {
+// return System.getProperty( "os.name" ) ;
+// }
+
+
+ /**
+ * Creation of the representation of the node in the Mapping point of view. It needs
+ * some information about the computing node, which will be exploited by mapping
+ * algorithms.
+ * @return A node from the Mapping library
+ */
+ public static GNode createGNode()
+ {
+ GNode n = new GNode() ;
+ int nbCore = 0 ;
+ int frequency = 0 ;
+ int memory = 0 ;
+ String name = "" ;
+
+// InputStream ips ;
+// InputStreamReader ipsr ;
+// BufferedReader br ;
+// String ligne ;
+//
+//
+// String fichier_cpu = "" ;
+// String fichier_mem = "" ;
+//
+//
+// if( getOS().equals( "Linux" ) )
+// {
+// fichier_cpu = "/proc/cpuinfo" ;
+// fichier_mem = "/proc/meminfo" ;
+//
+// /* Lecture des informations processeur */
+// try{
+// ips = new FileInputStream( fichier_cpu ) ;
+// ipsr = new InputStreamReader( ips ) ;
+// br = new BufferedReader( ipsr ) ;
+//
+//
+// while( ( ligne = br.readLine() ) != null )
+// {
+// if( ligne.contains( "processor" ) )
+// {
+// nb_coeurs ++ ;
+// }
+//
+// if( ligne.contains("cpu MHz") )
+// {
+// String tab[] = new String[2] ;
+//
+// tab = ligne.split( ":" ) ;
+//
+// frequence = ( int ) Double.parseDouble( tab[1] ) ;
+// }
+//
+// }
+//
+// br.close() ;
+// ipsr.close() ;
+// ips.close() ;
+//
+// } catch( Exception e ) {
+// System.out.println( e.toString() ) ;
+// }
+//
+//
+// /* Lecture des informations mémoire */
+// try {
+// ips = new FileInputStream( fichier_mem ) ;
+// ipsr = new InputStreamReader( ips ) ;
+// br = new BufferedReader( ipsr ) ;
+//
+// while( ( ligne = br.readLine() ) != null )
+// {
+// if( ligne.contains("MemTotal") )
+// {
+// String tab[] = new String[2] ;
+//
+// tab = ligne.split( ":" ) ;
+// ligne = tab[1].trim() ;
+// tab = ligne.split( " " ) ;
+// memoire = ( int ) Double.parseDouble( tab[0] ) ;
+// }
+//
+// }
+//
+// br.close() ;
+// ipsr.close() ;
+// ips.close() ;
+//
+// } catch( Exception e ) {
+// System.out.println( e.toString() ) ;
+// }
+//
+// }
+
+ /* Creating the hardware information retriever */
+ Sigar sigar = new Sigar() ;
+
+ /* CPU information */
+ CpuInfo cpuinfo[] = null ;
+ try {
+ cpuinfo = sigar.getCpuInfoList();
+ } catch (SigarException e) {
+ System.err.println( "Unable to retrieve CPU information !" ) ;
+ e.printStackTrace();
+ System.exit( 1 ) ;
+ }
+
+ int tmp = 0 ;
+ for( int i = 0 ; i < cpuinfo.length ; i++ )
+ {
+ nbCore++ ;
+ tmp+= cpuinfo[i].getMhz() ;
+ }
+
+ /* The frequency is the average of cores frequencies */
+ frequency = tmp / nbCore ;
+
+
+ /* Memory information */
+ Mem mem = null ;
+ try {
+ mem = sigar.getMem() ;
+ } catch (SigarException e) {
+ System.err.println( "Unable to retrieve memory information !" ) ;
+ e.printStackTrace();
+ System.exit( 1 ) ;
+ }
+
+ memory = (int)mem.getFree()/1000 ;
+
+
+ /* Host name */
+ try {
+ InetAddress addr = InetAddress.getLocalHost() ;
+ name = new String( addr.getCanonicalHostName() ) ;
+ } catch( final Exception e ) {
+ System.err.println( "Unalbe to retrieve host name !" ) ;
+ e.printStackTrace();
+ System.exit( 1 ) ;
+ }
+
+
+ /* Updating node information */
+ n.setFrequency( frequency ) ;
+ n.setMemory( memory ) ;
+ n.setNb_cores( nbCore ) ;
+ n.setName( name ) ;
+
+ return n ;
+ }
+
+ /**
+ * Creation of the representation of the grid, according to clusters into sites.
+ * This representation may only fit on Grid'5000 like architectures (with computing
+ * nodes name based on the following pattern |cluster_name|id_of_node_into_cluster|.|site_of_cluster|.|organisation|.*|).
+ * @param _an the list of computing nodes connected
+ * @return the grid's architecture
+ */
+ public static Grid createGridG5k( ArrayList<GNode> _an )
+ {
+ Grid gr = new Grid() ;
+
+ if( _an != null )
+ {
+ String temp ;
+ String nom, cluster, site ;
+
+
+ ArrayList<Cluster> clusters = new ArrayList<Cluster>() ;
+
+ for( int i = 0 ; i < _an.size() ; i++ )
+ {
+ /* Variables edition */
+ String tab[] = new String[ 5 ] ;
+
+ temp = _an.get( i ).getName().trim().replace('.', '!' ) ;
+
+ tab = temp.split( "!" ) ;
+
+ nom = tab[ 0 ] ;
+ site = tab[ 1 ] ;
+
+ tab = nom.split( "-" ) ;
+
+ // IUT
+ //cluster = "iut" ;
+
+ // G5K
+ cluster = tab[ 0 ] ;
+
+ //System.out.println( nom + " dans " + cluster + " du site " + site ) ;
+
+
+ /* Insertion du noeud dans son cluster */
+ boolean trouve = false ;
+
+ for( int j = 0 ; j < clusters.size() ; j++ )
+ {
+ if( clusters.get( j ).getName().equals( cluster ) && clusters.get( j ).getSite().equals( site ))
+ {
+ _an.get( i ).setInCluster( true ) ;
+ _an.get( i ).setSite( clusters.get( j ).getSite() ) ;
+ _an.get( i ).setCluster( clusters.get( j ).getName() ) ;
+ clusters.get( j ).addGNode( _an.get( i ) ) ;
+ trouve = true ;
+
+ break ;
+ }
+ }
+
+ if( ! trouve )
+ {
+ Cluster cl = new Cluster() ;
+
+ cl.setName( cluster ) ;
+ cl.setSite( site ) ;
+
+ _an.get( i ).setInCluster( true ) ;
+ _an.get( i ).setSite( site ) ;
+ _an.get( i ).setCluster( cluster ) ;
+
+ cl.addGNode( _an.get( i ) ) ;
+
+ clusters.add( cl ) ;
+ }
+ }
+
+ gr.addClusters( clusters ) ;
+
+ }
+
+ return gr ;
+ }
+
+
+ /**
+ * Write the Grid object in an XML file.
+ * @param _gl Grid graph to be write
+ * @param _file File's name
+ * @param _path File's path
+ */
+ public static void writeGrid( Grid _gl, String _file, String _path )
+ {
+ if ( ! _file.equals( "" ) && ! _file.endsWith( ".xml" ) )
+ {
+ _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
+ }
+
+ if( ! _file.equals( "" ) )
+ {
+ String path = "" ;
+
+ if( _path.length() != 0 )
+ {
+ path = _path+"/"+_file ;
+ } else {
+ path = new String( "./" + _file ) ;
+ }
+
+ XStream xstream = new XStream( new DomDriver() ) ;
+
+ String xml = xstream.toXML( _gl ) ;
+
+ PrintWriter ecrivain = null ;
+
+ try {
+ ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
+
+ ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
+ ecrivain.println( xml ) ;
+
+ ecrivain.close() ;
+ } catch( Exception e ) {
+ System.err.println( "\nError during the write opération !\n" ) ;
+ e.printStackTrace() ;
+ }
+ }
+ }
+
+ /**
+ * Write an application Graph in a file.
+ * @param _gr Application Graph to be write
+ * @param _file File's name
+ * @param _path File's path
+ */
+ public static void writeGraph( Graph _gr, String _file, String _path )
+ {
+ if ( ! _file.equals( "" ) && ! _file.endsWith( ".xml" ) )
+ {
+ _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
+ }
+
+ if( ! _file.equals( "" ) )
+ {
+ String path = "" ;
+
+ if( _path.length() != 0 )
+ {
+ path = _path+"/"+_file ;
+ } else {
+ path = new String( "./" + _file ) ;
+ }
+
+ XStream xstream = new XStream( new DomDriver() ) ;
+
+ String xml = xstream.toXML( _gr ) ;
+
+ PrintWriter ecrivain = null ;
+
+ try {
+ ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
+
+ ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
+ ecrivain.println( xml ) ;
+
+ ecrivain.close() ;
+ } catch( Exception e ) {
+ System.err.println( "\nError during the write opération !\n" ) ;
+ e.printStackTrace() ;
+ }
+ }
+ }
+
+
+ /**
+ * Read an application Graph from a file.
+ * @param _file File's name
+ * @return The application Graph read
+ */
+ public static Graph readGraph( String _file )
+ {
+ if ( _file.equals( "" ) || ! _file.endsWith( ".xml" ) )
+ {
+ System.err.println( "Bad file !\n" ) ;
+ return null ;
+ }
+
+ Graph gr = null ;
+
+ XStream xstream = new XStream( new DomDriver() ) ;
+
+ try {
+ gr = (Graph) xstream.fromXML( new FileInputStream( _file ) ) ;
+ } catch( FileNotFoundException e ) {
+ System.err.println( "File not found !\n" ) ;
+ e.printStackTrace();
+ return null ;
+ } catch( ClassCastException e ) {
+ System.err.println( "The file does not contain a Graph" ) ;
+ e.printStackTrace() ;
+ return null ;
+ }
+
+ return gr ;
+ }
+
+
+ /**
+ * Read a Grid graph from a file.
+ * @param _file File's name
+ * @return The Grid graph read
+ */
+ public static Grid readGrid( String _file )
+ {
+ if ( _file.equals( "" ) || ! _file.endsWith( ".xml" ) )
+ {
+ System.err.println( "Bad file !\n" ) ;
+ return null ;
+ }
+
+ Grid gr = null ;
+
+ XStream xstream = new XStream( new DomDriver() ) ;
+
+ try {
+ gr = (Grid) xstream.fromXML( new FileInputStream( _file ) ) ;
+ } catch( FileNotFoundException e ) {
+ System.err.println( "File not found !\n" ) ;
+ e.printStackTrace();
+ return null ;
+ } catch( ClassCastException e ) {
+ System.err.println( "The file does not contain a Grid" ) ;
+ e.printStackTrace() ;
+ return null ;
+ }
+
+ return gr ;
+ }
+
+}
+
+
+/** La programmation est un art, respectons ceux qui la pratiquent !! **/