1 /* Copyright (c) 2006-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 /** @addtogroup MSG_examples
9 * - <b>io/storage.c</b> demo of all main storage and file functions
12 /********************* Files and Storage handling ****************************
13 * This example implements all main storage and file functions of the MSG API
16 * - display information on the disks mounted by the current host
17 * - create a 200,000 bytes file
18 * - completely read the created file
19 * - write 100,000 bytes in the file
20 * - rename the created file
21 * - attach some user data to a disk
22 * - dump disk's contents
24 ******************************************************************************/
26 #include "simgrid/msg.h"
30 XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation");
32 static int host(int argc, char *argv[]){
33 const char* host_name = MSG_host_get_name(MSG_host_self());
35 // display information on the disks mounted by the current host
36 XBT_INFO("*** Storage info on %s ***", host_name);
38 xbt_dict_cursor_t cursor = NULL;
41 msg_storage_t storage = NULL;
43 // Retrieve all mount points of current host
44 xbt_dict_t storage_list = MSG_host_get_mounted_storage_list(MSG_host_self());
46 xbt_dict_foreach(storage_list,cursor,mount_name,storage_name) {
47 // For each disk mounted on host
48 XBT_INFO("Storage name: %s, mount name: %s", storage_name, mount_name);
49 storage = MSG_storage_get_by_name(storage_name);
51 // Retrieve disk's information
52 sg_size_t free_size = MSG_storage_get_free_size(storage);
53 sg_size_t used_size = MSG_storage_get_used_size(storage);
54 sg_size_t size = MSG_storage_get_size(storage);
56 XBT_INFO("Total size: %llu bytes", size);
57 XBT_INFO("Free size: %llu bytes", free_size);
58 XBT_INFO("Used size: %llu bytes", used_size);
60 xbt_dict_free(&storage_list);
63 // Create a 200,000 bytes file named './tmp/data.txt' on /sd1
64 char* file_name = xbt_strdup("/home/tmp/data.txt");
65 msg_file_t file = NULL;
66 sg_size_t write, read, file_size;
68 // Open an non-existing file amounts to create it!
69 file = MSG_file_open(file_name, NULL);
70 write = MSG_file_write(file, 200000); // Write 200,000 bytes
71 XBT_INFO("Create a %llu bytes file named '%s' on /sd1", write, file_name);
74 // check that sizes have changed
75 XBT_INFO("Free size: %llu bytes", MSG_storage_get_free_size(storage));
76 XBT_INFO("Used size: %llu bytes", MSG_storage_get_used_size(storage));
79 // Now retrieve the size of created file and read it completely
80 file_size = MSG_file_get_size(file);
81 MSG_file_seek(file, 0, SEEK_SET);
82 read = MSG_file_read(file, file_size);
83 XBT_INFO("Read %llu bytes on %s", read, file_name);
85 // Now write 100,000 bytes in tmp/data.txt
86 write = MSG_file_write(file, 100000); // Write 100,000 bytes
87 XBT_INFO("Write %llu bytes on %s", write, file_name);
90 storage_name = xbt_strdup("Disk4");
91 storage = MSG_storage_get_by_name(storage_name);
93 // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme
94 XBT_INFO("*** Move '/tmp/data.txt' into '/tmp/simgrid.readme'");
95 MSG_file_move(file, "/home/tmp/simgrid.readme");
97 // Attach some user data to the file
98 MSG_file_set_data(file, xbt_strdup("777"));
99 // Retrieve these data
100 char *data = MSG_file_get_data(file);
101 XBT_INFO("User data attached to the file: %s", data);
103 MSG_file_close(file);
106 // Now attach some user data to disk1
107 XBT_INFO("*** Get/set data for storage element: %s ***",storage_name);
109 data = MSG_storage_get_data(storage);
111 XBT_INFO("Get storage data: '%s'", data);
113 MSG_storage_set_data(storage, xbt_strdup("Some user data"));
114 data = MSG_storage_get_data(storage);
115 XBT_INFO("Set and get data: '%s'", data);
117 xbt_free(storage_name);
120 // Dump disks contents
121 XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self()));
122 xbt_dict_t contents = NULL;
123 contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts
124 xbt_dict_cursor_t curs, curs2 = NULL;
129 xbt_dict_foreach(contents, curs, mountname, content){
130 XBT_INFO("Print the content of mount point: %s",mountname);
131 xbt_dict_foreach(content,curs2,path,size){
132 XBT_INFO("%s size: %llu bytes", path,*((sg_size_t*)size));
134 xbt_dict_free(&content);
136 xbt_dict_free(&contents);
141 int main(int argc, char *argv[])
144 MSG_init(&argc, argv);
145 MSG_create_environment(argv[1]);
146 MSG_function_register("host", host);
147 xbt_dynar_t hosts = MSG_hosts_as_dynar();
148 MSG_process_create(NULL, host, NULL, xbt_dynar_get_as(hosts,0,msg_host_t) );
149 xbt_dynar_free(&hosts);
151 msg_error_t res = MSG_main();
152 XBT_INFO("Simulated time: %g", MSG_get_clock());