1 /* Copyright (c) 2015. The SimGrid Team.
2 * All rights reserved. */
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. */
9 #include "simgrid/msg.h"
10 #include "xbt.h" /* calloc, printf */
11 #include <xbt/replay.h>
13 XBT_LOG_NEW_DEFAULT_CATEGORY(storage_actions, "Messages specific for this example");
15 static xbt_dict_t opened_files = NULL;
17 #define ACT_DEBUG(...) \
18 if (XBT_LOG_ISENABLED(storage_actions, xbt_log_priority_verbose)) { \
19 char *NAME = xbt_str_join_array(action, " "); \
20 XBT_DEBUG(__VA_ARGS__); \
24 static void log_action(const char *const *action, double date)
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);
33 static msg_file_t get_file_descriptor(const char *file_name){
35 msg_file_t file = NULL;
37 sprintf(full_name, "%s:%s", MSG_process_get_name(MSG_process_self()), file_name);
39 file = (msg_file_t) xbt_dict_get_or_null(opened_files, full_name);
43 static sg_size_t parse_size(const char *string){
47 size = strtoul(string, &endptr, 10);
49 THROWF(unknown_error, 0, "%s is not a long unsigned int (a.k.a. sg_size_t)", string);
54 static void action_open(const char *const *action) {
55 const char *file_name = action[2];
57 msg_file_t file = NULL;
58 double clock = MSG_get_clock(); /* this "call" is free thanks to inlining */
60 sprintf(full_name, "%s:%s", MSG_process_get_name(MSG_process_self()),
63 ACT_DEBUG("Entering Open: %s (filename: %s)", NAME, file_name);
64 file = MSG_file_open(file_name, NULL);
66 xbt_dict_set(opened_files, full_name, file, NULL);
68 log_action(action, MSG_get_clock() - clock);
71 static void action_read(const char *const *action) {
72 const char *file_name = action[2];
73 const char *size_str = action[3];
74 msg_file_t file = NULL;
75 sg_size_t size = parse_size(size_str);
77 double clock = MSG_get_clock(); /* this "call" is free thanks to inlining */
79 file = get_file_descriptor(file_name);
81 ACT_DEBUG("Entering Read: %s (size: %llu)", NAME, size);
82 MSG_file_read(file, size);
84 log_action(action, MSG_get_clock() - clock);
87 static void action_close(const char *const *action) {
88 const char *file_name = action[2];
90 double clock = MSG_get_clock(); /* this "call" is free thanks to inlining */
92 file = get_file_descriptor(file_name);
94 ACT_DEBUG("Entering Close: %s (filename: %s)", NAME, file_name);
97 log_action(action, MSG_get_clock() - clock);
100 int main(int argc, char *argv[]) {
101 msg_error_t res = MSG_OK;
103 /* Check the given arguments */
104 MSG_init(&argc, argv);
105 /* Explicit initialization of the action module is required now*/
109 printf("Usage: %s platform_file deployment_file [action_files]\n", argv[0]);
111 ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
114 ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
119 /* Simulation setting */
120 MSG_create_environment(argv[1]);
122 /* No need to register functions as in classical MSG programs: the actions get started anyway */
123 MSG_launch_application(argv[2]);
125 /* Action registration */
126 xbt_replay_action_register("open", action_open);
127 xbt_replay_action_register("read", action_read);
128 xbt_replay_action_register("close", action_close);
131 opened_files = xbt_dict_new_homogeneous(NULL);
132 /* Actually do the simulation using MSG_action_trace_run */
133 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
135 XBT_INFO("Simulation time %g", MSG_get_clock());
138 xbt_dict_free(&opened_files);
140 /* Explicit finalization of the action module is required now*/