Logo AND Algorithmique Numérique Distribuée

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