Logo AND Algorithmique Numérique Distribuée

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