Logo AND Algorithmique Numérique Distribuée

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