HEX
Server: Apache/2.4.34 (Red Hat) OpenSSL/1.0.2k-fips
System: Linux WORDPRESS 3.10.0-1160.118.1.el7.x86_64 #1 SMP Thu Apr 4 03:33:23 EDT 2024 x86_64
User: digital (1020)
PHP: 7.2.24
Disabled: NONE
Upload Files
File: //opt/perf/examples/arm/jprimeudm.java
//*******************************************************************
// /opt/perf/examples/arm/jprimeudm.java
//
// This java program provides an example of how a simple program might
// make use of the ARM java wrappers.
//
// This program is similar to the "jprimenumbers" example, but this
// version makes use of User Defined Metrics as well.  See the ARM API
// Guide and Tracking Your Transactions documentation for details on
// UDMs.
//
// The program will prompt the user for a prime number, then will
// surround the calculation with an arm_start/arm_stop call.  The
// program will output whether the number was prime and the response
// time for the transaction.
//
// Some numbers which will make this think for a while include:
// 1772310923 (prime, just a few seconds)
// 1776769032023 (prime, from one to several minutes)
//
// Hewlett-Packard Performance Technology Center      08FEB01

import armapi.*;
import java.io.*;
import java.lang.NumberFormatException;
import java.lang.Thread;

public class jprimeudm {

  public static void main(String[] args) throws Exception
  {

    //
    // Initialize ARM with application and transaction IDs
    //
    ARMApplication primeapp = new ARMApplication("Prime_numbers_with_UDM","*");
    ARMTranDescription trandesc = new ARMTranDescription("Prime_Nums_UDM","Prime Numbs with UDMs");


    //
    // Set up metric's description and type (UDM meta-data)
    //
    trandesc.addMetric(1, ARMConstants.ARM_Gauge32,
                       "Metric 1: elapsed millisecs - is a GAUGE32");
    trandesc.addMetric(2, ARMConstants.ARM_Counter32,
                       "Metric 2: elapsed diffCntr - is a COUNTER32");
    trandesc.addMetric(3, ARMConstants.ARM_CntrDivr32,
                       "Metric 3: seconds elapsed - is a CNTR/DIV32");
    trandesc.addMetric(4, ARMConstants.ARM_Gauge64,
                       "Metric 4: current factor - is a GAUGE64");
    trandesc.addMetric(5, ARMConstants.ARM_Gauge64,
                       "Metric 5: factor limit - is a GAUGE64");
    trandesc.addMetric(6, ARMConstants.ARM_Gauge64,
                       "Metric 6: candidate prime - is a GAUGE64");
    trandesc.addStringMetric("Metric 7: is a STRING32");

    ARMTransaction mytran = primeapp.createTransaction(trandesc);
    ARMTransactionInstance thistran = mytran.createTransactionInstance();

    boolean continuing_process = true;  // always true
    InputStreamReader input = new InputStreamReader(System.in);
    BufferedReader buf = new BufferedReader(input);
    String Prime_candidate = new String();
    while ( continuing_process )
    {
      long candidateprime;   // number input by user
      long factor = 3;       // current factor being searched
      long factorlimit;      // high water for factor to search
      long current_time, start_time;  // holds time values in millisecs
      int elapsed_millisecs; // start minus current time
      boolean factorfound = false;   // flag to terminate search

      System.out.print("Please enter a number (1 to exit): ");

      Prime_candidate = buf.readLine();
      candidateprime = Long.parseLong(Prime_candidate);

      // transaction instnace begins once a number has been entered by the user

      //
      // Set initial UDMetric's data for this starting instance
      //
      ARMGauge32Metric met_1 = new ARMGauge32Metric(0);
      ARMCounter32Metric met_2 = new ARMCounter32Metric(0);
      int nDiv = 1000;  // divide milliseconds by 1000 to get float seconds
      ARMCntrDivr32Metric met_3 = new ARMCntrDivr32Metric(0, nDiv);
      ARMGauge64Metric met_4 = new ARMGauge64Metric(3);
      ARMGauge64Metric met_5 = new ARMGauge64Metric(0);
      ARMGauge64Metric met_6 = new ARMGauge64Metric(candidateprime);

      thistran.setMetricData(1, met_1);
      thistran.setMetricData(2, met_2);
      thistran.setMetricData(3, met_3);
      thistran.setMetricData(4, met_4);
      thistran.setMetricData(5, met_5);
      thistran.setMetricData(6, met_6);
      thistran.setStringMetricData("Input = " +  Prime_candidate);

      // start transaction instance
      thistran.startTran();

      // start timing
      elapsed_millisecs = 0;
      start_time = System.currentTimeMillis();

      // Check for exit requested:
      if ( candidateprime == 1 )
      {
        System.out.println("exiting.\n");
        thistran.stopTranInstance(ARMConstants.ARM_ABORT);
        primeapp.endARM();
        System.exit(0);
      }

      // check to see if reasonable number was input:
      if ( candidateprime <= 0 || candidateprime % 2 == 0 )
      {
        System.out.println("Please enter a positive, odd number.\n");
        thistran.stopTranInstance(ARMConstants.ARM_FAILED);
      }
      else
      {

        // The prime candidate was a reasonable number, so we'll start
        // looking for factors starting from 3 and going up to the
        // square root of the candidate.

        System.out.println();

        factorlimit = (long)java.lang.Math.sqrt(candidateprime);

        while( (factor <= factorlimit) &&
               (factorfound == false) )
        {

          // track current (and thus elapsed milliseconds
          current_time = System.currentTimeMillis();
          elapsed_millisecs = (int)(current_time - start_time);

          // update UDM data
            met_1.setData(elapsed_millisecs);
            met_2.setData(elapsed_millisecs);
            met_3.setData(elapsed_millisecs, nDiv);
            met_4.setData(factor);
            met_5.setData(factorlimit);
            thistran.setMetricData(1, met_1);
            thistran.setMetricData(2, met_2);
            thistran.setMetricData(3, met_3);
            thistran.setMetricData(4, met_4);
            thistran.setMetricData(5, met_5);
           //note metrics 6 and 7 stay the same so no need to update

          // update in-progress transaction with new data
          thistran.updateTranInstance();

          // check to see if we're finished
          if ( (candidateprime % factor) == 0 ) {
            factorfound = true;
          }
          else  {  // factor does not divide into candidate, continue search:
            factor = factor + 2;
          }   // end else

        } // end while searching for factor

        // After exiting from the while loop, print result
        if ( factorfound == false )
        {
          System.out.println(candidateprime  + " is a prime number.");
        }
        else
        {
          System.out.println(candidateprime + " is NOT a prime number.");
          System.out.println(candidateprime + " / " + factor + " = " +
                                  candidateprime / factor);
        }

        thistran.stopTranInstanceWithMetricUpdate(ARMConstants.ARM_GOOD);
        System.out.println("Transaction time: " +
                            elapsed_millisecs / 1000.0 + " seconds.\n");

      }   // end of else good number

    } // end of never-ending while loop

  } // end of main

} // end of class jprimeudm