Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sed -i -e 's/\t/ /g' [sources] Please people, stop using tabs
[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 static 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   msg_file_t file;
90   double clock = MSG_get_clock();       /* this "call" is free thanks to inlining */
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   /* Check the given arguments */
104   MSG_init(&argc, argv);
105   /* Explicit initialization of the action module is required now*/
106   MSG_action_init();
107
108   if (argc < 3) {
109     printf("Usage: %s platform_file deployment_file [action_files]\n", argv[0]);
110     printf
111         ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
112          argv[0]);
113     printf
114         ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
115          argv[0]);
116     exit(1);
117   }
118
119   /*  Simulation setting */
120   MSG_create_environment(argv[1]);
121
122   /* No need to register functions as in classical MSG programs: the actions get started anyway */
123   MSG_launch_application(argv[2]);
124
125   /*   Action registration */
126   xbt_replay_action_register("open", action_open);
127   xbt_replay_action_register("read", action_read);
128   xbt_replay_action_register("close", action_close);
129   
130   if (!opened_files)
131     opened_files = xbt_dict_new_homogeneous(NULL);
132   /* Actually do the simulation using MSG_action_trace_run */
133   res = MSG_action_trace_run(argv[3]);  // it's ok to pass a NULL argument here
134
135   XBT_INFO("Simulation time %g", MSG_get_clock());
136
137   if (opened_files)
138     xbt_dict_free(&opened_files);
139
140   /* Explicit finalization of the action module is required now*/
141   MSG_action_exit();
142
143   return !!res;
144 }                               /* end_of_main */