Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'mc++'
[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_mounted_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   char* file_name = xbt_strdup("/home/tmp/data.txt");
60   msg_file_t file = NULL;
61   sg_size_t write, read, file_size;
62
63   // Open an non-existing file amounts to create it!
64   file = MSG_file_open(file_name, NULL);
65   write = MSG_file_write(file, 200000);  // Write 200,000 bytes
66   XBT_INFO("Create a %llu bytes file named '%s' on /sd1", write, file_name);
67   MSG_file_dump(file);
68
69   // check that sizes have changed
70   XBT_INFO("Free size: %llu bytes", MSG_storage_get_free_size("/home"));
71   XBT_INFO("Used size: %llu bytes", MSG_storage_get_used_size("/home"));
72
73
74   // Now retrieve the size of created file and read it completely
75   file_size = MSG_file_get_size(file);
76   read = MSG_file_read(file, file_size);
77   XBT_INFO("Read %llu bytes on %s", read, file_name);
78
79   // Now write 100,000 bytes in tmp/data.txt
80   write = MSG_file_write(file, 100000);  // Write 100,000 bytes
81   XBT_INFO("Write %llu bytes on %s", write, file_name);
82   MSG_file_dump(file);
83
84   storage_name = xbt_strdup("Disk4");
85   storage = MSG_storage_get_by_name(storage_name);
86
87   // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme
88   XBT_INFO("*** Move '/tmp/data.txt' into '/tmp/simgrid.readme'");
89   MSG_file_move(file, "/home/tmp/simgrid.readme");
90
91   MSG_file_close(file);
92   free(file_name);
93
94   // Now attach some user data to disk1
95   XBT_INFO("*** Get/set data for storage element: %s ***",storage_name);
96
97   char *data = MSG_storage_get_data(storage);
98
99   XBT_INFO("Get data: '%s'", data);
100
101   MSG_storage_set_data(storage, xbt_strdup("Some user data"));
102   data = MSG_storage_get_data(storage);
103   XBT_INFO("Set and get data: '%s'", data);
104   xbt_free(data);
105   xbt_free(storage_name);
106
107
108   // Dump disks contents
109   XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self()));
110   xbt_dict_t contents = NULL;
111   contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts
112   xbt_dict_cursor_t curs, curs2 = NULL;
113   char* mountname;
114   xbt_dict_t content;
115   char* path;
116   sg_size_t *size;
117   xbt_dict_foreach(contents, curs, mountname, content){
118     XBT_INFO("Print the content of mount point: %s",mountname);
119     xbt_dict_foreach(content,curs2,path,size){
120        XBT_INFO("%s size: %llu bytes", path,*((sg_size_t*)size));
121     }
122   xbt_dict_free(&content);
123   }
124   xbt_dict_free(&contents);
125   return 1;
126 }
127
128
129 int main(int argc, char *argv[])
130 {
131
132   MSG_init(&argc, argv);
133   MSG_create_environment(argv[1]);
134   MSG_function_register("host", host);
135   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
136   MSG_process_create(NULL, host, NULL, xbt_dynar_get_as(hosts,0,msg_host_t) );
137   xbt_dynar_free(&hosts);
138
139   msg_error_t res = MSG_main();
140   XBT_INFO("Simulated time: %g", MSG_get_clock());
141
142   if (res == MSG_OK)
143     return 0;
144   else
145     return 1;
146 }