Beispiel #1
0
  /**
   * Returns object of class ‘"proc_time"’ which is a numeric vector of length 5, containing the
   * user, system, and total elapsed times for the currently running R process, and the cumulative
   * sum of user and system times of any child processes spawned by it on which it has waited.
   *
   * <p>_The ‘user time’ is the CPU time charged for the execution of user instructions of the
   * calling process. The ‘system time’ is the CPU time charged for execution by the system on
   * behalf of the calling process._
   */
  @Builtin("proc.time")
  public static DoubleVector procTime() {

    DoubleArrayVector.Builder result = new DoubleArrayVector.Builder();
    StringVector.Builder names = new StringVector.Builder();

    long totalCPUTime;
    long userCPUTime;
    long elapsedTime;

    // There doesn't seem to be any platform-independent way of accessing
    // CPU use for the whole JVM process, so we'll have to make do
    // with the timings for the thread we're running on.
    //
    // Additionally, the MX Beans may not be available in all environments,
    // so we need to fallback to app
    try {
      ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
      totalCPUTime = threadMXBean.getCurrentThreadCpuTime();
      userCPUTime = threadMXBean.getCurrentThreadUserTime();

      RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
      elapsedTime = runtimeMXBean.getUptime();
    } catch (Error e) {
      // ThreadMXBean is not available in all environments
      // Specifically, AppEngine will throw variously SecurityErrors or
      // ClassNotFoundErrors if we try to access these classes
      userCPUTime = totalCPUTime = java.lang.System.nanoTime();
      elapsedTime = new Date().getTime();
    }

    // user.self
    names.add("user.self");
    result.add(userCPUTime / NANOSECONDS_PER_SECOND);

    // sys.self
    names.add("sys.self");
    result.add((totalCPUTime - userCPUTime) / NANOSECONDS_PER_SECOND);

    // elapsed
    // (wall clock time)
    names.add("elapsed");
    result.add(elapsedTime);

    // AFAIK, we don't have any platform independent way of accessing
    // this info.

    // user.child
    names.add("user.child");
    result.add(0);

    // sys.child
    names.add("sys.child");
    result.add(0);

    result.setAttribute(Symbols.NAMES, names.build());
    result.setAttribute(Symbols.CLASS, StringVector.valueOf("proc_time"));
    return result.build();
  }
Beispiel #2
0
  /**
   * Utility function to extract information about files on the user's file systems.
   *
   * @param context current call Context
   * @param paths the list of files for which to return information
   * @return list column-oriented table of file information
   * @throws FileSystemException
   */
  @Internal("file.info")
  public static ListVector fileInfo(@Current Context context, StringVector paths)
      throws FileSystemException {

    DoubleArrayVector.Builder size = new DoubleArrayVector.Builder();
    LogicalArrayVector.Builder isdir = new LogicalArrayVector.Builder();
    IntArrayVector.Builder mode =
        (IntArrayVector.Builder)
            new IntArrayVector.Builder()
                .setAttribute(Symbols.CLASS, StringVector.valueOf("octmode"));
    DoubleArrayVector.Builder mtime = new DoubleArrayVector.Builder();
    StringVector.Builder exe = new StringVector.Builder();

    for (String path : paths) {
      if (StringVector.isNA(path)) {
        throw new EvalException("invalid filename argument");
      }
      FileObject file = context.resolveFile(path);
      if (file.exists()) {
        if (file.getType() == FileType.FILE) {
          size.add((int) file.getContent().getSize());
        } else {
          size.add(0);
        }
        isdir.add(file.getType() == FileType.FOLDER);
        mode.add(mode(file));
        try {
          mtime.add(file.getContent().getLastModifiedTime());
        } catch (Exception e) {
          mtime.add(0);
        }
        exe.add(file.getName().getBaseName().endsWith(".exe") ? "yes" : "no");
      } else {
        size.addNA();
        isdir.addNA();
        mode.addNA();
        mtime.addNA();
        exe.addNA();
      }
    }

    return ListVector.newNamedBuilder()
        .add("size", size)
        .add("isdir", isdir)
        .add("mode", mode)
        .add("mtime", mtime)
        .add("ctime", mtime)
        .add("atime", mtime)
        .add("exe", exe)
        .build();
  }