Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
various sonar cleanups
[simgrid.git] / examples / msg / platform-failures / platform-failures.c
1 /* Copyright (c) 2007-2018. 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 "simgrid/msg.h"
7
8 #include <stdio.h> /* snprintf */
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11
12 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
13
14 static int master(int argc, char *argv[])
15 {
16   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
17   double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
18   double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
19   long workers_count = xbt_str_parse_int(argv[4], "Invalid amount of workers: %s");
20
21   XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
22
23   for (int i = 0; i < number_of_tasks; i++) {
24     char mailbox[256];
25     snprintf(mailbox, 255, "worker-%ld", i % workers_count);
26     XBT_INFO("Send a message to %s", mailbox);
27     msg_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size, xbt_new0(double, 1));
28     *((double *) task->data) = MSG_get_clock();
29
30     switch ( MSG_task_send_with_timeout(task,mailbox,10.0) ) {
31     case MSG_OK:
32       XBT_INFO("Send to %s completed", mailbox);
33       break;
34
35     case MSG_HOST_FAILURE:
36       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
37       free(task->data);
38       MSG_task_destroy(task);
39       return 0;
40
41     case MSG_TRANSFER_FAILURE:
42       XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", mailbox);
43       free(task->data);
44       MSG_task_destroy(task);
45       break;
46
47     case MSG_TIMEOUT:
48       XBT_INFO ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox);
49       free(task->data);
50       MSG_task_destroy(task);
51       break;
52
53     default:
54       xbt_die( "Unexpected behavior");
55     }
56   }
57
58   XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
59   for (int i = 0; i < workers_count; i++) {
60     char mailbox[256];
61     snprintf(mailbox, 255, "worker-%ld", i % workers_count);
62     msg_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
63
64     switch (MSG_task_send_with_timeout(task,mailbox,1.0)) {
65     case MSG_HOST_FAILURE:
66       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
67       MSG_task_destroy(task);
68       break;
69
70     case MSG_TRANSFER_FAILURE:
71       XBT_INFO("Mmh. Can't reach '%s'! Nevermind. Let's keep going!", mailbox);
72       MSG_task_destroy(task);
73       break;
74
75     case MSG_TIMEOUT:
76       XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox);
77       MSG_task_destroy(task);
78       break;
79
80     case MSG_OK:
81       /* nothing */
82       break;
83
84     default:
85       xbt_die("Unexpected behavior with '%s'", mailbox);
86     }
87   }
88
89   XBT_INFO("Goodbye now!");
90   return 0;
91 }
92
93 static int worker(int argc, char *argv[])
94 {
95   char mailbox[80];
96
97   long id= xbt_str_parse_int(argv[1], "Invalid argument %s");
98
99   snprintf(mailbox, 79,"worker-%ld", id);
100
101   while (1) {
102     msg_task_t task = NULL;
103     XBT_INFO("Waiting a message on %s", mailbox);
104     int retcode = MSG_task_receive( &(task), mailbox);
105     if (retcode == MSG_OK) {
106       if (MSG_task_get_data(task) == FINALIZE) {
107         MSG_task_destroy(task);
108         break;
109       }
110       XBT_INFO("Start execution...");
111       retcode = MSG_task_execute(task);
112       if (retcode == MSG_OK) {
113         XBT_INFO("Execution complete.");
114         free(task->data);
115         MSG_task_destroy(task);
116       } else if (retcode == MSG_HOST_FAILURE) {
117         XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
118         free(task->data);
119         MSG_task_destroy(task);
120         return 0;
121       } else {
122         XBT_INFO("Hey ?! What's up ? ");
123         xbt_die("Unexpected behavior");
124       }
125     } else if (retcode == MSG_HOST_FAILURE) {
126       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
127       return 0;
128     } else if (retcode == MSG_TRANSFER_FAILURE) {
129       XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
130     } else {
131       xbt_die("Unexpected behavior");
132     }
133   }
134   XBT_INFO("I'm done. See you!");
135   return 0;
136 }
137
138 int main(int argc, char *argv[])
139 {
140   MSG_init(&argc, argv);
141   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
142              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
143
144   MSG_create_environment(argv[1]);
145
146   MSG_function_register("master", master);
147   MSG_function_register("worker", worker);
148   MSG_launch_application(argv[2]);
149
150   msg_error_t res = MSG_main();
151
152   XBT_INFO("Simulation time %g", MSG_get_clock());
153
154   return res != MSG_OK;
155 }