org/objectweb/proactive/loadbalancing/metrics/CPURanking/LinuxCPURanking.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.loadbalancing.metrics.CPURanking;
00032 
00033 import java.io.*;
00034 
00035 import org.objectweb.proactive.loadbalancing.LoadBalancer;
00036 import org.objectweb.proactive.loadbalancing.LoadBalancingConstants;
00037 import org.objectweb.proactive.loadbalancing.metrics.Metric;
00038 
00039 
00040 public class LinuxCPURanking implements Metric {
00041     private double ranking = 1;
00042         private double normaLoad;
00043         private RandomAccessFile statfile;
00044 
00048     public LinuxCPURanking() {
00049         BufferedReader br;
00050         String line = null;
00051         try {
00052             br = new BufferedReader(new FileReader("/proc/cpuinfo"));
00053 
00054             while ((line = br.readLine()) != null) {
00055                 if (line.startsWith("cpu MHz")) {
00056                     String[] splited = line.split(":");
00057                     double cpuClock = Double.parseDouble(splited[1]); // obtaining the cpu clock
00058                     ranking = cpuClock / 1000;
00059                 }
00060             }
00061         } catch (FileNotFoundException e) {
00062             e.printStackTrace();
00063         } catch (NumberFormatException e) {
00064             e.printStackTrace();
00065         } catch (IOException e) {
00066             e.printStackTrace();
00067         }
00068         
00069         this.normaLoad = 1.0;
00070         //load = 0;
00071         int nProcessors = 0;
00072         try {
00073                 nProcessors = Runtime.getRuntime().availableProcessors();
00074                 if (nProcessors > 1) this.normaLoad = 1/(1.0 * nProcessors);
00075             statfile = new RandomAccessFile("/proc/loadavg", "r");
00076         } catch (FileNotFoundException e) {
00077             e.printStackTrace();
00078         } catch (NumberFormatException e) {
00079             e.printStackTrace();
00080                 }
00081     }
00082     
00083     public void setRanking(double x) {
00084         ranking = x;
00085     }
00086 
00087     public void addToRanking(double x) {
00088         ranking += x;
00089     }
00090     
00091 
00092     /* -- IMPLEMENTS METRIC -- */
00093     
00097     public double getRanking() {
00098         return ranking;
00099     }
00100 
00101     
00102         public void takeDecision(LoadBalancer lb) {
00103                 double load = getLoad();
00104                 if (load > LoadBalancingConstants.OVERLOADED_THREASHOLD) {
00105           lb.startBalancing();
00106         } else if (load < LoadBalancingConstants.UNDERLOADED_THREASHOLD) {
00107            lb.stealWork();
00108         }
00109                 
00110         }
00111 
00112         public double getLoad() {
00113                  String cpuLine = null;
00114                 try {
00115                     statfile.seek(0);
00116                     cpuLine = statfile.readLine();
00117                 } catch (IOException e) {
00118                     return 1;
00119                 }
00120 
00121                 double min1;
00122                 
00123                 java.util.StringTokenizer st = new java.util.StringTokenizer(cpuLine," ");
00124                 min1 = Double.parseDouble(st.nextToken());
00125 
00126                return min1*normaLoad;
00127         }
00128 }

Generated on Mon Jan 22 15:16:11 2007 for ProActive by  doxygen 1.5.1