1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "surf_private.h"
8 #include "network_private.h"
9 #include "xbt/mallocator.h"
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_kernel);
17 const char *surf_action_state_names[6] = {
19 "SURF_ACTION_RUNNING",
22 "SURF_ACTION_TO_FREE",
23 "SURF_ACTION_NOT_IN_THE_SYSTEM"
26 /* Surf actions mallocator */
27 static xbt_mallocator_t action_mallocator = NULL;
28 static int action_mallocator_allocated_size = 0;
29 static void* surf_action_mallocator_new_f(void);
30 static void surf_action_mallocator_free_f(void* action);
31 static void surf_action_mallocator_reset_f(void* action);
34 * \brief Initializes the action module of Surf.
36 void surf_action_init(void) {
38 /* the action mallocator will always provide actions of the following size,
39 * so this size should be set to the maximum size of the surf action structures
41 action_mallocator_allocated_size = sizeof(s_surf_action_network_CM02_t);
42 action_mallocator = xbt_mallocator_new(65536, surf_action_mallocator_new_f,
43 surf_action_mallocator_free_f, surf_action_mallocator_reset_f);
47 * \brief Uninitializes the action module of Surf.
49 void surf_action_exit(void) {
51 xbt_mallocator_free(action_mallocator);
54 static void* surf_action_mallocator_new_f(void) {
55 return xbt_malloc(action_mallocator_allocated_size);
58 static void surf_action_mallocator_free_f(void* action) {
62 static void surf_action_mallocator_reset_f(void* action) {
63 memset(action, 0, action_mallocator_allocated_size);
66 void *surf_action_new(size_t size, double cost, surf_model_t model,
69 xbt_assert(size <= action_mallocator_allocated_size,
70 "Cannot create a surf action of size %zu: the mallocator only provides actions of size %d",
71 size, action_mallocator_allocated_size);
73 surf_action_t action = xbt_mallocator_get(action_mallocator);
76 action->remains = cost;
77 action->priority = 1.0;
78 action->max_duration = NO_MAX_DURATION;
79 action->start = surf_get_clock();
80 action->finish = -1.0;
81 action->model_type = model;
83 action->category = NULL;
87 action->state_set = model->states.failed_action_set;
89 action->state_set = model->states.running_action_set;
91 xbt_swag_insert(action, action->state_set);
96 e_surf_action_state_t surf_action_state_get(surf_action_t action)
98 surf_action_state_t action_state = &(action->model_type->states);
100 if (action->state_set == action_state->ready_action_set)
101 return SURF_ACTION_READY;
102 if (action->state_set == action_state->running_action_set)
103 return SURF_ACTION_RUNNING;
104 if (action->state_set == action_state->failed_action_set)
105 return SURF_ACTION_FAILED;
106 if (action->state_set == action_state->done_action_set)
107 return SURF_ACTION_DONE;
108 return SURF_ACTION_NOT_IN_THE_SYSTEM;
111 double surf_action_get_start_time(surf_action_t action)
113 return action->start;
116 double surf_action_get_finish_time(surf_action_t action)
118 /* keep the function behavior, some models (cpu_ti) change the finish time before the action end */
119 return action->remains == 0 ? action->finish : -1;
122 XBT_INLINE void surf_action_free(surf_action_t * action)
124 xbt_mallocator_release(action_mallocator, *action);
128 void surf_action_state_set(surf_action_t action,
129 e_surf_action_state_t state)
131 surf_action_state_t action_state = &(action->model_type->states);
132 XBT_IN("(%p,%s)", action, surf_action_state_names[state]);
133 xbt_swag_remove(action, action->state_set);
135 if (state == SURF_ACTION_READY)
136 action->state_set = action_state->ready_action_set;
137 else if (state == SURF_ACTION_RUNNING)
138 action->state_set = action_state->running_action_set;
139 else if (state == SURF_ACTION_FAILED)
140 action->state_set = action_state->failed_action_set;
141 else if (state == SURF_ACTION_DONE)
142 action->state_set = action_state->done_action_set;
144 action->state_set = NULL;
146 if (action->state_set)
147 xbt_swag_insert(action, action->state_set);
151 void surf_action_data_set(surf_action_t action, void *data)
156 XBT_INLINE void surf_action_ref(surf_action_t action)
162 void surf_action_suspend(surf_action_t action)
164 action->suspended = 1;