Logo AND Algorithmique Numérique Distribuée

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