Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7d220798148d0824cf7f77a32f4f06e765fce12c
[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   xbt_dict_free(&storage_list);
32
33   /* - Write 200,000 bytes on Disk4 */
34   storage = MSG_storage_get_by_name("Disk4");
35
36   /* - Open an non-existing file which amounts to create it. */
37   sg_size_t write = MSG_storage_write(storage, 200000); // Write 200,000 bytes
38   XBT_INFO("Wrote %llu bytes on 'Disk4'", write);
39
40   /*  - Retrieve the size of created file and read it completely */
41   sg_size_t read = MSG_storage_read(storage, 200000);
42   XBT_INFO("Read %llu bytes on 'Disk4'", read);
43
44   /* - Attach some user data to disk1 */
45   XBT_INFO("*** Get/set data for storage element: Disk4 ***");
46
47   char* data = MSG_storage_get_data(storage);
48
49   XBT_INFO("Get storage data: '%s'", data);
50
51   MSG_storage_set_data(storage, xbt_strdup("Some user data"));
52   data = MSG_storage_get_data(storage);
53   XBT_INFO("Set and get data: '%s'", data);
54   xbt_free(data);
55
56   return 1;
57 }
58
59 int main(int argc, char *argv[])
60 {
61   MSG_init(&argc, argv);
62
63   MSG_create_environment(argv[1]);
64   MSG_function_register("host", host);
65   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
66   MSG_process_create(NULL, host, NULL, xbt_dynar_get_as(hosts, 3, msg_host_t));
67   xbt_dynar_free(&hosts);
68
69   msg_error_t res = MSG_main();
70   XBT_INFO("Simulated time: %g", MSG_get_clock());
71
72   return res != MSG_OK;
73 }