Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Modification of Makefile and Utils.
[mapping.git] / src / and / Mapping / Utils.java
index 2127186..25cd4a7 100644 (file)
@@ -1,6 +1,5 @@
 package and.Mapping ;
 
-
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
@@ -9,11 +8,6 @@ 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;
 
@@ -25,12 +19,6 @@ import com.thoughtworks.xstream.io.xml.DomDriver;
  */
 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
@@ -39,126 +27,37 @@ public class Utils
         */
        public static GNode createGNode()
        {
+               /** Using the Linpack class **/
+               Linpack linpack = new Linpack() ;
+               
+               /**  **/
                GNode n = new GNode() ;
                int nbCore = 0 ;
                int frequency = 0 ;
+               int mflops = 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 **/
                
-               /* 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 ) ;
-               }
+               /* Amount of available computing cores */
+               nbCore = Runtime.getRuntime().availableProcessors() ;
                
-               int tmp = 0 ;
-               for( int i = 0 ; i < cpuinfo.length ; i++ )
+               /* Average MegaFlops of each core */
+               double tmp_mflops = 0 ;
+               int loop = 10 ;
+               
+               for( int i = 0 ; i < loop ; i++ )
                {
-                       nbCore++ ;
-                       tmp+= cpuinfo[i].getMhz() ;
+                       tmp_mflops += linpack.getMFlops() ;
+                       System.gc() ;
                }
+               mflops = (int) tmp_mflops / loop ;
                
-               /* 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 ;
+               /** Memory information **/
+               memory = (int) Runtime.getRuntime().maxMemory() / 1024 / 1024 ;
                
                
                /* Host name */
@@ -166,7 +65,7 @@ public class Utils
                        InetAddress addr = InetAddress.getLocalHost() ;
                        name = new String( addr.getCanonicalHostName() ) ;
                } catch( final Exception e ) {
-                       System.err.println( "Unalbe to retrieve host name !" ) ;
+                       System.err.println( "Unalbe to retrieve host's name !" ) ;
                        e.printStackTrace();
                        System.exit( 1 ) ;
                } 
@@ -174,6 +73,7 @@ public class Utils
                
                /* Updating node information */
                n.setFrequency( frequency ) ;
+               n.setMFlops( mflops ) ;
                n.setMemory( memory ) ;
                n.setNb_cores( nbCore ) ;
                n.setName( name ) ;
@@ -273,7 +173,13 @@ public class Utils
         */
        public static void writeGrid( Grid _gl, String _file, String _path )
        {
-               if ( ! _file.equals( "" ) && ! _file.endsWith( ".xml" ) )
+               if( _file.equals( "" ) )
+               {
+                       System.err.println( "No file's name !\n" ) ;
+                       return ;
+               }
+               
+               if ( ! _file.endsWith( ".xml" ) )
                {
                        _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
                }
@@ -317,7 +223,13 @@ public class Utils
         */
        public static void writeGraph( Graph _gr, String _file, String _path )
        {
-               if ( ! _file.equals( "" ) && ! _file.endsWith( ".xml" ) )
+               if( _file.equals( "" ) )
+               {
+                       System.err.println( "No file's name !\n" ) ;
+                       return ;
+               }
+               
+               if ( ! _file.endsWith( ".xml" ) )
                {
                        _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
                }
@@ -357,28 +269,43 @@ public class Utils
        /**
         * Read an application Graph from a file.
         * @param _file File's name
+        * @param _path File's path
         * @return The application Graph read
         */
-       public static Graph readGraph( String _file )
+       public static Graph readGraph( String _path, String _file )
        {
-               if ( _file.equals( "" ) || ! _file.endsWith( ".xml" ) )
+               if ( _file.equals( "" ) )
                {
-                       System.err.println( "Bad file !\n" ) ;
+                       System.err.println( "No file's name !\n" ) ;
                        return null ;
                }       
                
+               if ( ! _file.endsWith( ".xml" ) )
+               {
+                       _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
+               }
+               
+               String path = "" ;
+               
+               if( _path.length() != 0 )
+               {
+                       path = _path+"/"+_file ;
+               } else {
+                       path = new String( "./" + _file ) ;
+               }       
+                       
                Graph gr = null ;
                
                XStream xstream = new XStream( new DomDriver() ) ;
                
                try {
-                       gr = (Graph) xstream.fromXML( new FileInputStream( _file ) ) ;
+                       gr = (Graph) xstream.fromXML( new FileInputStream( path ) ) ;
                } 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" ) ;
+                       System.err.println( "The file does not contain a valid Graph" ) ;
                        e.printStackTrace() ;
                        return null ;
                }
@@ -390,28 +317,43 @@ public class Utils
        /**
         * Read a Grid graph from a file.
         * @param _file File's name
+        * @param _path File's path
         * @return The Grid graph read
         */
-       public static Grid readGrid( String _file )
-       {
-               if ( _file.equals( "" ) || ! _file.endsWith( ".xml" ) )
+       public static Grid readGrid( String _path, String _file )
+       {       
+               if ( _file.equals( "" ) )
                {
-                       System.err.println( "Bad file !\n" ) ;
+                       System.err.println( "No file's name !\n" ) ;
                        return null ;
                }       
                
+               if ( ! _file.endsWith( ".xml" ) )
+               {
+                       _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
+               }
+
+               String path = "" ;
+               
+               if( _path.length() != 0 )
+               {
+                       path = _path+"/"+_file ;
+               } else {
+                       path = new String( "./" + _file ) ;
+               }       
+               
                Grid gr = null ;
                
                XStream xstream = new XStream( new DomDriver() ) ;
                
                try {
-                       gr = (Grid) xstream.fromXML( new FileInputStream( _file ) ) ;
+                       gr = (Grid) xstream.fromXML( new FileInputStream( path ) ) ;
                } 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" ) ;
+                       System.err.println( "The file does not contain a valid Grid" ) ;
                        e.printStackTrace() ;
                        return null ;
                }