Commit c6040524 authored by Hiro's avatar Hiro 🏄
Browse files

Print memory used in human readable format

parent 341c03ba
Loading
Loading
Loading
Loading
Compare 467f5e13 to e1152533
Original line number Diff line number Diff line
Subproject commit 467f5e132c71b9dbbcdc2513927a742f6b1e43a1
Subproject commit e11525332d5d2f0b50f17815ba06de7afaec5db8
+36 −3
Original line number Diff line number Diff line
@@ -246,9 +246,11 @@ public class CreateIndexJson extends CollecTorMain {
          + "disk.");
      this.writeIndex(this.index, now);
      Runtime rt = Runtime.getRuntime();
      logger.info("Current memory usage is: free = {} B, total = {} B, "
          + "max = {} B.",
          rt.freeMemory(), rt.totalMemory(), rt.maxMemory());
      String freeMemory = formatMemory(rt.freeMemory());
      String totalMemory = formatMemory(rt.totalMemory());
      String maxMemory = formatMemory(rt.maxMemory());
      logger.info("Current memory usage is: free = {}, total = {}, max = {}.",
          freeMemory, totalMemory, maxMemory);
      logger.info("Pausing until next index update run.");
    } catch (IOException e) {
      logger.error("I/O error while updating index.json files. Trying again in "
@@ -256,6 +258,37 @@ public class CreateIndexJson extends CollecTorMain {
    }
  }

  /**
   * Format memory used into human readable string.
   *
   * @param bytes of used memory
   * @return String.format of human readable value
   */
  private String formatMemory(long bytes) {
    double value = (double) bytes;
    String unit = "B";

    if (value >= 1024) {
      value /= 1024;
      unit = "KB";
    }
    if (value >= 1024) {
      value /= 1024;
      unit = "MB";
    }
    if (value >= 1024) {
      value /= 1024;
      unit = "GB";
    }
    if (value >= 1024) {
      value /= 1024;
      unit = "TB";
    }

    return String.format("%.2f %s", value, unit);
  }


  /**
   * Prepare the {@code htdocs/} directory by checking whether all required
   * subdirectories exist and by creating them if not.