Logo AND Algorithmique Numérique Distribuée

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