1 /* Copyright (c) 2015-2017. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/msg.h"
7 #include "simgrid/plugins/file_system.h"
8 #include <xbt/replay.hpp>
10 XBT_LOG_NEW_DEFAULT_CATEGORY(storage_actions, "Messages specific for this example");
12 static xbt_dict_t opened_files = NULL;
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__); \
22 static void log_action(const char* const* action, double date)
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);
31 static msg_file_t get_file_descriptor(const char* file_name)
35 snprintf(full_name, 1023, "%s:%s", MSG_process_get_name(MSG_process_self()), file_name);
37 msg_file_t file = (msg_file_t)xbt_dict_get_or_null(opened_files, full_name);
41 static sg_size_t parse_size(const char* string)
46 size = strtoul(string, &endptr, 10);
48 THROWF(unknown_error, 0, "%s is not a long unsigned int (a.k.a. sg_size_t)", string);
52 static void action_open(const char* const* action)
54 const char* file_name = action[2];
56 msg_file_t file = NULL;
57 double clock = MSG_get_clock();
59 snprintf(full_name, 1023, "%s:%s", MSG_process_get_name(MSG_process_self()), file_name);
61 ACT_DEBUG("Entering Open: %s (filename: %s)", NAME, file_name);
62 file = MSG_file_open(file_name, NULL);
64 xbt_dict_set(opened_files, full_name, file, NULL);
66 log_action(action, MSG_get_clock() - clock);
69 static void action_read(const char* const* action)
71 const char* file_name = action[2];
72 const char* size_str = action[3];
73 sg_size_t size = parse_size(size_str);
75 double clock = MSG_get_clock();
77 msg_file_t file = get_file_descriptor(file_name);
79 ACT_DEBUG("Entering Read: %s (size: %llu)", NAME, size);
80 MSG_file_read(file, size);
82 log_action(action, MSG_get_clock() - clock);
85 static void action_close(const char* const* action)
87 const char* file_name = action[2];
89 double clock = MSG_get_clock();
91 file = get_file_descriptor(file_name);
93 ACT_DEBUG("Entering Close: %s (filename: %s)", NAME, file_name);
96 log_action(action, MSG_get_clock() - clock);
99 int main(int argc, char* argv[])
101 MSG_init(&argc, argv);
102 MSG_storage_file_system_init();
103 /* Explicit initialization of the action module is required */
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]);
112 MSG_create_environment(argv[1]);
113 MSG_launch_application(argv[2]);
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);
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
125 XBT_INFO("Simulation time %g", MSG_get_clock());
128 xbt_dict_free(&opened_files);
130 /* Explicit finalization of the action module is required now*/
133 return res != MSG_OK;