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;
15 xbt_dict_t props = MSG_storage_get_properties(storage);
16 if (xbt_dict_length(props) > 0){
17 XBT_INFO("\tProperties of mounted storage: %s", MSG_storage_get_name(storage));
18 xbt_dict_foreach(props, cursor, key, data)
19 XBT_INFO("\t\t'%s' -> '%s'", key, data);
21 XBT_INFO("\tNo property attached.");
25 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 sg_size_t 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 msg_file_t file = MSG_file_open(src, NULL);
38 sg_size_t file_size = MSG_file_get_size(file);
40 sg_size_t read = MSG_file_read(file, file_size);
41 XBT_INFO("%s has read %llu on %s",MSG_host_get_name(MSG_host_self()),read,src);
47 // Read src file on local disk and send a put message to remote host (size of message = size of src file)
48 static int hsm_put(const char *remote_host, const char *src, const char *dest){
49 // Read local src file, and return the size that was actually read
50 sg_size_t read_size = read_local_file(src);
53 XBT_INFO("%s sends %llu to %s",MSG_host_get_name(MSG_host_self()),read_size,remote_host);
54 msg_task_t to_execute = MSG_task_create((const char*)"hsm_put", 0, (double) read_size, (void*)dest);
55 MSG_task_send(to_execute, remote_host);
56 MSG_process_sleep(.4);
60 static void display_storage_content(msg_storage_t storage){
61 XBT_INFO("Print the content of the storage element: %s",MSG_storage_get_name(storage));
62 xbt_dict_cursor_t cursor = NULL;
65 xbt_dict_t content = MSG_storage_get_content(storage);
67 xbt_dict_foreach(content, cursor, file, psize)
68 XBT_INFO("\t%s size: %llu bytes", file, *psize);
70 XBT_INFO("\tNo content.");
72 xbt_dict_free(&content);
75 static void dump_storage_by_name(char *name){
76 XBT_INFO("*** Dump a storage element ***");
77 msg_storage_t storage = MSG_storage_get_by_name(name);
80 display_storage_content(storage);
83 XBT_INFO("Unable to retrieve storage element by its name: %s.", name);
87 static void get_set_storage_data(const char *storage_name){
88 XBT_INFO("*** GET/SET DATA for storage element: %s ***",storage_name);
89 msg_storage_t storage = MSG_storage_get_by_name(storage_name);
90 char *data = MSG_storage_get_data(storage);
91 XBT_INFO("Get data: '%s'", data);
93 MSG_storage_set_data(storage, xbt_strdup("Some data"));
94 data = MSG_storage_get_data(storage);
95 XBT_INFO("\tSet and get data: '%s'", data);
99 static void dump_platform_storages(void){
101 xbt_dynar_t storages = MSG_storages_as_dynar();
102 msg_storage_t storage;
103 xbt_dynar_foreach(storages, cursor, storage){
104 XBT_INFO("Storage %s is attached to %s", MSG_storage_get_name(storage), MSG_storage_get_host(storage));
105 MSG_storage_set_property_value(storage, "other usage", xbt_strdup("gpfs"));
107 xbt_dynar_free(&storages);
110 static void storage_info(msg_host_t host)
112 const char* host_name = MSG_host_get_name(host);
113 XBT_INFO("*** Storage info on %s ***", host_name);
115 xbt_dict_cursor_t cursor = NULL;
118 msg_storage_t storage;
120 xbt_dict_t storage_list = MSG_host_get_mounted_storage_list(MSG_host_self());
122 xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){
123 XBT_INFO("\tStorage name: %s, mount name: %s", storage_name, mount_name);
125 storage = MSG_storage_get_by_name(storage_name);
127 sg_size_t free_size = MSG_storage_get_free_size(storage);
128 sg_size_t used_size = MSG_storage_get_used_size(storage);
130 XBT_INFO("\t\tFree size: %llu bytes", free_size);
131 XBT_INFO("\t\tUsed size: %llu bytes", used_size);
133 display_storage_properties(storage);
134 dump_storage_by_name(storage_name);
136 xbt_dict_free(&storage_list);
139 static int client(int argc, char *argv[])
141 hsm_put("alice","/home/doc/simgrid/examples/msg/icomms/small_platform.xml","c:\\Windows\\toto.cxx");
142 hsm_put("alice","/home/doc/simgrid/examples/msg/parallel_task/test_ptask_deployment.xml","c:\\Windows\\titi.xml");
143 hsm_put("alice","/home/doc/simgrid/examples/msg/alias/masterslave_forwarder_with_alias.c","c:\\Windows\\tata.c");
145 msg_task_t finalize = MSG_task_create("finalize", 0, 0, NULL);
146 MSG_task_send(finalize, "alice");
148 get_set_storage_data("Disk1");
153 static int server(int argc, char *argv[])
155 msg_task_t to_execute = NULL;
156 XBT_ATTRIB_UNUSED int res;
158 storage_info(MSG_host_self());
160 XBT_INFO("Server waiting for transfers ...");
162 res = MSG_task_receive(&(to_execute), MSG_host_get_name(MSG_host_self()));
163 xbt_assert(res == MSG_OK, "MSG_task_get failed");
165 const char *task_name;
166 task_name = MSG_task_get_name(to_execute);
168 if (!strcmp(task_name, "finalize")) { // Shutdown ...
169 MSG_task_destroy(to_execute);
171 } else if(!strcmp(task_name,"hsm_put")){// Receive file to save
172 // Write file on local disk
173 char *dest = MSG_task_get_data(to_execute);
174 sg_size_t size_to_write = (sg_size_t)MSG_task_get_bytes_amount(to_execute);
175 write_local_file(dest, size_to_write);
178 MSG_task_destroy(to_execute);
182 storage_info(MSG_host_self());
183 dump_platform_storages();
187 int main(int argc, char *argv[])
189 MSG_init(&argc, argv);
191 /* Check the arguments */
192 xbt_assert(argc == 2,"Usage: %s platform_file\n", argv[0]);
194 MSG_create_environment(argv[1]);
196 MSG_process_create("server", server, NULL, MSG_get_host_by_name("alice"));
197 MSG_process_create("client", client, NULL, MSG_get_host_by_name("bob"));
199 msg_error_t res = MSG_main();
200 XBT_INFO("Simulated time: %g", MSG_get_clock());
202 return res != MSG_OK;