Logo AND Algorithmique Numérique Distribuée

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