Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update MSG_storage_file_rename
[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
24  /* To use PRIu64 format specifier for printing uint64_t (sg_storage_size_t) */
25 #include <inttypes.h>
26
27 XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation");
28
29 int host(int argc, char *argv[]){
30   const char* host_name = MSG_host_get_name(MSG_host_self());
31
32   // display information on the disks mounted by the current host
33   XBT_INFO("*** Storage info on %s ***", host_name);
34
35   xbt_dict_cursor_t cursor = NULL;
36   char* mount_name;
37   char* storage_name;
38   msg_storage_t storage;
39
40   // Retrieve all mount points of current host
41   xbt_dict_t storage_list = MSG_host_get_storage_list(MSG_host_self());
42
43   xbt_dict_foreach(storage_list,cursor,mount_name,storage_name)  {
44     // For each disk mounted on host
45
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_storage_size_t free_size = MSG_storage_get_free_size(mount_name);
51         sg_storage_size_t used_size = MSG_storage_get_used_size(mount_name);
52         sg_storage_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_free(storage_list);
59
60
61   // Create a 200,000 bytes file named './tmp/data.txt' on /sd1
62
63   char* mount = xbt_strdup("/sd1");
64   char* file_name = xbt_strdup("./tmp/data.txt");
65   msg_file_t file = NULL;
66   sg_storage_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("Total size: %"PRIu64" bytes", MSG_storage_get_free_size("/sd1"));
76   XBT_INFO("Free size: %"PRIu64" bytes", MSG_storage_get_used_size("/sd1"));
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   // Now rename file from /tmp/data.txt to /tmp/simgrid.readme
94   msg_storage_t st = MSG_storage_get_by_name("/sd1");
95   MSG_storage_file_rename(st, "/tmp/data.txt", "/tmp/simgrid.readme");
96
97   // Now attach some user data to disk1
98   storage_name = xbt_strdup("disk1");
99   XBT_INFO("*** Get/set data for storage element: %s ***",storage_name);
100
101   storage = MSG_storage_get_by_name(storage_name);
102   char *data = MSG_storage_get_data(storage);
103
104   XBT_INFO("Get data: '%s'", data);
105
106   MSG_storage_set_data(storage,strdup("Some user data"));
107   data = MSG_storage_get_data(storage);
108   XBT_INFO("Set and get data: '%s'", data);
109   free(storage_name);
110
111
112   // Dump disks contents
113   XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self()));
114   xbt_dict_t contents = NULL;
115   contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts
116   xbt_dict_cursor_t curs, curs2 = NULL;
117   char* mountname;
118   xbt_dict_t content;
119   char* path;
120   sg_storage_size_t *size;
121   xbt_dict_foreach(contents, curs, mountname, content){
122         XBT_INFO("Print the content of mount point: %s",mountname);
123     xbt_dict_foreach(content,curs2,path,size){
124           XBT_INFO("%s size: %"PRIu64" bytes", path,*((sg_storage_size_t*)size));
125     }
126   }
127
128   return 1;
129 }
130
131
132 int main(int argc, char *argv[])
133 {
134
135   MSG_init(&argc, argv);
136   /* Check the arguments */
137   if (argc < 3) {
138     printf("Usage: %s platform_file deployment_file \n", argv[0]);
139     return -1;
140   }
141
142   const char *platform_file = argv[1];
143   const char *deployment_file = argv[2];
144
145   MSG_create_environment(platform_file);
146   MSG_function_register("host", host);
147   MSG_launch_application(deployment_file);
148
149   msg_error_t res = MSG_main();
150   XBT_INFO("Simulated time: %g", MSG_get_clock());
151
152   if (res == MSG_OK)
153     return 0;
154   else
155     return 1;
156 }