Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e2778d0e55d5631a480b665ef8959d0459becf78
[simgrid.git] / examples / msg / io / storage.c
1 /* Copyright (c) 2006-2014. 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 /********************* Files and Storage handling ****************************
8  * This example implements all main storage and file functions of the MSG API
9  *
10  * Scenario :
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
18  *
19 ******************************************************************************/
20
21 #include "msg/msg.h"
22 #include "xbt/log.h"
23 #include "xbt/dict.h"
24
25 XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation");
26
27 static int host(int argc, char *argv[]){
28   const char* host_name = MSG_host_get_name(MSG_host_self());
29
30   // display information on the disks mounted by the current host
31   XBT_INFO("*** Storage info on %s ***", host_name);
32
33   xbt_dict_cursor_t cursor = NULL;
34   char* mount_name;
35   char* storage_name;
36   msg_storage_t storage;
37
38   // Retrieve all mount points of current host
39   xbt_dict_t storage_list = MSG_host_get_storage_list(MSG_host_self());
40
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);
45
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);
50
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);
54   }
55   xbt_dict_free(&storage_list);
56
57
58   // Create a 200,000 bytes file named './tmp/data.txt' on /sd1
59
60   char* mount = xbt_strdup("/home");
61   char* file_name = xbt_strdup("./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(mount, 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("/home"));
73   XBT_INFO("Used size: %llu bytes", MSG_storage_get_used_size("/home"));
74
75
76   // Now retrieve the size of created file and read it completely
77   file_size = MSG_file_get_size(file);
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   MSG_file_close(file);
87   free(mount);
88   free(file_name);
89
90   storage_name = xbt_strdup("Disk4");
91   storage = MSG_storage_get_by_name(storage_name);
92
93   // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme
94   XBT_INFO("*** Renaming './tmp/data.txt' into './tmp/simgrid.readme'");
95   MSG_storage_file_rename(storage, "./tmp/data.txt", "./tmp/simgrid.readme");
96
97   // Now attach some user data to disk1
98   XBT_INFO("*** Get/set data for storage element: %s ***",storage_name);
99
100   char *data = MSG_storage_get_data(storage);
101
102   XBT_INFO("Get data: '%s'", data);
103
104   MSG_storage_set_data(storage,strdup("Some user data"));
105   data = MSG_storage_get_data(storage);
106   XBT_INFO("Set and get data: '%s'", data);
107   xbt_free(storage_name);
108
109
110   // Dump disks contents
111   XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self()));
112   xbt_dict_t contents = NULL;
113   contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts
114   xbt_dict_cursor_t curs, curs2 = NULL;
115   char* mountname;
116   xbt_dict_t content;
117   char* path;
118   sg_size_t *size;
119   xbt_dict_foreach(contents, curs, mountname, content){
120     XBT_INFO("Print the content of mount point: %s",mountname);
121     xbt_dict_foreach(content,curs2,path,size){
122        XBT_INFO("%s size: %llu bytes", path,*((sg_size_t*)size));
123     }
124   xbt_dict_free(&content);
125   }
126   xbt_dict_free(&contents);
127   return 1;
128 }
129
130
131 int main(int argc, char *argv[])
132 {
133
134   MSG_init(&argc, argv);
135   MSG_create_environment(argv[1]);
136   MSG_function_register("host", host);
137   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
138   MSG_process_create(NULL, host, NULL, xbt_dynar_get_as(hosts,0,msg_host_t) );
139   xbt_dynar_free(&hosts);
140
141   msg_error_t res = MSG_main();
142   XBT_INFO("Simulated time: %g", MSG_get_clock());
143
144   if (res == MSG_OK)
145     return 0;
146   else
147     return 1;
148 }