Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / examples / c / io-disk-raw / io-disk-raw.c
1 /* Copyright (c) 2006-2023. 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_example, "Messages specific for this simulation");
19
20 static void host(int argc, 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_get_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_get_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_get_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_get_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_get_data(disk);
49
50   XBT_INFO("Get storage data: '%s'", data ? data : "No user data");
51
52   sg_disk_set_data(disk, xbt_strdup("Some user data"));
53   data = (char*)sg_disk_get_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 = sg_host_count();
67   sg_host_t* hosts  = sg_host_list();
68
69   for (size_t i = 0; i < host_count; i++) {
70     XBT_INFO("*** %s properties ****", sg_host_get_name(hosts[i]));
71     xbt_dict_t props         = sg_host_get_properties(hosts[i]);
72     xbt_dict_cursor_t cursor = NULL;
73     char* key;
74     void* data;
75     xbt_dict_foreach (props, cursor, key, data)
76       XBT_INFO("  %s -> %s", key, (char*)data);
77     xbt_dict_free(&props);
78   }
79
80   free(hosts);
81
82   sg_actor_create("", sg_host_by_name("bob"), host, 0, NULL);
83
84   simgrid_run();
85
86   XBT_INFO("Simulated time %g", simgrid_get_clock());
87
88   return 0;
89 }