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