Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
export the energy plugin to the java world
authorMartin Quinson <martin.quinson@loria.fr>
Tue, 19 Jan 2016 15:07:30 +0000 (16:07 +0100)
committerMartin Quinson <martin.quinson@loria.fr>
Tue, 19 Jan 2016 15:07:30 +0000 (16:07 +0100)
13 files changed:
.gitignore
examples/java/energy/CMakeLists.txt [new file with mode: 0644]
examples/java/energy/Energy.java [new file with mode: 0644]
examples/java/energy/EnergyConsumer.java [new file with mode: 0644]
examples/java/energy/energy.tesh [new file with mode: 0644]
src/bindings/java/jmsg.cpp
src/bindings/java/jmsg.h
src/bindings/java/jmsg_host.cpp
src/bindings/java/jmsg_host.h
src/bindings/java/org/simgrid/msg/Host.java
src/bindings/java/org/simgrid/msg/Msg.java
tools/cmake/MakeExe.cmake
tools/cmake/Tests.cmake

index 3e7397f..626c4c4 100644 (file)
@@ -1052,6 +1052,7 @@ examples/java/chord/java_chord_compiled
 examples/java/cloud/java_cloud_compiled
 examples/java/cloud/migration/java_cloud_migration_compiled
 examples/java/commTime/java_commTime_compiled
+examples/java/energy/java_energy_compiled
 examples/java/io/java_io_compiled
 examples/java/kademlia/java_kademlia_compiled
 examples/java/master_slave_bypass/java_master_slave_bypass_compiled
diff --git a/examples/java/energy/CMakeLists.txt b/examples/java/energy/CMakeLists.txt
new file mode 100644 (file)
index 0000000..f881650
--- /dev/null
@@ -0,0 +1,43 @@
+set(example java_energy)
+set(sources
+  ${CMAKE_CURRENT_SOURCE_DIR}/EnergyConsumer.java
+  ${CMAKE_CURRENT_SOURCE_DIR}/Energy.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}/energy.tesh
+  PARENT_SCOPE
+  )
+set(xml_files
+  ${xml_files}
+  ${CMAKE_CURRENT_SOURCE_DIR}/energyDeployment.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/energy/Energy.java b/examples/java/energy/Energy.java
new file mode 100644 (file)
index 0000000..8f70b7b
--- /dev/null
@@ -0,0 +1,41 @@
+/* Copyright (c) 2012-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 energy;
+
+import org.simgrid.msg.Host;
+import org.simgrid.msg.Msg;
+import org.simgrid.msg.MsgException;
+/**
+ * Example showing the use of the new experimental Cloud API.
+ */
+public class Energy {
+       public static final double task_comp_size = 10;
+       public static final double task_comm_size = 10;
+       public static final int hostNB = 2 ; 
+       public static void main(String[] args) throws MsgException {  
+               Msg.energyInit(); 
+           Msg.init(args); 
+           
+           if (args.length < 1) {
+               Msg.info("Usage  : Cloud platform_file");
+               Msg.info("Usage  : Cloud platform.xml");
+               System.exit(1);
+           }
+           /* Construct the platform */
+               Msg.createEnvironment(args[0]);
+               Host[] hosts = Host.all();
+               if (hosts.length < 1) {
+                       Msg.info("I need at least one host in the platform file, but " + args[0] + " contains only " + hosts.length + " hosts");
+                       System.exit(42);
+               }
+               /* Instanciate a process */
+               new EnergyConsumer(hosts[0],"energyConsumer",null).start();
+               /* Execute the simulation */
+               Msg.run();
+               
+    }
+}
diff --git a/examples/java/energy/EnergyConsumer.java b/examples/java/energy/EnergyConsumer.java
new file mode 100644 (file)
index 0000000..50b6ea8
--- /dev/null
@@ -0,0 +1,29 @@
+/* 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 energy;
+
+import org.simgrid.msg.Comm;
+import org.simgrid.msg.Host;
+import org.simgrid.msg.Msg;
+import org.simgrid.msg.MsgException;
+import org.simgrid.msg.Process;
+import org.simgrid.msg.Task;
+import org.simgrid.msg.TimeoutException;
+
+public class EnergyConsumer extends Process {
+       public EnergyConsumer(Host host, String name, String[] args) {
+               super(host,name,args);
+       }
+       @Override
+       public void main(String[] args) throws MsgException {
+          Msg.info("Currently consumed energy: "+getHost().getConsumedEnergy());
+          this.waitFor(10);
+          Msg.info("Currently consumed energy after sleeping 10 sec: "+getHost().getConsumedEnergy());
+          new Task(null, 1E9, 0).execute();
+          Msg.info("Currently consumed energy after executing 1E9 flops: "+getHost().getConsumedEnergy());        
+       }
+}
diff --git a/examples/java/energy/energy.tesh b/examples/java/energy/energy.tesh
new file mode 100644 (file)
index 0000000..9bd1cd5
--- /dev/null
@@ -0,0 +1,14 @@
+#! tesh
+
+! timeout 15
+
+$ java -classpath ${classpath:=.} energy/Energy ${srcdir:=.}/../platforms/energy_platform.xml
+> [0.000000] [jmsg/INFO] Using regular java threads.
+> [MyHost1:energyConsumer:(1) 0.000000] [jmsg/INFO] Currently consumed energy: 0.0
+> [MyHost1:energyConsumer:(1) 10.000000] [jmsg/INFO] Currently consumed energy after sleeping 10 sec: 1000.0
+> [MyHost1:energyConsumer:(1) 20.000000] [jmsg/INFO] Currently consumed energy after executing 1E9 flops: 3000.0
+> [20.000000] [jmsg/INFO] MSG_main finished; Cleaning up the simulation...
+> [20.000000] [surf_energy/INFO] Total energy of host MyHost1: 3000.000000 Joules
+> [20.000000] [surf_energy/INFO] Total energy of host MyHost2: 2000.000000 Joules
+> [20.000000] [surf_energy/INFO] Total energy of host MyHost3: 2000.000000 Joules
+
index a3479b3..b8300a4 100644 (file)
@@ -359,3 +359,10 @@ void java_main_jprocess(jobject jprocess)
 }
 }
 
+
+
+#include "simgrid/plugins/energy.h"
+JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Msg_energyInit(void) {
+  sg_energy_plugin_init();
+}
index bc8ad54..75ad3db 100644 (file)
@@ -40,6 +40,9 @@ JNIEXPORT void JNICALL
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs);
 
+JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Msg_energyInit(void);
+
 JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_debug(JNIEnv *, jclass,
                                                  jstring);
 
index accb02e..55cc4a1 100644 (file)
@@ -381,3 +381,17 @@ Java_org_simgrid_msg_Host_setAsyncMailbox(JNIEnv * env, jclass cls_arg, jobject
   env->ReleaseStringUTFChars((jstring) jname, name);
 
 }
+
+#include "simgrid/plugins/energy.h"
+JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getConsumedEnergy (JNIEnv *env, jobject jhost) {
+  msg_host_t host = jhost_get_native(env, jhost);
+
+  if (!host) {
+    jxbt_throw_notbound(env, "host", jhost);
+    return 0;
+  }
+
+  return MSG_host_get_consumed_energy(host);
+
+}
+
index b3d8b81..5a342f2 100644 (file)
@@ -214,6 +214,8 @@ Java_org_simgrid_msg_Host_all(JNIEnv *, jclass);
 JNIEXPORT void JNICALL 
 Java_org_simgrid_msg_Host_setAsyncMailbox(JNIEnv * env, jclass cls_arg, jobject jname);
 
+JNIEXPORT jdouble JNICALL Java_org_simgrid_msg_Host_getConsumedEnergy
+    (JNIEnv *, jobject);
 
 
 #endif                          /*!MSG_JHOST_H */
index 6738525..aa058cf 100644 (file)
@@ -206,6 +206,8 @@ public class Host {
         */
        public native String[] getAttachedStorage();
 
+       /** Returns the amount of Joules consumed by that host so far */
+       public native double getConsumedEnergy();
 
        /**
         * Class initializer, to initialize various JNI stuff
index b324db8..263a22f 100644 (file)
@@ -57,6 +57,9 @@ public final class Msg {
         * @param args            The arguments of the command line of the simulation.
         */
        public final static native void init(String[]args);
+       
+       /** Tell the kernel that you want to use the energy plugin */
+       public final static native void energyInit();
 
        /**
         * Run the MSG simulation.
index 19b8395..813105e 100644 (file)
@@ -9,6 +9,7 @@ add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/java/chord)
 add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/java/cloud)
 add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/java/cloud/migration)
 add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/java/commTime)
+add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/java/energy)
 add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/java/io)
 add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/java/kademlia)
 add_subdirectory(${CMAKE_HOME_DIRECTORY}/examples/java/master_slave_bypass)
index 219e8a3..c06f091 100644 (file)
@@ -522,6 +522,7 @@ IF(NOT enable_memcheck)
     ADD_TESH(java-cloud                          --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/java --setenv classpath=${TESH_CLASSPATH} --cd ${CMAKE_BINARY_DIR}/examples/java ${CMAKE_HOME_DIRECTORY}/examples/java/cloud/cloud.tesh)
     ADD_TESH(java-cloud-migration                --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/java --setenv classpath=${TESH_CLASSPATH} --cd ${CMAKE_BINARY_DIR}/examples/java ${CMAKE_HOME_DIRECTORY}/examples/java/cloud/migration/migration.tesh)
     ADD_TESH(java-commTime                       --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/java --setenv classpath=${TESH_CLASSPATH} --cd ${CMAKE_BINARY_DIR}/examples/java ${CMAKE_HOME_DIRECTORY}/examples/java/commTime/commtime.tesh)
+    ADD_TESH(java-energy                         --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/java --setenv classpath=${TESH_CLASSPATH} --cd ${CMAKE_BINARY_DIR}/examples/java ${CMAKE_HOME_DIRECTORY}/examples/java/energy/energy.tesh)
     ADD_TESH(java-kademlia                       --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/java --setenv classpath=${TESH_CLASSPATH} --cd ${CMAKE_BINARY_DIR}/examples/java ${CMAKE_HOME_DIRECTORY}/examples/java/kademlia/kademlia.tesh)
     ADD_TESH(java-kill                           --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/java --setenv classpath=${TESH_CLASSPATH} --cd ${CMAKE_BINARY_DIR}/examples/java ${CMAKE_HOME_DIRECTORY}/examples/java/master_slave_kill/kill.tesh)
     ADD_TESH(java-masterslave                    --setenv srcdir=${CMAKE_HOME_DIRECTORY}/examples/java --setenv classpath=${TESH_CLASSPATH} --cd ${CMAKE_BINARY_DIR}/examples/java ${CMAKE_HOME_DIRECTORY}/examples/java/masterslave/masterslave.tesh)