Logo AND Algorithmique Numérique Distribuée

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