From 6683debe0efeb6371e0cbafb4f0d461325dd59a5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20B=C3=A9daride?= Date: Wed, 19 Mar 2014 16:30:55 +0100 Subject: [PATCH] Add java surf plugin --- buildtools/Cmake/AddTests.cmake | 1 + buildtools/Cmake/CompleteInFiles.cmake | 3 + buildtools/Cmake/DefinePackages.cmake | 21 +++- buildtools/Cmake/MakeExe.cmake | 1 + buildtools/Cmake/MakeJava.cmake | 17 ++- buildtools/Cmake/Scripts/java_bundle.sh | 2 +- examples/java/surfPlugin/CMakeLists.txt | 47 ++++++++ examples/java/surfPlugin/Receiver.java | 36 ++++++ examples/java/surfPlugin/Sender.java | 55 +++++++++ examples/java/surfPlugin/TestPlugin.java | 37 ++++++ examples/java/surfPlugin/TracePlugin.java | 45 +++++++ .../java/surfPlugin/surfPluginDeployment.xml | 8 ++ .../java/surfPlugin/surfPluginPlatform.xml | 12 ++ examples/java/surfPlugin/surf_plugin.tesh | 23 ++++ src/bindings/java/org/simgrid/NativeLib.java | 112 ++++++++++++++++++ src/bindings/java/org/simgrid/msg/Msg.java | 98 ++------------- src/bindings/java/surf.i | 88 ++++++++++++++ src/bindings/java/surf_swig.cpp | 42 +++++++ src/bindings/java/surf_swig.hpp | 46 +++++++ src/surf/surf_interface.hpp | 4 +- 20 files changed, 603 insertions(+), 95 deletions(-) create mode 100644 examples/java/surfPlugin/CMakeLists.txt create mode 100644 examples/java/surfPlugin/Receiver.java create mode 100644 examples/java/surfPlugin/Sender.java create mode 100644 examples/java/surfPlugin/TestPlugin.java create mode 100644 examples/java/surfPlugin/TracePlugin.java create mode 100644 examples/java/surfPlugin/surfPluginDeployment.xml create mode 100644 examples/java/surfPlugin/surfPluginPlatform.xml create mode 100644 examples/java/surfPlugin/surf_plugin.tesh create mode 100644 src/bindings/java/org/simgrid/NativeLib.java create mode 100644 src/bindings/java/surf.i create mode 100644 src/bindings/java/surf_swig.cpp create mode 100644 src/bindings/java/surf_swig.hpp diff --git a/buildtools/Cmake/AddTests.cmake b/buildtools/Cmake/AddTests.cmake index 33ecd81c38..0accf2c718 100644 --- a/buildtools/Cmake/AddTests.cmake +++ b/buildtools/Cmake/AddTests.cmake @@ -625,6 +625,7 @@ if(NOT enable_memcheck) ADD_TEST(java-pingPong ${TESH_COMMAND} ${TESH_OPTION} --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/java --setenv classpath=${TESH_CLASSPATH} --cd ${CMAKE_BINARY_DIR}/examples/java ${CMAKE_HOME_DIRECTORY}/examples/java/pingPong/pingpong.tesh) ADD_TEST(java-priority ${TESH_COMMAND} ${TESH_OPTION} --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/java --setenv classpath=${TESH_CLASSPATH} --cd ${CMAKE_BINARY_DIR}/examples/java ${CMAKE_HOME_DIRECTORY}/examples/java/priority/priority.tesh) ADD_TEST(java-startKillTime ${TESH_COMMAND} ${TESH_OPTION} --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/java --setenv classpath=${TESH_CLASSPATH} --cd ${CMAKE_BINARY_DIR}/examples/java ${CMAKE_HOME_DIRECTORY}/examples/java/startKillTime/startKillTime.tesh) + ADD_TEST(java-surf-plugin ${TESH_COMMAND} ${TESH_OPTION} --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/java --setenv classpath=${TESH_CLASSPATH} --cd ${CMAKE_BINARY_DIR}/examples/java ${CMAKE_HOME_DIRECTORY}/examples/java/surfPlugin/surf_plugin.tesh) ADD_TEST(java-suspend ${TESH_COMMAND} ${TESH_OPTION} --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/java --setenv classpath=${TESH_CLASSPATH} --cd ${CMAKE_BINARY_DIR}/examples/java ${CMAKE_HOME_DIRECTORY}/examples/java/suspend/suspend.tesh) if(HAVE_TRACING) ADD_TEST(java-tracing ${TESH_COMMAND} ${TESH_OPTION} --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/java --setenv classpath=${TESH_CLASSPATH} --cd ${CMAKE_BINARY_DIR}/examples/java ${CMAKE_HOME_DIRECTORY}/examples/java/tracing/tracingPingPong.tesh) diff --git a/buildtools/Cmake/CompleteInFiles.cmake b/buildtools/Cmake/CompleteInFiles.cmake index 1085ffd7ad..5db87660cd 100644 --- a/buildtools/Cmake/CompleteInFiles.cmake +++ b/buildtools/Cmake/CompleteInFiles.cmake @@ -84,6 +84,9 @@ if(enable_java) find_package(JNI REQUIRED) message("-- [Java] JNI found: ${JNI_FOUND}") message("-- [Java] JNI include dirs: ${JNI_INCLUDE_DIRS}") + find_package(SWIG REQUIRED) + include(UseSWIG) + message("-- [Java] Swig found: ${SWIG_FOUND}") set(HAVE_Java 1) endif() if(enable_scala) diff --git a/buildtools/Cmake/DefinePackages.cmake b/buildtools/Cmake/DefinePackages.cmake index 3cda3678e7..2b7de553aa 100644 --- a/buildtools/Cmake/DefinePackages.cmake +++ b/buildtools/Cmake/DefinePackages.cmake @@ -460,6 +460,9 @@ set(JMSG_C_SRC ) set(JMSG_JAVA_SRC + src/bindings/java/org/simgrid/NativeLib.java + #src/bindings/java/org/simgrid/NativeException.java + src/bindings/java/org/simgrid/msg/As.java src/bindings/java/org/simgrid/msg/Comm.java src/bindings/java/org/simgrid/msg/File.java @@ -470,8 +473,7 @@ set(JMSG_JAVA_SRC src/bindings/java/org/simgrid/msg/Msg.java src/bindings/java/org/simgrid/msg/MsgException.java src/bindings/java/org/simgrid/msg/Mutex.java - src/bindings/java/org/simgrid/msg/NativeException.java - src/bindings/java/org/simgrid/msg/NativeLib.java + src/bindings/java/org/simgrid/msg/NativeException.java src/bindings/java/org/simgrid/msg/Process.java src/bindings/java/org/simgrid/msg/ProcessKilledError.java src/bindings/java/org/simgrid/msg/ProcessNotFoundException.java @@ -482,6 +484,20 @@ set(JMSG_JAVA_SRC src/bindings/java/org/simgrid/msg/TimeoutException.java src/bindings/java/org/simgrid/msg/TransferFailureException.java src/bindings/java/org/simgrid/msg/VM.java + + src/bindings/java/org/simgrid/surf/Surf.java + src/bindings/java/org/simgrid/surf/SurfJNI.java + src/bindings/java/org/simgrid/surf/Plugin.java + src/bindings/java/org/simgrid/surf/Model.java + src/bindings/java/org/simgrid/surf/Resource.java + src/bindings/java/org/simgrid/surf/Action.java + src/bindings/java/org/simgrid/surf/Cpu.java + src/bindings/java/org/simgrid/surf/CpuAction.java + src/bindings/java/org/simgrid/surf/NetworkLink.java + src/bindings/java/org/simgrid/surf/NetworkAction.java + + src/bindings/java/org/simgrid/surf/LmmConstraint.java + src/bindings/java/org/simgrid/surf/XbtDict.java ) set(JTRACE_C_SRC @@ -875,6 +891,7 @@ set(EXAMPLES_CMAKEFILES_TXT examples/java/cloud/CMakeLists.txt examples/java/cloud/migration/CMakeLists.txt examples/java/commTime/CMakeLists.txt + examples/java/surfPlugin/CMakeLists.txt examples/java/io/CMakeLists.txt examples/java/kademlia/CMakeLists.txt examples/java/master_slave_bypass/CMakeLists.txt diff --git a/buildtools/Cmake/MakeExe.cmake b/buildtools/Cmake/MakeExe.cmake index 72ec2fc407..1742b958f9 100644 --- a/buildtools/Cmake/MakeExe.cmake +++ b/buildtools/Cmake/MakeExe.cmake @@ -19,6 +19,7 @@ add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/java/mutualExclusion) add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/java/pingPong) add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/java/priority) add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/java/startKillTime) +add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/java/surfPlugin) add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/java/suspend) add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/java/tracing) diff --git a/buildtools/Cmake/MakeJava.cmake b/buildtools/Cmake/MakeJava.cmake index f1c43dedbf..63e64200e5 100644 --- a/buildtools/Cmake/MakeJava.cmake +++ b/buildtools/Cmake/MakeJava.cmake @@ -29,8 +29,6 @@ message("-- [Java] simgrid-java includes: ${CHECK_INCLUDES}") target_link_libraries(simgrid-java simgrid) - - if(WIN32) exec_program("java -d32 -version" OUTPUT_VARIABLE IS_32_BITS_JVM) @@ -65,6 +63,8 @@ set(LIBSIMGRID_SO libsimgrid${CMAKE_SHARED_LIBRARY_SUFFIX}) set(LIBSIMGRID_JAVA_SO ${CMAKE_SHARED_LIBRARY_PREFIX}simgrid-java${CMAKE_SHARED_LIBRARY_SUFFIX}) +set(LIBSURF_JAVA_SO + ${CMAKE_SHARED_LIBRARY_PREFIX}surf-java.so) ## Don't strip libraries if not in release mode ## @@ -87,6 +87,7 @@ set(JAVA_BUNDLE "${CMAKE_HOME_DIRECTORY}/buildtools/Cmake/Scripts/java_bundle.sh set(JAVA_BUNDLE_SO_FILES ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_SO} ${CMAKE_BINARY_DIR}/lib/${LIBSIMGRID_JAVA_SO} + ${CMAKE_BINARY_DIR}/lib/${LIBSURF_JAVA_SO} ) set(JAVA_BUNDLE_TXT_FILES ${CMAKE_HOME_DIRECTORY}/COPYING @@ -111,3 +112,15 @@ add_custom_command( COMMAND ${CMAKE_COMMAND} -E touch ${SIMGRID_JAR}_finalized ) add_custom_target(simgrid-java_jar ALL DEPENDS ${SIMGRID_JAR}_finalized) + +set(CMAKE_SWIG_FLAGS "-package" "org.simgrid.surf") +set(CMAKE_SWIG_OUTDIR "${CMAKE_HOME_DIRECTORY}/src/bindings/java/org/simgrid/surf") + +set(SURF_SWIG_FILE "${CMAKE_HOME_DIRECTORY}/src/bindings/java/surf.i") +set_source_files_properties(${SURF_SWIG_FILE} PROPERTIES CPLUSPLUS 1) +#set_source_files_properties(${SURF_SWIG_FILE} PROPERTIES SWIG_FLAGS "-includeall") +include_directories(${JNI_INCLUDE_DIRS}) +swig_add_module(surf-java java ${SURF_SWIG_FILE} "${CMAKE_HOME_DIRECTORY}/src/surf/surf_interface.hpp" "${CMAKE_HOME_DIRECTORY}/src/bindings/java/surf_swig.hpp" "${CMAKE_HOME_DIRECTORY}/src/bindings/java/surf_swig.cpp") +swig_link_libraries(surf-java simgrid) + +add_dependencies(simgrid-java surf-java) diff --git a/buildtools/Cmake/Scripts/java_bundle.sh b/buildtools/Cmake/Scripts/java_bundle.sh index 1c232b82a0..c0d51ce6be 100755 --- a/buildtools/Cmake/Scripts/java_bundle.sh +++ b/buildtools/Cmake/Scripts/java_bundle.sh @@ -20,7 +20,7 @@ JAVA=$2 STRIP=$3 shift 3 -JSG_BUNDLE=$("$JAVA" -classpath "$SIMGRID_JAR" org.simgrid.msg.NativeLib) +JSG_BUNDLE=$("$JAVA" -classpath "$SIMGRID_JAR" org.simgrid.NativeLib) # sanity check case "$JSG_BUNDLE" in diff --git a/examples/java/surfPlugin/CMakeLists.txt b/examples/java/surfPlugin/CMakeLists.txt new file mode 100644 index 0000000000..8204fcab61 --- /dev/null +++ b/examples/java/surfPlugin/CMakeLists.txt @@ -0,0 +1,47 @@ +cmake_minimum_required(VERSION 2.6) + +set(example java_surf_plugin) +set(sources + ${CMAKE_CURRENT_SOURCE_DIR}/TestPlugin.java + ${CMAKE_CURRENT_SOURCE_DIR}/TracePlugin.java + ${CMAKE_CURRENT_SOURCE_DIR}/Sender.java + ${CMAKE_CURRENT_SOURCE_DIR}/Receiver.java + ) + +if(enable_java) + add_custom_command( + COMMENT "Building ${example}..." + OUTPUT ${example}_compiled + DEPENDS ${sources} simgrid-java_jar ${SIMGRID_JAR} + COMMAND ${JAVA_COMPILE} -classpath ${SIMGRID_JAR} + -d ${CMAKE_CURRENT_BINARY_DIR}/.. ${sources} + COMMAND ${CMAKE_COMMAND} -E remove ${example}_compiled + COMMAND ${CMAKE_COMMAND} -E touch ${example}_compiled + ) + add_custom_target(${example} ALL DEPENDS ${example}_compiled) +endif() + +set(tesh_files + ${tesh_files} + ${CMAKE_CURRENT_SOURCE_DIR}/surf_plugin.tesh + PARENT_SCOPE + ) +set(xml_files + ${xml_files} + ${CMAKE_CURRENT_SOURCE_DIR}/surfPluginDeployment.xml + PARENT_SCOPE + ) +set(examples_src + ${examples_src} + ${sources} + PARENT_SCOPE + ) +set(bin_files + ${bin_files} + PARENT_SCOPE + ) +set(txt_files + ${txt_files} + ${CMAKE_CURRENT_SOURCE_DIR}/README + PARENT_SCOPE + ) diff --git a/examples/java/surfPlugin/Receiver.java b/examples/java/surfPlugin/Receiver.java new file mode 100644 index 0000000000..8d8158f8a6 --- /dev/null +++ b/examples/java/surfPlugin/Receiver.java @@ -0,0 +1,36 @@ +/* Copyright (c) 2006-2014. The SimGrid Team. + * All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +package surfPlugin; +import org.simgrid.msg.Host; +import org.simgrid.msg.Msg; +import org.simgrid.msg.MsgException; +import org.simgrid.msg.Task; +import org.simgrid.msg.Process; + +public class Receiver extends Process { + public Receiver(Host host, String name, String[]args) { + super(host,name,args); + } + final double commSizeLat = 1; + final double commSizeBw = 100000000; + + public void main(String[] args) throws MsgException { + + Msg.info("helloo!"); + double communicationTime=0; + + Msg.info("try to get a task"); + + Task task = Task.receive(getHost().getName()); + double timeGot = Msg.getClock(); + + Msg.info("Got at time "+ timeGot); + task.execute(); + + Msg.info("goodbye!"); + } +} diff --git a/examples/java/surfPlugin/Sender.java b/examples/java/surfPlugin/Sender.java new file mode 100644 index 0000000000..2f4a38268f --- /dev/null +++ b/examples/java/surfPlugin/Sender.java @@ -0,0 +1,55 @@ +/* Copyright (c) 2006-2014. The SimGrid Team. + * All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +package surfPlugin; +import org.simgrid.msg.Host; +import org.simgrid.msg.HostNotFoundException; +import org.simgrid.msg.Msg; +import org.simgrid.msg.MsgException; +import org.simgrid.msg.Process; +import org.simgrid.msg.Task; + +public class Sender extends Process { + public Sender(Host host, String name, String[] args) { + super(host,name,args); + } + private final double commSizeLat = 1; + final double commSizeBw = 100000000; + + public void main(String[] args) throws MsgException { + + Msg.info("helloo!"); + + int hostCount = args.length; + + Msg.info("host count: " + hostCount); + String mailboxes[] = new String[hostCount]; + double time; + double computeDuration = 10000; + Task task; + + for(int pos = 0; pos < args.length ; pos++) { + try { + mailboxes[pos] = Host.getByName(args[pos]).getName(); + } catch (HostNotFoundException e) { + Msg.info("Invalid deployment file: " + e.toString()); + System.exit(1); + } + } + + for (int pos = 0; pos < hostCount; pos++) { + time = Msg.getClock(); + + Msg.info("sender time: " + time); + + task = new Task("no name",computeDuration,commSizeLat); + + task.send(mailboxes[pos]); + } + + Msg.info("goodbye!"); + } +} diff --git a/examples/java/surfPlugin/TestPlugin.java b/examples/java/surfPlugin/TestPlugin.java new file mode 100644 index 0000000000..cf0752bade --- /dev/null +++ b/examples/java/surfPlugin/TestPlugin.java @@ -0,0 +1,37 @@ +/* Copyright (c) 2006-2014. The SimGrid Team. + * All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +package surfPlugin; +import org.simgrid.msg.Msg; +import org.simgrid.msg.NativeException; +import org.simgrid.surf.Surf; +import org.simgrid.surf.Cpu; + +public class TestPlugin { + + /* This only contains the launcher. If you do nothing more than than you can run + * java simgrid.msg.Msg + * which also contains such a launcher + */ + //static TracePlugin tp = new TracePlugin(); + + public static void main(String[] args) throws NativeException { + /* initialize the MSG simulation. Must be done before anything else (even logging). */ + Msg.init(args); + if(args.length < 2) { + Msg.info("Usage : TestPlugin platform_file deployment_file"); + Msg.info("example : TestPlugin ping_pong_platform.xml ping_pong_deployment.xml"); + System.exit(1); + } + TracePlugin tp = new TracePlugin(); + + /* construct the platform and deploy the application */ + Msg.createEnvironment(args[0]); + Msg.deployApplication(args[1]); + /* execute the simulation. */ + Msg.run(); + } +} diff --git a/examples/java/surfPlugin/TracePlugin.java b/examples/java/surfPlugin/TracePlugin.java new file mode 100644 index 0000000000..e1212745cf --- /dev/null +++ b/examples/java/surfPlugin/TracePlugin.java @@ -0,0 +1,45 @@ +package surfPlugin; + +import org.simgrid.surf.*; +import org.simgrid.msg.Msg; +import java.util.HashMap; + +public class TracePlugin extends Plugin { + + public TracePlugin() { + activateCpuCreatedCallback(); + //activateCpuDestructedCallback(); + activateCpuStateChangedCallback(); + activateCpuActionStateChangedCallback(); + + activateNetworkLinkCreatedCallback(); + //activateCpuDestructedCallback(); + activateNetworkLinkStateChangedCallback(); + activateNetworkActionStateChangedCallback(); + } + + public void cpuCreatedCallback(Cpu cpu) { + Msg.info("Trace: Cpu created "+cpu.getName()); + } + + public void cpuStateChangedCallback(Cpu cpu){ + Msg.info("Trace: Cpu state changed "+cpu.getName()); + } + + public void cpuActionStateChangedCallback(CpuAction action){ + Msg.info("Trace: CpuAction state changed "+action.getModel().getName()); + } + + public void networkLinkCreatedCallback(NetworkLink link) { + Msg.info("Trace: NetworkLink created "+link.getName()); + } + + public void networkLinkStateChangedCallback(NetworkLink link){ + Msg.info("Trace: NetworkLink state changed "+link.getName()); + } + + public void networkActionStateChangedCallback(NetworkAction action){ + Msg.info("Trace: NetworkAction state changed "+action.getModel().getName()); + } + +} diff --git a/examples/java/surfPlugin/surfPluginDeployment.xml b/examples/java/surfPlugin/surfPluginDeployment.xml new file mode 100644 index 0000000000..9101480bb0 --- /dev/null +++ b/examples/java/surfPlugin/surfPluginDeployment.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/examples/java/surfPlugin/surfPluginPlatform.xml b/examples/java/surfPlugin/surfPluginPlatform.xml new file mode 100644 index 0000000000..be9a6e1bf0 --- /dev/null +++ b/examples/java/surfPlugin/surfPluginPlatform.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/examples/java/surfPlugin/surf_plugin.tesh b/examples/java/surfPlugin/surf_plugin.tesh new file mode 100644 index 0000000000..c20953704e --- /dev/null +++ b/examples/java/surfPlugin/surf_plugin.tesh @@ -0,0 +1,23 @@ +#! tesh + +$ java -classpath ${classpath:=.} surfPlugin/TestPlugin ${srcdir:=.}/surfPlugin/surfPluginPlatform.xml ${srcdir:=.}/surfPlugin/surfPluginDeployment.xml +> [0.000000] [jmsg/INFO] Using regular java threads. Coroutines could speed your simulation up. +> [0.000000] [jmsg/INFO] Trace: NetworkLink created __loopback__ +> [0.000000] [jmsg/INFO] Trace: NetworkLink state changed __loopback__ +> [0.000000] [jmsg/INFO] Trace: Cpu created Jacquelin +> [0.000000] [jmsg/INFO] Trace: Cpu state changed Jacquelin +> [0.000000] [jmsg/INFO] Trace: Cpu created Boivin +> [0.000000] [jmsg/INFO] Trace: Cpu state changed Boivin +> [0.000000] [jmsg/INFO] Trace: NetworkLink created link +> [0.000000] [jmsg/INFO] Trace: NetworkLink state changed link +> [Jacquelin:surfPlugin.Sender:(1) 0.000000] [jmsg/INFO] helloo! +> [Jacquelin:surfPlugin.Sender:(1) 0.000000] [jmsg/INFO] host count: 1 +> [Jacquelin:surfPlugin.Sender:(1) 0.000000] [jmsg/INFO] sender time: 0.0 +> [Boivin:surfPlugin.Receiver:(2) 0.000000] [jmsg/INFO] helloo! +> [Boivin:surfPlugin.Receiver:(2) 0.000000] [jmsg/INFO] try to get a task +> [1.301001] [jmsg/INFO] Trace: NetworkAction state changed network +> [Boivin:surfPlugin.Receiver:(2) 1.301001] [jmsg/INFO] Got at time 1.3010010824742269 +> [Jacquelin:surfPlugin.Sender:(1) 1.301001] [jmsg/INFO] goodbye! +> [1.301103] [jmsg/INFO] Trace: CpuAction state changed cpu +> [Boivin:surfPlugin.Receiver:(2) 1.301103] [jmsg/INFO] goodbye! +> [1.301103] [jmsg/INFO] MSG_main finished; Cleaning up the simulation... diff --git a/src/bindings/java/org/simgrid/NativeLib.java b/src/bindings/java/org/simgrid/NativeLib.java new file mode 100644 index 0000000000..130c904f6c --- /dev/null +++ b/src/bindings/java/org/simgrid/NativeLib.java @@ -0,0 +1,112 @@ +package org.simgrid; + +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.File; + +public final class NativeLib { + + public static String getPath() { + String prefix = "NATIVE"; + String os = System.getProperty("os.name"); + String arch = System.getProperty("os.arch"); + + if (os.toLowerCase().startsWith("^win")) + os = "Windows"; + else if (os.contains("OS X")) + os = "Darwin"; + + if (arch.matches("^i[3-6]86$")) + arch = "x86"; + else if (arch.equalsIgnoreCase("amd64")) + arch = "x86_64"; + + os = os.replace(' ', '_'); + arch = arch.replace(' ', '_'); + + return prefix + "/" + os + "/" + arch + "/"; + } + public static void nativeInit(String name) { + try { + /* prefer the version on disk, if existing */ + System.loadLibrary(name); + } catch (UnsatisfiedLinkError e) { + /* If not found, unpack the one bundled into the jar file and use it */ + loadLib(name); + } + } + + private static void loadLib (String name) { + String Path = NativeLib.getPath(); + + String filename=name; + InputStream in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename); + + if (in == null) { + filename = "lib"+name+".so"; + in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename); + } + if (in == null) { + filename = name+".dll"; + in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename); + } + if (in == null) { + filename = "lib"+name+".dll"; + in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename); + } + if (in == null) { + filename = "lib"+name+".dylib"; + in = NativeLib.class.getClassLoader().getResourceAsStream(Path+filename); + } + if (in == null) { + throw new RuntimeException("Cannot find library "+name+" in path "+Path+". Sorry, but this jar does not seem to be usable on your machine."); + } + try { + // We must write the lib onto the disk before loading it -- stupid operating systems + File fileOut = new File(filename); + fileOut = File.createTempFile(name+"-", ".tmp"); + // don't leak the file on disk, but remove it on JVM shutdown + Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(fileOut.getAbsolutePath()))); + OutputStream out = new FileOutputStream(fileOut); + + /* copy the library in position */ + byte[] buffer = new byte[4096]; + int bytes_read; + while ((bytes_read = in.read(buffer)) != -1) // Read until EOF + out.write(buffer, 0, bytes_read); + + /* close all file descriptors, and load that shit */ + in.close(); + out.close(); + System.load(fileOut.getAbsolutePath()); + + } catch (Exception e) { + System.err.println("Cannot load the bindings to the "+name+" library: "); + e.printStackTrace(); + System.err.println("This jar file does not seem to fit your system, sorry"); + System.exit(1); + } + } + + /* A hackish mechanism used to remove the file containing our library when the JVM shuts down */ + private static class FileCleaner implements Runnable { + private String target; + public FileCleaner(String name) { + target = name; + } + public void run() { + try { + new File(target).delete(); + } catch(Exception e) { + System.out.println("Unable to clean temporary file "+target+" during shutdown."); + e.printStackTrace(); + } + } + } + + + public static void main(String[] args) { + System.out.println(getPath()); + } +} diff --git a/src/bindings/java/org/simgrid/msg/Msg.java b/src/bindings/java/org/simgrid/msg/Msg.java index 108c7b53b7..ec212a7d8e 100644 --- a/src/bindings/java/org/simgrid/msg/Msg.java +++ b/src/bindings/java/org/simgrid/msg/Msg.java @@ -7,6 +7,7 @@ * under the terms of the license (GNU LGPL) which comes with this package. */ package org.simgrid.msg; +import org.simgrid.NativeLib; import java.io.FileOutputStream; import java.io.InputStream; @@ -17,98 +18,19 @@ import java.io.File; public final class Msg { /* Statically load the library which contains all native functions used in here */ static private boolean isNativeInited = false; - public static void nativeInit() { - if (isNativeInited) - return; - try { - /* prefer the version on disk, if existing */ - System.loadLibrary("simgrid-java"); - } catch (UnsatisfiedLinkError e) { - /* If not found, unpack the one bundled into the jar file and use it */ - loadLib("simgrid"); - loadLib("simgrid-java"); - } + public static void nativeInit() { + if (isNativeInited) + return; + NativeLib.nativeInit("simgrid"); + NativeLib.nativeInit("simgrid-java"); isNativeInited = true; + } + static { - nativeInit(); + nativeInit(); } - private static void loadLib (String name) { - String Path = NativeLib.getPath(); - - String filename=name; - InputStream in = Msg.class.getClassLoader().getResourceAsStream(Path+filename); - - if (in == null) { - filename = "lib"+name+".so"; - in = Msg.class.getClassLoader().getResourceAsStream(Path+filename); - } - if (in == null) { - filename = name+".dll"; - in = Msg.class.getClassLoader().getResourceAsStream(Path+filename); - } - if (in == null) { - filename = "lib"+name+".dll"; - in = Msg.class.getClassLoader().getResourceAsStream(Path+filename); - } - if (in == null) { - filename = "lib"+name+".dylib"; - in = Msg.class.getClassLoader().getResourceAsStream(Path+filename); - } - if (in == null) { - throw new RuntimeException("Cannot find library "+name+" in path "+Path+". Sorry, but this jar does not seem to be usable on your machine."); - } -// Caching the file on disk: desactivated because it could fool the users -// if (new File(filename).isFile()) { -// // file was already unpacked -- use it directly -// System.load(new File(".").getAbsolutePath()+File.separator+filename); -// return; -// } - try { - // We must write the lib onto the disk before loading it -- stupid operating systems - File fileOut = new File(filename); -// if (!new File(".").canWrite()) { -// System.out.println("Cannot write in ."+File.separator+filename+"; unpacking the library into a temporary file instead"); - fileOut = File.createTempFile("simgrid-", ".tmp"); - // don't leak the file on disk, but remove it on JVM shutdown - Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(fileOut.getAbsolutePath()))); -// } -// System.out.println("Unpacking SimGrid native library to " + fileOut.getAbsolutePath()); - OutputStream out = new FileOutputStream(fileOut); - - /* copy the library in position */ - byte[] buffer = new byte[4096]; - int bytes_read; - while ((bytes_read = in.read(buffer)) != -1) // Read until EOF - out.write(buffer, 0, bytes_read); - - /* close all file descriptors, and load that shit */ - in.close(); - out.close(); - System.load(fileOut.getAbsolutePath()); - } catch (Exception e) { - System.err.println("Cannot load the bindings to the simgrid library: "); - e.printStackTrace(); - System.err.println("This jar file does not seem to fit your system, sorry"); - System.exit(1); - } - } - /* A hackish mechanism used to remove the file containing our library when the JVM shuts down */ - private static class FileCleaner implements Runnable { - private String target; - public FileCleaner(String name) { - target = name; - } - public void run() { - try { - new File(target).delete(); - } catch(Exception e) { - System.out.println("Unable to clean temporary file "+target+" during shutdown."); - e.printStackTrace(); - } - } - } - + /** Retrieve the simulation time * @return The simulation time. */ diff --git a/src/bindings/java/surf.i b/src/bindings/java/surf.i new file mode 100644 index 0000000000..4d69190b74 --- /dev/null +++ b/src/bindings/java/surf.i @@ -0,0 +1,88 @@ +/* File : example.i */ +%module(directors="1") Surf + +%pragma(java) jniclassimports=%{ +import org.simgrid.NativeLib; + +%} +%pragma(java) jniclasscode=%{ + static { + NativeLib.nativeInit("surf-java"); + } +%} + +%{ +#include "src/surf/cpu_interface.hpp" +#include "src/surf/network_interface.hpp" +#include "src/bindings/java/surf_swig.hpp" +#include "src/xbt/dict_private.h" +typedef struct lmm_constraint *lmm_constraint_t; +%} + +%feature("director") Plugin; + +%include "src/bindings/java/surf_swig.hpp" + +%nodefaultctor Model; +class Model { +public: + const char *getName(); +}; + +class Resource { +public: + Resource(); + const char *getName(); + virtual bool isUsed()=0; + lmm_constraint *getConstraint(); + s_xbt_dict *getProperties(); +}; + +class Cpu : public Resource { +public: + Cpu(); + ~Cpu(); + double getCurrentPowerPeak(); +}; + +class NetworkLink : public Resource { +public: + NetworkLink(); + ~NetworkLink(); +}; + +class Action { +public: + Model *getModel(); +}; + +class CpuAction : public Action { +public: +%extend { + Cpu *getCpu() {return getActionCpu($self);} +} +}; + +%nodefaultctor NetworkAction; +class NetworkAction : public Action { +public: +%extend { + double getLatency() {return $self->m_latency;} +} +}; + + +%rename lmm_constraint LmmConstraint; +struct lmm_constraint { +%extend { + double getUsage() {return lmm_constraint_get_usage($self);} +} +}; + +%rename s_xbt_dict XbtDict; +struct s_xbt_dict { +%extend { + char *getValue(char *key) {return (char*)xbt_dict_get_or_null($self, key);} +} +}; + diff --git a/src/bindings/java/surf_swig.cpp b/src/bindings/java/surf_swig.cpp new file mode 100644 index 0000000000..6da728c427 --- /dev/null +++ b/src/bindings/java/surf_swig.cpp @@ -0,0 +1,42 @@ +#include +#include "src/surf/surf_interface.hpp" +#include "surf_swig.hpp" + +double getClock() { + surf_get_clock(); +} + +void Plugin::activateCpuCreatedCallback(){ + surf_callback_connect(cpuCreatedCallbacks, boost::bind(&Plugin::cpuCreatedCallback, this, _1)); +} + +void Plugin::activateCpuDestructedCallback(){ + surf_callback_connect(cpuDestructedCallbacks, boost::bind(&Plugin::cpuDestructedCallback, this, _1)); +} + +void Plugin::activateCpuStateChangedCallback(){ + surf_callback_connect(cpuStateChangedCallbacks, boost::bind(&Plugin::cpuStateChangedCallback, this, _1)); +} + +void Plugin::activateCpuActionStateChangedCallback(){ + surf_callback_connect(cpuActionStateChangedCallbacks, boost::bind(&Plugin::cpuActionStateChangedCallback, this, _1)); +} + + +void Plugin::activateNetworkLinkCreatedCallback(){ + surf_callback_connect(networkLinkCreatedCallbacks, boost::bind(&Plugin::networkLinkCreatedCallback, this, _1)); +} + +void Plugin::activateNetworkLinkDestructedCallback(){ + surf_callback_connect(networkLinkDestructedCallbacks, boost::bind(&Plugin::networkLinkDestructedCallback, this, _1)); +} + +void Plugin::activateNetworkLinkStateChangedCallback(){ + surf_callback_connect(networkLinkStateChangedCallbacks, boost::bind(&Plugin::networkLinkStateChangedCallback, this, _1)); +} + +void Plugin::activateNetworkActionStateChangedCallback(){ + surf_callback_connect(networkActionStateChangedCallbacks, boost::bind(&Plugin::networkActionStateChangedCallback, this, _1)); +} + + diff --git a/src/bindings/java/surf_swig.hpp b/src/bindings/java/surf_swig.hpp new file mode 100644 index 0000000000..6066116a03 --- /dev/null +++ b/src/bindings/java/surf_swig.hpp @@ -0,0 +1,46 @@ +#include +#include +#include "src/surf/cpu_interface.hpp" +#include "src/surf/network_interface.hpp" +#include "src/surf/maxmin_private.hpp" + +double getClock(); + +class Plugin { +public: + virtual ~Plugin() { + std::cout << "Plugin::~Plugin()" << std:: endl; + } + + void exit() { + surf_exit(); + } + + void activateCpuCreatedCallback(); + virtual void cpuCreatedCallback(Cpu *cpu) {} + + void activateCpuDestructedCallback(); + virtual void cpuDestructedCallback(Cpu *cpu) {} + + void activateCpuStateChangedCallback(); + virtual void cpuStateChangedCallback(Cpu *cpu) {} + + void activateCpuActionStateChangedCallback(); + virtual void cpuActionStateChangedCallback(CpuAction *action) {} + + + void activateNetworkLinkCreatedCallback(); + virtual void networkLinkCreatedCallback(NetworkLink *link) {} + + void activateNetworkLinkDestructedCallback(); + virtual void networkLinkDestructedCallback(NetworkLink *link) {} + + void activateNetworkLinkStateChangedCallback(); + virtual void networkLinkStateChangedCallback(NetworkLink *link) {} + + void activateNetworkActionStateChangedCallback(); + virtual void networkActionStateChangedCallback(NetworkAction *action) {} + +}; + + diff --git a/src/surf/surf_interface.hpp b/src/surf/surf_interface.hpp index 6be1e2c40e..a56495f557 100644 --- a/src/surf/surf_interface.hpp +++ b/src/surf/surf_interface.hpp @@ -631,6 +631,8 @@ public: s_xbt_swag_hookup_t p_stateHookup; + ModelPtr getModel() {return p_model;} + protected: ActionListPtr p_stateSet; double m_priority; /**< priority (1.0 by default) */ @@ -639,8 +641,6 @@ protected: double m_maxDuration; /*< max_duration (may fluctuate until the task is completed) */ double m_finish; /**< finish time : this is modified during the run and fluctuates until the task is completed */ - ModelPtr getModel() {return p_model;} - private: int resourceUsed(void *resource_id); -- 2.20.1