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. */
7 #include "simgrid/msg.h"
8 #include <xbt/replay.h>
10 XBT_LOG_NEW_DEFAULT_CATEGORY(storage_actions, "Messages specific for this example");
11 /** @addtogroup MSG_examples
13 * - <b>I/O: actions-comm/actions-comm.c</b>. This example comes with a set of event handlers reproducing
14 * some classical I/O primitives (open, read, write, close, ...).
17 static xbt_dict_t opened_files = NULL;
19 #define ACT_DEBUG(...) \
20 if (XBT_LOG_ISENABLED(storage_actions, xbt_log_priority_verbose)) { \
21 char *NAME = xbt_str_join_array(action, " "); \
22 XBT_DEBUG(__VA_ARGS__); \
26 static void log_action(const char *const *action, double date)
28 if (XBT_LOG_ISENABLED(storage_actions, xbt_log_priority_verbose)) {
29 char *name = xbt_str_join_array(action, " ");
30 XBT_VERB("%s %f", name, date);
35 static msg_file_t get_file_descriptor(const char *file_name){
37 msg_file_t file = NULL;
39 sprintf(full_name, "%s:%s", MSG_process_get_name(MSG_process_self()), file_name);
41 file = (msg_file_t) xbt_dict_get_or_null(opened_files, full_name);
45 static sg_size_t parse_size(const char *string){
49 size = strtoul(string, &endptr, 10);
51 THROWF(unknown_error, 0, "%s is not a long unsigned int (a.k.a. sg_size_t)", string);
55 static void action_open(const char *const *action) {
56 const char *file_name = action[2];
58 msg_file_t file = NULL;
59 double clock = MSG_get_clock();
61 sprintf(full_name, "%s:%s", MSG_process_get_name(MSG_process_self()), file_name);
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();
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();
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 MSG_init(&argc, argv);
104 /* Explicit initialization of the action module is required now*/
107 xbt_assert(argc > 3,"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);
121 opened_files = xbt_dict_new_homogeneous(NULL);
122 /* Actually do the simulation using MSG_action_trace_run */
123 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*/