Logo AND Algorithmique Numérique Distribuée

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