00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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]);
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
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
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 }