Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Start to get rid of storage examples in MSG
[simgrid.git] / examples / c / io-disk-raw / io-disk-raw.c
1 /* Copyright (c) 2006-2020. 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/actor.h"
7 #include "simgrid/disk.h"
8 #include "simgrid/engine.h"
9 #include "simgrid/forward.h"
10 #include "simgrid/host.h"
11 #include "xbt/dict.h"
12 #include "xbt/log.h"
13 #include "xbt/sysdep.h"
14
15 #include <stddef.h>
16 #include <stdlib.h>
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(disk, "Messages specific for this simulation");
19
20 static void host(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
21 {
22   const char* host_name = sg_host_get_name(sg_host_self());
23
24   /* - Display information on the disks mounted by the current host */
25   XBT_INFO("*** Storage info on %s ***", host_name);
26
27   /* - Retrieve all disks from current host */
28   unsigned int disk_count;
29   sg_disk_t* disk_list;
30   sg_host_disks(sg_host_self(), &disk_count, &disk_list);
31
32   for (unsigned int i = 0; i < disk_count; i++)
33     XBT_INFO("Disk name: %s (read: %.0f B/s -- write: %.0f B/s ", sg_disk_name(disk_list[i]),
34              sg_disk_read_bandwidth(disk_list[i]), sg_disk_write_bandwidth(disk_list[i]));
35
36   /* - Write 400,000 bytes on Disk1 */
37   sg_disk_t disk  = disk_list[0];
38   sg_size_t write = sg_disk_write(disk, 400000);
39   XBT_INFO("Wrote %llu bytes on '%s'", write, sg_disk_name(disk));
40
41   /*  - Now read 200,000 bytes */
42   sg_size_t read = sg_disk_read(disk, 200000);
43   XBT_INFO("Read %llu bytes on '%s'", read, sg_disk_name(disk));
44
45   /* - Attach some user data to disk1 */
46   XBT_INFO("*** Get/set data for storage element: Disk1 ***");
47
48   char* data = (char*)sg_disk_data(disk);
49
50   XBT_INFO("Get storage data: '%s'", data ? data : "No user data");
51
52   sg_disk_data_set(disk, xbt_strdup("Some user data"));
53   data = (char*)sg_disk_data(disk);
54   XBT_INFO("Set and get data: '%s'", data);
55   free(data);
56   free(disk_list);
57 }
58
59 int main(int argc, char* argv[])
60 {
61   simgrid_init(&argc, argv);
62   simgrid_load_platform(argv[1]);
63
64   simgrid_register_function("host", host);
65
66   size_t host_count;
67   sg_host_t* hosts;
68   simgrid_get_all_hosts(&host_count, &hosts);
69
70   for (long i = 0; i < host_count; i++) {
71     XBT_INFO("*** %s properties ****", sg_host_get_name(hosts[i]));
72     xbt_dict_t props         = sg_host_get_properties(hosts[i]);
73     xbt_dict_cursor_t cursor = NULL;
74     char* key;
75     void* data;
76     xbt_dict_foreach (props, cursor, key, data)
77       XBT_INFO("  %s -> %s", key, (char*)data);
78     xbt_dict_free(&props);
79   }
80
81   free(hosts);
82
83   sg_actor_t actor = sg_actor_init("", sg_host_by_name("bob"));
84   sg_actor_start(actor, host, 0, NULL);
85
86   simgrid_run();
87
88   XBT_INFO("Simulated time %g", simgrid_get_clock());
89
90   return 0;
91 }