Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e59f88fb176226ae8e839a8e8deb1da7607235e9
[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.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 package org.simgrid.msg;
10 import java.util.ArrayList;
11
12 public class VM extends Host{
13         // Please note that we are not declaring a new bind variable 
14         //(the bind variable has been inherited from the super class Host)
15
16         /* Static functions */ 
17         // GetByName is inherited from the super class Host
18
19
20         private static ArrayList<VM> vms= new ArrayList<>();
21         private Host currentHost; 
22
23         /* Constructors / destructors */
24         /**
25          * Create a `basic' VM (i.e. 1 core, 1GB of RAM, other values are not taken into account).
26          */
27         public VM(Host host, String name) {
28                 this(host,name,1,1024, -1, null, -1,0 , 0);
29         }
30
31         /**
32          * Create a  VM
33          * @param host  Host node
34          * @param name name of the machine
35          * @param nCore number of core
36          * @param ramSize size of the RAM that should be allocated (in MBytes)
37          * @param netCap (not used for the moment)
38          * @param diskPath (not used for the moment)
39          * @param diskSize (not used for the moment)
40          * @param migNetSpeed (network bandwith allocated for migrations in MB/s, if you don't know put zero ;))
41          * @param dpIntensity (dirty page percentage according to migNetSpeed, [0-100], if you don't know put zero ;))
42          */
43         public VM(Host host, String name, int nCore,  int ramSize, 
44                         int netCap, String diskPath, int diskSize, int migNetSpeed, int dpIntensity){
45                 super();
46                 super.name = name; 
47                 this.currentHost = host; 
48                 create(host, name, nCore, ramSize, netCap, diskPath, diskSize, migNetSpeed, dpIntensity);
49                 vms.add(this);
50         }
51
52         public static VM[] all(){
53                 VM[] allvms = new VM[vms.size()];
54                 vms.toArray(allvms);
55                 return allvms;
56         }
57
58         public static VM getVMByName(String name){
59                 for (VM vm : vms){
60                         if (vm.getName().equals(name))
61                                 return vm;
62                 }
63                 return null; 
64         }
65         
66         public void destroy() {
67                 nativeFinalize();
68         }
69         private native void nativeFinalize();
70
71
72         /* JNI / Native code */
73
74         /* get/set property methods are inherited from the Host class. */
75
76         /** Returns whether the given VM is currently suspended
77          */     
78         public native int isCreated();
79
80         /** Returns whether the given VM is currently running
81          */
82         public native int isRunning();
83
84         /** Returns whether the given VM is currently running
85          */
86         public native int isMigrating();
87
88         /** Returns whether the given VM is currently suspended
89          */     
90         public native int isSuspended();
91
92         /** Returns whether the given VM is currently saving
93          */
94         public native int isSaving();
95
96         /** Returns whether the given VM is currently saved
97          */
98         public native int isSaved();
99
100         /** Returns whether the given VM is currently restoring its state
101          */
102         public native boolean isRestoring();
103
104         /**
105          * Natively implemented method create the VM.
106          * @param nCore number of core
107          * @param ramSize size of the RAM that should be allocated (in MB)
108          * @param netCap (not used for the moment)
109          * @param diskPath (not used for the moment)
110          * @param diskSize (not used for the moment)
111          * @param migNetSpeed (network bandwith allocated for migrations in MB/s, if you don't know put zero ;))
112          * @param dpIntensity (dirty page intensity, a percentage of migNetSpeed [0-100],  if you don't know put zero ;))
113          */
114         private native void create(Host host, String name, int nCore, int ramSize, 
115                         int netCap, String diskPath, int diskSize, int migNetSpeed, int dpIntensity);
116
117
118         /**
119          * Set a CPU bound for a given VM.
120          * @param bound in flops/s
121          */
122         public native void setBound(double bound);
123
124         /**
125          * start the VM
126          */
127         public native void start();
128
129
130         /**
131          * Immediately kills all processes within the given VM. Any memory that they allocated will be leaked.
132          * No extra delay occurs. If you want to simulate this too, you want to use a MSG_process_sleep() or something
133          */
134         public native void shutdown();
135
136         /**  
137          * Invoke native migration routine
138          */
139         public native void internalmig(Host destination) throws Exception; // TODO add throws DoubleMigrationException (i.e. when you call migrate on a VM that is already migrating);
140
141
142
143         /** Change the host on which all processes are running
144          * (pre-copy is implemented)
145          */     
146         public void migrate(Host destination) throws HostFailureException{
147                 try {
148                         this.internalmig(destination);
149                 } catch (Exception e){
150                   Msg.info("Migration of VM "+this.getName()+" to "+destination.getName()+" is impossible ("+e.getMessage()+")");
151                   throw new HostFailureException();
152                 }
153                 // If the migration correcly returned, then we should change the currentHost value. 
154                 this.currentHost = destination; 
155         }
156
157         /** Immediately suspend the execution of all processes within the given VM
158          *
159          * No suspension cost occurs. If you want to simulate this too, you want to
160          * use a \ref File.write() before or after, depending on the exact semantic
161          * of VM suspend to you.
162          */     
163         public native void suspend();
164
165         /** Immediately resumes the execution of all processes within the given VM
166          *
167          * No resume cost occurs. If you want to simulate this too, you want to
168          * use a \ref File.read() before or after, depending on the exact semantic
169          * of VM resume to you.
170          */
171         public native void resume();
172
173         /** Immediately suspend the execution of all processes within the given VM 
174          *  and save its state on the persistent HDD
175          *  Not yet implemented (for the moment it behaves like suspend)
176          *  No suspension cost occurs. If you want to simulate this too, you want to
177          *  use a \ref File.write() before or after, depending on the exact semantic
178          *  of VM suspend to you.
179          */     
180         public native void save();
181
182         /** Immediately resumes the execution of all processes previously saved 
183          * within the given VM
184          *  Not yet implemented (for the moment it behaves like resume)
185          *
186          * No resume cost occurs. If you want to simulate this too, you want to
187          * use a \ref File.read() before or after, depending on the exact semantic
188          * of VM resume to you.
189          */
190         public native void restore();
191
192
193         /**
194          * Class initializer, to initialize various JNI stuff
195          */
196         public static native void nativeInit();
197         static {
198                 nativeInit();
199         }
200 }