Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add c and tesh files for the new test
[simgrid.git] / examples / msg / cloud / two_tasks_vm.c
1 /*
2  * vm_bug.c
3  *
4  *  Created on: Jan 30, 2014
5  *      Author: bedaride
6  */
7
8 #include <stdio.h>
9 #include "msg/msg.h"
10 #include "xbt/sysdep.h"
11
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 msg_task_t atask = NULL;
18
19 static int computation_fun(int argc, char *argv[])
20 {
21   const char *pr_name = MSG_process_get_name(MSG_process_self());
22   const char *host_name = MSG_host_get_name(MSG_host_self());
23   double clock_sta, clock_end;
24   atask = MSG_task_create("Task1", 1e9, 1e9, NULL);
25   clock_sta = MSG_get_clock();
26   XBT_INFO("%s:%s task 1 created %g", host_name, pr_name, clock_sta);
27   MSG_task_execute(atask);
28   clock_end = MSG_get_clock();
29
30   XBT_INFO("%s:%s task 1 executed %g", host_name, pr_name, clock_end - clock_sta);
31
32   MSG_task_destroy(atask);
33   atask = NULL;
34
35   MSG_process_sleep(1);
36
37   atask = MSG_task_create("Task2", 1e10, 1e10, NULL);
38
39   clock_sta = MSG_get_clock();
40   XBT_INFO("%s:%s task 2 created %g", host_name, pr_name, clock_sta);
41   MSG_task_execute(atask);
42   clock_end = MSG_get_clock();
43
44   XBT_INFO("%s:%s task 2 executed %g", host_name, pr_name, clock_end - clock_sta);
45
46   MSG_task_destroy(atask);
47   atask = NULL;
48
49   return 0;
50 }
51
52 static void launch_computation_worker(msg_host_t host)
53 {
54   const char *pr_name = "compute";
55   char **argv = xbt_new(char *, 2);
56   argv[0] = xbt_strdup(pr_name);
57   argv[1] = NULL;
58
59   MSG_process_create_with_arguments(pr_name, computation_fun, NULL, host, 1, argv);
60 }
61
62 static int master_main(int argc, char *argv[])
63 {
64   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
65   msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
66   msg_vm_t vm0;
67   vm0 = MSG_vm_create_core(pm0, "VM0");
68
69   //MSG_process_sleep(1);
70
71   launch_computation_worker(pm0);
72
73   while(MSG_get_clock()<100) {
74         if (atask != NULL)
75           XBT_INFO("aTask remaining duration: %g", MSG_task_get_remaining_computation(atask));
76         MSG_process_sleep(1);
77   }
78
79   MSG_process_sleep(10000);
80   return 1;
81 }
82
83 static void launch_master(msg_host_t host)
84 {
85   const char *pr_name = "master_";
86   char **argv = xbt_new(char *, 2);
87   argv[0] = xbt_strdup(pr_name);
88   argv[1] = NULL;
89
90   MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
91 }
92
93 int main(int argc, char *argv[]){
94   MSG_init(&argc, argv);
95
96   xbt_assert(argc == 2);
97   MSG_create_environment(argv[1]);
98
99   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
100   msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
101   launch_master(pm0);
102
103   int res = MSG_main();
104   XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
105
106   return !(res == MSG_OK);
107 }
108
109
110
111