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;
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(mount_name);
48 sg_size_t used_size = MSG_storage_get_used_size(mount_name);
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("/home"));
71 XBT_INFO("Used size: %llu bytes", MSG_storage_get_used_size("/home"));
74 // Now retrieve the size of created file and read it completely
75 file_size = MSG_file_get_size(file);
76 read = MSG_file_read(file, file_size);
77 XBT_INFO("Read %llu bytes on %s", read, file_name);
79 // Now write 100,000 bytes in tmp/data.txt
80 write = MSG_file_write(file, 100000); // Write 100,000 bytes
81 XBT_INFO("Write %llu bytes on %s", write, file_name);
84 storage_name = xbt_strdup("Disk4");
85 storage = MSG_storage_get_by_name(storage_name);
87 // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme
88 XBT_INFO("*** Move '/tmp/data.txt' into '/tmp/simgrid.readme'");
89 MSG_file_move(file, "/home/tmp/simgrid.readme");
94 // Now attach some user data to disk1
95 XBT_INFO("*** Get/set data for storage element: %s ***",storage_name);
97 char *data = MSG_storage_get_data(storage);
99 XBT_INFO("Get data: '%s'", data);
101 MSG_storage_set_data(storage, xbt_strdup("Some user data"));
102 data = MSG_storage_get_data(storage);
103 XBT_INFO("Set and get data: '%s'", data);
105 xbt_free(storage_name);
108 // Dump disks contents
109 XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self()));
110 xbt_dict_t contents = NULL;
111 contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts
112 xbt_dict_cursor_t curs, curs2 = NULL;
117 xbt_dict_foreach(contents, curs, mountname, content){
118 XBT_INFO("Print the content of mount point: %s",mountname);
119 xbt_dict_foreach(content,curs2,path,size){
120 XBT_INFO("%s size: %llu bytes", path,*((sg_size_t*)size));
122 xbt_dict_free(&content);
124 xbt_dict_free(&contents);
129 int main(int argc, char *argv[])
132 MSG_init(&argc, argv);
133 MSG_create_environment(argv[1]);
134 MSG_function_register("host", host);
135 xbt_dynar_t hosts = MSG_hosts_as_dynar();
136 MSG_process_create(NULL, host, NULL, xbt_dynar_get_as(hosts,0,msg_host_t) );
137 xbt_dynar_free(&hosts);
139 msg_error_t res = MSG_main();
140 XBT_INFO("Simulated time: %g", MSG_get_clock());