Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
yet another cleaning pass
[simgrid.git] / examples / msg / io / file.c
1 /* Copyright (c) 2008-2010, 2012-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /** @addtogroup MSG_examples
8  * 
9  * @subsection MSG_ex_resources Other resource kinds
10  * 
11  * This section contains some sparse examples of how to use the other
12  * kind of resources, such as disk or GPU. These resources are quite
13  * experimental for now, but here we go anyway.
14  * 
15  * - <b>io/file.c</b> Example with the disk resource
16  */
17
18 #define FILENAME1 "/home/doc/simgrid/examples/platforms/g5k.xml"
19 #define FILENAME2 "c:\\Windows\\setupact.log"
20 #define FILENAME3 "/home/doc/simgrid/examples/platforms/g5k_cabinets.xml"
21 #define FILENAME4 "/home/doc/simgrid/examples/platforms/nancy.xml"
22
23 #include "simgrid/msg.h"
24
25 XBT_LOG_NEW_DEFAULT_CATEGORY(io_file, "Messages specific for this io example");
26
27 static int host(int argc, char *argv[])
28 {
29   msg_file_t file = NULL;
30   sg_size_t read,write;
31   msg_storage_t st;
32   const char* st_name;
33
34   if(!strcmp(MSG_process_get_name(MSG_process_self()),"0")){
35     file = MSG_file_open(FILENAME1, NULL);
36     MSG_file_dump(file);
37     st_name = "Disk4";
38   } else if(!strcmp(MSG_process_get_name(MSG_process_self()),"1")) {
39     file = MSG_file_open(FILENAME2, NULL);
40     st_name = "Disk2";
41   } else if(!strcmp(MSG_process_get_name(MSG_process_self()),"2")){
42     file = MSG_file_open(FILENAME3, NULL);
43     st_name = "Disk3";
44   } else if(!strcmp(MSG_process_get_name(MSG_process_self()),"3")){
45     file = MSG_file_open(FILENAME4, NULL);
46     st_name = "Disk1";
47   }
48   else xbt_die("FILENAME NOT DEFINED %s",MSG_process_get_name(MSG_process_self()));
49
50   const char* filename = MSG_file_get_name(file);
51   XBT_INFO("\tOpen file '%s'",filename);
52   st = MSG_storage_get_by_name(st_name);
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   /* Try to read for 10MB */
58   read = MSG_file_read(file, 10000000);
59   XBT_INFO("\tHave read %llu from '%s'",read,filename);
60
61   /* Write 100KB in file from the current position, i.e, end of file or 10MB */
62   write = MSG_file_write(file, 100000);
63   XBT_INFO("\tHave written %llu in '%s'. Size now is: %llu",write,filename, MSG_file_get_size(file));
64
65
66   XBT_INFO("\tCapacity of the storage element '%s' is stored on: %llu / %llu",
67             filename, MSG_storage_get_used_size(st), MSG_storage_get_size(st));
68
69   /* rewind 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   /* Try to read 110KB */
74   read = MSG_file_read(file, 110000);
75   XBT_INFO("\tHave read %llu from '%s' (of size %llu)",read,filename, MSG_file_get_size(file));
76
77   /* rewind once again to the beginning of the file */
78   XBT_INFO("\tComing back to the beginning of the stream for file '%s'", filename);
79   MSG_file_seek(file, 0, SEEK_SET);
80
81   /* Write 110KB in file from the current position, i.e, end of file or 10MB */
82   write = MSG_file_write(file, 110000);
83   XBT_INFO("\tHave written %llu in '%s'. Size now is: %llu", write,filename, MSG_file_get_size(file));
84
85   XBT_INFO("\tCapacity of the storage element '%s' is stored on: %llu / %llu",
86             filename, MSG_storage_get_used_size(st), MSG_storage_get_size(st));
87
88   XBT_INFO("\tClose file '%s'",filename);
89   MSG_file_close(file);
90
91   return 0;
92 }
93
94 int main(int argc, char **argv)
95 {
96   MSG_init(&argc, argv);
97   MSG_create_environment(argv[1]);
98   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
99   MSG_function_register("host", host);
100   unsigned long nb_hosts = xbt_dynar_length(hosts);
101   XBT_INFO("Number of host '%lu'",nb_hosts);
102   for(int i = 0 ; i<nb_hosts; i++){
103     char* name_host = bprintf("%d",i);
104     MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,i,msg_host_t) );
105     free(name_host);
106   }
107   xbt_dynar_free(&hosts);
108
109   int res = MSG_main();
110   XBT_INFO("Simulation time %g", MSG_get_clock());
111   return res != MSG_OK;
112 }