Skip to content

Commit 0415136

Browse files
committed
Snapshot VM startup time
1 parent 8f9b4d2 commit 0415136

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/GraalPythonMain.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@
6262

6363
public class GraalPythonMain extends AbstractLanguageLauncher {
6464
public static void main(String[] args) {
65+
long startupNanoTime = System.nanoTime();
66+
long startupWallClockTime = System.currentTimeMillis();
67+
if (GraalPythonMain.startupNanoTime == -1) {
68+
GraalPythonMain.startupNanoTime = startupNanoTime;
69+
}
70+
if (GraalPythonMain.startupWallClockTime == -1) {
71+
GraalPythonMain.startupWallClockTime = startupWallClockTime;
72+
}
6573
new GraalPythonMain().launch(args);
6674
}
6775

@@ -70,6 +78,9 @@ public static void main(String[] args) {
7078

7179
// provided by GraalVM bash launchers, ignored in native image mode
7280
private static final String BASH_LAUNCHER_EXEC_NAME = System.getProperty("org.graalvm.launcher.executablename");
81+
82+
private static long startupWallClockTime = -1;
83+
private static long startupNanoTime = -1;
7384

7485
private ArrayList<String> programArgs = null;
7586
private String commandString = null;
@@ -84,6 +95,7 @@ public static void main(String[] args) {
8495
private final boolean stdinIsInteractive = System.console() != null;
8596
private boolean unbufferedIO = false;
8697
private boolean multiContext = false;
98+
private boolean snaptshotStartup = false;
8799
private VersionAction versionAction = VersionAction.None;
88100
private List<String> givenArguments;
89101
private List<String> relaunchArgs;
@@ -247,6 +259,13 @@ protected List<String> preprocessArguments(List<String> givenArgs, Map<String, S
247259
i += 1;
248260
checkHashPycsMode = arguments.get(i);
249261
break;
262+
case "-snapshot-startup":
263+
if (wantsExperimental) {
264+
snaptshotStartup = true;
265+
} else {
266+
unrecognized.add(arg);
267+
}
268+
break;
250269
default:
251270
if (!arg.startsWith("-")) {
252271
inputFile = arg;
@@ -473,6 +492,10 @@ protected void launch(Builder contextBuilder) {
473492
int rc = 1;
474493
try (Context context = contextBuilder.build()) {
475494
runVersionAction(versionAction, context.getEngine());
495+
496+
if (snaptshotStartup) {
497+
evalInternal(context, "__graalpython__.startup_wall_clock_ts = " + startupWallClockTime + "; __graalpython__.startup_nano = " + startupNanoTime);
498+
}
476499

477500
if (!quietFlag && (verboseFlag || (commandString == null && inputFile == null && stdinIsInteractive))) {
478501
print("Python " + evalInternal(context, "import sys; sys.version + ' on ' + sys.platform").asString());

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalPythonModuleBuiltins.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ public void initialize(PythonCore core) {
145145
}
146146
this.builtinConstants.put("stdio_encoding", standardStreamEncoding);
147147
this.builtinConstants.put("stdio_error", standardStreamError);
148+
this.builtinConstants.put("startup_wall_clock_ts", -1L);
149+
this.builtinConstants.put("startup_nano", -1L);
148150
// we need these during core initialization, they are re-set in postInitialize
149151
postInitialize(core);
150152
}
@@ -510,4 +512,14 @@ boolean typeCheck(Object instance, Object cls,
510512
return lib.typeCheck(instance, cls);
511513
}
512514
}
515+
516+
@Builtin(name = "time_millis", minNumOfPositionalArgs = 0, maxNumOfPositionalArgs = 1, doc = "Like time.time() but in milliseconds resolution.")
517+
@GenerateNodeFactory
518+
public abstract static class TimeMillis extends PythonUnaryBuiltinNode {
519+
@Specialization
520+
@TruffleBoundary
521+
static long doIt(@SuppressWarnings("unused") Object dummy) {
522+
return System.currentTimeMillis();
523+
}
524+
}
513525
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/TimeModuleBuiltins.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,13 @@ public double time() {
230230
}
231231

232232
// time.monotonic_ns()
233-
@Builtin(name = "monotonic_ns", minNumOfPositionalArgs = 0, doc = "Similar to monotonic(), but return time as nanoseconds.")
233+
@Builtin(name = "monotonic_ns", minNumOfPositionalArgs = 0, maxNumOfPositionalArgs = 1, doc = "Similar to monotonic(), but return time as nanoseconds.")
234234
@GenerateNodeFactory
235-
public abstract static class PythonMonotonicNsNode extends PythonBuiltinNode {
235+
public abstract static class PythonMonotonicNsNode extends PythonUnaryBuiltinNode {
236236

237237
@Specialization
238238
@TruffleBoundary
239-
public long time() {
239+
static long time(@SuppressWarnings("unused") Object dummy) {
240240
return System.nanoTime();
241241
}
242242
}

0 commit comments

Comments
 (0)