Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fd40b25fb91686e4114dea4ed06973fd5f08175c
[simgrid.git] / testsuite / surf / surf_usage.c
1 /* A few basic tests for the surf library                                   */
2
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #ifdef __BORLANDC__
10 #pragma hdrstop
11 #endif
12
13 #include <stdio.h>
14 #include "simgrid/sg_config.h"
15 #include "surf/surf.h"
16 #include "surf/surf_resource.h"
17 #include "surf/surfxml_parse.h" // for reset callback
18
19 #include "xbt/log.h"
20 XBT_LOG_NEW_DEFAULT_CATEGORY(surf_test,
21                              "Messages specific for surf example");
22
23 const char *string_action(e_surf_action_state_t state);
24 const char *string_action(e_surf_action_state_t state)
25 {
26   switch (state) {
27   case (SURF_ACTION_READY):
28     return "SURF_ACTION_READY";
29   case (SURF_ACTION_RUNNING):
30     return "SURF_ACTION_RUNNING";
31   case (SURF_ACTION_FAILED):
32     return "SURF_ACTION_FAILED";
33   case (SURF_ACTION_DONE):
34     return "SURF_ACTION_DONE";
35   case (SURF_ACTION_NOT_IN_THE_SYSTEM):
36     return "SURF_ACTION_NOT_IN_THE_SYSTEM";
37   default:
38     return "INVALID STATE";
39   }
40 }
41
42 void test(char *platform);
43 void test(char *platform)
44 {
45   void *cpuA = NULL;
46   void *cpuB = NULL;
47   void *cardA = NULL;
48   void *cardB = NULL;
49   surf_action_t actionA = NULL;
50   surf_action_t actionB = NULL;
51   surf_action_t actionC = NULL;
52   e_surf_action_state_t stateActionA;
53   e_surf_action_state_t stateActionB;
54   e_surf_action_state_t stateActionC;
55   double now = -1.0;
56   xbt_cfg_set_parse(_sg_cfg_set, "cpu/model:Cas01");
57   xbt_cfg_set_parse(_sg_cfg_set, "network/model:CM02");
58   parse_platform_file(platform);
59
60   /*********************** CPU ***********************************/
61   XBT_DEBUG("%p", surf_cpu_model);
62   cpuA = surf_cpu_resource_by_name("Cpu A");
63   cpuB = surf_cpu_resource_by_name("Cpu B");
64
65   /* Let's check that those two processors exist */
66   XBT_DEBUG("%s : %p", surf_cpu_name(surf_cpu_resource_priv(cpuA)), cpuA);
67   XBT_DEBUG("%s : %p", surf_cpu_name(surf_cpu_resource_priv(cpuB)), cpuB);
68   surf_cpu_resource_priv(cpuA);
69   /* Let's do something on it */
70   actionA = surf_cpu_execute(surf_cpu_resource_priv(cpuA), 1000.0);
71   actionB = surf_cpu_execute(surf_cpu_resource_priv(cpuB), 1000.0);
72   actionC = surf_cpu_sleep(surf_cpu_resource_priv(cpuB), 7.32);
73
74   /* Use whatever calling style you want... */
75   stateActionA = surf_action_get_state(actionA);     /* When you know actionA model type */
76   stateActionB = surf_action_get_state(actionB);        /* If you're unsure about it's model type */
77   stateActionC = surf_action_get_state(actionC);     /* When you know actionA model type */
78
79   /* And just look at the state of these tasks */
80   XBT_DEBUG("actionA : %p (%s)", actionA, string_action(stateActionA));
81   XBT_DEBUG("actionB : %p (%s)", actionB, string_action(stateActionB));
82   XBT_DEBUG("actionC : %p (%s)", actionB, string_action(stateActionC));
83
84   /*********************** Network *******************************/
85   XBT_DEBUG("%p", surf_network_model);
86   cardA = sg_routing_edge_by_name_or_null("Cpu A");
87   cardB = sg_routing_edge_by_name_or_null("Cpu B");
88
89   /* Let's check that those two processors exist */
90   XBT_DEBUG("%s : %p", surf_routing_edge_name(cardA), cardA);
91   XBT_DEBUG("%s : %p", surf_routing_edge_name(cardB), cardB);
92
93   /* Let's do something on it */
94   surf_network_model_communicate(surf_network_model, cardA, cardB, 150.0, -1.0);
95
96   surf_solve(-1.0);                 /* Takes traces into account. Returns 0.0 */
97   do {
98     surf_action_t action = NULL;
99     now = surf_get_clock();
100     XBT_DEBUG("Next Event : %g", now);
101     XBT_DEBUG("\t CPU actions");
102     while ((action =
103             xbt_swag_extract(surf_model_failed_action_set((surf_model_t)surf_cpu_model)))) {
104       XBT_DEBUG("\t * Failed : %p", action);
105       surf_action_unref(action);
106     }
107     while ((action =
108             xbt_swag_extract(surf_model_done_action_set((surf_model_t)surf_cpu_model)))) {
109       XBT_DEBUG("\t * Done : %p", action);
110       surf_action_unref(action);
111     }
112     XBT_DEBUG("\t Network actions");
113     while ((action =
114             xbt_swag_extract(surf_model_failed_action_set((surf_model_t)surf_network_model)))) {
115       XBT_DEBUG("\t * Failed : %p", action);
116       surf_action_unref(action);
117     }
118     while ((action =
119             xbt_swag_extract(surf_model_done_action_set((surf_model_t)surf_network_model)))) {
120       XBT_DEBUG("\t * Done : %p", action);
121       surf_action_unref(action);
122     }
123
124   } while ((xbt_swag_size(surf_model_running_action_set((surf_model_t)surf_network_model)) ||
125             xbt_swag_size(surf_model_running_action_set((surf_model_t)surf_cpu_model))) &&
126            surf_solve(-1.0) >= 0.0);
127
128   XBT_DEBUG("Simulation Terminated");
129 }
130
131 #ifdef __BORLANDC__
132 #pragma argsused
133 #endif
134
135 int main(int argc, char **argv)
136 {
137   surf_init(&argc, argv);       /* Initialize some common structures */
138   if (argc == 1) {
139     fprintf(stderr, "Usage : %s platform.xml\n", argv[0]);
140     return 1;
141   }
142   test(argv[1]);
143
144   surf_exit();
145   return 0;
146 }