1 /* Copyright (c) 2006-2013. 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 /********************* Files and Storage handling ****************************
8 * This example implements all main storage and file functions of the MSG API
11 * - display information on the disks mounted by the current host
12 * - create a 200,000 bytes file
13 * - completely read the created file
14 * - write 100,000 more bytes in the file
15 * - rename the created file
16 * - attach some user data to a disk
17 * - dump disk's contents
19 ******************************************************************************/
25 /* To use PRIu64 format specifier for printing uint64_t (sg_storage_size_t) */
28 XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation");
30 static int host(int argc, char *argv[]){
31 const char* host_name = MSG_host_get_name(MSG_host_self());
33 // display information on the disks mounted by the current host
34 XBT_INFO("*** Storage info on %s ***", host_name);
36 xbt_dict_cursor_t cursor = NULL;
39 msg_storage_t storage;
41 // Retrieve all mount points of current host
42 xbt_dict_t storage_list = MSG_host_get_storage_list(MSG_host_self());
44 xbt_dict_foreach(storage_list,cursor,mount_name,storage_name) {
45 // For each disk mounted on host
46 XBT_INFO("Storage name: %s, mount name: %s", storage_name, mount_name);
47 storage = MSG_storage_get_by_name(storage_name);
49 // Retrieve disk's information
50 sg_storage_size_t free_size = MSG_storage_get_free_size(mount_name);
51 sg_storage_size_t used_size = MSG_storage_get_used_size(mount_name);
52 sg_storage_size_t size = MSG_storage_get_size(storage);
54 XBT_INFO("Total size: %"PRIu64" bytes", size);
55 XBT_INFO("Free size: %"PRIu64" bytes", free_size);
56 XBT_INFO("Used size: %"PRIu64" bytes", used_size);
58 xbt_dict_free(&storage_list);
61 // Create a 200,000 bytes file named './tmp/data.txt' on /sd1
63 char* mount = xbt_strdup("/home");
64 char* file_name = xbt_strdup("./tmp/data.txt");
65 msg_file_t file = NULL;
66 sg_storage_size_t write, read, file_size;
68 // Open an non-existing file amounts to create it!
69 file = MSG_file_open(mount, file_name, NULL);
70 write = MSG_file_write(file, 200000); // Write 200,000 bytes
71 XBT_INFO("Create a %"PRIu64" bytes file named '%s' on /sd1", write, file_name);
74 // check that sizes have changed
75 XBT_INFO("Free size: %"PRIu64" bytes", MSG_storage_get_free_size("/home"));
76 XBT_INFO("Used size: %"PRIu64" bytes", MSG_storage_get_used_size("/home"));
79 // Now retrieve the size of created file and read it completely
80 file_size = MSG_file_get_size(file);
81 read = MSG_file_read(file, file_size);
82 XBT_INFO("Read %"PRIu64" bytes on %s", read, file_name);
84 // Now write 100,000 more bytes in tmp/data.txt
85 write = MSG_file_write(file, 100000); // Write 100,000 bytes
86 XBT_INFO("Write %"PRIu64" more bytes on %s", write, file_name);
93 storage_name = xbt_strdup("Disk4");
94 storage = MSG_storage_get_by_name(storage_name);
96 // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme
97 XBT_INFO("*** Renaming './tmp/data.txt' into './tmp/simgrid.readme'");
98 MSG_storage_file_rename(storage, "./tmp/data.txt", "./tmp/simgrid.readme");
100 // Now attach some user data to disk1
101 XBT_INFO("*** Get/set data for storage element: %s ***",storage_name);
103 char *data = MSG_storage_get_data(storage);
105 XBT_INFO("Get data: '%s'", data);
107 MSG_storage_set_data(storage,strdup("Some user data"));
108 data = MSG_storage_get_data(storage);
109 XBT_INFO("Set and get data: '%s'", data);
110 xbt_free(storage_name);
113 // Dump disks contents
114 XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self()));
115 xbt_dict_t contents = NULL;
116 contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts
117 xbt_dict_cursor_t curs, curs2 = NULL;
121 sg_storage_size_t *size;
122 xbt_dict_foreach(contents, curs, mountname, content){
123 XBT_INFO("Print the content of mount point: %s",mountname);
124 xbt_dict_foreach(content,curs2,path,size){
125 XBT_INFO("%s size: %"PRIu64" bytes", path,*((sg_storage_size_t*)size));
127 xbt_dict_free(&content);
129 xbt_dict_free(&contents);
134 int main(int argc, char *argv[])
137 MSG_init(&argc, argv);
138 MSG_create_environment(argv[1]);
139 MSG_function_register("host", host);
140 xbt_dynar_t hosts = MSG_hosts_as_dynar();
141 MSG_process_create(NULL, host, NULL, xbt_dynar_get_as(hosts,0,msg_host_t) );
142 xbt_dynar_free(&hosts);
144 msg_error_t res = MSG_main();
145 XBT_INFO("Simulated time: %g", MSG_get_clock());