Dynamic Distribution Systems in Java

by Philip Rousselle



Listing One

import java.lang.Math;

import java.util.Random;

public class MonteCarloTest implements FofX {

    public double computeFof(double x)

    {

    return Math.sqrt(x);

    }

    public static void main(String[] args)

    {

      MonteCarlo     estimator = new MonteCarlo();

      MonteCarloTest function  = new MonteCarloTest();

      double answer;

      System.out.print("The area under the curve of the square ");

      System.out.print("root of x from 0 to 100 is ");

      answer = estimator.estimateIntegral(0.0, 100.0, 1000, 

                                                    new Random(), function);

      System.out.println(answer);

    }

}





Listing Two

import java.util.*;

public class MonteCarlo {

    public double estimateIntegral(double intervalStart, double intervalEnd, 

                              long iterations, Random random, FofX function)

    {

      double intermediate = 0.0;

      double intervalLength = intervalEnd - intervalStart;

      double x;

    for (int i = 0; i < iterations; i++)

    {

        x = intervalStart + (random.nextDouble() * intervalLength);

            intermediate += function.computeFof(x);

    }

    return (intermediate / iterations) * intervalLength;

    }

}





Listing Three

import java.lang.Math;

import java.util.Random;

import java.io.Serializable;

import COM.objectspace.voyager.*;



public class RemoteMonteCarloTest implements FofX, Serializable {

    public double computeFof(double x)

    {

        return Math.sqrt(x);

    }

    public static void main(String[] args)

      throws VoyagerException

    {

        VMonteCarlo estimator = null;

        try

        {

            estimator = (VMonteCarlo)

            VObject.forObjectAt("tme10.tivoli.com:7777/MCServer");

        }

        catch(ObjectNotFoundException e) { }

        if (estimator == null) 

        {

       estimator = new VMonteCarlo("tme10.tivoli.com:7777/MCServer");

           estimator.liveForever();

        }

        RemoteMonteCarloTest function  = new RemoteMonteCarloTest();

        double answer;

    System.out.print("The area under the curve of the square ");

    System.out.print("root of x from 0 to 100 is ");

    answer = estimator.estimateIntegral(0.0, 100.0, 1000,

                                              new Random(), function);

        System.out.println(answer);

    Voyager.shutdown();

    }

}





Listing Four

/* C implementation of a Java native method to determine if

 * the load on an IBM AIX workstation is LOW, MEDIUM or high

 */

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <time.h>

#include <rpcsvc/rstat.h>

#include <sys/param.h>

#include "SojournerNode.h"



#define LOW 0

#define MEDIUM 1

#define HIGH 2



int determine_instant_threshold(char *);



int determine_smooth_threshold(int, int, int, int *);

/*                             instant_threshold, old_instant_threshold, 

                               old_threshold, consecutive_change_cnt */  

void call_rstat(char *, float *);

/*              hostname, cpu_level                    */





/* Global data */

float cpu_limit = 200.0;

int current_threshold;

JNIEXPORT void JNICALL Java_SojournerNode_runNode

   (JNIEnv *env, jobject obj)

{

    char hostname[256];

    int old_threshold, instant_threshold, 

        old_instant_threshold, threshold_count;

    int loop_cnt;

    jclass clazz;

    jfieldID fid;

    threshold_count = 0;

    gethostname(hostname, sizeof(hostname));

    clazz = (*env)->GetObjectClass(env, obj);

    fid = (*env)->GetFieldID(env, clazz, "machineLoadLevel", "I");

    current_threshold = (*env)->GetIntField(env, obj, fid);

    while(1)

      {

        sleep(6);

        old_threshold = current_threshold;

        old_instant_threshold = instant_threshold;

        instant_threshold = determine_instant_threshold(hostname);

        current_threshold = determine_smooth_threshold(instant_threshold,

                    old_instant_threshold, old_threshold, &threshold_count);

        if (current_threshold != old_threshold)

          (*env)->SetIntField(env, obj, fid, current_threshold);

      }

}

int determine_instant_threshold(char *hostname)

{

    float new_cpu_level;

    call_rstat(hostname, &new_cpu_level);

    if ((new_cpu_level < (cpu_limit / 2.0))) return LOW;

    if (new_cpu_level < cpu_limit)           return MEDIUM;

    return HIGH;

}  

int determine_smooth_threshold(int instant_threshold, 

                        int old_instant_threshold, int old_smooth_threshold,

                        int *threshold_change_count)

{

    if (instant_threshold == old_smooth_threshold) 

      {

       *threshold_change_count = 0;



       return instant_threshold;

      }

    if (instant_threshold  == old_instant_threshold) 

                                              (*threshold_change_count)++;

    else {

           if (old_smooth_threshold == MEDIUM && 

                    (((instant_threshold == LOW) &&

                          (old_instant_threshold == HIGH)) ||

                     ((instant_threshold == HIGH) &&

                          (old_instant_threshold == LOW))))

             {

              *threshold_change_count = 1;

              return MEDIUM;

             }

           else *threshold_change_count++;

     }



     if (instant_threshold > old_smooth_threshold)

       {

        if (*threshold_change_count > 1)

          {

           *threshold_change_count = 0;

           return ++old_smooth_threshold;

          }

        else return old_smooth_threshold;

       }



     if (*threshold_change_count > 10) 

       {

        *threshold_change_count = 0;

        return --old_smooth_threshold;                    

       }

     return old_smooth_threshold;

} 

void call_rstat(char *hostname, float * cpu_queue_length)

{

   struct statstime rstat_output;

   rstat(hostname, &rstat_output);

   *cpu_queue_length = rstat_output.avenrun[0];

}

Listing Five

import java.lang.Math;

import java.util.Random;

import java.io.Serializable;

import COM.objectspace.voyager.*;



public class RemoteMonteCarloTest1 implements FofX, Serializable {

    public double computeFof(double x)

    {

   return Math.sqrt(x);

    }

    public static void main(String[] args)

    throws VoyagerException

    {

        VMonteCarlo estimator = null;

        try

        {

            estimator = (VMonteCarlo)

            VObject.forObjectAt(args[0] + ":7777/MCServer1");

        }

        catch(ObjectNotFoundException e) { }

        if (estimator == null) 

        {

           VSojournerMaster master = (VSojournerMaster)

               VObject.forObjectAt(args[0] + ":7777/sojournerNodeControl");

           estimator = new VMonteCarlo(args[0] + ":7777/MCServer1");

           estimator.liveForever();

           master.receiveGuest(estimator);

        }

    RemoteMonteCarloTest function  = new RemoteMonteCarloTest();

        double answer;

    System.out.print("The area under the curve of the square ");

    System.out.print("root of x from 0 to 100 is ");

    answer = estimator.estimateIntegral(0.0, 100.0, 1000,

                                            new Random(), function);

        System.out.println(answer);

    Voyager.shutdown();

    }

}



