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 * @section msg_ex_io Simulation disks and files
11 * This section lists some examples of storage simulation. This part of SimGrid is still preliminary.
13 * - <b>Basic example: io-storage/io-storage.c</b>. This example implements all main storage and file functions.
16 #include "simgrid/msg.h"
18 XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation");
20 static int host(int argc, char *argv[]){
21 const char* host_name = MSG_host_get_name(MSG_host_self());
23 /** - Display information on the disks mounted by the current host */
24 XBT_INFO("*** Storage info on %s ***", host_name);
26 xbt_dict_cursor_t cursor = NULL;
29 msg_storage_t storage = NULL;
31 /** - Retrieve all mount points of current host */
32 xbt_dict_t storage_list = MSG_host_get_mounted_storage_list(MSG_host_self());
34 xbt_dict_foreach(storage_list,cursor,mount_name,storage_name) {
35 /** - For each disk mounted on host:
36 - Retrieve disk's information */
37 XBT_INFO("Storage name: %s, mount name: %s", storage_name, mount_name);
38 storage = MSG_storage_get_by_name(storage_name);
40 sg_size_t free_size = MSG_storage_get_free_size(storage);
41 sg_size_t used_size = MSG_storage_get_used_size(storage);
42 sg_size_t size = MSG_storage_get_size(storage);
44 XBT_INFO("Total size: %llu bytes", size);
45 XBT_INFO("Free size: %llu bytes", free_size);
46 XBT_INFO("Used size: %llu bytes", used_size);
48 xbt_dict_free(&storage_list);
50 /** - Create a 200,000 bytes file named './tmp/data.txt' on /sd1 */
51 char* file_name = xbt_strdup("/home/tmp/data.txt");
52 msg_file_t file = NULL;
53 sg_size_t write, read, file_size;
55 /** - Open an non-existing file which amounts to create it. */
56 file = MSG_file_open(file_name, NULL);
57 write = MSG_file_write(file, 200000); // Write 200,000 bytes
58 XBT_INFO("Create a %llu bytes file named '%s' on /sd1", write, file_name);
61 /** - Check that sizes have changed */
62 XBT_INFO("Free size: %llu bytes", MSG_storage_get_free_size(storage));
63 XBT_INFO("Used size: %llu bytes", MSG_storage_get_used_size(storage));
65 /** - Retrieve the size of created file and read it completely */
66 file_size = MSG_file_get_size(file);
67 MSG_file_seek(file, 0, SEEK_SET);
68 read = MSG_file_read(file, file_size);
69 XBT_INFO("Read %llu bytes on %s", read, file_name);
71 /** - Then write 100,000 bytes in tmp/data.txt */
72 write = MSG_file_write(file, 100000); // Write 100,000 bytes
73 XBT_INFO("Write %llu bytes on %s", write, file_name);
76 storage_name = xbt_strdup("Disk4");
77 storage = MSG_storage_get_by_name(storage_name);
79 /** - Move file from ./tmp/data.txt to ./tmp/simgrid.readme */
80 XBT_INFO("*** Move '/tmp/data.txt' into '/tmp/simgrid.readme'");
81 MSG_file_move(file, "/home/tmp/simgrid.readme");
83 /** - Attach some user data to the file */
84 MSG_file_set_data(file, xbt_strdup("777"));
85 /** - Then retrieve this data */
86 char *data = MSG_file_get_data(file);
87 XBT_INFO("User data attached to the file: %s", data);
92 /** - Attach some user data to disk1 */
93 XBT_INFO("*** Get/set data for storage element: %s ***",storage_name);
95 data = MSG_storage_get_data(storage);
97 XBT_INFO("Get storage data: '%s'", data);
99 MSG_storage_set_data(storage, xbt_strdup("Some user data"));
100 data = MSG_storage_get_data(storage);
101 XBT_INFO("Set and get data: '%s'", data);
103 xbt_free(storage_name);
105 /** - Finally dump disks contents */
106 XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self()));
107 xbt_dict_t contents = NULL;
108 contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts
109 xbt_dict_cursor_t curs, curs2 = NULL;
114 xbt_dict_foreach(contents, curs, mountname, content){
115 XBT_INFO("Print the content of mount point: %s",mountname);
116 xbt_dict_foreach(content,curs2,path,size){
117 XBT_INFO("%s size: %llu bytes", path,*((sg_size_t*)size));
119 xbt_dict_free(&content);
121 xbt_dict_free(&contents);
125 int main(int argc, char *argv[])
127 MSG_init(&argc, argv);
128 MSG_create_environment(argv[1]);
129 MSG_function_register("host", host);
130 xbt_dynar_t hosts = MSG_hosts_as_dynar();
131 MSG_process_create(NULL, host, NULL, xbt_dynar_get_as(hosts,0,msg_host_t) );
132 xbt_dynar_free(&hosts);
134 msg_error_t res = MSG_main();
135 XBT_INFO("Simulated time: %g", MSG_get_clock());
137 return res != MSG_OK;