Logo AND Algorithmique Numérique Distribuée

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