1 /* Copyright (c) 2006-2016. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "simgrid/msg.h"
8 XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation");
10 static int host(int argc, char *argv[]){
11 const char* host_name = MSG_host_get_name(MSG_host_self());
13 /* - Display information on the disks mounted by the current host */
14 XBT_INFO("*** Storage info on %s ***", host_name);
16 xbt_dict_cursor_t cursor = NULL;
19 msg_storage_t storage = NULL;
21 /* - Retrieve all mount points of current host */
22 xbt_dict_t storage_list = MSG_host_get_mounted_storage_list(MSG_host_self());
24 xbt_dict_foreach(storage_list,cursor,mount_name,storage_name) {
25 /* - For each disk mounted on host:
26 - Retrieve disk's information */
27 XBT_INFO("Storage name: %s, mount name: %s", storage_name, mount_name);
28 storage = MSG_storage_get_by_name(storage_name);
30 sg_size_t free_size = MSG_storage_get_free_size(storage);
31 sg_size_t used_size = MSG_storage_get_used_size(storage);
32 sg_size_t size = MSG_storage_get_size(storage);
34 XBT_INFO("Total size: %llu bytes", size);
35 XBT_INFO("Free size: %llu bytes", free_size);
36 XBT_INFO("Used size: %llu bytes", used_size);
38 xbt_dict_free(&storage_list);
40 /* - Create a 200,000 bytes file named './tmp/data.txt' on /sd1 */
41 char* file_name = xbt_strdup("/home/tmp/data.txt");
42 msg_file_t file = NULL;
43 sg_size_t write, read, file_size;
45 /* - Open an non-existing file which amounts to create it. */
46 file = MSG_file_open(file_name, NULL);
47 write = MSG_file_write(file, 200000); // Write 200,000 bytes
48 XBT_INFO("Create a %llu bytes file named '%s' on /sd1", write, file_name);
51 /* - Check that sizes have changed */
52 XBT_INFO("Free size: %llu bytes", MSG_storage_get_free_size(storage));
53 XBT_INFO("Used size: %llu bytes", MSG_storage_get_used_size(storage));
55 /* - Retrieve the size of created file and read it completely */
56 file_size = MSG_file_get_size(file);
57 MSG_file_seek(file, 0, SEEK_SET);
58 read = MSG_file_read(file, file_size);
59 XBT_INFO("Read %llu bytes on %s", read, file_name);
61 /* - Then write 100,000 bytes in tmp/data.txt */
62 write = MSG_file_write(file, 100000); // Write 100,000 bytes
63 XBT_INFO("Write %llu bytes on %s", write, file_name);
66 storage_name = xbt_strdup("Disk4");
67 storage = MSG_storage_get_by_name(storage_name);
69 /* - Move file from ./tmp/data.txt to ./tmp/simgrid.readme */
70 XBT_INFO("*** Move '/tmp/data.txt' into '/tmp/simgrid.readme'");
71 MSG_file_move(file, "/home/tmp/simgrid.readme");
73 /* - Attach some user data to the file */
74 MSG_file_set_data(file, xbt_strdup("777"));
75 /* - Then retrieve this data */
76 char *data = MSG_file_get_data(file);
77 XBT_INFO("User data attached to the file: %s", data);
82 /* - Attach some user data to disk1 */
83 XBT_INFO("*** Get/set data for storage element: %s ***",storage_name);
85 data = MSG_storage_get_data(storage);
87 XBT_INFO("Get storage data: '%s'", data);
89 MSG_storage_set_data(storage, xbt_strdup("Some user data"));
90 data = MSG_storage_get_data(storage);
91 XBT_INFO("Set and get data: '%s'", data);
93 xbt_free(storage_name);
95 /* - Finally dump disks contents */
96 XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self()));
97 xbt_dict_t contents = NULL;
98 contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts
99 xbt_dict_cursor_t curs, curs2 = NULL;
104 xbt_dict_foreach(contents, curs, mountname, content){
105 XBT_INFO("Print the content of mount point: %s",mountname);
106 xbt_dict_foreach(content,curs2,path,size){
107 XBT_INFO("%s size: %llu bytes", path,*((sg_size_t*)size));
109 xbt_dict_free(&content);
111 xbt_dict_free(&contents);
115 int main(int argc, char *argv[])
117 MSG_init(&argc, argv);
118 MSG_create_environment(argv[1]);
119 MSG_function_register("host", host);
120 xbt_dynar_t hosts = MSG_hosts_as_dynar();
121 MSG_process_create(NULL, host, NULL, xbt_dynar_get_as(hosts,0,msg_host_t) );
122 xbt_dynar_free(&hosts);
124 msg_error_t res = MSG_main();
125 XBT_INFO("Simulated time: %g", MSG_get_clock());
127 return res != MSG_OK;