Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / teshsuite / msg / actions-storage / actions-storage.c
1 /* Copyright (c) 2015-2017. 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 #include <xbt/replay.hpp>
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(storage_actions, "Messages specific for this example");
11
12 static xbt_dict_t opened_files = NULL;
13
14 #define ACT_DEBUG(...)                                                                                                 \
15   if (XBT_LOG_ISENABLED(storage_actions, xbt_log_priority_verbose)) {                                                  \
16     char* NAME = xbt_str_join_array(action, " ");                                                                      \
17     XBT_DEBUG(__VA_ARGS__);                                                                                            \
18     xbt_free(NAME);                                                                                                    \
19   } else                                                                                                               \
20   ((void)0)
21
22 static void log_action(const char* const* action, double date)
23 {
24   if (XBT_LOG_ISENABLED(storage_actions, xbt_log_priority_verbose)) {
25     char* name = xbt_str_join_array(action, " ");
26     XBT_VERB("%s %f", name, date);
27     xbt_free(name);
28   }
29 }
30
31 static msg_file_t get_file_descriptor(const char* file_name)
32 {
33   char full_name[1024];
34
35   snprintf(full_name, 1023, "%s:%s", MSG_process_get_name(MSG_process_self()), file_name);
36
37   msg_file_t file = (msg_file_t)xbt_dict_get_or_null(opened_files, full_name);
38   return file;
39 }
40
41 static sg_size_t parse_size(const char* string)
42 {
43   sg_size_t size;
44   char* endptr;
45
46   size = strtoul(string, &endptr, 10);
47   if (*endptr != '\0')
48     THROWF(unknown_error, 0, "%s is not a long unsigned int (a.k.a. sg_size_t)", string);
49   return size;
50 }
51
52 static void action_open(const char* const* action)
53 {
54   const char* file_name = action[2];
55   char full_name[1024];
56   msg_file_t file = NULL;
57   double clock    = MSG_get_clock();
58
59   snprintf(full_name, 1023, "%s:%s", MSG_process_get_name(MSG_process_self()), file_name);
60
61   ACT_DEBUG("Entering Open: %s (filename: %s)", NAME, file_name);
62   file = MSG_file_open(file_name, NULL);
63
64   xbt_dict_set(opened_files, full_name, file, NULL);
65
66   log_action(action, MSG_get_clock() - clock);
67 }
68
69 static void action_read(const char* const* action)
70 {
71   const char* file_name = action[2];
72   const char* size_str  = action[3];
73   sg_size_t size        = parse_size(size_str);
74
75   double clock = MSG_get_clock();
76
77   msg_file_t file = get_file_descriptor(file_name);
78
79   ACT_DEBUG("Entering Read: %s (size: %llu)", NAME, size);
80   MSG_file_read(file, size);
81
82   log_action(action, MSG_get_clock() - clock);
83 }
84
85 static void action_close(const char* const* action)
86 {
87   const char* file_name = action[2];
88   msg_file_t file;
89   double clock = MSG_get_clock();
90
91   file = get_file_descriptor(file_name);
92
93   ACT_DEBUG("Entering Close: %s (filename: %s)", NAME, file_name);
94   MSG_file_close(file);
95
96   log_action(action, MSG_get_clock() - clock);
97 }
98
99 int main(int argc, char* argv[])
100 {
101   MSG_init(&argc, argv);
102   MSG_storage_file_system_init();
103   /* Explicit initialization of the action module is required */
104   MSG_action_init();
105
106   xbt_assert(argc > 3,
107              "Usage: %s platform_file deployment_file [action_files]\n"
108              "\texample: %s platform.xml deployment.xml actions # if all actions are in the same file\n"
109              "\texample: %s platform.xml deployment.xml # if actions are in separate files, specified in deployment\n",
110              argv[0], argv[0], argv[0]);
111
112   MSG_create_environment(argv[1]);
113   MSG_launch_application(argv[2]);
114
115   /*   Action registration */
116   xbt_replay_action_register("open", action_open);
117   xbt_replay_action_register("read", action_read);
118   xbt_replay_action_register("close", action_close);
119
120   if (opened_files == NULL)
121     opened_files = xbt_dict_new_homogeneous(NULL);
122   /* Actually do the simulation using MSG_action_trace_run */
123   msg_error_t res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
124
125   XBT_INFO("Simulation time %g", MSG_get_clock());
126
127   if (opened_files)
128     xbt_dict_free(&opened_files);
129
130   /* Explicit finalization of the action module is required now*/
131   MSG_action_exit();
132
133   return res != MSG_OK;
134 }