Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a393c95aacc521a2c94cbc5d0a8f3e779912d205
[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         /* Static functions */ 
15
16         private static ArrayList<VM> vms= new ArrayList<>();
17         private Host currentHost; 
18
19         /** Create a `basic' VM (i.e. 1GB of RAM, other values are not taken into account). */
20         public VM(Host host, String name) {
21                 this(host,name,1024, 0, 0);
22         }
23
24         /**
25          * Create a VM
26          * @param host Host node
27          * @param name name of the machine
28          * @param ramSize size of the RAM that should be allocated (in MBytes)
29          * @param migNetSpeed (network bandwith allocated for migrations in MB/s, if you don't know put zero ;))
30          * @param dpIntensity (dirty page percentage according to migNetSpeed, [0-100], if you don't know put zero ;))
31          */
32         public VM(Host host, String name, int ramSize, int migNetSpeed, int dpIntensity){
33                 super();
34                 super.name = name;
35                 this.currentHost = host; 
36                 create(host, name, ramSize, migNetSpeed, dpIntensity);
37                 vms.add(this);
38         }
39
40         public static VM[] all(){
41                 VM[] allvms = new VM[vms.size()];
42                 vms.toArray(allvms);
43                 return allvms;
44         }
45
46         public static VM getVMByName(String name){
47                 for (VM vm : vms){
48                         if (vm.getName().equals(name))
49                                 return vm;
50                 }
51                 return null; 
52         }
53         
54         public void destroy() {
55                 nativeFinalize();
56         }
57         private native void nativeFinalize();
58
59
60         /* JNI / Native code */
61
62         /** Returns whether the given VM is currently suspended */      
63         public native int isCreated();
64
65         /** Returns whether the given VM is currently running */
66         public native int isRunning();
67
68         /** Returns whether the given VM is currently running */
69         public native int isMigrating();
70
71         /** Returns whether the given VM is currently suspended */      
72         public native int isSuspended();
73
74         /** Returns whether the given VM is currently saving */
75         public native int isSaving();
76
77         /** Returns whether the given VM is currently saved */
78         public native int isSaved();
79
80         /** Returns whether the given VM is currently restoring its state */
81         public native boolean isRestoring();
82
83         /**
84          * Natively implemented method create the VM.
85          * @param ramSize size of the RAM that should be allocated (in MB)
86          * @param migNetSpeed (network bandwith allocated for migrations in MB/s, if you don't know put zero ;))
87          * @param dpIntensity (dirty page intensity, a percentage of migNetSpeed [0-100],  if you don't know put zero ;))
88          */
89         private native void create(Host host, String name, int ramSize, int migNetSpeed, int dpIntensity);
90
91
92         /**
93          * Set a CPU bound for a given VM.
94          * @param bound in flops/s
95          */
96         public native void setBound(double bound);
97
98         /**  start the VM */
99         public native void start();
100
101
102         /**
103          * Immediately kills all processes within the given VM. 
104          * 
105          * No extra delay occurs. If you want to simulate this too, you want to use a MSG_process_sleep()
106          */
107         public native void shutdown();
108
109         /** Invoke native migration routine */
110         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);
111
112
113
114         /** Change the host on which all processes are running
115          * (pre-copy is implemented)
116          */     
117         public void migrate(Host destination) throws HostFailureException{
118                 try {
119                         this.internalmig(destination);
120                 } catch (Exception e){
121                   Msg.info("Migration of VM "+this.getName()+" to "+destination.getName()+" is impossible ("+e.getMessage()+")");
122                   throw new HostFailureException();
123                 }
124                 // If the migration correcly returned, then we should change the currentHost value. 
125                 this.currentHost = destination; 
126         }
127
128         /** Immediately suspend the execution of all processes within the given VM
129          *
130          * No suspension cost occurs. If you want to simulate this too, you want to
131          * use a \ref File.write() before or after, depending on the exact semantic
132          * of VM suspend to you.
133          */     
134         public native void suspend();
135
136         /** Immediately resumes the execution of all processes within the given VM
137          *
138          * No resume cost occurs. If you want to simulate this too, you want to
139          * use a \ref File.read() before or after, depending on the exact semantic
140          * of VM resume to you.
141          */
142         public native void resume();
143
144         /** Immediately suspend the execution of all processes within the given VM 
145          *  and save its state on the persistent HDD
146          *  Not yet implemented (for the moment it behaves like suspend)
147          *  No suspension cost occurs. If you want to simulate this too, you want to
148          *  use a \ref File.write() before or after, depending on the exact semantic
149          *  of VM suspend to you.
150          */     
151         public native void save();
152
153         /** Immediately resumes the execution of all processes previously saved 
154          * within the given VM
155          *  Not yet implemented (for the moment it behaves like resume)
156          *
157          * No resume cost occurs. If you want to simulate this too, you want to
158          * use a \ref File.read() before or after, depending on the exact semantic
159          * of VM resume to you.
160          */
161         public native void restore();
162
163         /**  Class initializer (for JNI), don't do it yourself */
164         public static native void nativeInit();
165         static {
166                 nativeInit();
167         }
168 }