Logo AND Algorithmique Numérique Distribuée

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