Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
delete platform msg props
[simgrid.git] / examples / msg / properties / msg_prop.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2006. All rights reserved.        */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/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
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Property test");
19
20 int master(int argc, char *argv[]);
21 int slave(int argc, char *argv[]);
22 int forwarder(int argc, char *argv[]);
23 MSG_error_t test_all(const char *platform_file, const char *application_file);
24
25 typedef enum {
26   PORT_22 = 0,
27   MAX_CHANNEL
28 } channel_t;
29
30 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
31
32 /** Emitter function  */
33 int master(int argc, char *argv[])
34 {
35   int slaves_count = 0;
36   m_host_t *slaves = NULL;
37   m_task_t *todo = NULL;
38   int number_of_tasks = 0;
39   double task_comp_size = 0;
40   double task_comm_size = 0;
41
42
43   int i;
44
45   xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),
46          "Invalid argument %s\n",argv[1]);
47   xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),
48          "Invalid argument %s\n",argv[2]);
49   xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),
50          "Invalid argument %s\n",argv[3]);
51
52   {                  /*  Task creation */
53     char sprintf_buffer[64];
54
55     todo = xbt_new(m_task_t, sizeof(m_task_t) * number_of_tasks);
56
57     for (i = 0; i < number_of_tasks; i++) {
58       sprintf(sprintf_buffer, "Task_%d", i);
59       todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
60     }
61   }
62
63   {                  /* Process organisation */
64     slaves_count = argc - 4;
65     slaves = xbt_new(m_host_t, sizeof(m_host_t) * slaves_count);
66     xbt_dict_t props;    
67     for (i = 4; i < argc; i++) {     
68       slaves[i-4] = MSG_get_host_by_name(argv[i]);
69       xbt_assert1(slaves[i-4]!=NULL, "Unknown host %s. Stopping Now! ", argv[i]);
70
71       /* Get the property list of the host */
72       props = MSG_host_get_properties(slaves[i-4]);
73       xbt_dict_cursor_t cursor=NULL;
74       char *key,*data;
75
76       /* Print the properties of the host */
77       xbt_dict_foreach(props,cursor,key,data) {
78          INFO3("Property: %s for host: %s has value: %s",key,argv[i],data);
79       }
80
81      /* Try to get a property that does not exist */
82      char *noexist="Unknown";
83      const char*value = MSG_host_get_property_value(slaves[i-4],noexist);
84      if ( value == NULL) 
85        INFO2("Property: %s for host %s is undefined", noexist, argv[i]);
86      else
87        INFO3("Property: %s for host %s has value: %s", noexist, argv[i], value);
88
89       /* Modify an existing property test. First check it exists */\
90       INFO0("Trying to modify a host property");
91       char *exist="Hdd";
92       value = MSG_host_get_property_value(slaves[i-4],exist);
93       if ( value == NULL) 
94         INFO1("\tProperty: %s is undefined", exist);
95       else {
96         INFO2("\tProperty: %s old value: %s", exist, value);
97         xbt_dict_set(props, exist, strdup("250"), free);  
98       }
99  
100       /* Test if we have changed the value */
101       value = MSG_host_get_property_value(slaves[i-4],exist);
102       if ( value == NULL) 
103         INFO1("\tProperty: %s is undefined", exist);
104       else
105         INFO2("\tProperty: %s new value: %s", exist, value);
106     }
107   }
108
109   for (i = 0; i < number_of_tasks; i++) {
110     if(MSG_host_self()==slaves[i % slaves_count]) {
111     }
112     MSG_task_put(todo[i], slaves[i % slaves_count],
113                  PORT_22);
114   }
115   
116   for (i = 0; i < slaves_count; i++) 
117     MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
118                  slaves[i], PORT_22);
119   
120   free(slaves);
121   free(todo);
122   return 0;
123 } /* end_of_master */
124
125 /** Receiver function  */
126 int slave(int argc, char *argv[])
127 {
128   while(1) {
129     m_task_t task = NULL;
130     int a;
131     a = MSG_task_get(&(task), PORT_22);
132     if (a == MSG_OK) {
133       INFO1("Received \"%s\" ", MSG_task_get_name(task));
134       if(MSG_task_get_data(task)==FINALIZE) {
135         MSG_task_destroy(task);
136         break;
137       }
138
139       /* Get the property list of current slave process */
140       xbt_dict_t props = MSG_process_get_properties(MSG_process_self());
141       xbt_dict_cursor_t cursor=NULL;
142       char *key,*data;
143
144       /* Print the properties of the process */
145       xbt_dict_foreach(props,cursor,key,data) {
146          INFO3("Property: %s for process %s has value: %s",key,MSG_process_get_name(MSG_process_self()),data);
147       }
148
149       /* Try to get a property that does not exist */
150       char *noexist="UnknownProcessProp";
151       const char *value = MSG_process_get_property_value(MSG_process_self(),noexist);
152       if ( value == NULL) 
153         INFO2("Property: %s for process %s is undefined", noexist, MSG_process_get_name(MSG_process_self()));
154       else
155         INFO3("Property: %s for process %s has value: %s", noexist, MSG_process_get_name(MSG_process_self()), value);
156
157       MSG_task_execute(task);
158       MSG_task_destroy(task);
159     } else {
160         xbt_die("Hey ?! What's up ?");
161     }
162   }
163   return 0;
164 } /* end_of_slave */
165
166 /** Forwarder function */
167 int forwarder(int argc, char *argv[])
168 {
169   int i;
170   int slaves_count;
171   m_host_t *slaves;
172
173   {                  /* Process organisation */
174     slaves_count = argc - 1;
175     slaves = calloc(slaves_count, sizeof(m_host_t));
176     
177     for (i = 1; i < argc; i++) {
178       slaves[i-1] = MSG_get_host_by_name(argv[i]);
179       xbt_assert1(slaves[i-1]!=NULL, "Unknown host %s. Stopping Now! ", argv[i]);
180     }
181   }
182
183   i=0;
184   while(1) {
185     m_task_t task = NULL;
186     int a;
187     a = MSG_task_get(&(task), PORT_22);
188     if (a == MSG_OK) {
189       INFO1("Received \"%s\" ", MSG_task_get_name(task));
190       if(MSG_task_get_data(task)==FINALIZE) {
191         INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
192         for (i = 0; i < slaves_count; i++) 
193           MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
194                        slaves[i], PORT_22);
195         MSG_task_destroy(task);
196         break;
197       }
198       MSG_task_put(task, slaves[i % slaves_count],
199                    PORT_22);
200       i++;
201     } else {
202         xbt_die("Hey ?! What's up ?");    
203       }
204   }
205   return 0;
206 } /* end_of_forwarder */
207
208 /** Test function */
209 MSG_error_t test_all(const char *platform_file,
210                             const char *application_file)
211 {
212   MSG_error_t res = MSG_OK;
213
214   /* MSG_config("surf_workstation_model","KCCFLN05"); */
215   {                             /*  Simulation setting */
216     MSG_set_channel_number(MAX_CHANNEL);
217     MSG_paje_output("msg_test.trace");
218     MSG_create_environment(platform_file);
219   }
220   {                            /*   Application deployment */
221     MSG_function_register("master", master);
222     MSG_function_register("slave", slave);
223     MSG_function_register("forwarder", forwarder);
224     MSG_launch_application(application_file);
225   }
226   res = MSG_main();
227   
228   INFO1("Simulation time %g",MSG_get_clock());
229   return res;
230 } /* end_of_test_all */
231
232
233 /** Main function */
234 int main(int argc, char *argv[])
235 {
236   MSG_error_t res = MSG_OK;
237
238   MSG_global_init(&argc,argv);
239   if (argc < 3) {
240      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
241      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
242      exit(1);
243   }
244   res = test_all(argv[1],argv[2]);
245   MSG_clean();
246
247   if(res==MSG_OK)
248     return 0;
249   else
250     return 1;
251 } /* end_of_main */