Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
69d0713305cac0091527fa6aedafbe677cdc442d
[simgrid.git] / examples / msg / io-storage / io-storage.c
1 /* Copyright (c) 2006-2016. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/msg.h"
7 #include "simgrid/plugins/file_system.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation");
10
11 static int host(int argc, char *argv[]){
12   const char* host_name = MSG_host_get_name(MSG_host_self());
13
14   /* - Display information on the disks mounted by the current host */
15   XBT_INFO("*** Storage info on %s ***", host_name);
16
17   xbt_dict_cursor_t cursor = NULL;
18   char* mount_name;
19   char* storage_name;
20   msg_storage_t storage = NULL;
21
22   /* - Retrieve all mount points of current host */
23   xbt_dict_t storage_list = MSG_host_get_mounted_storage_list(MSG_host_self());
24
25   xbt_dict_foreach(storage_list,cursor,mount_name,storage_name)  {
26     /* - For each disk mounted on host:
27            - Retrieve disk's information */
28     XBT_INFO("Storage name: %s, mount name: %s", storage_name, mount_name);
29     storage = MSG_storage_get_by_name(storage_name);
30
31     sg_size_t free_size = MSG_storage_get_free_size(storage);
32     sg_size_t used_size = MSG_storage_get_used_size(storage);
33     sg_size_t size = MSG_storage_get_size(storage);
34
35     XBT_INFO("Total size: %llu bytes", size);
36     XBT_INFO("Free size: %llu bytes", free_size);
37     XBT_INFO("Used size: %llu bytes", used_size);
38   }
39   xbt_dict_free(&storage_list);
40
41   /* - Create a 200,000 bytes file named './tmp/data.txt' on /sd1 */
42   char* file_name = xbt_strdup("/home/tmp/data.txt");
43
44   /* - Open an non-existing file which amounts to create it. */
45   msg_file_t file = MSG_file_open(file_name, NULL);
46   sg_size_t write = MSG_file_write(file, 200000);  // Write 200,000 bytes
47   XBT_INFO("Create a %llu bytes file named '%s' on /sd1", write, file_name);
48   MSG_file_dump(file);
49
50   /* - Check that sizes have changed */
51   XBT_INFO("Free size: %llu bytes", MSG_storage_get_free_size(storage));
52   XBT_INFO("Used size: %llu bytes", MSG_storage_get_used_size(storage));
53
54   /*  - Retrieve the size of created file and read it completely */
55   sg_size_t file_size = MSG_file_get_size(file);
56   MSG_file_seek(file, 0, SEEK_SET);
57   sg_size_t read = MSG_file_read(file, file_size);
58   XBT_INFO("Read %llu bytes on %s", read, file_name);
59
60   /* - Then write 100,000 bytes in tmp/data.txt */
61   write = MSG_file_write(file, 100000);  // Write 100,000 bytes
62   XBT_INFO("Write %llu bytes on %s", write, file_name);
63   MSG_file_dump(file);
64
65   storage_name = xbt_strdup("Disk4");
66   storage = MSG_storage_get_by_name(storage_name);
67
68   /*  - Move file from ./tmp/data.txt to ./tmp/simgrid.readme */
69   XBT_INFO("*** Move '/tmp/data.txt' into '/tmp/simgrid.readme'");
70   MSG_file_move(file, "/home/tmp/simgrid.readme");
71
72   /* - Attach some user data to the file */
73   MSG_file_set_data(file, xbt_strdup("777"));
74   /* - Then retrieve this data */
75   char *data = MSG_file_get_data(file);
76   XBT_INFO("User data attached to the file: %s", data);
77   xbt_free(data);
78
79   MSG_file_close(file);
80   free(file_name);
81
82   /* - Attach some user data to disk1 */
83   XBT_INFO("*** Get/set data for storage element: %s ***",storage_name);
84
85   data = MSG_storage_get_data(storage);
86
87   XBT_INFO("Get storage data: '%s'", data);
88
89   MSG_storage_set_data(storage, xbt_strdup("Some user data"));
90   data = MSG_storage_get_data(storage);
91   XBT_INFO("Set and get data: '%s'", data);
92   xbt_free(data);
93   xbt_free(storage_name);
94
95   /* - Finally dump disks contents */
96   XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self()));
97   xbt_dict_t contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts
98   xbt_dict_cursor_t curs;
99   xbt_dict_cursor_t curs2 = NULL;
100   char* mountname;
101   xbt_dict_t content;
102   char* path;
103   sg_size_t* psize;
104   xbt_dict_foreach(contents, curs, mountname, content){
105     XBT_INFO("Print the content of mount point: %s",mountname);
106     xbt_dict_foreach (content, curs2, path, psize) {
107       XBT_INFO("%s size: %llu bytes", path, *psize);
108     }
109     xbt_dict_free(&content);
110   }
111   xbt_dict_free(&contents);
112   return 1;
113 }
114
115 int main(int argc, char *argv[])
116 {
117   MSG_init(&argc, argv);
118   MSG_storage_file_system_init();
119
120   MSG_create_environment(argv[1]);
121   MSG_function_register("host", host);
122   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
123   MSG_process_create(NULL, host, NULL, xbt_dynar_get_as(hosts, 3, msg_host_t));
124   xbt_dynar_free(&hosts);
125
126   msg_error_t res = MSG_main();
127   XBT_INFO("Simulated time: %g", MSG_get_clock());
128
129   return res != MSG_OK;
130 }