From: Samuel Lepetit Date: Thu, 14 Jun 2012 13:49:43 +0000 (+0200) Subject: Add an example for the cloud API (doesn't work yet). X-Git-Tag: v3_9_90~569^2~19^2~44 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/602eda3cfcfd79872ebc654fe288c761d782534a Add an example for the cloud API (doesn't work yet). Removed an useless MSG_process_set_data --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b442b39f2..9806f484e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -157,6 +157,10 @@ set(JAVA_EXAMPLES ${CMAKE_HOME_DIRECTORY}/examples/chord/FindSuccessorTask.java ${CMAKE_HOME_DIRECTORY}/examples/chord/GetPredecessorAnswerTask.java ${CMAKE_HOME_DIRECTORY}/examples/chord/NotifyTask.java + ${CMAKE_HOME_DIRECTORY}/examples/cloud/Cloud.java + ${CMAKE_HOME_DIRECTORY}/examples/cloud/FinalizeTask.java + ${CMAKE_HOME_DIRECTORY}/examples/cloud/Master.java + ${CMAKE_HOME_DIRECTORY}/examples/cloud/Slave.java ${CMAKE_HOME_DIRECTORY}/examples/commTime/FinalizeTask.java ${CMAKE_HOME_DIRECTORY}/examples/commTime/Master.java ${CMAKE_HOME_DIRECTORY}/examples/commTime/Slave.java @@ -391,6 +395,7 @@ add_custom_command( COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/async/*.java COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/bittorrent/*.java COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/chord/*.java + COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/cloud/*.java COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/commTime/*.java COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/io/*.java COMMAND ${JAVA_COMPILE} -d ${CMAKE_HOME_DIRECTORY}/examples -cp ${CMAKE_HOME_DIRECTORY}/simgrid.jar ${CMAKE_HOME_DIRECTORY}/examples/masterslave/*.java diff --git a/examples/cloud/Cloud.java b/examples/cloud/Cloud.java new file mode 100644 index 0000000000..69dd79b113 --- /dev/null +++ b/examples/cloud/Cloud.java @@ -0,0 +1,42 @@ +/* + * Copyright 2012. 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 cloud; + +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 Cloud { + public static final double task_comp_size = 10000000; + public static final double task_comm_size = 10000000; + + public static void main(String[] args) throws MsgException { + 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]); + /* Retrieve the 10 first hosts of the platform file */ + Host[] hosts = Host.all(); + if (hosts.length < 10) { + Msg.info("I need at least 10 hosts in the platform file, but " + args[0] + " contains only " + hosts.length + " hosts"); + System.exit(42); + } + new Master(hosts[0],"Master",hosts).start(); + /* Execute the simulation */ + Msg.run(); + + Msg.clean(); + } +} \ No newline at end of file diff --git a/examples/cloud/FinalizeTask.java b/examples/cloud/FinalizeTask.java new file mode 100644 index 0000000000..c7b5c54420 --- /dev/null +++ b/examples/cloud/FinalizeTask.java @@ -0,0 +1,15 @@ +/* + * Copyright 2012. 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 cloud; + +import org.simgrid.msg.Task; + +public class FinalizeTask extends Task { + public FinalizeTask(double compSize, double commSize) { + super("Finalize",compSize,commSize); + } +} \ No newline at end of file diff --git a/examples/cloud/Master.java b/examples/cloud/Master.java new file mode 100644 index 0000000000..791a956105 --- /dev/null +++ b/examples/cloud/Master.java @@ -0,0 +1,98 @@ +/* + * Copyright 2012. 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 cloud; + +import java.util.ArrayList; + +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.VM; + +public class Master extends Process { + private Host[] hosts; + + public Master(Host host, String name, Host[] hosts) { + super(host,name,null); + this.hosts = hosts; + } + public void main(String[] args) throws MsgException { + int slavesCount = 10; + + ArrayList vms = new ArrayList(); + + for (int i = 0; i < slavesCount; i++) { + Slave slave = new Slave(hosts[i],i); + slave.start(); + VM vm = new VM(hosts[i],1); + vm.bind(slave); + vms.add(vm); + } + Msg.info("Launched " + vms.size() + " VMs"); + + Msg.info("Send a first batch of work to everyone"); + workBatch(slavesCount); + + Msg.info("Now suspend all VMs, just for fun"); + for (int i = 0; i < vms.size(); i++) { + vms.get(i).suspend(); + } + + Msg.info("Wait a while"); + waitFor(2); + + Msg.info("Enough. Let's resume everybody."); + for (int i = 0; i < vms.size(); i++) { + vms.get(i).resume(); + } + + Msg.info("Sleep long enough for everyone to be done with previous batch of work"); + waitFor(1000 - Msg.getClock()); + + Msg.info("Add one more process per VM, and dispatch a batch of work to everyone"); + for (int i = 0; i < vms.size(); i++) { + VM vm = vms.get(i); + Slave slave = new Slave(hosts[i],i + slavesCount); + slave.start(); + vm.bind(slave); + } + + Msg.info("Migrate everyone to the second host."); + for (int i = 0; i < vms.size(); i++) { + vms.get(i).migrate(hosts[1]); + } + + Msg.info("Suspend everyone, move them to the third host, and resume them."); + for (int i = 0; i < vms.size(); i++) { + VM vm = vms.get(i); + vm.suspend(); + vm.migrate(hosts[2]); + } + + workBatch(slavesCount * 2); + + Msg.info("Let's shut down the simulation. 10 first processes will be shut down cleanly while the second half will forcefully get killed"); + + for (int i = 0; i < 10; i++) { + FinalizeTask task = new FinalizeTask(0,0); + task.send("slave_" + i); + } + + for (int i = 0; i < vms.size(); i++) { + vms.get(i).shutdown(); + } + } + + public void workBatch(int slavesCount) throws MsgException { + for (int i = 0; i < slavesCount; i++) { + Task task = new Task("Task_" + i, Cloud.task_comp_size, Cloud.task_comm_size); + task.send("slave_" + i); + } + } +} \ No newline at end of file diff --git a/examples/cloud/Slave.java b/examples/cloud/Slave.java new file mode 100644 index 0000000000..ca5ecaf085 --- /dev/null +++ b/examples/cloud/Slave.java @@ -0,0 +1,41 @@ +/* + * Copyright 2012. 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 cloud; + +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.TaskCancelledException; + +public class Slave extends Process { + private int number; + public Slave(Host host, int number) { + super(host,"Slave " + number,null); + this.number = number; + } + public void main(String[] args) throws MsgException { + while(true) { + Task task = Task.receive("slave_"+number); + + if (task instanceof FinalizeTask) { + break; + } + Msg.info("Received \"" + task.getName() + "\". Processing it."); + try { + task.execute(); + } catch (TaskCancelledException e) { + + } + // Msg.info("\"" + task.getName() + "\" done "); + } + + Msg.info("Received Finalize. I'm done. See you!"); + + } +} \ No newline at end of file diff --git a/org/simgrid/msg/VM.java b/org/simgrid/msg/VM.java index 137fa17249..c0841636bc 100644 --- a/org/simgrid/msg/VM.java +++ b/org/simgrid/msg/VM.java @@ -29,6 +29,7 @@ public class VM { */ public VM(Host host, int coreAmount) { this.coreAmount = coreAmount; + start(host,coreAmount); } /** * Natively implemented method starting the VM. @@ -76,5 +77,7 @@ public class VM { * of VM resume to you. */ public native void resume(); + + public native void shutdown(); } \ No newline at end of file diff --git a/src/jmsg_process.c b/src/jmsg_process.c index dc3d26271b..9115d7eef0 100644 --- a/src/jmsg_process.c +++ b/src/jmsg_process.c @@ -150,7 +150,6 @@ Java_org_simgrid_msg_Process_create(JNIEnv * env, /*argc, argv, properties*/ 0,NULL,NULL); MSG_process_set_kill_time(process, (double)jkill); - MSG_process_set_data(process,&process); /* bind the java process instance to the native process */ jprocess_bind(jprocess, process, env); diff --git a/src/jmsg_vm.c b/src/jmsg_vm.c index 38e4cca50c..1615e420ec 100644 --- a/src/jmsg_vm.c +++ b/src/jmsg_vm.c @@ -35,6 +35,7 @@ Java_org_simgrid_msg_VM_start(JNIEnv *env, jobject jvm, jobject jhost, jint jcor jvm_bind(env,jvm,vm); } + JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_VM_isSuspended(JNIEnv *env, jobject jvm) { msg_vm_t vm = jvm_get_native(env,jvm); @@ -52,6 +53,9 @@ Java_org_simgrid_msg_VM_bind(JNIEnv *env, jobject jvm, jobject jprocess) { msg_vm_t vm = jvm_get_native(env,jvm); m_process_t process = jprocess_to_native_process(jprocess,env); + xbt_assert((vm != NULL), "VM object is not binded"); + xbt_assert((process != NULL), "Process object is not binded."); + MSG_vm_bind(vm,process); } JNIEXPORT void JNICALL