1 /* Copyright (c) 2013-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
10 XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation");
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[]);
24 void storage_info(msg_host_t host)
26 const char* host_name = MSG_host_get_name(host);
27 XBT_INFO("*** Storage info on %s ***:", host_name);
29 xbt_dict_cursor_t cursor = NULL;
32 msg_storage_t storage;
34 xbt_dict_t storage_list = MSG_host_get_storage_list(MSG_host_self());
36 xbt_dict_foreach(storage_list,cursor,mount_name,storage_name)
38 XBT_INFO("Storage name: %s, mount name: %s", storage_name, mount_name);
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);
43 XBT_INFO("Free size: %llu bytes", free_size);
44 XBT_INFO("Used size: %llu bytes", used_size);
46 storage = MSG_storage_get_by_name(storage_name);
47 display_storage_properties(storage);
49 xbt_dict_free(&storage_list);
52 void display_storage_properties(msg_storage_t storage){
53 xbt_dict_cursor_t cursor = NULL;
55 xbt_dict_t props = MSG_storage_get_properties(storage);
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);
61 XBT_INFO("No property attached.");
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){
68 // Read local src file, and return the size that was actually read
69 sg_size_t read_size = read_local_file(src);
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);
79 sg_size_t write_local_file(char *dest, sg_size_t file_size)
82 msg_file_t file = MSG_file_open("/sd1",dest, NULL);
83 write = MSG_file_write(file, file_size);
88 sg_size_t read_local_file(const char *src)
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);
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);
101 void display_storage_info(msg_host_t host)
103 const char* host_name = MSG_host_get_name(host);
104 XBT_INFO("*** Storage info of: %s ***", host_name);
106 xbt_dict_cursor_t cursor = NULL;
110 xbt_dict_t storage_list = MSG_host_get_storage_list(host);
112 xbt_dict_foreach(storage_list,cursor,mount_name,storage_name)
114 dump_storage_by_name(storage_name);
116 xbt_dict_free(&storage_list);
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);
124 display_storage_content(storage);
127 XBT_INFO("Unable to retrieve storage element by its name: %s.", name);
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;
136 xbt_dict_t content = MSG_storage_get_content(storage);
138 xbt_dict_foreach(content, cursor, file, psize)
139 XBT_INFO("%s size: %llu bytes", file, *psize);
141 XBT_INFO("No content.");
143 xbt_dict_free(&content);
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);
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);
157 int client(int argc, char *argv[])
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");
163 msg_task_t finalize = MSG_task_create("finalize", 0, 0, NULL);
164 MSG_task_send(finalize, "server");
166 get_set_storage_data("cdisk");
167 display_storage_info(MSG_host_self());
172 int server(int argc, char *argv[])
174 msg_task_t to_execute = NULL;
175 _XBT_GNUC_UNUSED int res;
177 display_storage_info(MSG_host_self());
179 XBT_INFO("Server waiting for transfers");
181 res = MSG_task_receive(&(to_execute), MSG_host_get_name(MSG_host_self()));
182 xbt_assert(res == MSG_OK, "MSG_task_get failed");
184 const char *task_name;
185 task_name = MSG_task_get_name(to_execute);
187 if (!strcmp(task_name, "finalize")) { // Shutdown ...
188 MSG_task_destroy(to_execute);
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);
198 MSG_task_destroy(to_execute);
202 display_storage_info(MSG_host_self());
206 int main(int argc, char *argv[])
208 MSG_init(&argc, argv);
210 /* Check the arguments */
212 printf("Usage: %s platform_file deployment_file \n", argv[0]);
216 const char *platform_file = argv[1];
217 const char *deployment_file = argv[2];
219 MSG_create_environment(platform_file);
221 MSG_function_register("client", client);
222 MSG_function_register("server", server);
223 MSG_launch_application(deployment_file);
225 msg_error_t res = MSG_main();
226 XBT_INFO("Simulated time: %g", MSG_get_clock());