Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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
20   /* - Retrieve all mount points of current host */
21   xbt_dict_t storage_list = MSG_host_get_mounted_storage_list(MSG_host_self());
22
23   /* - For each disk mounted on host, display disk name and mount point */
24   xbt_dict_foreach (storage_list, cursor, mount_name, storage_name)
25     XBT_INFO("Storage name: %s, mount name: %s", storage_name, mount_name);
26
27   xbt_dict_free(&storage_list);
28
29   /* - Write 200,000 bytes on Disk4 */
30   msg_storage_t storage = MSG_storage_get_by_name("Disk4");
31   sg_size_t write = MSG_storage_write(storage, 200000); // Write 200,000 bytes
32   XBT_INFO("Wrote %llu bytes on 'Disk4'", write);
33
34   /*  - Now read 200,000 bytes */
35   sg_size_t read = MSG_storage_read(storage, 200000);
36   XBT_INFO("Read %llu bytes on 'Disk4'", read);
37
38   /* - Attach some user data to disk1 */
39   XBT_INFO("*** Get/set data for storage element: Disk4 ***");
40
41   char* data = MSG_storage_get_data(storage);
42
43   XBT_INFO("Get storage data: '%s'", data);
44
45   MSG_storage_set_data(storage, xbt_strdup("Some user data"));
46   data = MSG_storage_get_data(storage);
47   XBT_INFO("Set and get data: '%s'", data);
48   xbt_free(data);
49
50   return 1;
51 }
52
53 int main(int argc, char *argv[])
54 {
55   MSG_init(&argc, argv);
56
57   MSG_create_environment(argv[1]);
58   MSG_function_register("host", host);
59   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
60   MSG_process_create(NULL, host, NULL, xbt_dynar_get_as(hosts, 3, msg_host_t));
61   xbt_dynar_free(&hosts);
62
63   msg_error_t res = MSG_main();
64   XBT_INFO("Simulated time: %g", MSG_get_clock());
65
66   return res != MSG_OK;
67 }