Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
coverage madness in examples
[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 <stdio.h>
24 #include <stdlib.h>
25 #include "simgrid/msg.h"
26
27 int host(int argc, char *argv[]);
28
29 XBT_LOG_NEW_DEFAULT_CATEGORY(io_file,
30                              "Messages specific for this io example");
31
32 int host(int argc, char *argv[])
33 {
34   msg_file_t file = NULL;
35   sg_size_t read,write;
36   msg_storage_t st;
37   const char* st_name;
38
39   if(!strcmp(MSG_process_get_name(MSG_process_self()),"0")){
40     file = MSG_file_open(FILENAME1, NULL);
41     MSG_file_dump(file);
42     st_name = "Disk4";
43   } else if(!strcmp(MSG_process_get_name(MSG_process_self()),"1")) {
44     file = MSG_file_open(FILENAME2, NULL);
45     st_name = "Disk2";
46   } else if(!strcmp(MSG_process_get_name(MSG_process_self()),"2")){
47     file = MSG_file_open(FILENAME3, NULL);
48     st_name = "Disk3";
49   } else if(!strcmp(MSG_process_get_name(MSG_process_self()),"3")){
50     file = MSG_file_open(FILENAME4, NULL);
51     st_name = "Disk1";
52   }
53   else xbt_die("FILENAME NOT DEFINED %s",MSG_process_get_name(MSG_process_self()));
54
55   const char* filename = MSG_file_get_name(file);
56   XBT_INFO("\tOpen file '%s'",filename);
57   st = MSG_storage_get_by_name(st_name);
58
59   XBT_INFO("\tCapacity of the storage element '%s' is stored on: %llu / %llu",
60             filename, MSG_storage_get_used_size(st), MSG_storage_get_size(st));
61
62   /* Try to read for 10MB */
63   read = MSG_file_read(file, 10000000);
64   XBT_INFO("\tHave read %llu from '%s'",read,filename);
65
66   /* Write 100KB in file from the current position, i.e, end of file or 10MB */
67   write = MSG_file_write(file, 100000);
68   XBT_INFO("\tHave written %llu in '%s'. Size now is: %llu",write,filename,
69            MSG_file_get_size(file));
70
71
72   XBT_INFO("\tCapacity of the storage element '%s' is stored on: %llu / %llu",
73             filename, MSG_storage_get_used_size(st), MSG_storage_get_size(st));
74
75   /* rewind to the beginning of the file */
76   XBT_INFO("\tComing back to the beginning of the stream for file '%s'",
77            filename);
78   MSG_file_seek(file, 0, SEEK_SET);
79
80   /* Try to read 110KB */
81   read = MSG_file_read(file, 110000);
82   XBT_INFO("\tHave read %llu from '%s' (of size %llu)",read,filename,
83       MSG_file_get_size(file));
84
85   /* rewind once again to the beginning of the file */
86   XBT_INFO("\tComing back to the beginning of the stream for file '%s'",
87            filename);
88   MSG_file_seek(file, 0, SEEK_SET);
89
90   /* Write 110KB in file from the current position, i.e, end of file or 10MB */
91   write = MSG_file_write(file, 110000);
92   XBT_INFO("\tHave written %llu in '%s'. Size now is: %llu", write,filename,
93       MSG_file_get_size(file));
94
95   XBT_INFO("\tCapacity of the storage element '%s' is stored on: %llu / %llu",
96             filename, MSG_storage_get_used_size(st), MSG_storage_get_size(st));
97
98   XBT_INFO("\tClose file '%s'",filename);
99   MSG_file_close(file);
100
101
102   return 0;
103 }
104
105 int main(int argc, char **argv)
106 {
107   int i,res;
108   MSG_init(&argc, argv);
109   MSG_create_environment(argv[1]);
110   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
111   MSG_function_register("host", host);
112   unsigned long nb_hosts = xbt_dynar_length(hosts);
113   XBT_INFO("Number of host '%lu'",nb_hosts);
114   for(i = 0 ; i<nb_hosts; i++)
115   {
116     char* name_host = bprintf("%d",i);
117     MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,i,msg_host_t) );
118     free(name_host);
119   }
120   xbt_dynar_free(&hosts);
121
122   res = MSG_main();
123   XBT_INFO("Simulation time %g", MSG_get_clock());
124   return res != MSG_OK;
125 }