Logo AND Algorithmique Numérique Distribuée

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