Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
guess what? more refactoring!
[simgrid.git] / examples / msg / io / storage.c
1 /* Copyright (c) 2006-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /** @addtogroup MSG_examples
8  * 
9  * - <b>io/storage.c</b> demo of all main storage and file functions
10  */
11
12 /********************* Files and Storage handling ****************************
13  * This example implements all main storage and file functions of the MSG API
14  *
15  * Scenario :
16  * - display information on the disks mounted by the current host
17  * - create a 200,000 bytes file
18  * - completely read the created file
19  * - write 100,000 bytes in the file
20  * - rename the created file
21  * - attach some user data to a disk
22  * - dump disk's contents
23  *
24 ******************************************************************************/
25
26 #include "simgrid/msg.h"
27
28 XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation");
29
30 static int host(int argc, char *argv[]){
31   const char* host_name = MSG_host_get_name(MSG_host_self());
32
33   // display information on the disks mounted by the current host
34   XBT_INFO("*** Storage info on %s ***", host_name);
35
36   xbt_dict_cursor_t cursor = NULL;
37   char* mount_name;
38   char* storage_name;
39   msg_storage_t storage = NULL;
40
41   // Retrieve all mount points of current host
42   xbt_dict_t storage_list = MSG_host_get_mounted_storage_list(MSG_host_self());
43
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);
48
49     // Retrieve disk's information
50     sg_size_t free_size = MSG_storage_get_free_size(storage);
51     sg_size_t used_size = MSG_storage_get_used_size(storage);
52     sg_size_t size = MSG_storage_get_size(storage);
53
54     XBT_INFO("Total size: %llu bytes", size);
55     XBT_INFO("Free size: %llu bytes", free_size);
56     XBT_INFO("Used size: %llu bytes", used_size);
57   }
58   xbt_dict_free(&storage_list);
59
60   // Create a 200,000 bytes file named './tmp/data.txt' on /sd1
61   char* file_name = xbt_strdup("/home/tmp/data.txt");
62   msg_file_t file = NULL;
63   sg_size_t write, read, file_size;
64
65   // Open an non-existing file amounts to create it!
66   file = MSG_file_open(file_name, NULL);
67   write = MSG_file_write(file, 200000);  // Write 200,000 bytes
68   XBT_INFO("Create a %llu bytes file named '%s' on /sd1", write, file_name);
69   MSG_file_dump(file);
70
71   // check that sizes have changed
72   XBT_INFO("Free size: %llu bytes", MSG_storage_get_free_size(storage));
73   XBT_INFO("Used size: %llu bytes", MSG_storage_get_used_size(storage));
74
75   // Now retrieve the size of created file and read it completely
76   file_size = MSG_file_get_size(file);
77   MSG_file_seek(file, 0, SEEK_SET);
78   read = MSG_file_read(file, file_size);
79   XBT_INFO("Read %llu bytes on %s", read, file_name);
80
81   // Now write 100,000 bytes in tmp/data.txt
82   write = MSG_file_write(file, 100000);  // Write 100,000 bytes
83   XBT_INFO("Write %llu bytes on %s", write, file_name);
84   MSG_file_dump(file);
85
86   storage_name = xbt_strdup("Disk4");
87   storage = MSG_storage_get_by_name(storage_name);
88
89   // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme
90   XBT_INFO("*** Move '/tmp/data.txt' into '/tmp/simgrid.readme'");
91   MSG_file_move(file, "/home/tmp/simgrid.readme");
92
93   // Attach some user data to the file
94   MSG_file_set_data(file, xbt_strdup("777"));
95   // Retrieve these data
96   char *data = MSG_file_get_data(file);
97   XBT_INFO("User data attached to the file: %s", data);
98
99   MSG_file_close(file);
100   free(file_name);
101
102   // Now attach some user data to disk1
103   XBT_INFO("*** Get/set data for storage element: %s ***",storage_name);
104
105   data = MSG_storage_get_data(storage);
106
107   XBT_INFO("Get storage data: '%s'", data);
108
109   MSG_storage_set_data(storage, xbt_strdup("Some user data"));
110   data = MSG_storage_get_data(storage);
111   XBT_INFO("Set and get data: '%s'", data);
112   xbt_free(data);
113   xbt_free(storage_name);
114
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;
120   char* mountname;
121   xbt_dict_t content;
122   char* path;
123   sg_size_t *size;
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));
128     }
129   xbt_dict_free(&content);
130   }
131   xbt_dict_free(&contents);
132   return 1;
133 }
134
135 int main(int argc, char *argv[])
136 {
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);
143
144   msg_error_t res = MSG_main();
145   XBT_INFO("Simulated time: %g", MSG_get_clock());
146
147   return res != MSG_OK;
148 }