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"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation");
11 static void display_storage_properties(msg_storage_t storage){
12 xbt_dict_cursor_t cursor = NULL;
14 xbt_dict_t props = MSG_storage_get_properties(storage);
15 if (xbt_dict_length(props) > 0){
16 XBT_INFO("\tProperties of mounted storage: %s", MSG_storage_get_name(storage));
17 xbt_dict_foreach(props, cursor, key, data)
18 XBT_INFO("\t\t'%s' -> '%s'", key, data);
20 XBT_INFO("\tNo property attached.");
24 static sg_size_t write_local_file(const char *dest, sg_size_t file_size)
27 msg_file_t file = MSG_file_open(dest, NULL);
28 written = MSG_file_write(file, file_size);
29 XBT_INFO("%llu bytes on %llu bytes have been written by %s on /sd1",written, file_size,
30 MSG_host_get_name(MSG_host_self()));
35 static sg_size_t read_local_file(const char *src)
37 sg_size_t read, file_size;
38 msg_file_t file = MSG_file_open(src, NULL);
39 file_size = MSG_file_get_size(file);
41 read = MSG_file_read(file, file_size);
42 XBT_INFO("%s has read %llu on %s",MSG_host_get_name(MSG_host_self()),read,src);
48 // Read src file on local disk and send a put message to remote host (size of message = size of src file)
49 static int hsm_put(const char *remote_host, const char *src, const char *dest){
50 // Read local src file, and return the size that was actually read
51 sg_size_t read_size = read_local_file(src);
54 XBT_INFO("%s sends %llu to %s",MSG_host_get_name(MSG_host_self()),read_size,remote_host);
55 msg_task_t to_execute = MSG_task_create((const char*)"hsm_put", 0, (double) read_size, (void*)dest);
56 MSG_task_send(to_execute, remote_host);
57 MSG_process_sleep(.4);
61 static void display_storage_content(msg_storage_t storage){
62 XBT_INFO("Print the content of the storage element: %s",MSG_storage_get_name(storage));
63 xbt_dict_cursor_t cursor = NULL;
66 xbt_dict_t content = MSG_storage_get_content(storage);
68 xbt_dict_foreach(content, cursor, file, psize)
69 XBT_INFO("\t%s size: %llu bytes", file, *psize);
71 XBT_INFO("\tNo content.");
73 xbt_dict_free(&content);
76 static void dump_storage_by_name(char *name){
77 XBT_INFO("*** Dump a storage element ***");
78 msg_storage_t storage = MSG_storage_get_by_name(name);
81 display_storage_content(storage);
84 XBT_INFO("Unable to retrieve storage element by its name: %s.", name);
88 static void get_set_storage_data(const char *storage_name){
89 XBT_INFO("*** GET/SET DATA for storage element: %s ***",storage_name);
90 msg_storage_t storage = MSG_storage_get_by_name(storage_name);
91 char *data = MSG_storage_get_data(storage);
92 XBT_INFO("Get data: '%s'", data);
94 MSG_storage_set_data(storage, xbt_strdup("Some data"));
95 data = MSG_storage_get_data(storage);
96 XBT_INFO("\tSet and get data: '%s'", data);
100 static void dump_platform_storages(void){
102 xbt_dynar_t storages = MSG_storages_as_dynar();
103 msg_storage_t storage;
104 xbt_dynar_foreach(storages, cursor, storage){
105 XBT_INFO("Storage %s is attached to %s", MSG_storage_get_name(storage), MSG_storage_get_host(storage));
106 MSG_storage_set_property_value(storage, "other usage", xbt_strdup("gpfs"), xbt_free_f);
108 xbt_dynar_free(&storages);
111 static void storage_info(msg_host_t host)
113 const char* host_name = MSG_host_get_name(host);
114 XBT_INFO("*** Storage info on %s ***", host_name);
116 xbt_dict_cursor_t cursor = NULL;
119 msg_storage_t storage;
121 xbt_dict_t storage_list = MSG_host_get_mounted_storage_list(MSG_host_self());
123 xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){
124 XBT_INFO("\tStorage name: %s, mount name: %s", storage_name, mount_name);
126 storage = MSG_storage_get_by_name(storage_name);
128 sg_size_t free_size = MSG_storage_get_free_size(storage);
129 sg_size_t used_size = MSG_storage_get_used_size(storage);
131 XBT_INFO("\t\tFree size: %llu bytes", free_size);
132 XBT_INFO("\t\tUsed size: %llu bytes", used_size);
134 display_storage_properties(storage);
135 dump_storage_by_name(storage_name);
137 xbt_dict_free(&storage_list);
140 static int client(int argc, char *argv[])
142 hsm_put("alice","/home/doc/simgrid/examples/msg/icomms/small_platform.xml","c:\\Windows\\toto.cxx");
143 hsm_put("alice","/home/doc/simgrid/examples/msg/parallel_task/test_ptask_deployment.xml","c:\\Windows\\titi.xml");
144 hsm_put("alice","/home/doc/simgrid/examples/msg/alias/masterslave_forwarder_with_alias.c","c:\\Windows\\tata.c");
146 msg_task_t finalize = MSG_task_create("finalize", 0, 0, NULL);
147 MSG_task_send(finalize, "alice");
149 get_set_storage_data("Disk1");
154 static int server(int argc, char *argv[])
156 msg_task_t to_execute = NULL;
157 XBT_ATTRIB_UNUSED int res;
159 storage_info(MSG_host_self());
161 XBT_INFO("Server waiting for transfers ...");
163 res = MSG_task_receive(&(to_execute), MSG_host_get_name(MSG_host_self()));
164 xbt_assert(res == MSG_OK, "MSG_task_get failed");
166 const char *task_name;
167 task_name = MSG_task_get_name(to_execute);
169 if (!strcmp(task_name, "finalize")) { // Shutdown ...
170 MSG_task_destroy(to_execute);
172 } else if(!strcmp(task_name,"hsm_put")){// Receive file to save
173 // Write file on local disk
174 char *dest = MSG_task_get_data(to_execute);
175 sg_size_t size_to_write = (sg_size_t)MSG_task_get_bytes_amount(to_execute);
176 write_local_file(dest, size_to_write);
179 MSG_task_destroy(to_execute);
183 storage_info(MSG_host_self());
184 dump_platform_storages();
188 int main(int argc, char *argv[])
190 MSG_init(&argc, argv);
192 /* Check the arguments */
193 xbt_assert(argc > 2,"Usage: %s platform_file deployment_file \n", argv[0]);
195 MSG_create_environment(argv[1]);
197 MSG_function_register("client", client);
198 MSG_function_register("server", server);
199 MSG_launch_application(argv[2]);
201 msg_error_t res = MSG_main();
202 XBT_INFO("Simulated time: %g", MSG_get_clock());
204 return res != MSG_OK;