Commit 1b001998 authored by Ivo Anjo's avatar Ivo Anjo

Initial version of javartm

parents
build/
sde-rtm-*.txt
hs_err_pid*.log
This diff is collapsed.
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
# javartm library README
The javartm library allows Java/JVM users to use the "Restricted Transactional Memory" support available on some Intel CPUs. See [here][inteltsx] for more details.
In practice, this allows code running on the JVM to enjoy the benefits of Hardware Transactional Memory.
javartm is distributed under the terms of the GNU LGPLv3; see the COPYING and COPYING.LESSER files for more details.
## Hardware Requirements
As of this writing only CPUs based on the Intel Haswell architecture include the necessary hardware for RTM. See [here][inteltsx] for Intel's announcement, and [here][tsxspecs] for the specifications.
Testing on other CPUs can be done using the [Intel Software Development Emulator][intelsde].
## Building
Building is done with ant, and requires the [ant-cpptasks][ant-cpptasks] library. Note that the `ant-contrib-cpptasks` package in Ubuntu is not installed to the right place, and ant will not be able to use it automatically.
Building also requires gcc >= 4.8, as this is the first version that adds support for the RTM instructions. It might be possible to use icc or others (feedback on this is appreciated).
As of this writing, there is no stable release of gcc 4.8 --- you will need to obtain or build your own.
I plan on providing binary packages of javartm soon. If they aren't up yet, feel free to mail me to ask for one.
## Testing with Intel SDE
The script `scripts/runtest.sh` runs a very simple test of javartm using the Intel SDE.
## Who am I
My name is [Ivo Anjo][insthome] and I am a PhD student working on Software Transactional Memory and Speculative Parallelization at the [Software Engineering Group][eswweb] (ESW) at INESC-ID Lisboa.
Feel free to contact me about javartm at <ivo.anjo@ist.utl.pt>.
Some other software I am involved with and that might interest you is the [Java Versioned Software Transactional Memory (JVSTM)][jvstm], a Java library implementing TM and the [Fénix Framework][fenixf], a framework for building web-applications combining transactional memory and persistence.
Thanks for reading this far! :)
[inteltsx]: http://software.intel.com/en-us/blogs/2012/02/07/transactional-synchronization-in-haswell
[intelsde]: http://software.intel.com/en-us/articles/intel-software-development-emulator
[tsxspecs]: http://software.intel.com/sites/default/files/m/9/2/3/41604 "Intel Architecture Instruction Set Extensions Programming Reference"
[ant-cpptasks]: http://ant-contrib.sourceforge.net/cpptasks/index.html
[insthome]: https://fenix.ist.utl.pt/homepage/ist155460
[eswweb]: http://www.esw.inesc-id.pt/cgi-bin/moin.cgi
[jvstm]: http://esw.inesc-id.pt/git/jvstm.git/
[fenixf]: https://fenix-ashes.ist.utl.pt/trac/fenix-framework
\ No newline at end of file
<project name="javartm" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="java.src.dir" value="${src.dir}/java"/>
<property name="c.src.dir" value="${src.dir}/c"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jni.dir" value="${build.dir}/jni"/>
<property name="jars.dir" value="lib"/>
<property name="dist.dir" location="dist"/>
<property name="dist.version" value="0.1"/>
<property name="jar.file" value="${dist.dir}/javartm-${dist.version}.jar"/>
<path id="jars-classpath">
<fileset dir="${jars.dir}" >
<include name="*.jar"/>
</fileset>
</path>
<target name="clean">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
</target>
<target name="java-compile">
<mkdir dir="${classes.dir}"/>
<javac includeantruntime="false" srcdir="${java.src.dir}" destdir="${classes.dir}" debug="true" deprecation="true" debuglevel="lines,vars,source">
<compilerarg value="-Xlint:unchecked"/>
<classpath>
<path refid="jars-classpath"/>
</classpath>
</javac>
</target>
<target name="jni-headers" depends="java-compile">
<mkdir dir="${jni.dir}"/>
<javah destdir="${jni.dir}">
<classpath>
<path path="${classes.dir}"/>
</classpath>
<class name="javartm.Transaction"/>
</javah>
</target>
<!-- cpptasks support -->
<target name="jni-compile" depends="jni-headers">
<taskdef resource="cpptasks.tasks"/>
<cc outtype="shared" subsystem="console" objdir="${jni.dir}" outfile="${jni.dir}/javartm">
<compilerarg value="-Wall"/>
<compilerarg value="-O2"/>
<compilerarg value="-mrtm"/>
<fileset dir="${c.src.dir}" includes="**/*.c"/>
<includepath>
<path path="${java.home}/../include"/>
<path path="${jni.dir}"/>
</includepath>
</cc>
</target>
<target name="main" depends="clean,compile"/>
<!--<target name="jar" depends="main" description="Generates the javartm jar">
<mkdir dir="${dist.dir}"/>
<jar destfile="${jar.file}" basedir="${build.dir}"/>
</target>-->
<!-- aliases -->
<target name="compile" depends="jni-compile"/>
<target name="build" depends="compile"/>
<target name="clean-all" depends="clean"/>
<!--<target name="dist" depends="jar"/>-->
</project>
#!/bin/bash
# get our path
# http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
SCRIPT_DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_DIR=$SCRIPT_DIR/../
LIBS=`ls -d -1 $PROJECT_DIR/lib/* | paste -sd ":"`
CLASSES_DIR=$PROJECT_DIR/build/classes/
JNI_DIR=$PROJECT_DIR/build/jni/
export CLASSPATH=$CLASSES_DIR:$LIBS:$CLASSPATH
export LD_LIBRARY_PATH=$JNI_DIR:$LD_LIBRARY_PATH
# put custom gcc 4.8 git build in path
# this is specific to my machine, modify this accordingly if needed
export PATH=~/projects/intel-htm/gcc/bin/:$PATH
#!/bin/bash
# this is specific to my machine, modify this accordingly
SDE_DIR=~/projects/intel-htm/sde-bdw-external-5.38.0-2013-01-03-lin/
$SDE_DIR/sde64 -rtm_mode full -rtm_debug_log 1 -rtm_stats 1 -rtm_stats_call_stack 1 -rtm_cache_set_size 128 -- java javartm.Test
/*
* javartm: a Java library for Restricted Transactional Memory
* Copyright (C) 2013 Ivo Anjo <ivo.anjo@ist.utl.pt>
*
* This file is part of javartm.
*
* javartm is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* javartm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with javartm. If not, see <http://www.gnu.org/licenses/>.
*/
// COMPILE WITH gcc 4.8 and -mrtm !
// RTM intrinsics
#include <immintrin.h>
#ifndef __RTM__
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#if GCC_VERSION < 40800
#error RTM intrinsics support not found. GCC version >= 4.8 is needed for RTM support.
#else
#error RTM intrinsics support not found. Please compile with -mrtm.
#endif
#endif
#ifdef __RTM__
// Used to test cpuid support
#include <x86intrin.h>
#include <cpuid.h>
// Needed for JNI
#include <jni.h>
#include "javartm_Transaction.h"
// Other stuff
#include <stdio.h>
JNIEXPORT jboolean JNICALL Java_javartm_Transaction_rtmAvailable(JNIEnv *env, jclass cls) {
unsigned int eax, ebx, ecx, edx;
if (__get_cpuid_max(0, NULL) >= 7) {
__cpuid_count(7, 0, eax, ebx, ecx, edx);
if (ebx & bit_RTM) return 1;
}
return 0;
}
JNIEXPORT jboolean JNICALL Java_javartm_Transaction_inTransaction(JNIEnv *env, jclass cls) {
return _xtest();
}
JNIEXPORT jint JNICALL Java_javartm_Transaction_begin(JNIEnv *env, jclass cls) {
return _xbegin();
}
JNIEXPORT void JNICALL Java_javartm_Transaction_commit(JNIEnv *env, jclass cls) {
_xend();
}
JNIEXPORT void JNICALL Java_javartm_Transaction_abort(JNIEnv *env, jclass cls) {
_xabort(0);
}
JNIEXPORT jobject JNICALL Java_javartm_Transaction_doTransactionally(JNIEnv *env, jclass cls, jobject atomicBlock, jobject fallbackBlock) {
// TODO: Add some caching
jclass atomicBlockClass = (*env)->GetObjectClass(env, atomicBlock);
jmethodID callMethodId = (*env)->GetMethodID(env, atomicBlockClass, "call", "()Ljava/lang/Object;");
if (callMethodId == NULL) {
printf("Error finding method\n");
return NULL;
}
printf("Preparing execution...\n");
int res = _xbegin();
if (_xtest()) {
jobject retValue = (*env)->CallObjectMethod(env, atomicBlock, callMethodId);
_xend();
printf("Successful commit\n");
return retValue;
}
printf("Abort or failed to start tx res = %d\n", res);
jclass fallbackBlockClass = (*env)->GetObjectClass(env, fallbackBlock);
callMethodId = (*env)->GetMethodID(env, fallbackBlockClass, "call", "()Ljava/lang/Object;");
if (callMethodId == NULL) {
printf("Error finding method\n");
return NULL;
}
return (*env)->CallObjectMethod(env, fallbackBlock, callMethodId);
}
#endif // __RTM__
/*
* javartm: a Java library for Restricted Transactional Memory
* Copyright (C) 2013 Ivo Anjo <ivo.anjo@ist.utl.pt>
*
* This file is part of javartm.
*
* javartm is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* javartm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with javartm. If not, see <http://www.gnu.org/licenses/>.
*/
package javartm;
import java.util.concurrent.Callable;
public class Test {
public static int x = 0;
public static int y = 0;
public static void main(String[] args) {;
if (Transaction.rtmAvailable()) {
System.out.println("RTM is available");
} else {
System.out.println("RTM support does not seem to be available, proceeding anyway...");
}
boolean res;
// --
System.out.println("Trying to perform transaction (using doTransactionally)");
res = Transaction.doTransactionally(new Callable<Boolean>() {
public Boolean call() {
x = 1;
y = 1;
return true;
}
}, new Callable<Boolean>() {
public Boolean call() {
return false;
}
});
System.out.println("Transaction result: " + res + " (x: " + x + ", y: " + y + ")");
// --
System.out.println("Testing abort (using doTransactionally)");
res = Transaction.doTransactionally(new Callable<Boolean>() {
public Boolean call() {
Transaction.abort();
throw new AssertionError("Should never happen");
}
}, new Callable<Boolean>() {
public Boolean call() {
System.out.println("fallbackBlock!");
return true;
}
});
System.out.println("Transaction result: " + res + " (x: " + x + ", y: " + y + ")");
/*System.out.println("Trying to perform transaction (using manual API)");
res = false;
int txStatus = Transaction.begin();
if (txStatus != 0) {
x = 2;
y = 2;
res = true;
Transaction.commit();
}
System.out.println("Transaction result: " + res + " (x: " + x + ", y: " + y + ")");*/
}
}
/*
* javartm: a Java library for Restricted Transactional Memory
* Copyright (C) 2013 Ivo Anjo <ivo.anjo@ist.utl.pt>
*
* This file is part of javartm.
*
* javartm is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* javartm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with javartm. If not, see <http://www.gnu.org/licenses/>.
*/
package javartm;
import java.util.concurrent.Callable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Transaction {
private static final Logger Log = LoggerFactory.getLogger(Transaction.class);
static {
System.loadLibrary("javartm");
if (!rtmAvailable()) {
Log.warn("RTM not supported by current CPU. Attempting to use it may lead to JVM crashes");
}
}
public native static boolean rtmAvailable();
public native static boolean inTransaction();
public native static int begin();
public native static void commit();
public native static void abort();
//public native static void abort(byte reason);
public native static <V> V doTransactionally(Callable<V> atomicBlock, Callable<V> fallbackBlock);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment