Exemplo n.º 1
0
 private long getUserTime() {
   if (bean.isCurrentThreadCpuTimeSupported()) {
     return bean.getCurrentThreadUserTime();
   } else {
     throw new RuntimeException("UserTime NOT Supported");
   }
 }
Exemplo n.º 2
0
  public CPUClock() {
    threadMXBean = ManagementFactory.getThreadMXBean();

    if (!threadMXBean.isCurrentThreadCpuTimeSupported()) {
      throw new IllegalStateException("Information about thread CPU times not supported");
    }
  }
Exemplo n.º 3
0
 /* Get system time in nanoseconds. */
 public static long getSystemTime() {
   ThreadMXBean bean = ManagementFactory.getThreadMXBean();
   return bean.isCurrentThreadCpuTimeSupported()
       ? (bean.getCurrentThreadCpuTime() - bean.getCurrentThreadUserTime())
       : 0L;
 }
  // main genetic algorithm process
  public static double[] genetic(int[][] state, double cost[], double[] parameters) {
    // record the start cpu time
    long time = 0;
    long bestTime = 0;
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    if (bean.isCurrentThreadCpuTimeSupported()) time = bean.getCurrentThreadCpuTime();

    // get the mutation parameters
    int mf = (int) parameters[0];
    int mc = (int) parameters[1];
    double mg = parameters[2];

    int numSet = state.length; // number of columns
    int numEle = state[0].length; // number of rows
    double totalFit = 0; // total fitness value of the population
    double avgFit = 0; // the average fitness value of the population
    int belowFit =
        0; // the number of solutions in the population with a fitness below the average fitness of
           // the population
    double bestFit = Double.MAX_VALUE; // the best fitness value of the population
    double[] results = new double[3]; // the results of the genetic set covering

    // sort the columns by their costs increasingly, same costs by the number of rows they can cover
    // decreasingly
    Integer[] sortedSet = new Integer[numSet];
    for (int i = 0; i < numSet; i++) sortedSet[i] = i;

    // sort the rows by the number of columns that can cover them
    Integer[] sortedEle = new Integer[numEle];
    for (int i = 0; i < numEle; i++) sortedEle[i] = i;

    int[] eleEle = new int[numEle]; // the rows removed
    int[] eleSet = new int[numSet]; // the columns removed

    // the number of columns that cover a row
    int[] weightSet = new int[numSet];
    for (int i = 0; i < numSet; i++) {
      for (int j = 0; j < numEle; j++) {
        if (state[i][j] != 0) {
          weightSet[i]++;
        }
      }

      if (weightSet[i] == 0) eleSet[i] = 1;
    }

    // the number of rows that a column can cover
    int[] weightEle = new int[numEle];
    for (int i = 0; i < numEle; i++) {
      for (int j = 0; j < numSet; j++) {
        if (state[j][i] != 0) {
          weightEle[i]++;
        }
      }

      if (weightEle[i] == 0) eleEle[i] = 1;
    }

    // sort the rows and columns
    HashMap<Integer, Tools.Set> cmpSet = new HashMap<Integer, Tools.Set>();
    for (int i = 0; i < numSet; i++) {
      Tools.Set s = new Tools.Set();
      s.cost = cost[i];
      s.count = weightSet[i];
      cmpSet.put(i, s);
    }

    HashMap<Integer, Integer> cmpEle = new HashMap<Integer, Integer>();
    for (int i = 0; i < numEle; i++) {
      cmpEle.put(i, weightEle[i]);
    }

    new Quicksort<Tools.Set, Integer>().sort(sortedSet, cmpSet, Tools.setCmp);
    new Quicksort<Integer, Integer>().sort(sortedEle, cmpEle, Tools.intCmp);

    // generate the elite columns
    int[] mutate = new int[numSet];
    for (int i = 0; i < numEle; i++) {
      if (eleEle[i] != 0) continue;

      int k = 0;
      if (weightEle[i] >= 5) k = 5;
      else k = weightEle[i];

      for (int j = 0; j < numSet; j++) {
        int index = sortedSet[j];
        if (eleSet[i] != 0) continue;

        if (state[index][i] != 0) {
          mutate[index] = 1;
          k--;
        }
        if (k == 0) break;
      }
    }

    // generate the initial population
    int[][] population = new int[100][numSet];
    for (int i = 0; i < numSet; i++) {
      if (eleSet[i] != 0) {
        for (int j = 0; j < 100; j++) {
          population[j][i] = -1;
        }
      }
    }

    population(state, eleEle, sortedSet, weightEle, population);

    // calculate the fitness information of the population
    double[] fitness = new double[100];
    for (int i = 0; i < 100; i++) {
      fitness[i] = fitness(population[i], cost);
      totalFit += fitness[i];
    }

    avgFit = totalFit / 100;
    for (int i = 0; i < 100; i++) {
      if (fitness[i] > avgFit) belowFit++;
      else if (fitness[i] < bestFit) {
        bestFit = fitness[i];
      }
    }

    // main genetic body
    int t = 0; // terminate until 100000 new descendants generated
    while (t < 100000) {
      int[] parents = tournament(population, fitness, 2); // tournament generate two parents

      // crossover to generate the child
      int[] candidate =
          crossover(
              population[parents[0]],
              population[parents[1]],
              fitness[parents[0]],
              fitness[parents[1]]);

      // mutate the child
      mutation(mf, mc, mg, t, candidate, mutate);

      // make the child a feasible solution
      int[] coverCount = new int[numEle];
      for (int i = 0; i < numEle; i++) {
        if (eleEle[i] != 0) coverCount[i] = -1;
      }
      for (int i = 0; i < numSet; i++) {
        if (eleSet[i] != 0) continue;
        if (candidate[i] > 0) {
          for (int j = 0; j < numEle; j++) {
            if (eleEle[j] != 0) continue;

            if (state[i][j] != 0) coverCount[j]++;
          }
        }
      }

      feasible(candidate, state, cost, eleSet, eleEle, sortedEle, sortedSet);

      // check if the child is a duplicate of the population, if it is not, update the fitness
      // information of the population
      // randomly select a solution in population with a below average fitness value, replace this
      // solution with the new child
      if (!duplicate(population, candidate)) {
        int index = -1;
        int k = RND.nextInt(belowFit);

        for (int i = 0; i < 100; i++) {
          if (fitness[i] > avgFit && k > 0) {
            k--;
          } else if (fitness[i] > avgFit && k == 0) {
            index = i;
          }
        }

        totalFit -= fitness[index];
        population[index] = candidate;
        fitness[index] = fitness(candidate, cost);
        totalFit += fitness[index];
        avgFit = totalFit / 100;
        belowFit = 0;
        for (int i = 0; i < 100; i++) {
          if (fitness[i] > avgFit) belowFit++;
        }
        if (fitness[index] < bestFit) {
          // record the cpu time that find the best solution
          if (bean.isCurrentThreadCpuTimeSupported())
            bestTime = bean.getCurrentThreadCpuTime() - time;
          bestFit = fitness[index];
        }

        t++;
      }
    }

    // calculate the end cpu time
    if (bean.isCurrentThreadCpuTimeSupported()) time = bean.getCurrentThreadCpuTime() - time;

    // return the cpu time and best result
    results[0] = bestFit;
    results[1] = bestTime;
    results[2] = time;

    return results;
  }
Exemplo n.º 5
0
  /**
   * Retrieve a JVM information text formatted using the given StringManager.
   *
   * @param requestedSm the StringManager to use
   * @return the formatted JVM information text
   */
  private static String getVMInfo(StringManager requestedSm) {
    StringBuilder sb = new StringBuilder();

    synchronized (timeformat) {
      sb.append(timeformat.format(new Date()));
    }
    sb.append(CRLF);

    sb.append(requestedSm.getString("diagnostics.vmInfoRuntime"));
    sb.append(":" + CRLF);
    sb.append(INDENT1 + "vmName: " + runtimeMXBean.getVmName() + CRLF);
    sb.append(INDENT1 + "vmVersion: " + runtimeMXBean.getVmVersion() + CRLF);
    sb.append(INDENT1 + "vmVendor: " + runtimeMXBean.getVmVendor() + CRLF);
    sb.append(INDENT1 + "specName: " + runtimeMXBean.getSpecName() + CRLF);
    sb.append(INDENT1 + "specVersion: " + runtimeMXBean.getSpecVersion() + CRLF);
    sb.append(INDENT1 + "specVendor: " + runtimeMXBean.getSpecVendor() + CRLF);
    sb.append(
        INDENT1 + "managementSpecVersion: " + runtimeMXBean.getManagementSpecVersion() + CRLF);
    sb.append(INDENT1 + "name: " + runtimeMXBean.getName() + CRLF);
    sb.append(INDENT1 + "startTime: " + runtimeMXBean.getStartTime() + CRLF);
    sb.append(INDENT1 + "uptime: " + runtimeMXBean.getUptime() + CRLF);
    sb.append(
        INDENT1 + "isBootClassPathSupported: " + runtimeMXBean.isBootClassPathSupported() + CRLF);
    sb.append(CRLF);

    sb.append(requestedSm.getString("diagnostics.vmInfoOs"));
    sb.append(":" + CRLF);
    sb.append(INDENT1 + "name: " + operatingSystemMXBean.getName() + CRLF);
    sb.append(INDENT1 + "version: " + operatingSystemMXBean.getVersion() + CRLF);
    sb.append(INDENT1 + "architecture: " + operatingSystemMXBean.getArch() + CRLF);
    sb.append(
        INDENT1 + "availableProcessors: " + operatingSystemMXBean.getAvailableProcessors() + CRLF);
    sb.append(
        INDENT1 + "systemLoadAverage: " + operatingSystemMXBean.getSystemLoadAverage() + CRLF);
    sb.append(CRLF);

    sb.append(requestedSm.getString("diagnostics.vmInfoThreadMxBean"));
    sb.append(":" + CRLF);
    sb.append(
        INDENT1
            + "isCurrentThreadCpuTimeSupported: "
            + threadMXBean.isCurrentThreadCpuTimeSupported()
            + CRLF);
    sb.append(
        INDENT1 + "isThreadCpuTimeSupported: " + threadMXBean.isThreadCpuTimeSupported() + CRLF);
    sb.append(INDENT1 + "isThreadCpuTimeEnabled: " + threadMXBean.isThreadCpuTimeEnabled() + CRLF);
    sb.append(
        INDENT1
            + "isObjectMonitorUsageSupported: "
            + threadMXBean.isObjectMonitorUsageSupported()
            + CRLF);
    sb.append(
        INDENT1
            + "isSynchronizerUsageSupported: "
            + threadMXBean.isSynchronizerUsageSupported()
            + CRLF);
    sb.append(
        INDENT1
            + "isThreadContentionMonitoringSupported: "
            + threadMXBean.isThreadContentionMonitoringSupported()
            + CRLF);
    sb.append(
        INDENT1
            + "isThreadContentionMonitoringEnabled: "
            + threadMXBean.isThreadContentionMonitoringEnabled()
            + CRLF);
    sb.append(CRLF);

    sb.append(requestedSm.getString("diagnostics.vmInfoThreadCounts"));
    sb.append(":" + CRLF);
    sb.append(INDENT1 + "daemon: " + threadMXBean.getDaemonThreadCount() + CRLF);
    sb.append(INDENT1 + "total: " + threadMXBean.getThreadCount() + CRLF);
    sb.append(INDENT1 + "peak: " + threadMXBean.getPeakThreadCount() + CRLF);
    sb.append(INDENT1 + "totalStarted: " + threadMXBean.getTotalStartedThreadCount() + CRLF);
    sb.append(CRLF);

    sb.append(requestedSm.getString("diagnostics.vmInfoStartup"));
    sb.append(":" + CRLF);
    for (String arg : runtimeMXBean.getInputArguments()) {
      sb.append(INDENT1 + arg + CRLF);
    }
    sb.append(CRLF);

    sb.append(requestedSm.getString("diagnostics.vmInfoPath"));
    sb.append(":" + CRLF);
    sb.append(INDENT1 + "bootClassPath: " + runtimeMXBean.getBootClassPath() + CRLF);
    sb.append(INDENT1 + "classPath: " + runtimeMXBean.getClassPath() + CRLF);
    sb.append(INDENT1 + "libraryPath: " + runtimeMXBean.getLibraryPath() + CRLF);
    sb.append(CRLF);

    sb.append(requestedSm.getString("diagnostics.vmInfoClassLoading"));
    sb.append(":" + CRLF);
    sb.append(INDENT1 + "loaded: " + classLoadingMXBean.getLoadedClassCount() + CRLF);
    sb.append(INDENT1 + "unloaded: " + classLoadingMXBean.getUnloadedClassCount() + CRLF);
    sb.append(INDENT1 + "totalLoaded: " + classLoadingMXBean.getTotalLoadedClassCount() + CRLF);
    sb.append(INDENT1 + "isVerbose: " + classLoadingMXBean.isVerbose() + CRLF);
    sb.append(CRLF);

    sb.append(requestedSm.getString("diagnostics.vmInfoClassCompilation"));
    sb.append(":" + CRLF);
    sb.append(INDENT1 + "name: " + compilationMXBean.getName() + CRLF);
    sb.append(
        INDENT1 + "totalCompilationTime: " + compilationMXBean.getTotalCompilationTime() + CRLF);
    sb.append(
        INDENT1
            + "isCompilationTimeMonitoringSupported: "
            + compilationMXBean.isCompilationTimeMonitoringSupported()
            + CRLF);
    sb.append(CRLF);

    for (MemoryManagerMXBean mbean : memoryManagerMXBeans) {
      sb.append(requestedSm.getString("diagnostics.vmInfoMemoryManagers", mbean.getName()));
      sb.append(":" + CRLF);
      sb.append(INDENT1 + "isValid: " + mbean.isValid() + CRLF);
      sb.append(INDENT1 + "mbean.getMemoryPoolNames: " + CRLF);
      String[] names = mbean.getMemoryPoolNames();
      Arrays.sort(names);
      for (String name : names) {
        sb.append(INDENT2 + name + CRLF);
      }
      sb.append(CRLF);
    }

    for (GarbageCollectorMXBean mbean : garbageCollectorMXBeans) {
      sb.append(requestedSm.getString("diagnostics.vmInfoGarbageCollectors", mbean.getName()));
      sb.append(":" + CRLF);
      sb.append(INDENT1 + "isValid: " + mbean.isValid() + CRLF);
      sb.append(INDENT1 + "mbean.getMemoryPoolNames: " + CRLF);
      String[] names = mbean.getMemoryPoolNames();
      Arrays.sort(names);
      for (String name : names) {
        sb.append(INDENT2 + name + CRLF);
      }
      sb.append(INDENT1 + "getCollectionCount: " + mbean.getCollectionCount() + CRLF);
      sb.append(INDENT1 + "getCollectionTime: " + mbean.getCollectionTime() + CRLF);
      sb.append(CRLF);
    }

    sb.append(requestedSm.getString("diagnostics.vmInfoMemory"));
    sb.append(":" + CRLF);
    sb.append(INDENT1 + "isVerbose: " + memoryMXBean.isVerbose() + CRLF);
    sb.append(
        INDENT1
            + "getObjectPendingFinalizationCount: "
            + memoryMXBean.getObjectPendingFinalizationCount()
            + CRLF);
    sb.append(formatMemoryUsage("heap", memoryMXBean.getHeapMemoryUsage()));
    sb.append(formatMemoryUsage("non-heap", memoryMXBean.getNonHeapMemoryUsage()));
    sb.append(CRLF);

    for (MemoryPoolMXBean mbean : memoryPoolMXBeans) {
      sb.append(requestedSm.getString("diagnostics.vmInfoMemoryPools", mbean.getName()));
      sb.append(":" + CRLF);
      sb.append(INDENT1 + "isValid: " + mbean.isValid() + CRLF);
      sb.append(INDENT1 + "getType: " + mbean.getType() + CRLF);
      sb.append(INDENT1 + "mbean.getMemoryManagerNames: " + CRLF);
      String[] names = mbean.getMemoryManagerNames();
      Arrays.sort(names);
      for (String name : names) {
        sb.append(INDENT2 + name + CRLF);
      }
      sb.append(INDENT1 + "isUsageThresholdSupported: " + mbean.isUsageThresholdSupported() + CRLF);
      try {
        sb.append(INDENT1 + "isUsageThresholdExceeded: " + mbean.isUsageThresholdExceeded() + CRLF);
      } catch (UnsupportedOperationException ex) {
        // IGNORE
      }
      sb.append(
          INDENT1
              + "isCollectionUsageThresholdSupported: "
              + mbean.isCollectionUsageThresholdSupported()
              + CRLF);
      try {
        sb.append(
            INDENT1
                + "isCollectionUsageThresholdExceeded: "
                + mbean.isCollectionUsageThresholdExceeded()
                + CRLF);
      } catch (UnsupportedOperationException ex) {
        // IGNORE
      }
      try {
        sb.append(INDENT1 + "getUsageThreshold: " + mbean.getUsageThreshold() + CRLF);
      } catch (UnsupportedOperationException ex) {
        // IGNORE
      }
      try {
        sb.append(INDENT1 + "getUsageThresholdCount: " + mbean.getUsageThresholdCount() + CRLF);
      } catch (UnsupportedOperationException ex) {
        // IGNORE
      }
      try {
        sb.append(
            INDENT1 + "getCollectionUsageThreshold: " + mbean.getCollectionUsageThreshold() + CRLF);
      } catch (UnsupportedOperationException ex) {
        // IGNORE
      }
      try {
        sb.append(
            INDENT1
                + "getCollectionUsageThresholdCount: "
                + mbean.getCollectionUsageThresholdCount()
                + CRLF);
      } catch (UnsupportedOperationException ex) {
        // IGNORE
      }
      sb.append(formatMemoryUsage("current", mbean.getUsage()));
      sb.append(formatMemoryUsage("collection", mbean.getCollectionUsage()));
      sb.append(formatMemoryUsage("peak", mbean.getPeakUsage()));
      sb.append(CRLF);
    }

    sb.append(requestedSm.getString("diagnostics.vmInfoSystem"));
    sb.append(":" + CRLF);
    Map<String, String> props = runtimeMXBean.getSystemProperties();
    ArrayList<String> keys = new ArrayList<String>(props.keySet());
    Collections.sort(keys);
    for (String prop : keys) {
      sb.append(INDENT1 + prop + ": " + props.get(prop) + CRLF);
    }
    sb.append(CRLF);

    sb.append(requestedSm.getString("diagnostics.vmInfoLogger"));
    sb.append(":" + CRLF);
    List<String> loggers = loggingMXBean.getLoggerNames();
    Collections.sort(loggers);
    for (String logger : loggers) {
      sb.append(
          INDENT1
              + logger
              + ": level="
              + loggingMXBean.getLoggerLevel(logger)
              + ", parent="
              + loggingMXBean.getParentLoggerName(logger)
              + CRLF);
    }
    sb.append(CRLF);

    return sb.toString();
  }
Exemplo n.º 6
0
 protected long getTimeInMilliseconds() {
   if (bean.isCurrentThreadCpuTimeSupported())
     return bean.getCurrentThreadCpuTime() / nanosecondsPerMillisecond;
   else return System.nanoTime() / nanosecondsPerMillisecond;
 }
Exemplo n.º 7
0
 private static long getCpuTime() {
   ThreadMXBean bean = ManagementFactory.getThreadMXBean();
   return bean.isCurrentThreadCpuTimeSupported() ? bean.getCurrentThreadCpuTime() : 0L;
 }