1 /* Copyright (c) 2006-2016. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/msg.h"
8 XBT_LOG_NEW_DEFAULT_CATEGORY(storage,"Messages specific for this simulation");
10 static int host(int argc, char *argv[]){
11 const char* host_name = MSG_host_get_name(MSG_host_self());
13 /* - Display information on the disks mounted by the current host */
14 XBT_INFO("*** Storage info on %s ***", host_name);
16 xbt_dict_cursor_t cursor = NULL;
19 msg_storage_t storage = NULL;
21 /* - Retrieve all mount points of current host */
22 xbt_dict_t storage_list = MSG_host_get_mounted_storage_list(MSG_host_self());
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);
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);
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);
38 xbt_dict_free(&storage_list);
40 /* - Create a 200,000 bytes file named './tmp/data.txt' on /sd1 */
41 char* file_name = xbt_strdup("/home/tmp/data.txt");
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);
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));
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);
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);
64 storage_name = xbt_strdup("Disk4");
65 storage = MSG_storage_get_by_name(storage_name);
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");
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);
81 /* - Attach some user data to disk1 */
82 XBT_INFO("*** Get/set data for storage element: %s ***",storage_name);
84 data = MSG_storage_get_data(storage);
86 XBT_INFO("Get storage data: '%s'", data);
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);
92 xbt_free(storage_name);
94 /* - Finally dump disks contents */
95 XBT_INFO("*** Dump content of %s ***",MSG_host_get_name(MSG_host_self()));
96 xbt_dict_t contents = NULL;
97 contents = MSG_host_get_storage_content(MSG_host_self()); // contents is a dict of dicts
98 xbt_dict_cursor_t curs;
99 xbt_dict_cursor_t curs2 = NULL;
104 xbt_dict_foreach(contents, curs, mountname, content){
105 XBT_INFO("Print the content of mount point: %s",mountname);
106 xbt_dict_foreach(content,curs2,path,size){
107 XBT_INFO("%s size: %llu bytes", path, (sg_size_t)size);
109 xbt_dict_free(&content);
111 xbt_dict_free(&contents);
115 int main(int argc, char *argv[])
117 MSG_init(&argc, argv);
118 MSG_create_environment(argv[1]);
119 MSG_function_register("host", host);
120 xbt_dynar_t hosts = MSG_hosts_as_dynar();
121 MSG_process_create(NULL, host, NULL, xbt_dynar_get_as(hosts, 3, msg_host_t));
122 xbt_dynar_free(&hosts);
124 msg_error_t res = MSG_main();
125 XBT_INFO("Simulated time: %g", MSG_get_clock());
127 return res != MSG_OK;