Skip to content

Commit

Permalink
[JVM] Adjust 'getrusage' to fix rakudo build
Browse files Browse the repository at this point in the history
The api for getrusage has been changed -- it takes an argument now.[1]

Also, this commit adds a workaround for non-working native arrays
in Rakudo's ThreadPoolScheduler.[2] Declaring @rusage as a native int
array comes out as a VMArrayInstance_i instead of a VMArrayInstance.
This really needs a clean fix, but I was unable to do that.

[1] rakudo/rakudo@c71fa17d8d
[2] rakudo/rakudo#1666
  • Loading branch information
usev6 committed Mar 28, 2018
1 parent 8333643 commit 0736a0c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/vm/jvm/QAST/Compiler.nqp
Expand Up @@ -2807,7 +2807,7 @@ QAST::OperationsJAST.map_classlib_core_op('sleep', $TYPE_OPS, 'sleep', [$RT_NUM]
QAST::OperationsJAST.map_classlib_core_op('getenvhash', $TYPE_OPS, 'getenvhash', [], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('getpid', $TYPE_OPS, 'getpid', [], $RT_INT, :tc);
QAST::OperationsJAST.map_classlib_core_op('jvmgetproperties', $TYPE_OPS, 'jvmgetproperties', [], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('getrusage', $TYPE_OPS, 'getrusage', [], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('getrusage', $TYPE_OPS, 'getrusage', [$RT_OBJ], $RT_OBJ, :tc);

# thread related opcodes
QAST::OperationsJAST.map_classlib_core_op('newthread', $TYPE_OPS, 'newthread', [$RT_OBJ, $RT_INT], $RT_OBJ, :tc);
Expand Down
37 changes: 26 additions & 11 deletions src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java
Expand Up @@ -125,6 +125,7 @@
import org.perl6.nqp.sixmodel.reprs.SemaphoreInstance;
import org.perl6.nqp.sixmodel.reprs.VMArray;
import org.perl6.nqp.sixmodel.reprs.VMArrayInstance;
import org.perl6.nqp.sixmodel.reprs.VMArrayInstance_i;
import org.perl6.nqp.sixmodel.reprs.VMArrayInstance_i16;
import org.perl6.nqp.sixmodel.reprs.VMArrayInstance_i32;
import org.perl6.nqp.sixmodel.reprs.VMArrayInstance_i8;
Expand Down Expand Up @@ -5572,17 +5573,31 @@ public static long getpid(ThreadContext tc) {
/* There's not a getrusage (with Windows fakery) equivalent on JVM, sadly.
* The main reason this op exists is for the thread pool scheduler in
* Rakudo, and we can get (or fake up enough of) what it needs. */
public static SixModelObject getrusage(ThreadContext tc) {
SixModelObject BOOTIntArray = tc.gc.BOOTIntArray;
SixModelObject res = BOOTIntArray.st.REPR.allocate(tc, BOOTIntArray.st);
long cpuNanos = ((OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean())
.getProcessCpuTime();
long cpuMillis = cpuNanos / 1000;
tc.native_i = cpuMillis / 1000000; // UTIME_SEC
res.bind_pos_native(tc, 0);
tc.native_i = cpuMillis % 1000000; // UTIME_MSEC
res.bind_pos_native(tc, 1);
return res;
public static SixModelObject getrusage(SixModelObject res, ThreadContext tc) {
if (Ops.isconcrete(res, tc) == 1) {
long cpuNanos = ((OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean())
.getProcessCpuTime();
long cpuMillis = cpuNanos / 1000;
if (res instanceof VMArrayInstance_i) {
tc.native_i = cpuMillis / 1000000; // UTIME_SEC
res.bind_pos_native(tc, 0);
tc.native_i = cpuMillis % 1000000; // UTIME_MSEC
res.bind_pos_native(tc, 1);
}
/* TODO remove workar^H^H^Hdirty hack for non-working native arrays in ThreadPoolScheduler */
/* https://github.com/rakudo/rakudo/issues/1666 */
else {
SixModelObject Int = tc.curFrame.codeRef.staticInfo.compUnit.hllConfig.intBoxType;
res.bind_pos_boxed(tc, 0, box_i(cpuMillis / 1000000, Int, tc)); // UTIME_SEC
res.bind_pos_boxed(tc, 1, box_i(cpuMillis % 1000000, Int, tc)); // UTIME_MSEC
res.bind_pos_boxed(tc, 2, box_i(0, Int, tc)); // STIME_SEC
res.bind_pos_boxed(tc, 3, box_i(0, Int, tc)); // STIME_SEC
}
return res;
}
else {
throw new RuntimeException("getrusage needs a concrete 64bit int array, got " + res.getClass().getSimpleName());
}
}

public static SixModelObject jvmgetproperties(ThreadContext tc) {
Expand Down

1 comment on commit 0736a0c

@usev6
Copy link
Contributor Author

@usev6 usev6 commented on 0736a0c Mar 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh well, I messed up the commit message. It was the other way around:
Declaring @rusage as a native int array comes out as a VMArrayInstance instead of a VMArrayInstance_i.

Please sign in to comment.