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;
00032 
00033 import java.io.FileNotFoundException;
00034 import java.io.IOException;
00035 import java.io.RandomAccessFile;
00036 
00037 
00038 public class LoadMonitorLinux{
00039     private double normaLoad;
00040     private RandomAccessFile statfile;
00041 
00042     public LoadMonitorLinux(LoadBalancer lb) {
00043        
00044         this.normaLoad = 1.0;
00045         
00046         int nProcessors = 0;
00047         try {
00048                 nProcessors = Runtime.getRuntime().availableProcessors();
00049                 if (nProcessors > 1) this.normaLoad = 1/(1.0 * nProcessors);
00050             statfile = new RandomAccessFile("/proc/loadavg", "r");
00051             calculateLoad();
00052         } catch (FileNotFoundException e) {
00053             e.printStackTrace();
00054         } catch (NumberFormatException e) {
00055             e.printStackTrace();
00056                 }
00057     }
00058 
00059     public synchronized double getLoad() {
00060         String cpuLine = null;
00061         try {
00062             statfile.seek(0);
00063             cpuLine = statfile.readLine();
00064         } catch (IOException e) {
00065             return 1;
00066         }
00067 
00068         double min1;
00069         
00070         java.util.StringTokenizer st = new java.util.StringTokenizer(cpuLine," ");
00071         min1 = Double.parseDouble(st.nextToken());
00072 
00073        return min1*normaLoad;
00074     }
00075 
00076     protected synchronized void calculateLoad() {
00077 
00078         double newload = getLoad();
00079        
00080     }
00081 }