MATSIM
Gbl.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * Gbl.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.gbl;
22 
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.lang.management.ManagementFactory;
30 import java.lang.management.ThreadMXBean;
31 import java.net.URL;
32 import java.nio.charset.StandardCharsets;
33 
38 public abstract class Gbl {
39 
40  private static final Logger log = LogManager.getLogger(Gbl.class);
41 
42  public final static String ONLYONCE = " This message given only once.";
43 
44  public final static String FUTURE_SUPPRESSED = " Future occurences of this logging statement are suppressed." ;
45 
46  public final static String SEPARATOR = "****************************" ;
47 
48  public static final String CREATE_ROUTING_ALGORITHM_WARNING_MESSAGE = "This class wants to overwrite createRoutingAlgorithm(), which is no longer possible. Making createRoutingAlgorithm() non-final would not help since, after recent code changes, it is only used during initialization but not in replanning. kai, may'13. Aborting ...";
49 
50  public static final String NOT_IMPLEMENTED = "not implemented" ;
51 
52  public static final String ABSORBED_INTO_CORE="This execution path is no longer supported. The functionality has been absorbed into the core." ;
53  public static final String INVALID = "invalid";
54 
55  public static final void printMemoryUsage() {
56  long totalMem = Runtime.getRuntime().totalMemory();
57  long freeMem = Runtime.getRuntime().freeMemory();
58  long usedMem = totalMem - freeMem;
59  log.info("used RAM: " + usedMem + "B = " + (usedMem/1024) + "kB = " + (usedMem/1024/1024) + "MB" +
60  " free: " + freeMem + "B = " + (freeMem/1024/1024) + "MB total: " + totalMem + "B = " + (totalMem/1024/1024) + "MB");
61  }
62 
63  public static final void printSystemInfo() {
64  log.info("JVM: " + System.getProperty("java.version") + "; "
65  + System.getProperty("java.vm.vendor") + "; "
66  + System.getProperty("java.vm.info") + "; "
67  + System.getProperty("sun.arch.data.model") + "-bit");
68  log.info("OS: " + System.getProperty("os.name") + "; "
69  + System.getProperty("os.version") + "; "
70  + System.getProperty("os.arch"));
71  log.info("CPU cores: " + Runtime.getRuntime().availableProcessors());
72  log.info("max. Memory: " + Runtime.getRuntime().maxMemory() / 1024.0 / 1024.0 + "MB (" + Runtime.getRuntime().maxMemory() + "B)");
73  }
74 
75  public static final void printRunCommand() {
76  log.info("Command: " + System.getProperty("sun.java.command"));
77  }
78 
79  public static final String getBuildInfoString() {
80  return getBuildInfoString("MATSim", "/revision.txt");
81  }
82 
87  public static final String getBuildInfoString(String component, String resourceFilename) {
88  String revision = null;
89  String date = null;
90  URL url = Gbl.class.getResource(resourceFilename);
91  if (url != null) {
92  try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8))) {
93  revision = reader.readLine();
94  date = reader.readLine();
95  } catch (IOException e) {
96  e.printStackTrace();
97  }
98  if (revision == null) {
99  return component + "-Build: unknown";
100  } else {
101  return component + "-Build: " + revision + " (" + date + ")";
102  }
103  } else {
104  return component + "-Build: unknown";
105  }
106  }
107 
108  public static final void printBuildInfo() {
109  printBuildInfo("MATSim", "/revision.txt");
110  }
111 
112  public static final void printBuildInfo(String component, String resourceFilename) {
113  String infoString = getBuildInfoString(component, resourceFilename);
114  log.info(infoString);
115  }
116 
118  // time measurement
120 
121  private static long measurementStartTime = Long.MAX_VALUE;
122 
123  private static final String printTime() {
124  if (Gbl.measurementStartTime == Long.MAX_VALUE) {
125  log.error("Did not start measurements.");
126  return "";
127  }
128  return printTimeDiff(System.currentTimeMillis(), Gbl.measurementStartTime);
129  }
130 
131  private static final String printTimeDiff(final long later, final long earlier) {
132  long elapsedTimeMillis = later - earlier;
133  float elapsedTimeSec = elapsedTimeMillis/1000F;
134  float elapsedTimeMin = elapsedTimeMillis/(60*1000F);
135  float elapsedTimeHour = elapsedTimeMillis/(60*60*1000F);
136  float elapsedTimeDay = elapsedTimeMillis/(24*60*60*1000F);
137 
138  return elapsedTimeMillis + " msecs; " +
139  elapsedTimeSec + " secs; " +
140  elapsedTimeMin + " mins; " +
141  elapsedTimeHour + " hours; " +
142  elapsedTimeDay + " days ###";
143  }
144 
145  public static final void startMeasurement() {
146  Gbl.measurementStartTime = System.currentTimeMillis();
147  }
148 
149  public static final void printElapsedTime() {
150  log.info("### elapsed time: " + Gbl.printTime());
151  }
152 
153  public static final void printRoundTime() {
154  log.info("### round time: " + Gbl.printTime());
156  }
157 
159  // thread performance
161 
162  private final static ThreadMXBean tbe = ManagementFactory.getThreadMXBean();
163 
170  public static final boolean enableThreadCpuTimeMeasurement() {
171  if (tbe.isThreadCpuTimeSupported()) {
172  tbe.setThreadCpuTimeEnabled(true);
173  return true;
174  }
175  return false;
176  }
177 
182  public static final double getThreadCpuTime(final Thread thread) {
183  if (tbe.isThreadCpuTimeEnabled()) {
184  return tbe.getThreadCpuTime(thread.getId()) / 1.0e9;
185  }
186  return -1;
187  }
188 
194  public static final void printThreadCpuTime(final Thread thread) {
195  if (tbe.isThreadCpuTimeEnabled()) {
196  log.info("Thread performance: Thread=" + thread.getName() + " cpu-time=" + getThreadCpuTime(thread) + "sec");
197  }
198  }
199 
203  public static final void printCurrentThreadCpuTime() {
204  printThreadCpuTime(Thread.currentThread());
205  }
206 
207  public static void assertIf( boolean flag ) {
208  if ( !flag ) {
209  throw new RuntimeException("assertion error; follow stack trace") ;
210  }
211  }
212  public static void assertNotNull( Object obj ) {
213  if ( obj==null ) {
214  throw new RuntimeException( "Object is null; follow stack trace" ) ;
215  }
216  }
217  public static void assertNotNull( Object obj, String msg ) {
218  if ( obj==null ) {
219  throw new RuntimeException( msg ) ;
220  }
221  }
222  public static void fail() {
223  throw new RuntimeException("failure; follow stack trace") ;
224  }
225 
226  public final static String RUN_MOB_SIM_NO_LONGER_POSSIBLE = "overriding runMobSim() no longer possible. use the following syntax instead:\n"
227  + "controler.addOverridingModule(new AbstractModule(){\n"
228  + "@Override public void install() {\n"
229  + "this.bindMobsim().toProvider(MyMobsimProvider.class) ;\n"
230  + "}\n"
231  + "});\n"
232  + "See, e.g., the RunMobsimWithMultipleModeVehiclesExample class under tutorial.*. Talk to MZ or KN if you need help. kai, may'15";
233 
234  public final static String SET_UP_IS_NOW_FINAL = "controler.setUp() is now final. You should be able to do whatever you need to do with a "
235  + "ControlerStartupListener. Please talk to MZ or KN if you have difficulties. kai, may'15";
236 
237  public static final String LOAD_DATA_IS_NOW_FINAL = "controler.loadData() is now final. If you need this functionality, use ScenarioUtils.loadScenario(...), "
238  + "then modify the scenario, then pass it into new Controler( scenario ). Talk to MZ or KN if you need help. kai, may'15";
239 
240  public static final String CONTROLER_IS_NOW_FINAL = "The Controler class is now final. Everything that used to be "
241  + "possible by inheritance should now be doable by other constructs. See tutorial.programming.* for examples. Please talk"
242  + "to MZ or KN if you would like to get help. kai, may'15" ;
243 
244  public static final String RETROFIT_CONTROLER = Gbl.CONTROLER_IS_NOW_FINAL + " I tried to adapt this to new syntax"
245  + "but please check functionality. kai, mar'15" ;
246 
247  public static final String PROBLEM_WITH_ACCESS_EGRESS = "When the TripRouter also generates access/egress legs, within-day replanning "
248  + "needs to sort out if it wants that, or if it just wants to replan the current leg. kai, feb'16" ;
249 
250  public static final String WRONG_IMPLEMENTATION = "wrong implementation of interface; " ;
251 
252  public static final String COPY_PASTE_FROM_CORE_NO_LONGER_WORKING="Another solution for this has been found in the core, and thus this copy-and-paste from the core is no longer working." ;
253 
254  public static String aboutToWrite( String what, String filename ) {
255  return "about to write " + what + " to: " + filename ;
256  }
257  public static String aboutToRead( String what, URL url ) {
258  return "about to read " + what + " from: " + url ;
259  }
260  public static String aboutToRead( String what, String filename ) {
261  return "about to read " + what + " from: " + filename ;
262  }
263 
264 }
static final String ONLYONCE
Definition: Gbl.java:42
static final void printRunCommand()
Definition: Gbl.java:75
static final String LOAD_DATA_IS_NOW_FINAL
Definition: Gbl.java:237
static final void startMeasurement()
Definition: Gbl.java:145
static void assertNotNull(Object obj, String msg)
Definition: Gbl.java:217
static void assertIf(boolean flag)
Definition: Gbl.java:207
static final String ABSORBED_INTO_CORE
Definition: Gbl.java:52
static final String CONTROLER_IS_NOW_FINAL
Definition: Gbl.java:240
static void fail()
Definition: Gbl.java:222
static final void printBuildInfo(String component, String resourceFilename)
Definition: Gbl.java:112
static final String FUTURE_SUPPRESSED
Definition: Gbl.java:44
static final void printRoundTime()
Definition: Gbl.java:153
static final String printTime()
Definition: Gbl.java:123
static final String SET_UP_IS_NOW_FINAL
Definition: Gbl.java:234
static String aboutToRead(String what, URL url)
Definition: Gbl.java:257
static final String SEPARATOR
Definition: Gbl.java:46
static final String printTimeDiff(final long later, final long earlier)
Definition: Gbl.java:131
static final String RETROFIT_CONTROLER
Definition: Gbl.java:244
static final String getBuildInfoString()
Definition: Gbl.java:79
static long measurementStartTime
Definition: Gbl.java:121
static final String NOT_IMPLEMENTED
Definition: Gbl.java:50
static void assertNotNull(Object obj)
Definition: Gbl.java:212
static final String getBuildInfoString(String component, String resourceFilename)
Definition: Gbl.java:87
static final String CREATE_ROUTING_ALGORITHM_WARNING_MESSAGE
Definition: Gbl.java:48
static final void printMemoryUsage()
Definition: Gbl.java:55
static String aboutToWrite(String what, String filename)
Definition: Gbl.java:254
static final void printElapsedTime()
Definition: Gbl.java:149
static final void printBuildInfo()
Definition: Gbl.java:108
static final ThreadMXBean tbe
Definition: Gbl.java:162
static final String WRONG_IMPLEMENTATION
Definition: Gbl.java:250
static final void printSystemInfo()
Definition: Gbl.java:63
static final String COPY_PASTE_FROM_CORE_NO_LONGER_WORKING
Definition: Gbl.java:252
static final Logger log
Definition: Gbl.java:40
static final void printThreadCpuTime(final Thread thread)
Definition: Gbl.java:194
static final String PROBLEM_WITH_ACCESS_EGRESS
Definition: Gbl.java:247
static final double getThreadCpuTime(final Thread thread)
Definition: Gbl.java:182
static final String INVALID
Definition: Gbl.java:53
static String aboutToRead(String what, String filename)
Definition: Gbl.java:260
static final boolean enableThreadCpuTimeMeasurement()
Definition: Gbl.java:170
static final String RUN_MOB_SIM_NO_LONGER_POSSIBLE
Definition: Gbl.java:226
static final void printCurrentThreadCpuTime()
Definition: Gbl.java:203