Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2b8eafe5f53f5af4ff92eb9ce3062cfe3b0cce5f
[simgrid.git] / examples / msg / io / storage.c
1 /* Copyright (c) 2006-2013. 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 more 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  /* To use PRIu64 format specifier for printing uint64_t (sg_size_t) */
26 #include <inttypes.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;
40
41   // Retrieve all mount points of current host
42   xbt_dict_t storage_list = MSG_host_get_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(mount_name);
51     sg_size_t used_size = MSG_storage_get_used_size(mount_name);
52     sg_size_t size = MSG_storage_get_size(storage);
53
54     XBT_INFO("Total size: %"PRIu64" bytes", size);
55     XBT_INFO("Free size: %"PRIu64" bytes", free_size);
56     XBT_INFO("Used size: %"PRIu64" bytes", used_size);
57   }
58   xbt_dict_free(&storage_list);
59
60
61   // Create a 200,000 bytes file named './tmp/data.txt' on /sd1
62
63   char* mount = xbt_strdup("/home");
64   char* file_name = xbt_strdup("./tmp/data.txt");
65   msg_file_t file = NULL;
66   sg_size_t write, read, file_size;
67
68   // Open an non-existing file amounts to create it!
69   file = MSG_file_open(mount, file_name, NULL);
70   write = MSG_file_write(file, 200000);  // Write 200,000 bytes
71   XBT_INFO("Create a %"PRIu64" bytes file named '%s' on /sd1", write, file_name);
72   MSG_file_dump(file);
73
74   // check that sizes have changed
75   XBT_INFO("Free size: %"PRIu64" bytes", MSG_storage_get_free_size("/home"));
76   XBT_INFO("Used size: %"PRIu64" bytes", MSG_storage_get_used_size("/home"));
77
78
79   // Now retrieve the size of created file and read it completely
80   file_size = MSG_file_get_size(file);
81   read = MSG_file_read(file, file_size);
82   XBT_INFO("Read %"PRIu64" bytes on %s", read, file_name);
83
84   // Now write 100,000 more bytes in tmp/data.txt
85   write = MSG_file_write(file, 100000);  // Write 100,000 bytes
86   XBT_INFO("Write %"PRIu64" more bytes on %s", write, file_name);
87   MSG_file_dump(file);
88
89   MSG_file_close(file);
90   free(mount);
91   free(file_name);
92
93   storage_name = xbt_strdup("Disk4");
94   storage = MSG_storage_get_by_name(storage_name);
95
96   // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme
97   XBT_INFO("*** Renaming './tmp/data.txt' into './tmp/simgrid.readme'");
98   MSG_storage_file_rename(storage, "./tmp/data.txt", "./tmp/simgrid.readme");
99
100   // Now attach some user data to disk1
101   XBT_INFO("*** Get/set data for storage element: %s ***",storage_name);
102
103   char *data = MSG_storage_get_data(storage);
104
105   XBT_INFO("Get data: '%s'", data);
106
107   MSG_storage_set_data(storage,strdup("Some user data"));
108   data = MSG_storage_get_data(storage);
109   XBT_INFO("Set and get data: '%s'", data);
110   xbt_free(storage_name);
111
112
113   // Dump disks contents
114   XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self()));
115   xbt_dict_t contents = NULL;
116   contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts
117   xbt_dict_cursor_t curs, curs2 = NULL;
118   char* mountname;
119   xbt_dict_t content;
120   char* path;
121   sg_size_t *size;
122   xbt_dict_foreach(contents, curs, mountname, content){
123     XBT_INFO("Print the content of mount point: %s",mountname);
124     xbt_dict_foreach(content,curs2,path,size){
125        XBT_INFO("%s size: %"PRIu64" bytes", path,*((sg_size_t*)size));
126     }
127   xbt_dict_free(&content);
128   }
129   xbt_dict_free(&contents);
130   return 1;
131 }
132
133
134 int main(int argc, char *argv[])
135 {
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   if (res == MSG_OK)
148     return 0;
149   else
150     return 1;
151 }