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(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[]);
23 void storage_info(msg_host_t host)
25 const char* host_name = MSG_host_get_name(host);
26 XBT_INFO("*** Storage info on %s ***", host_name);
28 xbt_dict_cursor_t cursor = NULL;
31 msg_storage_t storage;
33 xbt_dict_t storage_list = MSG_host_get_mounted_storage_list(MSG_host_self());
35 xbt_dict_foreach(storage_list,cursor,mount_name,storage_name)
37 XBT_INFO("\tStorage name: %s, mount name: %s", storage_name, mount_name);
39 storage = MSG_storage_get_by_name(storage_name);
41 sg_size_t free_size = MSG_storage_get_free_size(storage);
42 sg_size_t used_size = MSG_storage_get_used_size(storage);
44 XBT_INFO("\t\tFree size: %llu bytes", free_size);
45 XBT_INFO("\t\tUsed size: %llu bytes", used_size);
47 display_storage_properties(storage);
48 dump_storage_by_name(storage_name);
50 xbt_dict_free(&storage_list);
53 void display_storage_properties(msg_storage_t storage){
54 xbt_dict_cursor_t cursor = NULL;
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);
62 XBT_INFO("\tNo property attached.");
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){
69 // Read local src file, and return the size that was actually read
70 sg_size_t read_size = read_local_file(src);
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);
80 sg_size_t write_local_file(const char *dest, sg_size_t file_size)
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()));
90 sg_size_t read_local_file(const char *src)
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);
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);
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);
108 display_storage_content(storage);
111 XBT_INFO("Unable to retrieve storage element by its name: %s.", name);
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;
120 xbt_dict_t content = MSG_storage_get_content(storage);
122 xbt_dict_foreach(content, cursor, file, psize)
123 XBT_INFO("\t%s size: %llu bytes", file, *psize);
125 XBT_INFO("\tNo content.");
127 xbt_dict_free(&content);
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);
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);
142 int client(int argc, char *argv[])
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");
148 msg_task_t finalize = MSG_task_create("finalize", 0, 0, NULL);
149 MSG_task_send(finalize, "server");
151 get_set_storage_data("cdisk");
156 int server(int argc, char *argv[])
158 msg_task_t to_execute = NULL;
159 _XBT_GNUC_UNUSED int res;
161 storage_info(MSG_host_self());
163 XBT_INFO("Server waiting for transfers ...");
165 res = MSG_task_receive(&(to_execute), MSG_host_get_name(MSG_host_self()));
166 xbt_assert(res == MSG_OK, "MSG_task_get failed");
168 const char *task_name;
169 task_name = MSG_task_get_name(to_execute);
171 if (!strcmp(task_name, "finalize")) { // Shutdown ...
172 MSG_task_destroy(to_execute);
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);
182 MSG_task_destroy(to_execute);
186 storage_info(MSG_host_self());
190 int main(int argc, char *argv[])
192 MSG_init(&argc, argv);
194 /* Check the arguments */
196 printf("Usage: %s platform_file deployment_file \n", argv[0]);
200 const char *platform_file = argv[1];
201 const char *deployment_file = argv[2];
203 MSG_create_environment(platform_file);
205 MSG_function_register("client", client);
206 MSG_function_register("server", server);
207 MSG_launch_application(deployment_file);
209 msg_error_t res = MSG_main();
210 XBT_INFO("Simulated time: %g", MSG_get_clock());