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 sg_size_t free_size = MSG_storage_get_free_size(mount_name);
40 sg_size_t used_size = MSG_storage_get_used_size(mount_name);
42 XBT_INFO("\t\tFree size: %llu bytes", free_size);
43 XBT_INFO("\t\tUsed size: %llu bytes", used_size);
45 storage = MSG_storage_get_by_name(storage_name);
46 display_storage_properties(storage);
47 dump_storage_by_name(storage_name);
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);
56 if (xbt_dict_length(props) > 0){
57 XBT_INFO("\tProperties of mounted storage: %s", MSG_storage_get_name(storage));
58 xbt_dict_foreach(props, cursor, key, data)
59 XBT_INFO("\t\t'%s' -> '%s'", key, data);
61 XBT_INFO("\tNo 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);
75 MSG_process_sleep(.4);
79 sg_size_t write_local_file(const char *dest, sg_size_t file_size)
82 msg_file_t file = MSG_file_open(dest, NULL);
83 written = MSG_file_write(file, file_size);
84 XBT_INFO("%llu bytes on %llu bytes have been written by %s on /sd1",written, file_size, MSG_host_get_name(MSG_host_self()));
89 sg_size_t read_local_file(const char *src)
91 sg_size_t read, file_size;
92 msg_file_t file = MSG_file_open(src, NULL);
93 file_size = MSG_file_get_size(file);
95 read = MSG_file_read(file, file_size);
96 XBT_INFO("%s has read %llu on %s",MSG_host_get_name(MSG_host_self()),read,src);
102 void dump_storage_by_name(char *name){
103 XBT_INFO("*** Dump a storage element ***");
104 msg_storage_t storage = MSG_storage_get_by_name(name);
107 display_storage_content(storage);
110 XBT_INFO("Unable to retrieve storage element by its name: %s.", name);
114 void display_storage_content(msg_storage_t storage){
115 XBT_INFO("Print the content of the storage element: %s",MSG_storage_get_name(storage));
116 xbt_dict_cursor_t cursor = NULL;
119 xbt_dict_t content = MSG_storage_get_content(storage);
121 xbt_dict_foreach(content, cursor, file, psize)
122 XBT_INFO("\t%s size: %llu bytes", file, *psize);
124 XBT_INFO("\tNo content.");
126 xbt_dict_free(&content);
129 void get_set_storage_data(const char *storage_name){
130 XBT_INFO("*** GET/SET DATA for storage element: %s ***",storage_name);
131 msg_storage_t storage = MSG_storage_get_by_name(storage_name);
132 char *data = MSG_storage_get_data(storage);
133 XBT_INFO("Get data: '%s'", data);
135 MSG_storage_set_data(storage, xbt_strdup("Some data"));
136 data = MSG_storage_get_data(storage);
137 XBT_INFO("\tSet and get data: '%s'", data);
141 int client(int argc, char *argv[])
143 hsm_put("server","/sd1/doc/simgrid/examples/cxx/autoDestination/FinalizeTask.cxx","/sd2/scratch/toto.cxx");
144 hsm_put("server","/sd1/doc/simgrid/examples/cxx/autoDestination/autoDestination_deployment.xml","/sd2/scratch/titi.xml");
145 hsm_put("server","/sd1/doc/simgrid/examples/cxx/autoDestination/Slave.cxx","/sd2/scratch/tata.cxx");
147 msg_task_t finalize = MSG_task_create("finalize", 0, 0, NULL);
148 MSG_task_send(finalize, "server");
150 get_set_storage_data("cdisk");
155 int server(int argc, char *argv[])
157 msg_task_t to_execute = NULL;
158 _XBT_GNUC_UNUSED int res;
160 storage_info(MSG_host_self());
162 XBT_INFO("Server waiting for transfers ...");
164 res = MSG_task_receive(&(to_execute), MSG_host_get_name(MSG_host_self()));
165 xbt_assert(res == MSG_OK, "MSG_task_get failed");
167 const char *task_name;
168 task_name = MSG_task_get_name(to_execute);
170 if (!strcmp(task_name, "finalize")) { // Shutdown ...
171 MSG_task_destroy(to_execute);
174 else if(!strcmp(task_name,"hsm_put")){// Receive file to save
175 // Write file on local disk
176 char *dest = MSG_task_get_data(to_execute);
177 sg_size_t size_to_write = (sg_size_t)MSG_task_get_data_size(to_execute);
178 write_local_file(dest, size_to_write);
181 MSG_task_destroy(to_execute);
185 storage_info(MSG_host_self());
189 int main(int argc, char *argv[])
191 MSG_init(&argc, argv);
193 /* Check the arguments */
195 printf("Usage: %s platform_file deployment_file \n", argv[0]);
199 const char *platform_file = argv[1];
200 const char *deployment_file = argv[2];
202 MSG_create_environment(platform_file);
204 MSG_function_register("client", client);
205 MSG_function_register("server", server);
206 MSG_launch_application(deployment_file);
208 msg_error_t res = MSG_main();
209 XBT_INFO("Simulated time: %g", MSG_get_clock());