summaryrefslogtreecommitdiff
path: root/NET/worlds/core/SystemInfo.java
blob: 594c88ddcd054b5c9adde9eb3322e43c3a05d554 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package NET.worlds.core;

import NET.worlds.console.Main;
import NET.worlds.console.MainCallback;
import NET.worlds.console.MainTerminalCallback;
import java.io.PrintStream;

public class SystemInfo implements MainCallback, MainTerminalCallback {
   private static SystemInfo instance = new SystemInfo();
   private static final com.sun.management.OperatingSystemMXBean operatingSystemMXBean =
      (com.sun.management.OperatingSystemMXBean) java.lang.management.ManagementFactory.getOperatingSystemMXBean();
   private long _lastFrame = 0L;
   private long _lastReport = 0L;
   private long _min;
   private long _max;
   private long _avg;
   private long _avgCount = 0L;

   private SystemInfo() {
      Main.register(this);
   }

   @Override
   public void mainCallback() {
      long curTime = Std.getFastTime();
      long frameTime = curTime - this._lastFrame;
      if (frameTime < this._min) {
         this._min = frameTime;
      }

      if (frameTime > this._max) {
         this._max = frameTime;
      }

      this._avg += frameTime;
      this._avgCount++;
      if (curTime - this._lastReport > 300000L) {
         if (this._lastFrame != 0L) {
            System.out.println(logTime() + "Frame rate report:" + this._min + "/" + this._avg / this._avgCount + "/" + this._max);
         }

         this._min = 10000L;
         this._max = 0L;
         this._avg = 0L;
         this._avgCount = 0L;
         this._lastReport = curTime;
      }

      this._lastFrame = curTime;
   }

   @Override
   public void terminalCallback() {
      Main.unregister(this);
      if (this._avgCount != 0L) {
         System.out.println(logTime() + "Frame rate report:" + this._min + "/" + this._avg / this._avgCount + "/" + this._max);
      }
   }

   public static void Record(PrintStream out) {
      out.println("DISK REPORT:");
      recordPath(out, "Windows SYSTEM path", GetSystemDirectory());
      recordPath(out, "Current Working Directory", GetCurrentDirectory());
      out.println("");
      out.println("JAVA MEMORY:");
      out.println(logTime() + "\t Free memory: " + Runtime.getRuntime().freeMemory());
      out.println(logTime() + "\tTotal memory: " + Runtime.getRuntime().totalMemory());
      out.println("");
      out.println("WINDOWS MEMORY:");
      out.println(logTime() + "\tTotal Physical Memory: " + GetTotalPhysicalMemory());
      out.println(logTime() + "\tAvail Physical Memory: " + GetAvailPhysicalMemory());
      out.println(logTime() + "\t   Total Paged Memory: " + GetTotalPagedMemory());
      out.println(logTime() + "\t   Avail Paged Memory: " + GetAvailPagedMemory());
      out.println("");
      out.println(logTime() + "Java Properties: " + System.getProperties());
      out.println(logTime() + "Number of CPUs: " + GetNumberOfProcessors());
      out.println(logTime() + "Processor Type: " + GetProcessorType());
      out.println(logTime() + "Platform  Type: " + GetPlatformID());
   }

   private static void recordPath(PrintStream out, String comment, String path) {
      out.println(logTime() + comment + ": " + path);
      if (path != null) {
         String drive = path.substring(0, 2);
         out.println(logTime() + "\tFree disk space (" + drive + "): " + GetDiskFreeSpace(drive + "\\") + " KB");
      }
   }

   public static String logTime() {
      return "[" + Std.getRealTime() + "] ";
   }

   public static int GetDiskFreeSpace(String var0) {
      try {
         java.io.File file = (var0 != null) ? new java.io.File(var0) : new java.io.File(".");
         
         return (int)(file.getFreeSpace() / 1024L);
      } catch (Exception e) {
         return 0;
      }
   }

   public static int GetDiskFreeSpace() {
      return GetDiskFreeSpace(null);
   }

   public static String GetSystemDirectory() {
      return System.getProperty("java.home", "");
   }

   public static String GetCurrentDirectory() {
      return System.getProperty("user.dir", "");
   }

   public static int GetTotalPhysicalMemory() {
      return (int)(operatingSystemMXBean.getTotalPhysicalMemorySize() / 1024L);
   }

   public static int GetAvailPhysicalMemory() {
      return (int)(operatingSystemMXBean.getFreePhysicalMemorySize() / 1024L);
   }

   public static int GetTotalPagedMemory() {
      return (int)(operatingSystemMXBean.getTotalSwapSpaceSize() / 1024L);
   }

   public static int GetAvailPagedMemory() {
      return (int)(operatingSystemMXBean.getFreeSwapSpaceSize() / 1024L);
   }

   public static String GetPlatformID() {
      return System.getProperty("os.name", "Unknown") + " " + System.getProperty("os.version", "");
   }

   public static int GetNumberOfProcessors() {
      return Runtime.getRuntime().availableProcessors();
   }

   public static String GetProcessorType() {
      return System.getProperty("os.arch", "Unknown");
   }
}