1 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/actor.h"
7 #include "simgrid/engine.h"
8 #include "simgrid/exec.h"
9 #include "simgrid/host.h"
10 #include "simgrid/mailbox.h"
11 #include "simgrid/plugins/live_migration.h"
12 #include "simgrid/vm.h"
16 #include "xbt/sysdep.h"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(cloud_masterworker, "Messages specific for this example");
21 #define FINALIZE 221297 /* a magic number to tell people to stop working */
23 const double comp_size = 10000000;
24 const long comm_size = 10000000;
26 static void send_tasks(int nb_workers)
28 for (int i = 0; i < nb_workers; i++) {
29 char mbox_name[MAXMBOXLEN];
30 snprintf(mbox_name, MAXMBOXLEN, "MBOX:WRK%02d", i);
31 double* payload = xbt_malloc(sizeof(double));
33 sg_mailbox_t mbox = sg_mailbox_by_name(mbox_name);
35 XBT_INFO("Send to mailbox(%s)", mbox_name);
36 sg_mailbox_put(mbox, payload, comm_size);
40 static void worker_fun(int argc, char* argv[])
42 const char* pr_name = sg_actor_self_get_name();
43 char mbox_name[MAXMBOXLEN];
44 snprintf(mbox_name, MAXMBOXLEN, "MBOX:%s", pr_name);
45 sg_mailbox_t mbox = sg_mailbox_by_name(mbox_name);
46 double* payload = NULL;
48 XBT_INFO("%s is listening on mailbox(%s)", pr_name, mbox_name);
51 payload = (double*)sg_mailbox_get(mbox);
53 XBT_INFO("%s received from mailbox(%s)", pr_name, mbox_name);
55 if (*payload == FINALIZE) {
60 sg_actor_execute(*payload);
61 XBT_INFO("%s executed", pr_name);
66 static void master_fun(int argc, char* argv[])
68 sg_host_t* worker_pms = sg_actor_self_get_data();
70 sg_vm_t* vms = xbt_malloc(2 * sizeof(sg_vm_t));
72 /* Launch VMs and worker actors. One VM per PM, and one worker actor per VM. */
73 XBT_INFO("# Launch 2 VMs");
74 for (int i = 0; i < 2; i++) {
75 char* vm_name = bprintf("VM%02d", i);
76 char* pr_name = bprintf("WRK%02d", i);
78 sg_host_t pm = worker_pms[i];
80 XBT_INFO("create %s on PM(%s)", vm_name, sg_host_get_name(pm));
81 sg_vm_t vm = sg_vm_create_core(pm, vm_name);
83 sg_vm_set_ramsize(vm, 1L * 1024 * 1024 * 1024); // 1GiB
88 XBT_INFO("put an actor (%s) on %s", pr_name, vm_name);
89 sg_actor_create(pr_name, (sg_host_t)vm, worker_fun, 0, NULL);
95 /* Send a bunch of work to every one */
96 XBT_INFO("# Send to 2 worker actors");
99 XBT_INFO("# Suspend all VMs");
100 for (int i = 0; i < 2; i++) {
101 XBT_INFO("suspend %s", sg_vm_get_name(vms[i]));
102 sg_vm_suspend(vms[i]);
105 XBT_INFO("# Wait a while");
106 sg_actor_sleep_for(2);
108 XBT_INFO("# Resume all VMs");
109 for (int i = 0; i < 2; i++) {
110 sg_vm_resume(vms[i]);
113 XBT_INFO("# Sleep long enough for everyone to be done with previous batch of work");
114 sg_actor_sleep_for(10 - simgrid_get_clock());
116 XBT_INFO("# Add one more actor on each VM");
117 for (int i = 0; i < 2; i++) {
118 char* vm_name = bprintf("VM%02d", i);
119 char* pr_name = bprintf("WRK%02d", i + 2);
121 XBT_INFO("put an actor (%s) on %s", pr_name, vm_name);
122 sg_actor_create(pr_name, (sg_host_t)vms[i], worker_fun, 0, NULL);
128 XBT_INFO("# Send to 4 worker actors");
131 sg_host_t worker_pm0 = worker_pms[0];
132 sg_host_t worker_pm1 = worker_pms[1];
134 XBT_INFO("# Migrate all VMs to PM(%s)", sg_host_get_name(worker_pm0));
135 for (int i = 0; i < 2; i++) {
136 sg_vm_migrate(vms[i], worker_pm0);
139 XBT_INFO("# Migrate all VMs to PM(%s)", sg_host_get_name(worker_pm1));
140 for (int i = 0; i < 2; i++) {
141 sg_vm_migrate(vms[i], worker_pm1);
144 XBT_INFO("# Shutdown the half of worker actors gracefully. The remaining half will be forcibly killed.");
145 for (int i = 0; i < 2; i++) {
146 char mbox_name[MAXMBOXLEN];
147 snprintf(mbox_name, MAXMBOXLEN, "MBOX:WRK%02d", i);
148 sg_mailbox_t mbox = sg_mailbox_by_name(mbox_name);
149 double* payload = xbt_malloc(sizeof(double));
151 sg_mailbox_put(mbox, payload, 0);
154 XBT_INFO("# Wait a while before effective shutdown.");
155 sg_actor_sleep_for(2);
157 XBT_INFO("# Shutdown and destroy all the VMs. The remaining worker actors will be forcibly killed.");
158 for (int i = 0; i < 2; i++) {
159 XBT_INFO("shutdown %s", sg_vm_get_name(vms[i]));
160 sg_vm_shutdown(vms[i]);
161 XBT_INFO("destroy %s", sg_vm_get_name(vms[i]));
162 sg_vm_destroy(vms[i]);
165 XBT_INFO("# Goodbye now!");
169 int main(int argc, char* argv[])
171 simgrid_init(&argc, argv);
172 sg_vm_live_migration_plugin_init();
174 xbt_assert(argc > 1, "Usage: %s example/platforms/cluster_backbone.xml\n", argv[0]);
176 /* Load the platform file */
177 simgrid_load_platform(argv[1]);
179 /* Retrieve hosts from the platform file */
180 sg_host_t* pms = sg_host_list();
182 /* we need a master node and worker nodes */
183 xbt_assert(sg_host_count() > 2, "need at least 3 hosts");
185 /* the first pm is the master, the others are workers */
186 sg_host_t master_pm = pms[0];
188 sg_host_t* worker_pms = xbt_malloc(2 * sizeof(sg_host_t));
189 for (int i = 0; i < 2; i++)
190 worker_pms[i] = pms[i + 1];
194 sg_actor_t actor = sg_actor_init("master", master_pm);
195 sg_actor_set_data(actor, worker_pms);
196 sg_actor_start(actor, master_fun, 0, NULL);
199 XBT_INFO("Bye (simulation time %g)", simgrid_get_clock());