Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
improve coverage in teshsuite
[simgrid.git] / teshsuite / msg / host_on_off / host_on_off.c
1 /* Copyright (c) 2010-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include "simgrid/msg.h"            /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
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 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19
20 /** Emitter function  */
21 int master(int argc, char *argv[])
22 {
23   double task_comp_size = 5E7;
24   double task_comm_size = 1E6;
25
26   char mailbox[256];
27   msg_task_t task = NULL;
28   msg_host_t jupiter = MSG_host_by_name("Jupiter");
29   sprintf(mailbox, "jupi");
30
31   task = MSG_task_create("task on", task_comp_size, task_comm_size, NULL);
32   XBT_INFO("Sending \"%s\"", task->name);
33   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
34     MSG_task_destroy(task);
35
36   MSG_process_sleep(1);
37   MSG_host_off(jupiter);
38
39   task = MSG_task_create("task off", task_comp_size, task_comm_size, NULL);
40   XBT_INFO("Sending \"%s\"", task->name);
41   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
42     MSG_task_destroy(task);
43
44   MSG_host_on(jupiter);
45   xbt_swag_t jupi_processes = MSG_host_get_process_list(jupiter);
46   void *process;
47   xbt_swag_foreach(process, jupi_processes) {
48     MSG_process_kill(process);
49   }
50
51   task = MSG_task_create("task on without proc", task_comp_size, task_comm_size, NULL);
52   XBT_INFO("Sending \"%s\"", task->name);
53   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
54     MSG_task_destroy(task);
55
56   char **argvF = xbt_new(char*, 2);
57   argvF[0] = xbt_strdup("slave");
58   MSG_process_create_with_arguments("slave", slave, NULL, MSG_host_by_name("Jupiter"), 1, argvF);
59
60   task = MSG_task_create("task on with proc", task_comp_size, task_comm_size, NULL);
61   XBT_INFO("Sending \"%s\"", task->name);
62   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
63     MSG_task_destroy(task);
64
65   task = MSG_task_create("finalize", 0, 0, 0);
66   XBT_INFO("Sending \"%s\"", task->name);
67   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
68     MSG_task_destroy(task);
69
70   XBT_INFO("Goodbye now!");
71   return 0;
72 }                               /* end_of_master */
73
74 /** Receiver function  */
75 int slave(int argc, char *argv[])
76 {
77   msg_task_t task = NULL;
78   XBT_ATTRIB_UNUSED int res;
79   int id = -1;
80   char mailbox[80];
81
82   sprintf(mailbox, "jupi");
83
84   while (1) {
85     res = MSG_task_receive(&(task), mailbox);
86     xbt_assert(res == MSG_OK, "MSG_task_get failed");
87
88     if (!strcmp(MSG_task_get_name(task), "finalize")) {
89       MSG_task_destroy(task);
90       break;
91     }
92     MSG_task_execute(task);
93     XBT_INFO("Task \"%s\" done", MSG_task_get_name(task));
94
95     MSG_task_destroy(task);
96     task = NULL;
97     id--;
98   }
99   XBT_INFO("I'm done. See you!");
100   return 0;
101 }                               /* end_of_slave */
102
103 /** Main function */
104 int main(int argc, char *argv[])
105 {
106   msg_error_t res;
107   const char *platform_file;
108   const char *application_file;
109
110   MSG_init(&argc, argv);
111   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
112              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
113   
114   platform_file = argv[1];
115   application_file = argv[2];
116
117   {                             /*  Simulation setting */
118     MSG_create_environment(platform_file);
119   }
120   {                             /*   Application deployment */
121     MSG_function_register("master", master);
122     MSG_function_register("slave", slave);
123
124     MSG_launch_application(application_file);
125   }
126   res = MSG_main();
127
128   XBT_INFO("Simulation time %g", MSG_get_clock());
129
130   return res != MSG_OK;
131 }                               /* end_of_main */