Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
first prototype of storage action replay
[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 <stdio.h>
8 #include <stdlib.h>
9 #include "simgrid/msg.h"
10 #include "xbt.h"                /* calloc, printf */
11 #include <xbt/replay.h>
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(storage_actions, "Messages specific for this example");
14
15 static xbt_dict_t opened_files = NULL;
16
17 #define ACT_DEBUG(...) \
18   if (XBT_LOG_ISENABLED(storage_actions, xbt_log_priority_verbose)) {  \
19     char *NAME = xbt_str_join_array(action, " ");              \
20     XBT_DEBUG(__VA_ARGS__);                                    \
21     xbt_free(NAME);                                            \
22   } else ((void)0)
23
24 static void log_action(const char *const *action, double date)
25 {
26   if (XBT_LOG_ISENABLED(storage_actions, xbt_log_priority_verbose)) {
27     char *name = xbt_str_join_array(action, " ");
28     XBT_VERB("%s %f", name, date);
29     xbt_free(name);
30   }
31 }
32
33 msg_file_t get_file_descriptor(const char *file_name){
34   char full_name[1024];
35   msg_file_t file = NULL;
36
37   sprintf(full_name, "%s:%s", MSG_process_get_name(MSG_process_self()), file_name);
38
39   file = (msg_file_t) xbt_dict_get_or_null(opened_files, full_name);
40   return file;
41 }
42
43 static sg_size_t parse_size(const char *string){
44   sg_size_t size;
45   char *endptr;
46   
47   size = strtoul(string, &endptr, 10);
48   if (*endptr != '\0')
49     THROWF(unknown_error, 0, "%s is not a long unsigned int (a.k.a. sg_size_t)", string);
50   return size;
51 }
52
53
54 static void action_open(const char *const *action) {
55   const char *file_name = action[2];
56   char full_name[1024];
57   msg_file_t file = NULL;
58   double clock = MSG_get_clock();       /* this "call" is free thanks to inlining */
59
60   sprintf(full_name, "%s:%s", MSG_process_get_name(MSG_process_self()), 
61           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();       /* this "call" is free thanks to inlining */
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   char full_name[1024];
90   msg_file_t file;
91   double clock = MSG_get_clock();       /* this "call" is free thanks to inlining */
92
93   file = get_file_descriptor(file_name);
94
95   ACT_DEBUG("Entering Close: %s (filename: %s)", NAME, file_name);
96   MSG_file_close(file);
97
98   log_action(action, MSG_get_clock() - clock);
99 }
100
101 int main(int argc, char *argv[]) {
102   msg_error_t res = MSG_OK;
103
104   /* Check the given arguments */
105   MSG_init(&argc, argv);
106   /* Explicit initialization of the action module is required now*/
107   MSG_action_init();
108
109   if (argc < 3) {
110     printf("Usage: %s platform_file deployment_file [action_files]\n", argv[0]);
111     printf
112         ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
113          argv[0]);
114     printf
115         ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
116          argv[0]);
117     exit(1);
118   }
119
120   /*  Simulation setting */
121   MSG_create_environment(argv[1]);
122
123   /* No need to register functions as in classical MSG programs: the actions get started anyway */
124   MSG_launch_application(argv[2]);
125
126   /*   Action registration */
127   xbt_replay_action_register("open", action_open);
128   xbt_replay_action_register("read", action_read);
129   xbt_replay_action_register("close", action_close);
130   
131   if (!opened_files)
132     opened_files = xbt_dict_new_homogeneous(NULL);
133   /* Actually do the simulation using MSG_action_trace_run */
134   res = MSG_action_trace_run(argv[3]);  // it's ok to pass a NULL argument here
135
136   XBT_INFO("Simulation time %g", MSG_get_clock());
137
138   if (opened_files)
139     xbt_dict_free(&opened_files);
140
141   /* Explicit finalization of the action module is required now*/
142   MSG_action_exit();
143
144   return !!MSG_OK;
145 }                               /* end_of_main */