1 /* Copyright (c) 2013-2015. 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. */
7 #include "simgrid/msg.h"
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 void dump_platform_storages(void);
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_mounted_storage_list(MSG_host_self());
36 xbt_dict_foreach(storage_list,cursor,mount_name,storage_name)
38 XBT_INFO("\tStorage name: %s, mount name: %s", storage_name, mount_name);
40 storage = MSG_storage_get_by_name(storage_name);
42 sg_size_t free_size = MSG_storage_get_free_size(storage);
43 sg_size_t used_size = MSG_storage_get_used_size(storage);
45 XBT_INFO("\t\tFree size: %llu bytes", free_size);
46 XBT_INFO("\t\tUsed size: %llu bytes", used_size);
48 display_storage_properties(storage);
49 dump_storage_by_name(storage_name);
51 xbt_dict_free(&storage_list);
54 void display_storage_properties(msg_storage_t storage){
55 xbt_dict_cursor_t cursor = NULL;
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);
63 XBT_INFO("\tNo property attached.");
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){
70 // Read local src file, and return the size that was actually read
71 sg_size_t read_size = read_local_file(src);
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);
81 sg_size_t write_local_file(const char *dest, sg_size_t file_size)
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()));
91 sg_size_t read_local_file(const char *src)
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);
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);
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);
109 display_storage_content(storage);
112 XBT_INFO("Unable to retrieve storage element by its name: %s.", name);
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;
121 xbt_dict_t content = MSG_storage_get_content(storage);
123 xbt_dict_foreach(content, cursor, file, psize)
124 XBT_INFO("\t%s size: %llu bytes", file, *psize);
126 XBT_INFO("\tNo content.");
128 xbt_dict_free(&content);
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);
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);
143 void dump_platform_storages(void){
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);
151 xbt_dynar_free(&storages);
154 int client(int argc, char *argv[])
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");
160 msg_task_t finalize = MSG_task_create("finalize", 0, 0, NULL);
161 MSG_task_send(finalize, "alice");
163 get_set_storage_data("Disk1");
168 int server(int argc, char *argv[])
170 msg_task_t to_execute = NULL;
171 XBT_ATTRIB_UNUSED int res;
173 storage_info(MSG_host_self());
175 XBT_INFO("Server waiting for transfers ...");
177 res = MSG_task_receive(&(to_execute), MSG_host_get_name(MSG_host_self()));
178 xbt_assert(res == MSG_OK, "MSG_task_get failed");
180 const char *task_name;
181 task_name = MSG_task_get_name(to_execute);
183 if (!strcmp(task_name, "finalize")) { // Shutdown ...
184 MSG_task_destroy(to_execute);
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);
194 MSG_task_destroy(to_execute);
198 storage_info(MSG_host_self());
199 dump_platform_storages();
203 int main(int argc, char *argv[])
205 MSG_init(&argc, argv);
207 /* Check the arguments */
209 printf("Usage: %s platform_file deployment_file \n", argv[0]);
213 const char *platform_file = argv[1];
214 const char *deployment_file = argv[2];
216 MSG_create_environment(platform_file);
218 MSG_function_register("client", client);
219 MSG_function_register("server", server);
220 MSG_launch_application(deployment_file);
222 msg_error_t res = MSG_main();
223 XBT_INFO("Simulated time: %g", MSG_get_clock());