Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bfd271544cfbf0e61bedc3438594c2bb1764b08c
[simgrid.git] / src / bindings / java / org / simgrid / msg / VM.java
1 /* JNI interface to virtual machine in Simgrid */
2
3 /* Copyright (c) 2006-2014. 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 import java.util.ArrayList;
10
11 public class VM extends Host {
12         // No need to declare a new bind variable: we use the one inherited from the super class Host
13
14         private static ArrayList<VM> vms= new ArrayList<>();
15         private Host currentHost; 
16
17         /** Create a `basic' VM (i.e. 1GB of RAM, other values are not taken into account). */
18         public VM(Host host, String name) {
19                 this(host,name,1024, 0, 0);
20         }
21
22         /**
23          * Create a VM
24          * @param host Host node
25          * @param name name of the machine
26          * @param ramSize size of the RAM that should be allocated (in MBytes)
27          * @param migNetSpeed (network bandwith allocated for migrations in MB/s, if you don't know put zero ;))
28          * @param dpIntensity (dirty page percentage according to migNetSpeed, [0-100], if you don't know put zero ;))
29          */
30         public VM(Host host, String name, int ramSize, int migNetSpeed, int dpIntensity){
31                 super();
32                 super.name = name;
33                 this.currentHost = host; 
34                 create(host, name, ramSize, migNetSpeed, dpIntensity);
35                 vms.add(this);
36         }
37
38         public native static VM[] all();
39
40         public static VM getVMByName(String name){
41                 for (VM vm : vms){
42                         if (vm.getName().equals(name))
43                                 return vm;
44                 }
45                 return null; 
46         }
47         
48         /** Shutdown and unref the VM. 
49          * 
50          * Actually, this strictly equivalent to shutdown().
51          * In C and in libvirt, the destroy function also releases the memory associated to the VM, 
52          * but this is not the way it goes in Java. The VM will only get destroyed by the garbage 
53          * collector when it is not referenced anymore by your variables. So, to see the VM really 
54          * destroyed, don't call this function but simply release any ref you have on it. 
55          */
56         public void destroy() {
57                 shutdown();
58 ///             vms.remove(this);
59         }
60
61         /* Make sure that the GC also destroys the C object */
62         protected void finalize() throws Throwable {
63                 nativeFinalize();
64         }
65         public native void nativeFinalize();
66
67         /** Returns whether the given VM is currently suspended */      
68         public native int isCreated();
69
70         /** Returns whether the given VM is currently running */
71         public native int isRunning();
72
73         /** Returns whether the given VM is currently running */
74         public native int isMigrating();
75
76         /** Returns whether the given VM is currently suspended */      
77         public native int isSuspended();
78
79         /**
80          * Natively implemented method create the VM.
81          * @param ramSize size of the RAM that should be allocated (in MB)
82          * @param migNetSpeed (network bandwith allocated for migrations in MB/s, if you don't know put zero ;))
83          * @param dpIntensity (dirty page intensity, a percentage of migNetSpeed [0-100],  if you don't know put zero ;))
84          */
85         private native void create(Host host, String name, int ramSize, int migNetSpeed, int dpIntensity);
86
87
88         /**
89          * Set a CPU bound for a given VM.
90          * @param bound in flops/s
91          */
92         public native void setBound(double bound);
93
94         /**  start the VM */
95         public native void start();
96
97
98         /**
99          * Immediately kills all processes within the given VM. 
100          * 
101          * No extra delay occurs. If you want to simulate this too, you want to use a MSG_process_sleep()
102          */
103         public native void shutdown();
104
105         /** Change the host on which all processes are running
106          * (pre-copy is implemented)
107          */     
108         public void migrate(Host destination) throws HostFailureException{
109                 try {
110                         this.nativeMigration(destination);
111                 } catch (Exception e){
112                   Msg.info("Migration of VM "+this.getName()+" to "+destination.getName()+" is impossible ("+e.getMessage()+")");
113                   throw new HostFailureException();
114                 }
115                 // If the migration correcly returned, then we should change the currentHost value. 
116                 this.currentHost = destination; 
117         }
118         private native void nativeMigration(Host destination) throws Exception;
119
120         /** Immediately suspend the execution of all processes within the given VM
121          *
122          * No suspension cost occurs. If you want to simulate this too, you want to
123          * use a \ref File.write() before or after, depending on the exact semantic
124          * of VM suspend to you.
125          */     
126         public native void suspend();
127
128         /** Immediately resumes the execution of all processes within the given VM
129          *
130          * No resume cost occurs. If you want to simulate this too, you want to
131          * use a \ref File.read() before or after, depending on the exact semantic
132          * of VM resume to you.
133          */
134         public native void resume();
135
136         /**  Class initializer (for JNI), don't do it yourself */
137         public static native void nativeInit();
138         static {
139                 nativeInit();
140         }
141 }