1 /* Copyright (c) 2006-2014. 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 bytes in the file
15 * - rename the created file
16 * - attach some user data to a disk
17 * - dump disk's contents
19 ******************************************************************************/
25 XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation");
27 static int host(int argc, char *argv[]){
28 const char* host_name = MSG_host_get_name(MSG_host_self());
30 // display information on the disks mounted by the current host
31 XBT_INFO("*** Storage info on %s ***", host_name);
33 xbt_dict_cursor_t cursor = NULL;
36 msg_storage_t storage = NULL;
38 // Retrieve all mount points of current host
39 xbt_dict_t storage_list = MSG_host_get_mounted_storage_list(MSG_host_self());
41 xbt_dict_foreach(storage_list,cursor,mount_name,storage_name) {
42 // For each disk mounted on host
43 XBT_INFO("Storage name: %s, mount name: %s", storage_name, mount_name);
44 storage = MSG_storage_get_by_name(storage_name);
46 // Retrieve disk's information
47 sg_size_t free_size = MSG_storage_get_free_size(storage);
48 sg_size_t used_size = MSG_storage_get_used_size(storage);
49 sg_size_t size = MSG_storage_get_size(storage);
51 XBT_INFO("Total size: %llu bytes", size);
52 XBT_INFO("Free size: %llu bytes", free_size);
53 XBT_INFO("Used size: %llu bytes", used_size);
55 xbt_dict_free(&storage_list);
58 // Create a 200,000 bytes file named './tmp/data.txt' on /sd1
59 char* file_name = xbt_strdup("/home/tmp/data.txt");
60 msg_file_t file = NULL;
61 sg_size_t write, read, file_size;
63 // Open an non-existing file amounts to create it!
64 file = MSG_file_open(file_name, NULL);
65 write = MSG_file_write(file, 200000); // Write 200,000 bytes
66 XBT_INFO("Create a %llu bytes file named '%s' on /sd1", write, file_name);
69 // check that sizes have changed
70 XBT_INFO("Free size: %llu bytes", MSG_storage_get_free_size(storage));
71 XBT_INFO("Used size: %llu bytes", MSG_storage_get_used_size(storage));
74 // Now retrieve the size of created file and read it completely
75 file_size = MSG_file_get_size(file);
76 MSG_file_seek(file, 0, SEEK_SET);
77 read = MSG_file_read(file, file_size);
78 XBT_INFO("Read %llu bytes on %s", read, file_name);
80 // Now write 100,000 bytes in tmp/data.txt
81 write = MSG_file_write(file, 100000); // Write 100,000 bytes
82 XBT_INFO("Write %llu bytes on %s", write, file_name);
85 storage_name = xbt_strdup("Disk4");
86 storage = MSG_storage_get_by_name(storage_name);
88 // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme
89 XBT_INFO("*** Move '/tmp/data.txt' into '/tmp/simgrid.readme'");
90 MSG_file_move(file, "/home/tmp/simgrid.readme");
92 // Attach some user data to the file
93 MSG_file_set_data(file, xbt_strdup("777"));
94 // Retrieve these data
95 char *data = MSG_file_get_data(file);
96 XBT_INFO("User data attached to the file: %s", data);
101 // Now attach some user data to disk1
102 XBT_INFO("*** Get/set data for storage element: %s ***",storage_name);
104 data = MSG_storage_get_data(storage);
106 XBT_INFO("Get storage data: '%s'", data);
108 MSG_storage_set_data(storage, xbt_strdup("Some user data"));
109 data = MSG_storage_get_data(storage);
110 XBT_INFO("Set and get data: '%s'", data);
112 xbt_free(storage_name);
115 // Dump disks contents
116 XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self()));
117 xbt_dict_t contents = NULL;
118 contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts
119 xbt_dict_cursor_t curs, curs2 = NULL;
124 xbt_dict_foreach(contents, curs, mountname, content){
125 XBT_INFO("Print the content of mount point: %s",mountname);
126 xbt_dict_foreach(content,curs2,path,size){
127 XBT_INFO("%s size: %llu bytes", path,*((sg_size_t*)size));
129 xbt_dict_free(&content);
131 xbt_dict_free(&contents);
136 int main(int argc, char *argv[])
139 MSG_init(&argc, argv);
140 MSG_create_environment(argv[1]);
141 MSG_function_register("host", host);
142 xbt_dynar_t hosts = MSG_hosts_as_dynar();
143 MSG_process_create(NULL, host, NULL, xbt_dynar_get_as(hosts,0,msg_host_t) );
144 xbt_dynar_free(&hosts);
146 msg_error_t res = MSG_main();
147 XBT_INFO("Simulated time: %g", MSG_get_clock());