Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
do not use win_storage_content.txt anymore
[simgrid.git] / teshsuite / msg / io-file / io-file.c
1 /* Copyright (c) 2008-2019. 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 #include <stdio.h> /* SEEK_SET */
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(io_file, "Messages specific for this io example");
12
13 static int host(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[])
14 {
15   msg_file_t file = NULL;
16   sg_size_t read;
17   sg_size_t write;
18   msg_storage_t st;
19   const char* st_name;
20
21   switch (MSG_process_self_PID()) {
22     case 1:
23       file    = MSG_file_open("/tmp/include/surf/simgrid_dtd.h", NULL);
24       st_name = "Disk2";
25       break;
26     case 2:
27       file    = MSG_file_open("/home/doc/simgrid/examples/platforms/nancy.xml", NULL);
28       st_name = "Disk1";
29       break;
30     case 3:
31       file    = MSG_file_open("/home/doc/simgrid/examples/platforms/g5k_cabinets.xml", NULL);
32       st_name = "Disk3";
33       break;
34     case 4:
35       file = MSG_file_open("/home/doc/simgrid/examples/platforms/g5k.xml", NULL);
36       MSG_file_dump(file);
37       st_name = "Disk4";
38       break;
39     default:
40       xbt_die("FILENAME NOT DEFINED %s", MSG_process_get_name(MSG_process_self()));
41   }
42
43   const char* filename = MSG_file_get_name(file);
44   XBT_INFO("\tOpen file '%s'", filename);
45   st = MSG_storage_get_by_name(st_name);
46
47   XBT_INFO("\tCapacity of the storage element '%s' is stored on: %llu / %llu", filename, MSG_storage_get_used_size(st),
48            MSG_storage_get_size(st));
49
50   /* Try to read for 10MB */
51   read = MSG_file_read(file, 10000000);
52   XBT_INFO("\tHave read %llu from '%s'", read, filename);
53
54   /* Write 100KB in file from the current position, i.e, end of file or 10MB */
55   write = MSG_file_write(file, 100000);
56   XBT_INFO("\tHave written %llu in '%s'. Size now is: %llu", write, filename, MSG_file_get_size(file));
57
58   XBT_INFO("\tCapacity of the storage element '%s' is stored on: %llu / %llu", filename, MSG_storage_get_used_size(st),
59            MSG_storage_get_size(st));
60
61   /* rewind to the beginning of the file */
62   XBT_INFO("\tComing back to the beginning of the stream for file '%s'", filename);
63   MSG_file_seek(file, 0, SEEK_SET);
64
65   /* Try to read 110KB */
66   read = MSG_file_read(file, 110000);
67   XBT_INFO("\tHave read %llu from '%s' (of size %llu)", read, filename, MSG_file_get_size(file));
68
69   /* rewind once again to the beginning of the file */
70   XBT_INFO("\tComing back to the beginning of the stream for file '%s'", filename);
71   MSG_file_seek(file, 0, SEEK_SET);
72
73   /* Write 110KB in file from the current position, i.e, end of file or 10MB */
74   write = MSG_file_write(file, 110000);
75   XBT_INFO("\tHave written %llu in '%s'. Size now is: %llu", write, filename, MSG_file_get_size(file));
76
77   XBT_INFO("\tCapacity of the storage element '%s' is stored on: %llu / %llu", filename, MSG_storage_get_used_size(st),
78            MSG_storage_get_size(st));
79
80   if (MSG_process_self_PID() == 1) {
81     XBT_INFO("\tUnlink file '%s'", MSG_file_get_name(file));
82     MSG_file_unlink(file);
83   } else {
84     XBT_INFO("\tClose file '%s'", filename);
85     MSG_file_close(file);
86   }
87   return 0;
88 }
89
90 int main(int argc, char** argv)
91 {
92   MSG_init(&argc, argv);
93   MSG_storage_file_system_init();
94
95   MSG_create_environment(argv[1]);
96   xbt_dynar_t hosts = MSG_hosts_as_dynar();
97   MSG_function_register("host", host);
98   unsigned long nb_hosts = xbt_dynar_length(hosts);
99   XBT_INFO("Number of host '%lu'", nb_hosts);
100   for (int i = 0; i < nb_hosts; i++) {
101     MSG_process_create("host", host, NULL, xbt_dynar_get_as(hosts, i, msg_host_t));
102   }
103   xbt_dynar_free(&hosts);
104
105   int res = MSG_main();
106   XBT_INFO("Simulation time %.6f", MSG_get_clock());
107   return res != MSG_OK;
108 }