/**
   * Update the information about completed thread that ran for runtime in milliseconds
   *
   * <p>This method updates all of the key timing and tracking information in the factory so that
   * thread can be retired. After this call the factory shouldn't have a pointer to the thread any
   * longer
   *
   * @param thread the thread whose information we are updating
   */
  @Ensures({"getTotalTime() >= old(getTotalTime())"})
  public synchronized void threadIsDone(final Thread thread) {
    nThreadsAnalyzed++;

    if (DEBUG) logger.warn("UpdateThreadInfo called");

    final long threadID = thread.getId();
    final ThreadInfo info = bean.getThreadInfo(thread.getId());
    final long totalTimeNano = bean.getThreadCpuTime(threadID);
    final long userTimeNano = bean.getThreadUserTime(threadID);
    final long systemTimeNano = totalTimeNano - userTimeNano;
    final long userTimeInMilliseconds = nanoToMilli(userTimeNano);
    final long systemTimeInMilliseconds = nanoToMilli(systemTimeNano);

    if (info != null) {
      if (DEBUG)
        logger.warn(
            "Updating thread with user runtime "
                + userTimeInMilliseconds
                + " and system runtime "
                + systemTimeInMilliseconds
                + " of which blocked "
                + info.getBlockedTime()
                + " and waiting "
                + info.getWaitedTime());
      incTimes(State.BLOCKING, info.getBlockedTime());
      incTimes(State.WAITING, info.getWaitedTime());
      incTimes(State.USER_CPU, userTimeInMilliseconds);
      incTimes(State.WAITING_FOR_IO, systemTimeInMilliseconds);
    }
  }
  public boolean search() {

    Thread tread = java.lang.Thread.currentThread();
    java.lang.management.ThreadMXBean b = java.lang.management.ManagementFactory.getThreadMXBean();

    long startCPU = b.getThreadCpuTime(tread.getId());
    long startUser = b.getThreadUserTime(tread.getId());

    boolean result = store.consistency();
    System.out.println("*** consistency = " + result);

    Search<SetVar> label = new DepthFirstSearch<SetVar>();

    SelectChoicePoint<SetVar> select =
        new SimpleSelect<SetVar>(
            vars.toArray(new SetVar[vars.size()]),
            new MinLubCard<SetVar>(),
            new MaxGlbCard<SetVar>(),
            new IndomainSetMin<SetVar>());

    //	label.setSolutionListener(new SetSimpleSolutionListener<SetVar>());
    label.getSolutionListener().searchAll(false);
    label.getSolutionListener().recordSolutions(false);

    result = label.labeling(store, select);

    if (result) {
      System.out.println("*** Yes");
      for (int i = 0; i < weeks; i++) {
        for (int j = 0; j < groups; j++) {
          System.out.print(golferGroup[i][j].dom() + " ");
        }
        System.out.println();
      }
    } else System.out.println("*** No");

    System.out.println(
        "ThreadCpuTime = " + (b.getThreadCpuTime(tread.getId()) - startCPU) / (long) 1e+6 + "ms");
    System.out.println(
        "ThreadUserTime = "
            + (b.getThreadUserTime(tread.getId()) - startUser) / (long) 1e+6
            + "ms");

    return result;
  }
Exemple #3
0
  /**
   * Formats the thread dump header for one thread.
   *
   * @param ti the ThreadInfo describing the thread
   * @return the formatted thread dump header
   */
  private static String getThreadDumpHeader(ThreadInfo ti) {
    StringBuilder sb = new StringBuilder("\"" + ti.getThreadName() + "\"");
    sb.append(" Id=" + ti.getThreadId());
    sb.append(" cpu=" + threadMXBean.getThreadCpuTime(ti.getThreadId()) + " ns");
    sb.append(" usr="******" ns");
    sb.append(" blocked " + ti.getBlockedCount() + " for " + ti.getBlockedTime() + " ms");
    sb.append(" waited " + ti.getWaitedCount() + " for " + ti.getWaitedTime() + " ms");

    if (ti.isSuspended()) {
      sb.append(" (suspended)");
    }
    if (ti.isInNative()) {
      sb.append(" (running in native)");
    }
    sb.append(CRLF);
    sb.append(INDENT3 + "java.lang.Thread.State: " + ti.getThreadState());
    sb.append(CRLF);
    return sb.toString();
  }
  protected String generateThreadDump() {
    StringBuilder dump = new StringBuilder();
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true);
    long currentThreadId = Thread.currentThread().getId();
    for (ThreadInfo threadInfo : threadInfos) {
      long threadId = threadInfo.getThreadId();
      if (threadId == currentThreadId) {
        continue;
      }
      dump.append('"');
      dump.append(threadInfo.getThreadName());
      dump.append("\" ");

      Thread.State state = threadInfo.getThreadState();
      dump.append("\n\tjava.lang.Thread.State: ");
      dump.append(state);
      if (threadInfo.getLockName() != null) {
        dump.append(", on lock=").append(threadInfo.getLockName());
      }
      if (threadInfo.getLockOwnerName() != null) {
        dump.append(", owned by ").append(threadInfo.getLockOwnerName());
        dump.append(", id=").append(threadInfo.getLockOwnerId());
      }
      if (THREAD_CPU_TIME_INFO_AVAILABLE) {
        dump.append(", cpu=").append(threadMXBean.getThreadCpuTime(threadId)).append(" nsecs");
        dump.append(", usr="******" nsecs");
      }
      if (THREAD_CONTENTION_INFO_AVAILABLE) {
        dump.append(", blocked=").append(threadInfo.getBlockedTime()).append(" msecs");
        dump.append(", waited=").append(threadInfo.getWaitedTime()).append(" msecs");
      }
      StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
      for (StackTraceElement stackTraceElement : stackTraceElements) {
        dump.append("\n\t\tat ");
        dump.append(stackTraceElement);
      }
      dump.append("\n\n");
    }
    return dump.toString();
  }
Exemple #5
0
  private static SimpleOrderedMap<Object> getThreadInfo(ThreadInfo ti, ThreadMXBean tmbean) {
    SimpleOrderedMap<Object> info = new SimpleOrderedMap<Object>();
    long tid = ti.getThreadId();

    info.add("id", tid);
    info.add("name", ti.getThreadName());
    info.add("state", ti.getThreadState().toString());

    if (ti.getLockName() != null) {
      info.add("lock", ti.getLockName());
    }
    if (ti.isSuspended()) {
      info.add("suspended", true);
    }
    if (ti.isInNative()) {
      info.add("native", true);
    }

    if (tmbean.isThreadCpuTimeSupported()) {
      info.add("cpuTime", formatNanos(tmbean.getThreadCpuTime(tid)));
      info.add("userTime", formatNanos(tmbean.getThreadUserTime(tid)));
    }

    if (ti.getLockOwnerName() != null) {
      SimpleOrderedMap<Object> owner = new SimpleOrderedMap<Object>();
      owner.add("name", ti.getLockOwnerName());
      owner.add("id", ti.getLockOwnerId());
    }

    // Add the stack trace
    int i = 0;
    String[] trace = new String[ti.getStackTrace().length];
    for (StackTraceElement ste : ti.getStackTrace()) {
      trace[i++] = ste.toString();
    }
    info.add("stackTrace", trace);
    return info;
  }
  private void printThread() {
    long now = System.currentTimeMillis();

    long diffLastThreadDump = now - lastThreadDump;
    logger.info("diffLastThreadDump:" + diffLastThreadDump);
    if (diffLastThreadDump > 60000) {
      logger.info("had not sent all threads for a while.. will do so now");
      lastThreadDump = now;

      try {
        ThreadMXBean t = ManagementFactory.getThreadMXBean();
        long threads[] = t.getAllThreadIds();
        ThreadInfo[] tinfo = t.getThreadInfo(threads, 40);

        StringBuilder sb = new StringBuilder("All Threads");

        for (int i = 0; i < tinfo.length; i++) {
          ThreadInfo e = tinfo[i];

          try {
            StackTraceElement[] el = e.getStackTrace();
            sb.append(
                "\n\n"
                    + e.getThreadName()
                    + "\n"
                    + " "
                    + " Thread id = "
                    + e.getThreadId()
                    + " "
                    + e.getThreadState());
            if (e.getThreadState().equals(State.BLOCKED)) {
              sb.append(
                  "\n\nBlocked info: "
                      + e.getBlockedCount()
                      + ":"
                      + e.getBlockedTime()
                      + ":"
                      + e.getLockName()
                      + ":"
                      + e.getLockOwnerId()
                      + ":"
                      + e.getLockOwnerName()
                      + "\n"
                      + " "
                      + " Thread id = "
                      + e.getThreadId()
                      + " "
                      + e.getThreadState());

              ThreadInfo eBlockedThread = t.getThreadInfo(e.getLockOwnerId(), 40);
              StackTraceElement[] elBlockedThread = eBlockedThread.getStackTrace();
              sb.append(
                  "\n\n    "
                      + e.getThreadName()
                      + "\n"
                      + " "
                      + " Thread id = "
                      + eBlockedThread.getThreadId()
                      + " "
                      + eBlockedThread.getThreadState());

              if (elBlockedThread == null || elBlockedThread.length == 0) {
                sb.append("        no stack trace available");
              } else {
                for (int n = 0; n < elBlockedThread.length; n++) {
                  if (n != 0) sb.append("\n");

                  StackTraceElement frame = elBlockedThread[n];

                  if (frame == null) {
                    sb.append("        null stack frame");
                    continue;
                  }

                  sb.append("        ");
                  sb.append(frame.toString());
                }
              }
            }

            if (el == null || el.length == 0) {
              sb.append("    no stack trace available");
              continue;
            }

            for (int n = 0; n < el.length; n++) {
              if (n != 0) sb.append("\n");

              StackTraceElement frame = el[n];

              if (frame == null) {
                sb.append("    null stack frame");
                continue;
              }

              sb.append("    ");
              sb.append(frame.toString());
            }
          } catch (Exception e2) {
          }
        }

        String warningEmailReceiver = CmsPropertyHandler.getWarningEmailReceiver();
        if (warningEmailReceiver != null
            && !warningEmailReceiver.equals("")
            && warningEmailReceiver.indexOf("@warningEmailReceiver@") == -1) {
          try {
            logger.info("Mailing..");
            MailServiceFactory.getService()
                .sendEmail(
                    CmsPropertyHandler.getMailContentType(),
                    warningEmailReceiver,
                    warningEmailReceiver,
                    null,
                    null,
                    null,
                    null,
                    message,
                    sb.toString().replaceAll("\n", "<br/>"),
                    "utf-8");
          } catch (Exception e) {
            logger.error("Could not send mail:" + e.getMessage(), e);
          }
        }
      } catch (Throwable e) {
        logger.error("Error generating message:" + e.getMessage(), e);
      }
    }

    // Only sends if the last stack was sent more than 3 seconds ago.
    if ((now - lastSentTimer) > 10000) {
      lastSentTimer = System.currentTimeMillis();

      StackTraceElement[] el = targetThread.getStackTrace();

      ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
      long stopTimeInNs = threadMXBean.getThreadUserTime(targetThread.getId());
      long diff = (stopTimeInNs - startTimeInNs) / 1000000000;

      StringBuffer stackString = new StringBuffer("\n\n" + message + "\n\n");
      stackString.append("ServerName: " + getServerName() + "\n");
      stackString.append("Maximum memory (MB): " + (maxMemory / 1024 / 1024) + "\n");
      stackString.append("Used memory (MB): " + ((totalMemory - freeMemory) / 1024 / 1024) + "\n");
      stackString.append("Free memory (MB): " + (freeMemory / 1024 / 1024) + "\n");
      stackString.append("Total memory (MB): " + (totalMemory / 1024 / 1024) + "\n");
      stackString.append("Number of current requests: " + numberOfCurrentRequests + "\n");
      stackString.append("Number of active requests: " + numberOfActiveRequests + "\n");
      stackString.append("Number of long requests: " + longThreadMonitorsSize + "\n");
      stackString.append("Current thread time: " + diff + " seconds\n");
      stackString.append(
          "Average time: " + RequestAnalyser.getRequestAnalyser().getAverageElapsedTime() + "\n");
      stackString.append(
          "Longest time: " + RequestAnalyser.getRequestAnalyser().getMaxElapsedTime() + "\n");
      stackString.append("Original url: " + getOriginalFullURL() + "\n");
      stackString.append("UserInfo: " + getUserInfo() + "\n");
      stackString.append("--------------------------------------------\n\n");
      stackString.append("Thread with id [" + targetThread.getId() + "] at report time:\n");

      if (el != null && el.length != 0) {
        for (int j = 0; j < el.length; j++) {
          StackTraceElement frame = el[j];
          if (frame == null) stackString.append("    null stack frame" + "\n");
          else stackString.append("    ").append(frame.toString()).append("\n");

          // if((stackString.indexOf("infoglue") > -1 && j > 20) || j > 35)
          //	break;
        }
      }

      if (targetThread.getState().equals(State.BLOCKED)) {
        ThreadMXBean t = ManagementFactory.getThreadMXBean();

        ThreadInfo e = t.getThreadInfo(targetThread.getId(), 40);
        stackString.append(
            "\n\nBlocked info: "
                + e.getBlockedCount()
                + ":"
                + e.getBlockedTime()
                + ":"
                + e.getLockName()
                + ":"
                + e.getLockOwnerId()
                + ":"
                + e.getLockOwnerName()
                + "\n"
                + " "
                + " Thread id = "
                + e.getThreadId()
                + " "
                + e.getThreadState());

        ThreadInfo eBlockedThread = t.getThreadInfo(e.getLockOwnerId(), 40);
        StackTraceElement[] elBlockedThread = eBlockedThread.getStackTrace();
        stackString.append(
            "\n\nBlocked thread: "
                + e.getThreadName()
                + "\n"
                + " "
                + " Thread id = "
                + eBlockedThread.getThreadId()
                + " "
                + eBlockedThread.getThreadState());

        if (elBlockedThread == null || elBlockedThread.length == 0) {
          stackString.append("        no stack trace available");
        } else {
          for (int n = 0; n < elBlockedThread.length; n++) {
            if (n != 0) stackString.append("\n");

            StackTraceElement frame = elBlockedThread[n];

            if (frame == null) {
              stackString.append("        null stack frame");
              continue;
            }

            stackString.append("        ");
            stackString.append(frame.toString());
          }
        }
      }

      stackString.append(
          "\n\n**********************************\nConcurrent long threads (Only an excerpt of all)\n**********************************");

      ThreadMXBean t = ManagementFactory.getThreadMXBean();

      List threadMonitors = RequestAnalyser.getLongThreadMonitors();
      Iterator threadMonitorsIterator = threadMonitors.iterator();
      int threadCount = 0;
      while (threadMonitorsIterator.hasNext() && threadCount < 5) {
        SimpleThreadMonitor tm = (SimpleThreadMonitor) threadMonitorsIterator.next();

        if (targetThread.getId() == tm.getThreadId()) continue;

        long threads[] = {tm.getThreadId()};
        ThreadInfo[] tinfo = t.getThreadInfo(threads, 40);

        stackString
            .append("\n\n---------------------------------\nConcurrent long thread [")
            .append(tm.getThreadId())
            .append("]:\n");
        stackString
            .append("Elapsed time:")
            .append(tm.getElapsedTime())
            .append("\n Thread id: ")
            .append(tm.getThreadId())
            .append("\n Original url: ")
            .append(tm.getOriginalFullURL())
            .append(")");

        for (int i = 0; i < tinfo.length; i++) {
          ThreadInfo e = tinfo[i];

          el = e.getStackTrace();

          if (el != null && el.length != 0) {
            for (int n = 0; n < el.length; n++) {
              StackTraceElement frame = el[n];
              if (frame == null) stackString.append("    null stack frame\n");
              else stackString.append("    null stack frame").append(frame.toString()).append("\n");
            }
          }
        }

        threadCount++;
      }

      logger.warn(stackString);
    } else {
      logger.warn(
          "A thread took to long but the system seems to be really clogged so we don't send this one.");
    }
  }
  /**
   * Gets the results for the supplied train and test datasets. Now performs a deep copy of the
   * classifier before it is built and evaluated (just in case the classifier is not initialized
   * properly in buildClassifier()).
   *
   * @param train the training Instances.
   * @param test the testing Instances.
   * @return the results stored in an array. The objects stored in the array may be Strings,
   *     Doubles, or null (for the missing value).
   * @throws Exception if a problem occurs while getting the results
   */
  public Object[] getResult(Instances train, Instances test) throws Exception {

    if (train.classAttribute().type() != Attribute.NUMERIC) {
      throw new Exception("Class attribute is not numeric!");
    }
    if (m_Template == null) {
      throw new Exception("No classifier has been specified");
    }
    ThreadMXBean thMonitor = ManagementFactory.getThreadMXBean();
    boolean canMeasureCPUTime = thMonitor.isThreadCpuTimeSupported();
    if (canMeasureCPUTime && !thMonitor.isThreadCpuTimeEnabled())
      thMonitor.setThreadCpuTimeEnabled(true);

    int addm = (m_AdditionalMeasures != null) ? m_AdditionalMeasures.length : 0;
    Object[] result = new Object[RESULT_SIZE + addm + m_numPluginStatistics];
    long thID = Thread.currentThread().getId();
    long CPUStartTime = -1,
        trainCPUTimeElapsed = -1,
        testCPUTimeElapsed = -1,
        trainTimeStart,
        trainTimeElapsed,
        testTimeStart,
        testTimeElapsed;
    Evaluation eval = new Evaluation(train);
    m_Classifier = AbstractClassifier.makeCopy(m_Template);

    trainTimeStart = System.currentTimeMillis();
    if (canMeasureCPUTime) CPUStartTime = thMonitor.getThreadUserTime(thID);
    m_Classifier.buildClassifier(train);
    if (canMeasureCPUTime) trainCPUTimeElapsed = thMonitor.getThreadUserTime(thID) - CPUStartTime;
    trainTimeElapsed = System.currentTimeMillis() - trainTimeStart;
    testTimeStart = System.currentTimeMillis();
    if (canMeasureCPUTime) CPUStartTime = thMonitor.getThreadUserTime(thID);
    eval.evaluateModel(m_Classifier, test);
    if (canMeasureCPUTime) testCPUTimeElapsed = thMonitor.getThreadUserTime(thID) - CPUStartTime;
    testTimeElapsed = System.currentTimeMillis() - testTimeStart;
    thMonitor = null;

    m_result = eval.toSummaryString();
    // The results stored are all per instance -- can be multiplied by the
    // number of instances to get absolute numbers
    int current = 0;
    result[current++] = new Double(train.numInstances());
    result[current++] = new Double(eval.numInstances());

    result[current++] = new Double(eval.meanAbsoluteError());
    result[current++] = new Double(eval.rootMeanSquaredError());
    result[current++] = new Double(eval.relativeAbsoluteError());
    result[current++] = new Double(eval.rootRelativeSquaredError());
    result[current++] = new Double(eval.correlationCoefficient());

    result[current++] = new Double(eval.SFPriorEntropy());
    result[current++] = new Double(eval.SFSchemeEntropy());
    result[current++] = new Double(eval.SFEntropyGain());
    result[current++] = new Double(eval.SFMeanPriorEntropy());
    result[current++] = new Double(eval.SFMeanSchemeEntropy());
    result[current++] = new Double(eval.SFMeanEntropyGain());

    // Timing stats
    result[current++] = new Double(trainTimeElapsed / 1000.0);
    result[current++] = new Double(testTimeElapsed / 1000.0);
    if (canMeasureCPUTime) {
      result[current++] = new Double((trainCPUTimeElapsed / 1000000.0) / 1000.0);
      result[current++] = new Double((testCPUTimeElapsed / 1000000.0) / 1000.0);
    } else {
      result[current++] = new Double(Utils.missingValue());
      result[current++] = new Double(Utils.missingValue());
    }

    // sizes
    if (m_NoSizeDetermination) {
      result[current++] = -1.0;
      result[current++] = -1.0;
      result[current++] = -1.0;
    } else {
      ByteArrayOutputStream bastream = new ByteArrayOutputStream();
      ObjectOutputStream oostream = new ObjectOutputStream(bastream);
      oostream.writeObject(m_Classifier);
      result[current++] = new Double(bastream.size());
      bastream = new ByteArrayOutputStream();
      oostream = new ObjectOutputStream(bastream);
      oostream.writeObject(train);
      result[current++] = new Double(bastream.size());
      bastream = new ByteArrayOutputStream();
      oostream = new ObjectOutputStream(bastream);
      oostream.writeObject(test);
      result[current++] = new Double(bastream.size());
    }

    // Prediction interval statistics
    result[current++] = new Double(eval.coverageOfTestCasesByPredictedRegions());
    result[current++] = new Double(eval.sizeOfPredictedRegions());

    if (m_Classifier instanceof Summarizable) {
      result[current++] = ((Summarizable) m_Classifier).toSummaryString();
    } else {
      result[current++] = null;
    }

    for (int i = 0; i < addm; i++) {
      if (m_doesProduce[i]) {
        try {
          double dv =
              ((AdditionalMeasureProducer) m_Classifier).getMeasure(m_AdditionalMeasures[i]);
          if (!Utils.isMissingValue(dv)) {
            Double value = new Double(dv);
            result[current++] = value;
          } else {
            result[current++] = null;
          }
        } catch (Exception ex) {
          System.err.println(ex);
        }
      } else {
        result[current++] = null;
      }
    }

    // get the actual metrics from the evaluation object
    List<AbstractEvaluationMetric> metrics = eval.getPluginMetrics();
    if (metrics != null) {
      for (AbstractEvaluationMetric m : metrics) {
        if (m.appliesToNumericClass()) {
          List<String> statNames = m.getStatisticNames();
          for (String s : statNames) {
            result[current++] = new Double(m.getStatistic(s));
          }
        }
      }
    }

    if (current != RESULT_SIZE + addm + m_numPluginStatistics) {
      throw new Error("Results didn't fit RESULT_SIZE");
    }
    return result;
  }