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 ******************************************************************************/
24 /* To use PRIu64 format specifier for printing uint64_t (sg_storage_size_t) */
27 XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation");
29 int host(int argc, char *argv[]){
30 const char* host_name = MSG_host_get_name(MSG_host_self());
32 // display information on the disks mounted by the current host
33 XBT_INFO("*** Storage info on %s ***", host_name);
35 xbt_dict_cursor_t cursor = NULL;
38 msg_storage_t storage;
40 // Retrieve all mount points of current host
41 xbt_dict_t storage_list = MSG_host_get_storage_list(MSG_host_self());
43 xbt_dict_foreach(storage_list,cursor,mount_name,storage_name) {
44 // 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_free(storage_list);
61 // Create a 200,000 bytes file named './tmp/data.txt' on /sd1
63 char* mount = xbt_strdup("/sd1");
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("Total size: %"PRIu64" bytes", MSG_storage_get_free_size("/sd1"));
76 XBT_INFO("Free size: %"PRIu64" bytes", MSG_storage_get_used_size("/sd1"));
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 // Now rename file from /tmp/data.txt to /tmp/simgrid.readme
94 msg_storage_t st = MSG_storage_get_by_name("/sd1");
95 MSG_storage_file_rename(st, "/tmp/data.txt", "/tmp/simgrid.readme");
97 // Now attach some user data to disk1
98 storage_name = xbt_strdup("disk1");
99 XBT_INFO("*** Get/set data for storage element: %s ***",storage_name);
101 storage = MSG_storage_get_by_name(storage_name);
102 char *data = MSG_storage_get_data(storage);
104 XBT_INFO("Get data: '%s'", data);
106 MSG_storage_set_data(storage,strdup("Some user data"));
107 data = MSG_storage_get_data(storage);
108 XBT_INFO("Set and get data: '%s'", data);
112 // Dump disks contents
113 XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self()));
114 xbt_dict_t contents = NULL;
115 contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts
116 xbt_dict_cursor_t curs, curs2 = NULL;
120 sg_storage_size_t *size;
121 xbt_dict_foreach(contents, curs, mountname, content){
122 XBT_INFO("Print the content of mount point: %s",mountname);
123 xbt_dict_foreach(content,curs2,path,size){
124 XBT_INFO("%s size: %"PRIu64" bytes", path,*((sg_storage_size_t*)size));
132 int main(int argc, char *argv[])
135 MSG_init(&argc, argv);
136 /* Check the arguments */
138 printf("Usage: %s platform_file deployment_file \n", argv[0]);
142 const char *platform_file = argv[1];
143 const char *deployment_file = argv[2];
145 MSG_create_environment(platform_file);
146 MSG_function_register("host", host);
147 MSG_launch_application(deployment_file);
149 msg_error_t res = MSG_main();
150 XBT_INFO("Simulated time: %g", MSG_get_clock());