1 /* Java bindings of the s4u::VirtualMachine */
3 /* Copyright (c) 2006-2017. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 package org.simgrid.msg;
10 public class VM extends Host {
11 // No need to declare a new bind variable: we use the one inherited from the super class Host
13 private Host currentHost;
15 /** Create a `basic' VM (i.e. 1GB of RAM, other values are not taken into account). */
16 public VM(Host host, String name) {
17 this(host,name,1024, 0, 0);
22 * @param host Host node
23 * @param name name of the machine
24 * @param ramSize size of the RAM that should be allocated (in MBytes)
25 * @param migNetSpeed (network bandwith allocated for migrations in MB/s, if you don't know put zero ;))
26 * @param dpIntensity (dirty page percentage according to migNetSpeed, [0-100], if you don't know put zero ;))
28 public VM(Host host, String name, int ramSize, int migNetSpeed, int dpIntensity){
31 this.currentHost = host;
32 create(host, name, ramSize, migNetSpeed, dpIntensity);
35 /** Retrieve the list of all existing VMs */
36 public native static VM[] all();
38 /** Retrieve a VM from its name */
39 public native static VM getVMByName(String name);
41 /** Shutdown and unref the VM.
43 * Actually, this strictly equivalent to shutdown().
44 * In C and in libvirt, the destroy function also releases the memory associated to the VM,
45 * but this is not the way it goes in Java. The VM will only get destroyed by the garbage
46 * collector when it is not referenced anymore by your variables. So, to see the VM really
47 * destroyed, don't call this function but simply release any ref you have on it.
49 public void destroy() {
53 /* Make sure that the GC also destroys the C object */
54 protected void finalize() throws Throwable {
57 private native void nativeFinalize();
59 /** Returns whether the given VM is currently suspended */
60 public native int isCreated();
62 /** Returns whether the given VM is currently running */
63 public native int isRunning();
65 /** Returns whether the given VM is currently running */
66 public native int isMigrating();
68 /** Returns whether the given VM is currently suspended */
69 public native int isSuspended();
72 * Natively implemented method create the VM.
73 * @param ramSize size of the RAM that should be allocated (in MB)
74 * @param migNetSpeed (network bandwith allocated for migrations in MB/s, if you don't know put zero ;))
75 * @param dpIntensity (dirty page intensity, a percentage of migNetSpeed [0-100], if you don't know put zero ;))
77 private native void create(Host host, String name, int ramSize, int migNetSpeed, int dpIntensity);
81 * Set a CPU bound for a given VM.
82 * @param bound in flops/s
84 public native void setBound(double bound);
87 public native void start();
91 * Immediately kills all processes within the given VM.
93 * No extra delay occurs. If you want to simulate this too, you want to use a MSG_process_sleep()
95 public native void shutdown();
97 /** Change the host on which all processes are running
98 * (pre-copy is implemented)
100 public void migrate(Host destination) throws HostFailureException{
102 this.nativeMigration(destination);
103 } catch (Exception e){
104 Msg.info("Migration of VM "+this.getName()+" to "+destination.getName()+" is impossible ("+e.getMessage()+")");
105 throw new HostFailureException();
107 // If the migration correcly returned, then we should change the currentHost value.
108 this.currentHost = destination;
110 private native void nativeMigration(Host destination) throws Exception;
112 /** Immediately suspend the execution of all processes within the given VM
114 * No suspension cost occurs. If you want to simulate this too, you want to
115 * use a \ref File.write() before or after, depending on the exact semantic
116 * of VM suspend to you.
118 public native void suspend();
120 /** Immediately resumes the execution of all processes within the given VM
122 * No resume cost occurs. If you want to simulate this too, you want to
123 * use a \ref File.read() before or after, depending on the exact semantic
124 * of VM resume to you.
126 public native void resume();
128 /** Class initializer (for JNI), don't do it yourself */
129 private static native void nativeInit();