Logo AND Algorithmique Numérique Distribuée

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