Logo AND Algorithmique Numérique Distribuée

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