Logo AND Algorithmique Numérique Distribuée

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