Logo AND Algorithmique Numérique Distribuée

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