Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cc84983669daa3193b91ae76630d2a4b32e3de23
[simgrid.git] / examples / msg / io / remote.c
1 /* Copyright (c) 2014-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  * - <b>io/remote.c</b> Example of delegated I/O operations
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include "simgrid/msg.h"
15
16 #define INMEGA (1024*1024)
17
18 int host(int argc, char *argv[]);
19
20 XBT_LOG_NEW_DEFAULT_CATEGORY(remote_io,
21                              "Messages specific for this io example");
22
23
24 int host(int argc, char *argv[]){
25   msg_file_t file = NULL;
26   const char* filename;
27   sg_size_t read, write;
28
29   file = MSG_file_open(argv[1], NULL);
30   filename = MSG_file_get_name(file);
31   XBT_INFO("Opened file '%s'",filename);
32   MSG_file_dump(file);
33
34   XBT_INFO("Try to read %llu from '%s'",MSG_file_get_size(file),filename);
35   read = MSG_file_read(file, MSG_file_get_size(file));
36   XBT_INFO("Have read %llu from '%s'. Offset is now at: %llu",read,filename,
37       MSG_file_tell(file));
38   XBT_INFO("Seek back to the begining of the stream...");
39   MSG_file_seek(file, 0, SEEK_SET);
40   XBT_INFO("Offset is now at: %llu", MSG_file_tell(file));
41
42   MSG_file_close(file);
43
44   if (argc > 5){
45     file = MSG_file_open(argv[2], NULL);
46     filename = MSG_file_get_name(file);
47     XBT_INFO("Opened file '%s'",filename);
48     XBT_INFO("Try to write %llu MiB to '%s'",
49         MSG_file_get_size(file)/1024,
50         filename);
51     write = MSG_file_write(file, MSG_file_get_size(file)*1024);
52     XBT_INFO("Have written %llu bytes to '%s'.",write,filename);
53
54     msg_host_t src, dest;
55     src= MSG_host_self();
56     dest = MSG_host_by_name(argv[3]);
57     if (xbt_str_parse_int(argv[5], "Argument 5 (move or copy) must be an int, not '%s'")) {
58       XBT_INFO("Move '%s' (of size %llu) from '%s' to '%s'", filename,
59            MSG_file_get_size(file), MSG_host_get_name(src),
60            argv[3]);
61       MSG_file_rmove(file, dest, argv[4]);
62     } else {
63       XBT_INFO("Copy '%s' (of size %llu) from '%s' to '%s'", filename,
64            MSG_file_get_size(file), MSG_host_get_name(src),
65            argv[3]);
66       MSG_file_rcopy(file, dest, argv[4]);
67       MSG_file_close(file);
68     }
69   }
70
71   return 0;
72 }
73
74
75
76 int main(int argc, char **argv)
77 {
78   int res;
79   unsigned int cur;
80   xbt_dynar_t storages;
81   msg_storage_t st;
82
83   MSG_init(&argc, argv);
84   MSG_create_environment(argv[1]);
85   MSG_function_register("host", host);
86   MSG_launch_application(argv[2]);
87
88   storages = MSG_storages_as_dynar();
89   xbt_dynar_foreach(storages, cur, st){
90     XBT_INFO("Init: %llu MiB used on '%s'",
91         MSG_storage_get_used_size(st)/INMEGA,
92         MSG_storage_get_name(st));
93   }
94
95   res = MSG_main();
96
97   xbt_dynar_foreach(storages, cur, st){
98     XBT_INFO("Init: %llu MiB used on '%s'",
99         MSG_storage_get_used_size(st)/INMEGA,
100         MSG_storage_get_name(st));
101   }
102   xbt_dynar_free_container(&storages);
103
104   XBT_INFO("Simulation time %g", MSG_get_clock());
105   return res != MSG_OK;
106 }