[m-rev.] For review: library predicates for Java using JNI

James Goddard goddardjames at yahoo.com
Thu Jan 29 12:05:05 AEDT 2004


Estimated hours taken: 10
Branches: main

Implement some library predicates for Java using JNI.

java/runtime/Native.java:
	A new class to which uses JNI to provide any native functionality
	required by predicates of the standard library in Java.
	So far it only provides methods relating to timing.

java/runtime/Native.c:
	Source code, written in C, which implements all the native methods of
	mercury.runtime.Native.  Note that this implementation makes use of the
	existing C implementation of the equivalent functions.

java/runtime/Mmakefile:
	Rules for compiling a shared object from Native.c.

library/time.m:
	Implement the following predicates for Java using Native interface:
		time__c_clock/3
		time__clocks_per_sec/1
		time__times/7

library/benchmarking.m:
	Implement the following predicates for Java using Native interface:
		get_user_cpu_miliseconds/1

library/Mmakefile:
	Added rules for incorporating the Native shared object.


Index: java/runtime/Native.java
===================================================================
RCS file: java/runtime/Native.java
diff -N java/runtime/Native.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ java/runtime/Native.java	29 Jan 2004 00:48:02 -0000
@@ -0,0 +1,121 @@
+//
+// Copyright (C) 2004 The University of Melbourne.
+// This file may only be copied under the terms of the GNU Library General
+// Public License - see the file COPYING.LIB in the Mercury distribution.
+//
+//
+// This class exists to provide any native functionality required by the Java
+// implementation.  It uses JNI to access a shared object which it searches
+// for in all the directories listed in the current CLASSPATH.
+// 
+// At the moment the only services provided are those relating to timing.
+//
+
+package mercury.runtime;
+
+public class Native {
+	/*
+	** SHARED_OBJ is the name of the shared object which contains all the
+	** compiled native code.
+	*/
+	private	static final java.lang.String	SHARED_OBJ	= "Native.so";
+
+	/*
+	** available and isAvailable() are true when native functionality
+	** is available. (ie SHARED_OBJ was loaded successfully)
+	*/
+	private	static boolean			available	= false;
+
+	public static boolean isAvailable() {
+		return available;
+	}
+
+	static {
+		available = load_library();
+	}
+
+	/*
+	** load_library():
+	** 	Searches all the directories listed in the classpath,
+	** 	(including the directories containing each jar file) for
+	** 	the shared object SHARED_OBJ and attempts to load this file
+	** 	if found.
+	** 	Returns true if successful, false otherwise.
+	*/
+	private static boolean load_library() {
+		java.util.StringTokenizer classpath =
+				new java.util.StringTokenizer(
+				java.lang.System.getProperty("java.class.path")
+				,java.lang.System.getProperty("path.separator")
+				);
+
+		while (classpath.hasMoreTokens()) {
+			java.io.File dir;
+			java.io.File entry = new java.io.File(
+					classpath.nextToken());
+
+			try {
+				if (entry.isDirectory()) {
+					dir = entry;
+				} else {
+					dir = entry.getParentFile();
+				}
+				if (dir == null) {
+					dir = new java.io.File("");
+				}
+
+				java.io.File[] match = dir.listFiles(
+						new java.io.FilenameFilter() {
+						public boolean accept(
+							java.io.File dir,
+							java.lang.String name)
+						{
+						return name.equals(SHARED_OBJ);
+						}
+						});
+
+				java.lang.System.load(
+						match[0].getAbsolutePath());
+				System.out.println(match[0].getAbsolutePath());
+				return true;
+			}
+			catch (java.lang.Exception e) {
+				continue;
+			}
+		} // while classpath.hasMoreTokens()
+
+		return false;
+	} // load_library()
+
+	/*
+	** clock():
+	** 	Calls clock() from the <time.h> library.
+	*/
+	public static native int clock();
+
+	/*
+	** clocks_per_sec():
+	**	Returns the number of "clocks" per sec as defined by
+	**	CLOCKS_PER_SEC.
+	*/
+	public static native int clocks_per_sec();
+
+	/*
+	** times():
+	** 	Calls times() from the <time.h> library.  Results are returned
+	** 	in an int array of the form:
+	**	{ <return value>, utime, stime, cutime, cstime }
+	**	If POSIX times are not available, <return value> will be set
+	**	to -1, and the other values are undefined.
+	**	Returns null if the array cannot be constructed.
+	*/
+	public static native int[] times();
+
+	/*
+	** get_user_cpu_miliseconds():
+	**	Native method to return the CPU time consumed by the process,
+	**	in miliseconds, from an arbitrary initial time.
+	*/
+	public static native int get_user_cpu_miliseconds();
+}
+
Index: java/runtime/Native.c
===================================================================
RCS file: java/runtime/Native.c
diff -N java/runtime/Native.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ java/runtime/Native.c	29 Jan 2004 00:14:12 -0000
@@ -0,0 +1,99 @@
+/*
+** Copyright (C) 2004 The University of Melbourne.
+** This file may only be copied under the terms of the GNU Library General
+** Public License - see the file COPYING.LIB in the Mercury distribution.
+*/
+
+/*
+** File: Native.c	- Native code for java/runtime/Native.java
+*/
+
+#include <jni.h>
+
+/*
+ * Class:     Native
+ * Method:    clock
+ * Signature: ()I
+ */
+JNIEXPORT jint JNICALL Java_mercury_runtime_Native_clock(JNIEnv *, jclass);
+
+/*
+ * Class:     Native
+ * Method:    clocks_per_sec
+ * Signature: ()I
+ */
+JNIEXPORT jint JNICALL Java_mercury_runtime_Native_clocks_1per_1sec(
+		JNIEnv *, jclass);
+
+/*
+ * Class:     Native
+ * Method:    times
+ * Signature: ()[I
+ */
+JNIEXPORT jintArray JNICALL Java_mercury_runtime_Native_times(
+		JNIEnv *, jclass);
+
+/*
+ * Class:     Native
+ * Method:    get_user_cpu_miliseconds
+ * Signature: ()I
+ */
+JNIEXPORT jint JNICALL Java_mercury_runtime_Native_get_1user_1cpu_1miliseconds(
+		JNIEnv *, jclass);
+
+#include "../../runtime/mercury_imp.h"
+#include "../../runtime/mercury_timing.h"
+
+#include <time.h>
+#ifdef MR_HAVE_SYS_TYPES_H
+	#include <sys/types.h>
+#endif
+#ifdef MR_HAVE_SYS_TIMES_H
+	#include <sys/times.h>
+#endif
+#ifdef MR_HAVE_UNISTD_H
+	#include <unistd.h>
+#endif
+
+JNIEXPORT jint JNICALL
+Java_mercury_runtime_Native_clock(JNIEnv *env, jclass obj) {
+	return (MR_Integer) clock();
+}
+
+JNIEXPORT jint JNICALL
+Java_mercury_runtime_Native_clocks_1per_1sec(JNIEnv *env, jclass obj) {
+	return CLOCKS_PER_SEC;
+}
+
+JNIEXPORT jintArray JNICALL
+Java_mercury_runtime_Native_times(JNIEnv *env, jclass obj) {
+	jint		intarray[5];
+	jintArray	result;
+
+#ifdef MR_HAVE_POSIX_TIMES
+	struct tms	t;
+
+	intarray[0] = (MR_Integer) times(&t);
+	intarray[1] = (MR_Integer) t.tms_utime;
+	intarray[2] = (MR_Integer) t.tms_stime;
+	intarray[3] = (MR_Integer) t.tms_cutime;
+	intarray[4] = (MR_Integer) t.tms_cstime;
+#else
+	intarray[0] = -1;
+#endif
+
+	result = (*env)->NewIntArray(env, 5);
+	if (result != NULL) {
+		(*env)->SetIntArrayRegion(env, result, 0, 5, intarray);
+	}
+
+	return result;
+}
+
+JNIEXPORT jint JNICALL
+Java_mercury_runtime_Native_get_1user_1cpu_1miliseconds(
+		JNIEnv *env, jclass obj)
+{
+	return MR_get_user_cpu_miliseconds();
+}
+
Index: java/runtime/Mmakefile
===================================================================
RCS file: java/runtime/Mmakefile
diff -N java/runtime/Mmakefile
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ java/runtime/Mmakefile	29 Jan 2004 00:21:15 -0000
@@ -0,0 +1,24 @@
+
+# Copyright (C) 2004 The University of Melbourne.
+# This file may only be copied under the terms of the GNU Library General
+# Public License - see the file COPYING.LIB in the Mercury distribution.
+#
+# This Makefile compiles the shared object for use with mercury.runtime.Native
+#
+
+
+MAIN_TARGET=Native.so
+
+
+Native.so:	Native.o ../../runtime/mercury_timing.o
+		gcc -shared Native.o ../../runtime/mercury_timing.o \
+				-o Native.so
+
+Native.o:	Native.c
+		gcc -c Native.c
+
+realclean_local: clean
+
+clean:
+		rm -f Native.o Native.so
+
Index: library/time.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/time.m,v
retrieving revision 1.40
diff -u -d -r1.40 time.m
--- library/time.m	22 Jan 2004 05:00:26 -0000	1.40
+++ library/time.m	29 Jan 2004 00:09:03 -0000
@@ -255,8 +255,18 @@
 		.UserProcessorTime.Ticks;
 }").
 */
-
-% XXX Java implementation still to come, will require some native code.
+:- pragma foreign_proc("Java", time__c_clock(Ret::out, _IO0::di, _IO::uo),
+	[will_not_call_mercury, promise_pure, tabled_for_io],
+"
+	if (mercury.runtime.Native.isAvailable()) {
+		Ret = mercury.runtime.Native.clock();
+	} else {
+		throw new java.lang.RuntimeException(
+				""time__clock is not implemented "" +
+				""in pure Java.  Native dynamic link "" +
+				""library is required."");
+	}
+").
 
 %-----------------------------------------------------------------------------%
 
@@ -273,7 +283,18 @@
 	// TicksPerSecond is guaranteed to be 10,000,000
 	Ret = (int) System.TimeSpan.TicksPerSecond;
 }").
-% XXX Java implementation still to come, will require some native code.
+:- pragma foreign_proc("Java", time__clocks_per_sec = (Ret::out),
+	[will_not_call_mercury, promise_pure],
+"
+	if (mercury.runtime.Native.isAvailable()) {
+		Ret = mercury.runtime.Native.clocks_per_sec();
+	} else {
+		throw new java.lang.RuntimeException(
+				""time__clocks_per_sec is not implemented "" +
+				""in pure Java.  Native dynamic link "" +
+				""library is required."");
+	}
+").
 
 %-----------------------------------------------------------------------------%
 
@@ -312,7 +333,31 @@
 	MR_update_io(IO0, IO);
 }").
 
-% XXX Java implementation still to come, will require some native code.
+:- pragma foreign_proc("Java",
+	time__c_times(Ret::out, Ut::out, St::out, CUt::out,
+                               CSt::out, _IO0::di, _IO::uo),
+	[will_not_call_mercury, promise_pure, tabled_for_io],
+"
+	if (mercury.runtime.Native.isAvailable()) {
+		int[] times = mercury.runtime.Native.times();
+		if (times != null) {
+			Ret	= times[0];
+			Ut	= times[1];
+			St	= times[2];
+			CUt	= times[3];
+			CSt	= times[4];
+		} else {
+			throw new java.lang.RuntimeException(
+					""time_times failed to construct "" +
+					""integer array"");
+		}
+	} else {
+		throw new java.lang.RuntimeException(
+				""time__times is not implemented "" +
+				""in pure Java.  Native dynamic link "" +
+				""library is required."");
+	}
+").
 
 %-----------------------------------------------------------------------------%
 
Index: library/benchmarking.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/benchmarking.m,v
retrieving revision 1.56
diff -u -d -r1.56 benchmarking.m
--- library/benchmarking.m	20 Jan 2004 23:07:36 -0000	1.56
+++ library/benchmarking.m	28 Jan 2004 22:40:18 -0000
@@ -701,6 +701,18 @@
 	Time = (int) (1000 * System::Diagnostics::Counter::GetElapsed());
 ").
 */
+:- pragma foreign_proc("Java",
+	get_user_cpu_miliseconds(Time::out), [will_not_call_mercury],
+"
+	if (mercury.runtime.Native.isAvailable()) {
+		Time = mercury.runtime.Native.get_user_cpu_miliseconds();
+	} else {
+		throw new java.lang.RuntimeException(
+				""get_user_cpu_miliseconds is not "" +
+				""implemented in pure Java.  Native "" +
+				""dynamic link library is required."");
+	}
+").
 
 /*
 ** To prevent the C compiler from optimizing the benchmark code
Index: library/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/mercury/library/Mmakefile,v
retrieving revision 1.128
diff -u -d -r1.128 Mmakefile
--- library/Mmakefile	27 Jan 2004 00:20:28 -0000	1.128
+++ library/Mmakefile	29 Jan 2004 00:42:49 -0000
@@ -244,12 +244,19 @@
 # jar file.  At this point we also add the runtime classes to a jar file.
 # Note that this stage makes use of the symbolic links created earlier to
 # ensure that the path names are correct within the jar files.
-JARS = $(STD_LIB_NAME).jar $(STD_LIB_NAME).runtime.jar
+# We also compile NATIVE_SO here and copy it into the library directory.
+
+JARS = $(STD_LIB_NAME).jar $(STD_LIB_NAME).runtime.jar $(NATIVE_SO)
 
 .PHONY:	jars
 jars:	classes
 	jar cf $(STD_LIB_NAME).jar mercury/*.class
 	jar cf $(STD_LIB_NAME).runtime.jar mercury/runtime/*.class
+	+cd mercury/runtime && mmake $(NATIVE_SO)
+	cp mercury/runtime/$(NATIVE_SO) .
+
+# This shared object is needed to run some of the standard library methods.
+NATIVE_SO = Native.so
 
 #-----------------------------------------------------------------------------#
 
@@ -404,6 +411,7 @@
 	rm -f liblibrary.$A liblibrary.so library.init
 	rm -f $($(STD_LIB_NAME).mods:%=%.h)
 	rm -f tags
+	rm -f $(JARS)
 
 #-----------------------------------------------------------------------------#
 
@@ -485,6 +493,8 @@
 else
 
 ifneq (,$(findstring java,$(GRADE)))
+
+# Copy the jars and NATIVE_SO to INSTALL_JAVA_LIBRARY_DIR.
 
 .PHONY: install_library
 install_library: jars
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list