Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2127186faf97fa357e412866a2885ff803ff437a
[mapping.git] / src / and / Mapping / Utils.java
1 package and.Mapping ;
2
3
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;
11
12 import org.hyperic.sigar.CpuInfo;
13 import org.hyperic.sigar.Mem;
14 import org.hyperic.sigar.Sigar;
15 import org.hyperic.sigar.SigarException;
16
17 import com.thoughtworks.xstream.XStream;
18 import com.thoughtworks.xstream.io.xml.DomDriver;
19
20
21
22 /**
23  * Class providing some tools to the library
24  * @author Séastien Miquée
25  */
26 public class Utils 
27 {
28 //      public static String getOS()
29 //      {
30 //              return System.getProperty( "os.name" ) ;
31 //      }
32         
33         
34         /**
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
37          * algorithms.
38          * @return A node from the Mapping library
39          */
40         public static GNode createGNode()
41         {
42                 GNode n = new GNode() ;
43                 int nbCore = 0 ;
44                 int frequency = 0 ;
45                 int memory = 0 ;
46                 String name = "" ;
47                 
48 //              InputStream ips ; 
49 //              InputStreamReader ipsr ;
50 //              BufferedReader br ;
51 //              String ligne ;
52 //              
53 //              
54 //              String fichier_cpu = "" ;
55 //              String fichier_mem = "" ;
56 //              
57 //              
58 //              if( getOS().equals( "Linux" ) )
59 //              {
60 //                      fichier_cpu = "/proc/cpuinfo" ;
61 //                      fichier_mem = "/proc/meminfo" ;         
62 //              
63 //                      /* Lecture des informations processeur */
64 //                      try{
65 //                              ips = new FileInputStream( fichier_cpu ) ; 
66 //                              ipsr = new InputStreamReader( ips ) ;
67 //                              br = new BufferedReader( ipsr ) ;
68 //                      
69 //                      
70 //                              while( ( ligne = br.readLine() ) != null )
71 //                              {
72 //                                      if( ligne.contains( "processor" ) )
73 //                                      {
74 //                                              nb_coeurs ++ ;
75 //                                      }       
76 //                              
77 //                                      if( ligne.contains("cpu MHz") )
78 //                                      {
79 //                                              String tab[] = new String[2] ;
80 //
81 //                                              tab = ligne.split( ":" ) ;
82 //                                      
83 //                                              frequence = ( int ) Double.parseDouble( tab[1] ) ;
84 //                                      }
85 //
86 //                              }
87 //                              
88 //                              br.close() ;
89 //                              ipsr.close() ;
90 //                              ips.close() ;
91 //                      
92 //                      } catch( Exception e ) {
93 //                              System.out.println( e.toString() ) ;
94 //                      }
95 //              
96 //              
97 //                      /* Lecture des informations mémoire */
98 //                      try {
99 //                              ips = new FileInputStream( fichier_mem ) ; 
100 //                              ipsr = new InputStreamReader( ips ) ;
101 //                              br = new BufferedReader( ipsr ) ;
102 //                      
103 //                              while( ( ligne = br.readLine() ) != null )
104 //                              {                               
105 //                                      if( ligne.contains("MemTotal") )
106 //                                      {
107 //                                              String tab[] = new String[2] ;
108 //                                      
109 //                                              tab = ligne.split( ":" ) ;
110 //                                              ligne = tab[1].trim() ;
111 //                                              tab = ligne.split( " " ) ;
112 //                                              memoire = ( int ) Double.parseDouble( tab[0] ) ;
113 //                                      }
114 //
115 //                              }
116 //                      
117 //                              br.close() ;
118 //                              ipsr.close() ;
119 //                              ips.close() ;
120 //                      
121 //                      } catch( Exception e ) {
122 //                              System.out.println( e.toString() ) ;
123 //                      }
124 //              
125 //              }
126                 
127                 /* Creating the hardware information retriever */
128                 Sigar sigar = new Sigar() ;
129                 
130                 /* CPU information */
131                 CpuInfo cpuinfo[] = null ;
132                 try {
133                         cpuinfo = sigar.getCpuInfoList();
134                 } catch (SigarException e) {
135                         System.err.println( "Unable to retrieve CPU information !" ) ;
136                         e.printStackTrace();
137                         System.exit( 1 ) ;
138                 }
139                 
140                 int tmp = 0 ;
141                 for( int i = 0 ; i < cpuinfo.length ; i++ )
142                 {
143                         nbCore++ ;
144                         tmp+= cpuinfo[i].getMhz() ;
145                 }
146                 
147                 /* The frequency is the average of cores frequencies */
148                 frequency = tmp / nbCore ;
149                 
150                 
151                 /* Memory information */
152                 Mem mem = null ;
153                 try {
154                         mem = sigar.getMem() ;
155                 } catch (SigarException e) {
156                         System.err.println( "Unable to retrieve memory information !" ) ;
157                         e.printStackTrace();
158                         System.exit( 1 ) ;
159                 }
160                 
161                 memory = (int)mem.getFree()/1000 ;
162                 
163                 
164                 /* Host name */
165                 try {
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 !" ) ;
170                         e.printStackTrace();
171                         System.exit( 1 ) ;
172                 } 
173
174                 
175                 /* Updating node information */
176                 n.setFrequency( frequency ) ;
177                 n.setMemory( memory ) ;
178                 n.setNb_cores( nbCore ) ;
179                 n.setName( name ) ;
180                 
181                 return n ;
182         }
183
184         /**
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
190          */
191         public static Grid createGridG5k( ArrayList<GNode> _an )
192         {
193                 Grid gr = new Grid() ;
194                 
195                 if( _an != null )
196                 {
197                         String temp ;
198                         String nom, cluster, site ;
199                 
200                         
201                         ArrayList<Cluster> clusters = new ArrayList<Cluster>() ;
202                         
203                         for( int i = 0 ; i < _an.size() ; i++ )
204                         {
205                                 /* Variables edition */
206                                 String tab[] = new String[ 5 ] ;                                
207                                 
208                                 temp = _an.get( i ).getName().trim().replace('.', '!' ) ;
209                                 
210                                 tab = temp.split( "!" ) ;
211                                 
212                                 nom = tab[ 0 ] ;
213                                 site = tab[ 1 ] ;
214                                 
215                                 tab = nom.split( "-" ) ;
216                                 
217                                 // IUT
218                                 //cluster = "iut" ;
219                                 
220                                 // G5K
221                                 cluster = tab[ 0 ] ;
222                                 
223                                 //System.out.println( nom + " dans " + cluster + " du site " + site ) ;
224                                 
225                                 
226                                 /* Insertion du noeud dans son cluster */
227                                 boolean trouve = false ;
228                                 
229                                 for( int j = 0 ; j < clusters.size() ; j++ )
230                                 {
231                                         if( clusters.get( j ).getName().equals( cluster ) && clusters.get( j ).getSite().equals( site ))
232                                         {
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 ) ) ;
237                                                 trouve  = true ;
238                                                 
239                                                 break ;
240                                         }
241                                 }
242                                 
243                                 if( ! trouve )
244                                 {
245                                         Cluster cl = new Cluster() ;
246                                         
247                                         cl.setName( cluster ) ;
248                                         cl.setSite( site ) ;
249                                         
250                                         _an.get( i ).setInCluster( true ) ;
251                                         _an.get( i ).setSite( site ) ;
252                                         _an.get( i ).setCluster( cluster ) ;
253                                         
254                                         cl.addGNode( _an.get( i ) ) ;
255                                         
256                                         clusters.add( cl ) ;
257                                 }       
258                         }
259                         
260                         gr.addClusters( clusters ) ;
261                         
262                 }
263                 
264                 return gr ;
265         }
266
267         
268         /**
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
273          */
274         public static void writeGrid( Grid _gl, String _file, String _path )
275         {
276                 if ( ! _file.equals( "" ) && ! _file.endsWith( ".xml" ) )
277                 {
278                         _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
279                 }
280                 
281                 if( ! _file.equals( "" ) )
282                 {
283                         String path = "" ;
284                         
285                         if( _path.length() != 0 )
286                         {
287                                 path = _path+"/"+_file ;
288                         } else {
289                                 path = new String( "./" + _file ) ;
290                         }
291                         
292                         XStream xstream = new XStream( new DomDriver() ) ;
293                         
294                         String xml = xstream.toXML( _gl ) ;
295                         
296                         PrintWriter ecrivain = null ;
297
298                         try {
299                                 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
300
301                                 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
302                                 ecrivain.println( xml ) ;
303
304                                 ecrivain.close() ;
305                         } catch( Exception e ) {
306                                 System.err.println( "\nError during the write opération !\n" ) ; 
307                                 e.printStackTrace() ;
308                         }
309                 }
310         }
311         
312         /**
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
317          */
318         public static void writeGraph( Graph _gr, String _file, String _path )
319         {
320                 if ( ! _file.equals( "" ) && ! _file.endsWith( ".xml" ) )
321                 {
322                         _file = _file + ".xml"; // On ajoute l'extension xml au nom du fichier
323                 }
324                 
325                 if( ! _file.equals( "" ) )
326                 {
327                         String path = "" ;
328                         
329                         if( _path.length() != 0 )
330                         {
331                                 path = _path+"/"+_file ;
332                         } else {
333                                 path = new String( "./" + _file ) ;
334                         }
335                         
336                         XStream xstream = new XStream( new DomDriver() ) ;
337                         
338                         String xml = xstream.toXML( _gr ) ;
339                         
340                         PrintWriter ecrivain = null ;
341
342                         try {
343                                 ecrivain = new PrintWriter( new OutputStreamWriter( new FileOutputStream( path ), "UTF8" ) ) ;
344
345                                 ecrivain.println( "<?xml version=\"1.0\" encoding=\"UTF8\"?>" ) ;
346                                 ecrivain.println( xml ) ;
347
348                                 ecrivain.close() ;
349                         } catch( Exception e ) {
350                                 System.err.println( "\nError during the write opération !\n" ) ;
351                                 e.printStackTrace() ;
352                         }
353                 }
354         }
355         
356         
357         /**
358          * Read an application Graph from a file.
359          * @param _file File's name
360          * @return The application Graph read
361          */
362         public static Graph readGraph( String _file )
363         {
364                 if ( _file.equals( "" ) || ! _file.endsWith( ".xml" ) )
365                 {
366                         System.err.println( "Bad file !\n" ) ;
367                         return null ;
368                 }       
369                 
370                 Graph gr = null ;
371                 
372                 XStream xstream = new XStream( new DomDriver() ) ;
373                 
374                 try {
375                         gr = (Graph) xstream.fromXML( new FileInputStream( _file ) ) ;
376                 } catch( FileNotFoundException e ) {
377                         System.err.println( "File not found !\n" ) ;
378                         e.printStackTrace();
379                         return null ;
380                 } catch( ClassCastException e ) {
381                         System.err.println( "The file does not contain a Graph" ) ;
382                         e.printStackTrace() ;
383                         return null ;
384                 }
385                 
386                 return gr ;
387         }
388
389         
390         /**
391          * Read a Grid graph from a file.
392          * @param _file File's name
393          * @return The Grid graph read
394          */
395         public static Grid readGrid( String _file )
396         {
397                 if ( _file.equals( "" ) || ! _file.endsWith( ".xml" ) )
398                 {
399                         System.err.println( "Bad file !\n" ) ;
400                         return null ;
401                 }       
402                 
403                 Grid gr = null ;
404                 
405                 XStream xstream = new XStream( new DomDriver() ) ;
406                 
407                 try {
408                         gr = (Grid) xstream.fromXML( new FileInputStream( _file ) ) ;
409                 } catch( FileNotFoundException e ) {
410                         System.err.println( "File not found !\n" ) ;
411                         e.printStackTrace();
412                         return null ;
413                 } catch( ClassCastException e ) {
414                         System.err.println( "The file does not contain a Grid" ) ;
415                         e.printStackTrace() ;
416                         return null ;
417                 }
418                 
419                 return gr ;
420         }
421         
422 }
423
424
425 /** La programmation est un art, respectons ceux qui la pratiquent !! **/