Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4eb77b0ee48a1be372d4ceffc94fdb8c10d31622
[simgrid.git] / src / msg / msg_vm.c
1 /* Copyright (c) 2012. The SimGrid Team. All rights reserved.               */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "msg_private.h"
7 #include "xbt/sysdep.h"
8 #include "xbt/log.h"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_vm, msg,
11                                 "Cloud-oriented parts of the MSG API");
12
13 /** @brief Create a new (empty) VMs
14  *  @ingroup msg_VMs
15  *
16  *  @bug it is expected that in the future, the coreAmount parameter will be used
17  *  to add extra constraints on the execution, but the argument is ignored for now.
18  */
19
20 msg_vm_t MSG_vm_start(m_host_t location, int coreAmount) {
21   msg_vm_t res = xbt_new0(s_msg_vm_t,1);
22   res->all_vms_hookup.prev = NULL;
23   res->host_vms_hookup.prev = NULL;
24   res->state = msg_vm_state_running;
25   res->location = location;
26   res->coreAmount = coreAmount;
27   res->processes = xbt_dynar_new(sizeof(m_process_t),NULL);
28
29   xbt_swag_insert(res,msg_global->vms);
30   xbt_swag_insert(res,location->vms);
31
32   return res;
33 }
34 /** @brief Returns a newly constructed dynar containing all existing VMs in the system
35  *  @ingroup msg_VMs
36  *
37  * Don't forget to free the dynar after use.
38  */
39 xbt_dynar_t MSG_vms_as_dynar(void) {
40   xbt_dynar_t res = xbt_dynar_new(sizeof(msg_vm_t),NULL);
41   msg_vm_t vm;
42   xbt_swag_foreach(vm,msg_global->vms) {
43     xbt_dynar_push(res,&vm);
44   }
45   return res;
46 }
47
48 /** @brief Returns whether the given VM is currently suspended
49  *  @ingroup msg_VMs
50  */
51 int MSG_vm_is_suspended(msg_vm_t vm) {
52   return vm->state == msg_vm_state_suspended;
53 }
54 /** @brief Returns whether the given VM is currently running
55  *  @ingroup msg_VMs
56  */
57 int MSG_vm_is_running(msg_vm_t vm) {
58   return vm->state == msg_vm_state_running;
59 }
60 /** @brief Add the given process into the VM.
61  *  @ingroup msg_VMs
62  *
63  * Afterward, when the VM is migrated or suspended or whatever, the process will have the corresponding handling, too.
64  *
65  * @bug for now, if a binded process terminates, every VM functions will segfault. Baaaad.
66  */
67 void MSG_vm_bind(msg_vm_t vm, m_process_t process) {
68         /* check if the process is already in a VM */
69         simdata_process_t simdata = simcall_process_get_data(process);
70         if (simdata->vm) {
71                 msg_vm_t old_vm = simdata->vm;
72                 int pos = xbt_dynar_search(old_vm->processes,&process);
73                 xbt_dynar_remove_at(old_vm->processes,pos, NULL);
74         }
75         /* check if the host is in the right host */
76         if (simdata->m_host != vm->location) {
77                 MSG_process_migrate(process,vm->location);
78         }
79         simdata->vm = vm;
80
81         XBT_DEBUG("binding Process %s to %p",MSG_process_get_name(process),vm);
82
83   xbt_dynar_push_as(vm->processes,m_process_t,process);
84 }
85 /** @brief Removes the given process from the given VM, and kill it
86  *  @ingroup msg_VMs
87  *
88  *  Will raise a not_found exception if the process were not binded to that VM
89  */
90 void MSG_vm_unbind(msg_vm_t vm, m_process_t process) {
91   int pos = xbt_dynar_search(vm->processes,process);
92   xbt_dynar_remove_at(vm->processes,pos, NULL);
93   MSG_process_kill(process);
94 }
95
96 /** @brief Immediately change the host on which all processes are running
97  *  @ingroup msg_VMs
98  *
99  * No migration cost occurs. If you want to simulate this too, you want to use a
100  * MSG_task_send() before or after, depending on whether you want to do cold or hot
101  * migration.
102  */
103 void MSG_vm_migrate(msg_vm_t vm, m_host_t destination) {
104   unsigned int cpt;
105   m_process_t process;
106   xbt_dynar_foreach(vm->processes,cpt,process) {
107     MSG_process_migrate(process,destination);
108   }
109   xbt_swag_remove(vm,vm->location->vms);
110   xbt_swag_insert_at_tail(vm,destination->vms);
111   vm->location = destination;
112 }
113
114 /** @brief Immediately suspend the execution of all processes within the given VM
115  *  @ingroup msg_VMs
116  *
117  * No suspension cost occurs. If you want to simulate this too, you want to
118  * use a \ref MSG_file_write() before or after, depending on the exact semantic
119  * of VM suspend to you.
120  */
121 void MSG_vm_suspend(msg_vm_t vm) {
122   unsigned int cpt;
123   m_process_t process;
124   xbt_dynar_foreach(vm->processes,cpt,process) {
125     XBT_DEBUG("suspend process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
126     MSG_process_suspend(process);
127   }
128 }
129
130 /** @brief Immediately resumes the execution of all processes within the given VM
131  *  @ingroup msg_VMs
132  *
133  * No resume cost occurs. If you want to simulate this too, you want to
134  * use a \ref MSG_file_read() before or after, depending on the exact semantic
135  * of VM resume to you.
136  */
137 void MSG_vm_resume(msg_vm_t vm) {
138   unsigned int cpt;
139   m_process_t process;
140   xbt_dynar_foreach(vm->processes,cpt,process) {
141         XBT_DEBUG("resume process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
142     MSG_process_resume(process);
143   }
144 }
145
146 /** @brief Immediately kills all processes within the given VM. Any memory that they allocated will be leaked.
147  *  @ingroup msg_VMs
148  *
149  * No extra delay occurs. If you want to simulate this too, you want to
150  * use a #MSG_process_sleep() or something. I'm not quite sure.
151  */
152 void MSG_vm_shutdown(msg_vm_t vm) {
153   unsigned int cpt;
154   m_process_t process;
155   XBT_DEBUG("%d processes in the VM",xbt_dynar_length(vm->processes));
156   while (xbt_dynar_length(vm->processes) > 0) {
157         process = xbt_dynar_get_as(vm->processes,0,m_process_t);
158         MSG_process_kill(process);
159   }
160 }
161
162 /** @brief Destroy a msg_vm_t.
163  *  @ingroup msg_VMs
164  */
165 void MSG_vm_destroy(msg_vm_t vm) {
166         unsigned int cpt;
167         m_process_t process;
168         xbt_dynar_foreach(vm->processes,cpt,process) {
169                 //FIXME: Slow ?
170                 simdata_process_t simdata = simcall_process_get_data(process);
171                 simdata->vm = NULL;
172         }
173         xbt_dynar_free(&vm->processes);
174         xbt_free(vm);
175 }