1 /* JNI interface to virtual machine in Simgrid */
3 /* Copyright (c) 2006-2014. The SimGrid Team. All rights reserved. */
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. */
8 package org.simgrid.msg;
9 import java.util.ArrayList;
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
14 /* Static functions */
16 private static ArrayList<VM> vms= new ArrayList<>();
17 private Host currentHost;
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);
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 ;))
32 public VM(Host host, String name, int ramSize, int migNetSpeed, int dpIntensity){
35 this.currentHost = host;
36 create(host, name, ramSize, migNetSpeed, dpIntensity);
40 public static VM[] all(){
41 VM[] allvms = new VM[vms.size()];
46 public static VM getVMByName(String name){
48 if (vm.getName().equals(name))
54 public void destroy() {
57 private native void nativeFinalize();
60 /* JNI / Native code */
62 /** Returns whether the given VM is currently suspended */
63 public native int isCreated();
65 /** Returns whether the given VM is currently running */
66 public native int isRunning();
68 /** Returns whether the given VM is currently running */
69 public native int isMigrating();
71 /** Returns whether the given VM is currently suspended */
72 public native int isSuspended();
75 * Natively implemented method create the VM.
76 * @param ramSize size of the RAM that should be allocated (in MB)
77 * @param migNetSpeed (network bandwith allocated for migrations in MB/s, if you don't know put zero ;))
78 * @param dpIntensity (dirty page intensity, a percentage of migNetSpeed [0-100], if you don't know put zero ;))
80 private native void create(Host host, String name, int ramSize, int migNetSpeed, int dpIntensity);
84 * Set a CPU bound for a given VM.
85 * @param bound in flops/s
87 public native void setBound(double bound);
90 public native void start();
94 * Immediately kills all processes within the given VM.
96 * No extra delay occurs. If you want to simulate this too, you want to use a MSG_process_sleep()
98 public native void shutdown();
100 /** Invoke native migration routine */
101 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);
105 /** Change the host on which all processes are running
106 * (pre-copy is implemented)
108 public void migrate(Host destination) throws HostFailureException{
110 this.internalmig(destination);
111 } catch (Exception e){
112 Msg.info("Migration of VM "+this.getName()+" to "+destination.getName()+" is impossible ("+e.getMessage()+")");
113 throw new HostFailureException();
115 // If the migration correcly returned, then we should change the currentHost value.
116 this.currentHost = destination;
119 /** Immediately suspend the execution of all processes within the given VM
121 * No suspension cost occurs. If you want to simulate this too, you want to
122 * use a \ref File.write() before or after, depending on the exact semantic
123 * of VM suspend to you.
125 public native void suspend();
127 /** Immediately resumes the execution of all processes within the given VM
129 * No resume cost occurs. If you want to simulate this too, you want to
130 * use a \ref File.read() before or after, depending on the exact semantic
131 * of VM resume to you.
133 public native void resume();
135 /** Class initializer (for JNI), don't do it yourself */
136 public static native void nativeInit();