Logo AND Algorithmique Numérique Distribuée

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