@Override
  public void parse(String cmdResult[]) {
    long swap, swapFree, mem, memFree;
    swap = swapFree = mem = memFree = -1;

    for (int line = 0; line < cmdResult.length; line++) {
      String fields[] = cmdResult[line].replace(':', ' ').split("\\s+");
      if (fields[0].equalsIgnoreCase("MemTotal")) {
        mem = Gpr.parseIntSafe(fields[1]);
        host.getResources().setMem(mem);
      }
      if (fields[0].equalsIgnoreCase("MemFree")) memFree = Gpr.parseLongSafe(fields[1]);
      if (fields[0].equalsIgnoreCase("SwapTotal")) swap = Gpr.parseLongSafe(fields[1]);
      if (fields[0].equalsIgnoreCase("SwapFree")) swapFree = Gpr.parseLongSafe(fields[1]);

      if (debug) {
        Gpr.debug("line[" + line + "]: " + cmdResult[line]);
        for (int i = 0; i < fields.length; i++)
          Gpr.debug("\tfields[" + i + "]: '" + fields[i] + "'");
      }
    }

    // Calculate swap usage
    if (debug) Gpr.debug("swap" + swap + " swapFree:" + swapFree);
    if ((swap > 0) && (swapFree >= 0)) {
      float swapUsage = ((float) (swap - swapFree)) / ((float) swap);
      host.getHealth().setSwapUsage(swapUsage);
    }

    // Calculate memory usage
    if (debug) Gpr.debug("mem:" + mem + " memFree:" + memFree);
    if ((mem > 0) && (memFree >= 0)) {
      float memUsage = ((float) (mem - memFree)) / ((float) mem);
      host.getHealth().setMemUsage(memUsage);
    }
  }
Example #2
0
  /** Add symbols to global scope */
  void initilaizeGlobalScope() {
    if (debug) log("Initialize global scope.");

    // Reset Global scope
    Scope.resetGlobalScope();
    Scope globalScope = Scope.getGlobalScope();

    // --
    // Get default veluas from command line or config file
    // ---

    // Command line parameters override defaults
    String cpusStr =
        config.getString(ExpressionTask.TASK_OPTION_CPUS, "1"); // Default number of cpus: 1
    long cpus = Gpr.parseIntSafe(cpusStr);
    if (cpus <= 0)
      throw new RuntimeException("Number of cpus must be a positive number ('" + cpusStr + "')");

    long mem =
        Gpr.parseMemSafe(
            config.getString(
                ExpressionTask.TASK_OPTION_MEM,
                "-1")); // Default amount of memory: -1 (unrestricted)
    String node = config.getString(ExpressionTask.TASK_OPTION_NODE, "");
    if (queue == null) queue = config.getString(ExpressionTask.TASK_OPTION_QUEUE, "");
    if (system == null)
      system =
          config.getString(
              ExpressionTask.TASK_OPTION_SYSTEM, ExecutionerType.LOCAL.toString().toLowerCase());
    if (taskFailCount < 0)
      taskFailCount = Gpr.parseIntSafe(config.getString(ExpressionTask.TASK_OPTION_RETRY, "0"));

    long oneDay = 1L * 24 * 60 * 60;
    long timeout =
        Gpr.parseLongSafe(config.getString(ExpressionTask.TASK_OPTION_TIMEOUT, "" + oneDay));
    long wallTimeout =
        Gpr.parseLongSafe(config.getString(ExpressionTask.TASK_OPTION_WALL_TIMEOUT, "" + oneDay));

    // ---
    // Add global symbols
    // ---
    globalScope.add(
        new ScopeSymbol(
            Scope.GLOBAL_VAR_PROGRAM_NAME,
            Type.STRING,
            "")); // Now is empty, but they are assigned later
    globalScope.add(new ScopeSymbol(Scope.GLOBAL_VAR_PROGRAM_PATH, Type.STRING, ""));

    // Task related variables: Default values
    globalScope.add(
        new ScopeSymbol(
            ExpressionTask.TASK_OPTION_SYSTEM,
            Type.STRING,
            system)); // System type: "local", "ssh", "cluster", "aws", etc.
    globalScope.add(
        new ScopeSymbol(ExpressionTask.TASK_OPTION_CPUS, Type.INT, cpus)); // Default number of cpus
    globalScope.add(
        new ScopeSymbol(
            ExpressionTask.TASK_OPTION_MEM,
            Type.INT,
            mem)); // Default amount of memory (unrestricted)
    globalScope.add(
        new ScopeSymbol(
            ExpressionTask.TASK_OPTION_QUEUE, Type.STRING, queue)); // Default queue: none
    globalScope.add(
        new ScopeSymbol(ExpressionTask.TASK_OPTION_NODE, Type.STRING, node)); // Default node: none
    globalScope.add(
        new ScopeSymbol(
            ExpressionTask.TASK_OPTION_CAN_FAIL,
            Type.BOOL,
            false)); // Task fail triggers checkpoint & exit (a task cannot fail)
    globalScope.add(
        new ScopeSymbol(
            ExpressionTask.TASK_OPTION_ALLOW_EMPTY,
            Type.BOOL,
            false)); // Tasks are allowed to have empty output file/s
    globalScope.add(
        new ScopeSymbol(
            ExpressionTask.TASK_OPTION_RETRY,
            Type.INT,
            (long) taskFailCount)); // Task fail can be re-tried (re-run) N times before considering
    // failed.
    globalScope.add(
        new ScopeSymbol(
            ExpressionTask.TASK_OPTION_TIMEOUT, Type.INT, timeout)); // Task default timeout
    globalScope.add(
        new ScopeSymbol(
            ExpressionTask.TASK_OPTION_WALL_TIMEOUT,
            Type.INT,
            wallTimeout)); // Task default wall-timeout

    // Number of local CPUs
    // Kilo, Mega, Giga, Tera, Peta.
    LinkedList<ScopeSymbol> constants = new LinkedList<ScopeSymbol>();
    constants.add(new ScopeSymbol(Scope.GLOBAL_VAR_LOCAL_CPUS, Type.INT, (long) Gpr.NUM_CORES));
    constants.add(new ScopeSymbol(Scope.GLOBAL_VAR_K, Type.INT, 1024L));
    constants.add(new ScopeSymbol(Scope.GLOBAL_VAR_M, Type.INT, 1024L * 1024L));
    constants.add(new ScopeSymbol(Scope.GLOBAL_VAR_G, Type.INT, 1024L * 1024L * 1024L));
    constants.add(new ScopeSymbol(Scope.GLOBAL_VAR_T, Type.INT, 1024L * 1024L * 1024L * 1024L));
    constants.add(
        new ScopeSymbol(Scope.GLOBAL_VAR_P, Type.INT, 1024L * 1024L * 1024L * 1024L * 1024L));
    constants.add(new ScopeSymbol(Scope.GLOBAL_VAR_MINUTE, Type.INT, 60L));
    constants.add(new ScopeSymbol(Scope.GLOBAL_VAR_HOUR, Type.INT, (long) (60 * 60)));
    constants.add(new ScopeSymbol(Scope.GLOBAL_VAR_DAY, Type.INT, (long) (24 * 60 * 60)));
    constants.add(new ScopeSymbol(Scope.GLOBAL_VAR_WEEK, Type.INT, (long) (7 * 24 * 60 * 60)));

    // Math constants
    constants.add(new ScopeSymbol(Scope.GLOBAL_VAR_E, Type.REAL, Math.E));
    constants.add(new ScopeSymbol(Scope.GLOBAL_VAR_PI, Type.REAL, Math.PI));

    // Add all constants
    for (ScopeSymbol ss : constants) {
      ss.setConstant(true);
      globalScope.add(ss);
    }

    // Set "physical" path
    String path;
    try {
      path = new File(".").getCanonicalPath();
    } catch (IOException e) {
      throw new RuntimeException("Cannot get cannonical path for current dir");
    }
    globalScope.add(new ScopeSymbol(ExpressionTask.TASK_OPTION_PHYSICAL_PATH, Type.STRING, path));

    // Set all environment variables
    Map<String, String> envMap = System.getenv();
    for (String varName : envMap.keySet()) {
      String varVal = envMap.get(varName);
      globalScope.add(new ScopeSymbol(varName, Type.STRING, varVal));
    }

    // Command line arguments (default: empty list)
    // This is properly set in 'initializeArgs()' method, but
    // we have to set something now, otherwise we'll get a "variable
    // not found" error at compiler time, if the program attempts
    // to use 'args'.
    Scope.getGlobalScope()
        .add(
            new ScopeSymbol(
                Scope.GLOBAL_VAR_ARGS_LIST, TypeList.get(Type.STRING), new ArrayList<String>()));
  }