Logo AND Algorithmique Numérique Distribuée

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