Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update msg-storage tesh
[simgrid.git] / teshsuite / msg / storage / storage.c
1 #include "msg/msg.h"
2 #include "xbt/log.h"
3
4 XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation");
5
6 void storage_info(msg_host_t host);
7 void display_storage_properties(msg_storage_t storage);
8 int hsm_put(const char *remote_host, const char *src, const char *dest);
9 sg_storage_size_t write_local_file(char *dest, sg_storage_size_t file_size);
10 sg_storage_size_t read_local_file(const char *src);
11 void display_storage_info(msg_host_t host);
12 void dump_storage_by_name(char *name);
13 void display_storage_content(msg_storage_t storage);
14 void get_set_storage_data(const char *storage_name);
15 int client(int argc, char *argv[]);
16 int server(int argc, char *argv[]);
17
18 void storage_info(msg_host_t host)
19 {
20   const char* host_name = MSG_host_get_name(host);
21   XBT_INFO("*** Storage info on %s ***:", host_name);
22
23   xbt_dict_cursor_t cursor = NULL;
24   char* mount_name;
25   char* storage_name;
26   msg_storage_t storage;
27
28   xbt_dict_t storage_list = MSG_host_get_storage_list(MSG_host_self());
29
30   xbt_dict_foreach(storage_list,cursor,mount_name,storage_name)
31   {
32         XBT_INFO("Storage name: %s, mount name: %s", storage_name, mount_name);
33
34     sg_storage_size_t free_size = MSG_storage_get_free_size(mount_name);
35     sg_storage_size_t used_size = MSG_storage_get_used_size(mount_name);
36
37     XBT_INFO("Free size: %zu bytes", free_size);
38     XBT_INFO("Used size: %zu bytes", used_size);
39
40     storage = MSG_storage_get_by_name(storage_name);
41     display_storage_properties(storage);
42   }
43 }
44
45 void display_storage_properties(msg_storage_t storage){
46   xbt_dict_cursor_t cursor = NULL;
47   char *key, *data;
48   xbt_dict_t props = MSG_storage_get_properties(storage);
49   if (props){
50     XBT_INFO("Properties of mounted storage: %s", MSG_storage_get_name(storage));
51     xbt_dict_foreach(props, cursor, key, data)
52           XBT_INFO("'%s' -> '%s'", key, data);
53   }else{
54         XBT_INFO("No property attached.");
55   }
56 }
57
58 // Read src file on local disk and send a put message to remote host (size of message = size of src file)
59 int hsm_put(const char *remote_host, const char *src, const char *dest){
60
61   // Read local src file, and return the size that was actually read
62   sg_storage_size_t read_size = read_local_file(src);
63
64   // Send file
65   XBT_INFO("%s sends %zu to %s",MSG_host_get_name(MSG_host_self()),read_size,remote_host);
66   msg_task_t to_execute = MSG_task_create((const char*)"hsm_put", 0, (double) read_size, (void*)dest);
67   MSG_task_send(to_execute, remote_host);
68
69   return 1;
70 }
71
72 sg_storage_size_t write_local_file(char *dest, sg_storage_size_t file_size)
73 {
74   sg_storage_size_t write;
75   msg_file_t file = MSG_file_open("/sd1",dest, NULL);
76   write = MSG_file_write(file, file_size);
77   MSG_file_close(file);
78   return write;
79 }
80
81 sg_storage_size_t read_local_file(const char *src)
82 {
83   sg_storage_size_t read, file_size;
84   msg_file_t file = MSG_file_open("/sd1",src, NULL);
85   file_size = MSG_file_get_size(file);
86
87   read = MSG_file_read(file, file_size);
88   XBT_INFO("%s has read %zu on %s",MSG_host_get_name(MSG_host_self()),read,src);
89   MSG_file_close(file);
90
91   return read;
92 }
93
94 void display_storage_info(msg_host_t host)
95 {
96   const char* host_name = MSG_host_get_name(host);
97   XBT_INFO("*** Storage info of: %s ***", host_name);
98
99   xbt_dict_cursor_t cursor = NULL;
100   char* mount_name;
101   char* storage_name;
102
103   xbt_dict_t storage_list = MSG_host_get_storage_list(host);
104
105   xbt_dict_foreach(storage_list,cursor,mount_name,storage_name)
106   {
107     dump_storage_by_name(storage_name);
108   }
109 }
110
111 void dump_storage_by_name(char *name){
112   XBT_INFO("*** Dump a storage element ***");
113   msg_storage_t storage = MSG_storage_get_by_name(name);
114
115   if(storage){
116     display_storage_content(storage);
117   }
118   else{
119     XBT_INFO("Unable to retrieve storage element by its name: %s.", name);
120   }
121 }
122
123 void display_storage_content(msg_storage_t storage){
124   XBT_INFO("Print the content of the storage element: %s",MSG_storage_get_name(storage));
125   xbt_dict_cursor_t cursor = NULL;
126   char *file;
127   sg_storage_size_t size;
128   xbt_dict_t content = MSG_storage_get_content(storage);
129   if (content){
130     xbt_dict_foreach(content, cursor, file, size)
131           XBT_INFO("%s size: %zu bytes", file, size);
132   }else{
133         XBT_INFO("No content.");
134   }
135 }
136
137 void get_set_storage_data(const char *storage_name){
138         XBT_INFO("*** GET/SET DATA for storage element: %s ***",storage_name);
139         msg_storage_t storage = MSG_storage_get_by_name(storage_name);
140         char *data = MSG_storage_get_data(storage);
141         XBT_INFO("Get data: '%s'", data);
142         MSG_storage_set_data(storage,strdup("Some data"));
143         data = MSG_storage_get_data(storage);
144         XBT_INFO("Set and get data: '%s'", data);
145 }
146
147 int client(int argc, char *argv[])
148 {
149   hsm_put("server","./doc/simgrid/examples/cxx/autoDestination/FinalizeTask.cxx","./scratch/toto.xml");
150   hsm_put("server","./doc/simgrid/examples/cxx/autoDestination/autoDestination_deployment.xml","./scratch/titi.cxx");
151   hsm_put("server","./doc/simgrid/examples/cxx/autoDestination/Slave.cxx","./scratch/tata.cxx");
152
153   msg_task_t finalize = MSG_task_create("finalize", 0, 0, NULL);
154   MSG_task_send(finalize, "server");
155
156   get_set_storage_data("cdisk");
157   display_storage_info(MSG_host_self());
158
159   return 1;
160 }
161
162 int server(int argc, char *argv[])
163 {
164   msg_task_t to_execute = NULL;
165   _XBT_GNUC_UNUSED int res;
166
167   display_storage_info(MSG_host_self());
168
169   XBT_INFO("Server waiting for transfers");
170   while(1){
171     res = MSG_task_receive(&(to_execute), MSG_host_get_name(MSG_host_self()));
172     xbt_assert(res == MSG_OK, "MSG_task_get failed");
173
174     const char *task_name;
175     task_name = MSG_task_get_name(to_execute);
176
177     if (!strcmp(task_name, "finalize")) { // Shutdown ...
178       MSG_task_destroy(to_execute);
179       break;
180     }
181     else if(!strcmp(task_name,"hsm_put")){// Receive file to save
182       // Write file on local disk
183       char *dest = MSG_task_get_data(to_execute);
184       sg_storage_size_t size_to_write = (sg_storage_size_t)MSG_task_get_data_size(to_execute);
185       write_local_file(dest, size_to_write);
186         }
187
188     MSG_task_destroy(to_execute);
189     to_execute = NULL;
190   }
191
192   display_storage_info(MSG_host_self());
193   return 1;
194 }
195
196 int main(int argc, char *argv[])
197 {
198   MSG_init(&argc, argv);
199
200   /* Check the arguments */
201   if (argc < 3) {
202     printf("Usage: %s platform_file deployment_file \n", argv[0]);
203     return -1;
204   }
205
206   const char *platform_file = argv[1];
207   const char *deployment_file = argv[2];
208
209   MSG_create_environment(platform_file);
210
211   MSG_function_register("client", client);
212   MSG_function_register("server", server);
213   MSG_launch_application(deployment_file);
214
215   msg_error_t res = MSG_main();
216   XBT_INFO("Simulated time: %g", MSG_get_clock());
217
218   if (res == MSG_OK)
219     return 0;
220   else
221     return 1;
222 }