My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
msu_state.c
Go to the documentation of this file.
1 /*
2 START OF LICENSE STUB
3  DeDOS: Declarative Dispersion-Oriented Software
4  Copyright (C) 2017 University of Pennsylvania, Georgetown University
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 END OF LICENSE STUB
19 */
25 #include "msu_message.h"
26 #include "rt_stats.h"
27 #include "logging.h"
28 #include "uthash.h"
29 #include "local_msu.h"
30 
32 #define CHECK_STATE_REPLACEMENT 1
33 
35 struct msu_state {
39  size_t size;
41  int group_id;
44  int32_t id;
46  void *data;
49 };
50 
51 int msu_num_states(struct local_msu *msu) {
52  return HASH_COUNT(msu->item_state);
53 }
54 
55 void *msu_init_state(struct local_msu *msu, struct msu_msg_key *key, size_t size) {
56  struct msu_state *state = malloc(sizeof(*state));
57  memcpy(&state->key, &key->key, key->key_len);
58  state->group_id = key->group_id;
59  state->data = malloc(size);
60  state->size = size;
61  state->id = key->id;
62 
63  log(LOG_STATE_MANAGEMENT, "Allocating new state of size %d for msu %d, key %d",
64  (int)size, msu->id, (int)key->id);
65 
66 #if CHECK_STATE_REPLACEMENT
67  struct msu_state *old_state = NULL;
68  HASH_REPLACE(hh, msu->item_state, key, key->key_len, state, old_state);
69  if (old_state != NULL) {
70  log_warn("Replacing old state! Not freeing! Bad!");
71  }
72 #else
73  HASH_ADD(hh, msu->item_state, key, key->key_len, state);
74 #endif
75 
76  increment_stat(MSU_MEM_ALLOC, msu->id, (double)(sizeof(*state) + size));
78 
79  return state->data;
80 }
81 
82 void *msu_get_state(struct local_msu *msu, struct msu_msg_key *key, size_t *size) {
83  struct msu_state *state;
84  HASH_FIND(hh, msu->item_state, &key->key, key->key_len, state);
85  log(LOG_STATE_MANAGEMENT, "Accessing state for MSU %d, key %d",
86  msu->id, (int)key->id);
87  if (state == NULL) {
88  log(LOG_STATE_MANAGEMENT, "State does not exist");
89  return NULL;
90  }
91  if (size != NULL) {
92  *size = state->size;
93  }
94  return state->data;
95 }
96 
97 int msu_free_state(struct local_msu *msu, struct msu_msg_key *key) {
98  struct msu_state *state;
99  HASH_FIND(hh, msu->item_state, &key->key, key->key_len, state);
100  log(LOG_STATE_MANAGEMENT, "Freeing state for MSU %d, key %d",
101  msu->id, (int)key->id);
102  if (state == NULL) {
103  log_warn("State for MSU %d, key %d does not exist",
104  msu->id, (int)key->id);
105  return -1;
106  }
107  HASH_DEL(msu->item_state, state);
108 
110  (double)(-1 * (int)(sizeof(*state) + state->size)));
111  increment_stat(MSU_NUM_STATES, msu->id, -1);
112  free(state->data);
113  free(state);
114  return 0;
115 }
116 
117 void msu_free_all_state(struct local_msu *msu) {
118  struct msu_state *state, *tmp;
119  HASH_ITER(hh, msu->item_state, state, tmp) {
120  HASH_DEL(msu->item_state, state);
121  free(state->data);
122  free(state);
123  }
124 }
#define HASH_FIND(hh, head, keyptr, keylen, out)
Definition: uthash.h:150
Collecting statistics within the runtime.
int32_t id
A unique identifier for the state (not used at the moment, will be utilized in state transfer) ...
Definition: msu_state.c:44
#define HASH_REPLACE(hh, head, fieldname, keylen_in, add, replaced)
Definition: uthash.h:230
void msu_free_all_state(struct local_msu *msu)
Frees all state structures associated with the given MSU.
Definition: msu_state.c:117
struct composite_key key
The full, arbitrary-length, unique key (used in state)
Definition: msu_message.h:48
void * msu_get_state(struct local_msu *msu, struct msu_msg_key *key, size_t *size)
Gets the state allocated with the given key.
Definition: msu_state.c:82
int32_t id
A shorter, often hashed id for the key of fixed length (used in routing)
Definition: msu_message.h:52
Logging of status messages to the terminal.
unsigned int id
Unique ID for a local MSU.
Definition: local_msu.h:54
void * msu_init_state(struct local_msu *msu, struct msu_msg_key *key, size_t size)
Initializes a new MSU state of the given size with the provided key.
Definition: msu_state.c:55
struct msu_state * item_state
Keyed state structure, accessible through queue item IDs.
Definition: local_msu.h:63
Declares the structures and functions applicable to MSUs on the local machine.
struct composite_key key
The key with which the state is stored.
Definition: msu_state.c:37
The structure that represents an MSU located on the local machine.
Definition: local_msu.h:38
void * data
The payload of the state.
Definition: msu_state.c:46
#define HASH_ADD(hh, head, fieldname, keylen_in, add)
Definition: uthash.h:336
The composite key is used to store a key of arbitrary length (up to 192 bytes).
Definition: msu_message.h:36
The structure contining msu state.
Definition: msu_state.c:35
size_t size
The size of the data being stored.
Definition: msu_state.c:39
#define HASH_COUNT(head)
Definition: uthash.h:1022
UT_hash_handle hh
For use with the UT hash structure.
Definition: msu_state.c:48
int group_id
Used to mark a route ID when storing state.
Definition: msu_message.h:55
int increment_stat(enum stat_id stat_id, unsigned int item_id, double value)
Increments the given statistic by the provided value.
Definition: rt_stats.c:389
#define log(level, fmt,...)
Log at a custom level.
Definition: logging.h:147
int group_id
An ID to facilitate the transferring of state between msus (not implemented yet)
Definition: msu_state.c:41
Messages passed to MSUs.
#define HASH_ITER(hh, head, el, tmp)
Definition: uthash.h:1016
int msu_free_state(struct local_msu *msu, struct msu_msg_key *key)
Frees the state assocated with the given MSU and key.
Definition: msu_state.c:97
#define HASH_DEL(head, delptr)
Definition: uthash.h:411
int msu_num_states(struct local_msu *msu)
Definition: msu_state.c:51
Used to uniquely identify the source of a message, used in state storage as well as routing...
Definition: msu_message.h:46
size_t key_len
The length of the composite key.
Definition: msu_message.h:50
#define log_warn(fmt,...)
Definition: logging.h:113
state
Definition: http_parser.c:298