Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dcccb937173b8ccdaa3a9b55e4206db093951be3
[simgrid.git] / teshsuite / msg / storage / storage_basic.c
1 /* Copyright (c) 2013-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation");
10
11 static void display_storage_properties(msg_storage_t storage){
12   xbt_dict_cursor_t cursor = NULL;
13   char *key, *data;
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);
19   }else{
20   XBT_INFO("\tNo property attached.");
21   }
22 }
23
24 static sg_size_t write_local_file(const char *dest, sg_size_t file_size)
25 {
26   sg_size_t written;
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()));
31   MSG_file_close(file);
32   return written;
33 }
34
35 static sg_size_t read_local_file(const char *src)
36 {
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);
40
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);
43   MSG_file_close(file);
44
45   return read;
46 }
47
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);
52
53   // Send file
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);
58   return 1;
59 }
60
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;
64   char *file;
65   sg_size_t *psize;
66   xbt_dict_t content = MSG_storage_get_content(storage);
67   if (content){
68     xbt_dict_foreach(content, cursor, file, psize)
69     XBT_INFO("\t%s size: %llu bytes", file, *psize);
70   } else {
71     XBT_INFO("\tNo content.");
72   }
73   xbt_dict_free(&content);
74 }
75
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);
79
80   if(storage){
81     display_storage_content(storage);
82   }
83   else{
84     XBT_INFO("Unable to retrieve storage element by its name: %s.", name);
85   }
86 }
87
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);
93
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);
97   xbt_free(data);
98 }
99
100 static void dump_platform_storages(void){
101   unsigned int cursor;
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);
107   }
108   xbt_dynar_free(&storages);
109 }
110
111 static void storage_info(msg_host_t host)
112 {
113   const char* host_name = MSG_host_get_name(host);
114   XBT_INFO("*** Storage info on %s ***", host_name);
115
116   xbt_dict_cursor_t cursor = NULL;
117   char* mount_name;
118   char* storage_name;
119   msg_storage_t storage;
120
121   xbt_dict_t storage_list = MSG_host_get_mounted_storage_list(MSG_host_self());
122
123   xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){
124     XBT_INFO("\tStorage name: %s, mount name: %s", storage_name, mount_name);
125
126     storage = MSG_storage_get_by_name(storage_name);
127
128     sg_size_t free_size = MSG_storage_get_free_size(storage);
129     sg_size_t used_size = MSG_storage_get_used_size(storage);
130
131     XBT_INFO("\t\tFree size: %llu bytes", free_size);
132     XBT_INFO("\t\tUsed size: %llu bytes", used_size);
133
134     display_storage_properties(storage);
135     dump_storage_by_name(storage_name);
136   }
137   xbt_dict_free(&storage_list);
138 }
139
140 static int client(int argc, char *argv[])
141 {
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");
145
146   msg_task_t finalize = MSG_task_create("finalize", 0, 0, NULL);
147   MSG_task_send(finalize, "alice");
148
149   get_set_storage_data("Disk1");
150
151   return 1;
152 }
153
154 static int server(int argc, char *argv[])
155 {
156   msg_task_t to_execute = NULL;
157   XBT_ATTRIB_UNUSED int res;
158
159   storage_info(MSG_host_self());
160
161   XBT_INFO("Server waiting for transfers ...");
162   while(1){
163     res = MSG_task_receive(&(to_execute), MSG_host_get_name(MSG_host_self()));
164     xbt_assert(res == MSG_OK, "MSG_task_get failed");
165
166     const char *task_name;
167     task_name = MSG_task_get_name(to_execute);
168
169     if (!strcmp(task_name, "finalize")) { // Shutdown ...
170       MSG_task_destroy(to_execute);
171       break;
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);
177     }
178
179     MSG_task_destroy(to_execute);
180     to_execute = NULL;
181   }
182
183   storage_info(MSG_host_self());
184   dump_platform_storages();
185   return 1;
186 }
187
188 int main(int argc, char *argv[])
189 {
190   MSG_init(&argc, argv);
191
192   /* Check the arguments */
193   xbt_assert(argc == 2,"Usage: %s platform_file\n", argv[0]);
194
195   MSG_create_environment(argv[1]);
196
197   MSG_process_create("server", server, NULL, MSG_get_host_by_name("alice"));
198   MSG_process_create("client", client, NULL, MSG_get_host_by_name("bob"));
199
200   msg_error_t res = MSG_main();
201   XBT_INFO("Simulated time: %g", MSG_get_clock());
202
203   return res != MSG_OK;
204 }