Logo AND Algorithmique Numérique Distribuée

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