Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mv msg/msg.h simgrid/msg.h
[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 /** @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 #include "xbt/log.h"
28 #include "xbt/dict.h"
29
30 XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation");
31
32 static int host(int argc, char *argv[]){
33   const char* host_name = MSG_host_get_name(MSG_host_self());
34
35   // display information on the disks mounted by the current host
36   XBT_INFO("*** Storage info on %s ***", host_name);
37
38   xbt_dict_cursor_t cursor = NULL;
39   char* mount_name;
40   char* storage_name;
41   msg_storage_t storage = NULL;
42
43   // Retrieve all mount points of current host
44   xbt_dict_t storage_list = MSG_host_get_mounted_storage_list(MSG_host_self());
45
46   xbt_dict_foreach(storage_list,cursor,mount_name,storage_name)  {
47     // For each disk mounted on host
48     XBT_INFO("Storage name: %s, mount name: %s", storage_name, mount_name);
49     storage = MSG_storage_get_by_name(storage_name);
50
51     // Retrieve disk's information
52     sg_size_t free_size = MSG_storage_get_free_size(storage);
53     sg_size_t used_size = MSG_storage_get_used_size(storage);
54     sg_size_t size = MSG_storage_get_size(storage);
55
56     XBT_INFO("Total size: %llu bytes", size);
57     XBT_INFO("Free size: %llu bytes", free_size);
58     XBT_INFO("Used size: %llu bytes", used_size);
59   }
60   xbt_dict_free(&storage_list);
61
62
63   // Create a 200,000 bytes file named './tmp/data.txt' on /sd1
64   char* file_name = xbt_strdup("/home/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(file_name, NULL);
70   write = MSG_file_write(file, 200000);  // Write 200,000 bytes
71   XBT_INFO("Create a %llu 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: %llu bytes", MSG_storage_get_free_size(storage));
76   XBT_INFO("Used size: %llu bytes", MSG_storage_get_used_size(storage));
77
78
79   // Now retrieve the size of created file and read it completely
80   file_size = MSG_file_get_size(file);
81   MSG_file_seek(file, 0, SEEK_SET);
82   read = MSG_file_read(file, file_size);
83   XBT_INFO("Read %llu bytes on %s", read, file_name);
84
85   // Now write 100,000 bytes in tmp/data.txt
86   write = MSG_file_write(file, 100000);  // Write 100,000 bytes
87   XBT_INFO("Write %llu bytes on %s", write, file_name);
88   MSG_file_dump(file);
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("*** Move '/tmp/data.txt' into '/tmp/simgrid.readme'");
95   MSG_file_move(file, "/home/tmp/simgrid.readme");
96
97   // Attach some user data to the file
98   MSG_file_set_data(file, xbt_strdup("777"));
99   // Retrieve these data
100   char *data = MSG_file_get_data(file);
101   XBT_INFO("User data attached to the file: %s", data);
102
103   MSG_file_close(file);
104   free(file_name);
105
106   // Now attach some user data to disk1
107   XBT_INFO("*** Get/set data for storage element: %s ***",storage_name);
108
109   data = MSG_storage_get_data(storage);
110
111   XBT_INFO("Get storage data: '%s'", data);
112
113   MSG_storage_set_data(storage, xbt_strdup("Some user data"));
114   data = MSG_storage_get_data(storage);
115   XBT_INFO("Set and get data: '%s'", data);
116   xbt_free(data);
117   xbt_free(storage_name);
118
119
120   // Dump disks contents
121   XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self()));
122   xbt_dict_t contents = NULL;
123   contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts
124   xbt_dict_cursor_t curs, curs2 = NULL;
125   char* mountname;
126   xbt_dict_t content;
127   char* path;
128   sg_size_t *size;
129   xbt_dict_foreach(contents, curs, mountname, content){
130     XBT_INFO("Print the content of mount point: %s",mountname);
131     xbt_dict_foreach(content,curs2,path,size){
132        XBT_INFO("%s size: %llu bytes", path,*((sg_size_t*)size));
133     }
134   xbt_dict_free(&content);
135   }
136   xbt_dict_free(&contents);
137   return 1;
138 }
139
140
141 int main(int argc, char *argv[])
142 {
143
144   MSG_init(&argc, argv);
145   MSG_create_environment(argv[1]);
146   MSG_function_register("host", host);
147   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
148   MSG_process_create(NULL, host, NULL, xbt_dynar_get_as(hosts,0,msg_host_t) );
149   xbt_dynar_free(&hosts);
150
151   msg_error_t res = MSG_main();
152   XBT_INFO("Simulated time: %g", MSG_get_clock());
153
154   if (res == MSG_OK)
155     return 0;
156   else
157     return 1;
158 }