Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement natively VM_getVMByName. Java VMs are now freed
[simgrid.git] / src / bindings / java / org / simgrid / msg / VM.java
1 /* Java bindings of the s4u::VirtualMachine */
2
3 /* Copyright (c) 2006-2017. The SimGrid Team. All rights reserved.          */
4
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. */
7
8 package org.simgrid.msg;
9
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
12
13         private Host currentHost; 
14
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);
18         }
19
20         /**
21          * Create a VM
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 ;))
27          */
28         public VM(Host host, String name, int ramSize, int migNetSpeed, int dpIntensity){
29                 super();
30                 super.name = name;
31                 this.currentHost = host; 
32                 create(host, name, ramSize, migNetSpeed, dpIntensity);
33         }
34
35         /** Retrieve the list of all existing VMs */
36         public native static VM[] all();
37
38         /** Retrieve a VM from its name */
39         public native static VM getVMByName(String name);
40         
41         /** Shutdown and unref the VM. 
42          * 
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. 
48          */
49         public void destroy() {
50                 shutdown();
51         }
52
53         /* Make sure that the GC also destroys the C object */
54         protected void finalize() throws Throwable {
55                 nativeFinalize();
56         }
57         private native void nativeFinalize();
58
59         /** Returns whether the given VM is currently suspended */      
60         public native int isCreated();
61
62         /** Returns whether the given VM is currently running */
63         public native int isRunning();
64
65         /** Returns whether the given VM is currently running */
66         public native int isMigrating();
67
68         /** Returns whether the given VM is currently suspended */      
69         public native int isSuspended();
70
71         /**
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 ;))
76          */
77         private native void create(Host host, String name, int ramSize, int migNetSpeed, int dpIntensity);
78
79
80         /**
81          * Set a CPU bound for a given VM.
82          * @param bound in flops/s
83          */
84         public native void setBound(double bound);
85
86         /**  start the VM */
87         public native void start();
88
89
90         /**
91          * Immediately kills all processes within the given VM. 
92          * 
93          * No extra delay occurs. If you want to simulate this too, you want to use a MSG_process_sleep()
94          */
95         public native void shutdown();
96
97         /** Change the host on which all processes are running
98          * (pre-copy is implemented)
99          */     
100         public void migrate(Host destination) throws HostFailureException{
101                 try {
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();
106                 }
107                 // If the migration correcly returned, then we should change the currentHost value. 
108                 this.currentHost = destination; 
109         }
110         private native void nativeMigration(Host destination) throws Exception;
111
112         /** Immediately suspend the execution of all processes within the given VM
113          *
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.
117          */     
118         public native void suspend();
119
120         /** Immediately resumes the execution of all processes within the given VM
121          *
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.
125          */
126         public native void resume();
127
128         /**  Class initializer (for JNI), don't do it yourself */
129         private static native void nativeInit();
130         static {
131                 nativeInit();
132         }
133 }