Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4165f23eafc8e8a751f15c58e0c82bdc4c6d51be
[simgrid.git] / examples / msg / actions-storage / actions-storage.c
1 /* Copyright (c) 2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "simgrid/msg.h"
8 #include <xbt/replay.h>
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(storage_actions, "Messages specific for this example");
11 /** @addtogroup MSG_examples
12  *
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, ...).
15  */
16
17 static xbt_dict_t opened_files = NULL;
18
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__);                                    \
23     xbt_free(NAME);                                            \
24   } else ((void)0)
25
26 static void log_action(const char *const *action, double date)
27 {
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);
31     xbt_free(name);
32   }
33 }
34
35 static msg_file_t get_file_descriptor(const char *file_name){
36   char full_name[1024];
37   msg_file_t file = NULL;
38
39   sprintf(full_name, "%s:%s", MSG_process_get_name(MSG_process_self()), file_name);
40
41   file = (msg_file_t) xbt_dict_get_or_null(opened_files, full_name);
42   return file;
43 }
44
45 static sg_size_t parse_size(const char *string){
46   sg_size_t size;
47   char *endptr;
48
49   size = strtoul(string, &endptr, 10);
50   if (*endptr != '\0')
51     THROWF(unknown_error, 0, "%s is not a long unsigned int (a.k.a. sg_size_t)", string);
52   return size;
53 }
54
55 static void action_open(const char *const *action) {
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   sprintf(full_name, "%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   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);
76
77   double clock = MSG_get_clock();
78
79   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   const char *file_name = action[2];
89   msg_file_t file;
90   double clock = MSG_get_clock();
91
92   file = get_file_descriptor(file_name);
93
94   ACT_DEBUG("Entering Close: %s (filename: %s)", NAME, file_name);
95   MSG_file_close(file);
96
97   log_action(action, MSG_get_clock() - clock);
98 }
99
100 int main(int argc, char *argv[]) {
101   msg_error_t res = MSG_OK;
102
103   MSG_init(&argc, argv);
104   /* Explicit initialization of the action module is required now*/
105   MSG_action_init();
106
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]);
111
112   MSG_create_environment(argv[1]);
113   MSG_launch_application(argv[2]);
114
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);
119
120   if (!opened_files)
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
124
125   XBT_INFO("Simulation time %g", MSG_get_clock());
126
127   if (opened_files)
128     xbt_dict_free(&opened_files);
129
130   /* Explicit finalization of the action module is required now*/
131   MSG_action_exit();
132
133   return res!=MSG_OK;
134 }