aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-plugin/src/cache_policies/cs_lru.c
blob: 45c4b5b79d3a429ff94ab96517e0d7440b9653ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "../hashtb.h"
#include "../strategy_dpo_manager.h"
#include "../error.h"
#include "cs_lru.h"
#include "cs_policy.h"

hicn_cs_policy_vft_t hicn_cs_lru = {
  .hicn_cs_insert = &hicn_cs_lru_insert,
  .hicn_cs_update = &hicn_cs_lru_update_head,
  .hicn_cs_dequeue = &hicn_cs_lru_dequeue,
  .hicn_cs_delete_get = &hicn_cs_lru_delete_get,
  .hicn_cs_trim = &hicn_cs_lru_trim,
  .hicn_cs_flush = &hicn_cs_lru_flush,
};

/*
 * Insert a new CS element at the head of the CS LRU
 */
void
hicn_cs_lru_insert (hicn_pit_cs_t * p, hicn_hash_node_t * node,
		    hicn_pcs_entry_t * pcs, hicn_cs_policy_t * policy_state)
{
  hicn_hash_node_t *lrunode;
  hicn_pcs_entry_t *lrupcs;
  u32 idx;

  idx = hicn_hashtb_node_idx_from_node (p->pcs_table, node);

  if (policy_state->head != 0)
    {
      lrunode = hicn_hashtb_node_from_idx (p->pcs_table, policy_state->head);
      lrupcs = hicn_pit_get_data (lrunode);

      ASSERT (lrupcs->u.cs.cs_lru_prev == 0);
      lrupcs->u.cs.cs_lru_prev = idx;

      pcs->u.cs.cs_lru_prev = 0;
      pcs->u.cs.cs_lru_next = policy_state->head;

      policy_state->head = idx;
    }
  else
    {
      ASSERT (policy_state->tail == 0);	/* We think the list is
					 * empty */

      policy_state->head = policy_state->tail = idx;

      pcs->u.cs.cs_lru_next = pcs->u.cs.cs_lru_prev = 0;
    }

  policy_state->count++;
}

void
hicn_cs_lru_delete_get (hicn_pit_cs_t * p, hicn_cs_policy_t * policy_state,
			hicn_hash_node_t ** nodep,
			hicn_pcs_entry_t ** pcs_entry,
			hicn_hash_entry_t ** hash_entry)
{
  *nodep = hicn_hashtb_node_from_idx (p->pcs_table, policy_state->tail);
  *pcs_entry = hicn_pit_get_data (*nodep);

  *hash_entry = hicn_hashtb_get_entry (p->pcs_table, (*nodep)->entry_idx,
				       (*nodep)->bucket_id,
				       (*nodep)->hn_flags &
				       HICN_HASH_NODE_OVERFLOW_BUCKET);
}

/*
 * Dequeue an LRU element, for example when it has expired.
 */
void
hicn_cs_lru_dequeue (hicn_pit_cs_t * pit, hicn_hash_node_t * pnode,
		     hicn_pcs_entry_t * pcs, hicn_cs_policy_t * lru)
{
  hicn_hash_node_t *lrunode;
  hicn_pcs_entry_t *lrupcs;

  if (pcs->u.cs.cs_lru_prev != 0)
    {
      /* Not already on the head of the LRU */
      lrunode = hicn_hashtb_node_from_idx (pit->pcs_table,
					   pcs->u.cs.cs_lru_prev);
      lrupcs = hicn_pit_get_data (lrunode);

      lrupcs->u.cs.cs_lru_next = pcs->u.cs.cs_lru_next;
    }
  else
    {
      ASSERT (lru->head ==
	      hicn_hashtb_node_idx_from_node (pit->pcs_table, pnode));
      lru->head = pcs->u.cs.cs_lru_next;
    }

  if (pcs->u.cs.cs_lru_next != 0)
    {
      /* Not already the end of the LRU */
      lrunode = hicn_hashtb_node_from_idx (pit->pcs_table,
					   pcs->u.cs.cs_lru_next);
      lrupcs = hicn_pit_get_data (lrunode);

      lrupcs->u.cs.cs_lru_prev = pcs->u.cs.cs_lru_prev;
    }
  else
    {
      /* This was the last LRU element */
      ASSERT (lru->tail ==
	      hicn_hashtb_node_idx_from_node (pit->pcs_table, pnode));
      lru->tail = pcs->u.cs.cs_lru_prev;
    }

  pcs->u.cs.cs_lru_next = pcs->u.cs.cs_lru_prev = 0;
  lru->count--;
}

/*
 * Move a CS LRU element to the head, probably after it's been used.
 */
void
hicn_cs_lru_update_head (hicn_pit_cs_t * pit, hicn_hash_node_t * pnode,
			 hicn_pcs_entry_t * pcs, hicn_cs_policy_t * lru)
{
  if (pcs->u.cs.cs_lru_prev != 0)
    {
      /*
       * Not already on the head of the LRU, detach it from its
       * current position
       */
      hicn_cs_lru_dequeue (pit, pnode, pcs, lru);

      /* Now detached from the list; attach at head */
      hicn_cs_lru_insert (pit, pnode, pcs, lru);

    }
  else
    {
      /* The element is already dequeue */
      if (pcs->u.cs.cs_lru_next == 0)
	{
	  /* Now detached from the list; attach at head */
	  hicn_cs_lru_insert (pit, pnode, pcs, lru);
	}
      ASSERT (lru->head ==
	      hicn_hashtb_node_idx_from_node (pit->pcs_table, pnode));
    }
}

/*
 * Remove a batch of nodes from the CS LRU, copying their node indexes into
 * the caller's array. We expect this is done when the LRU size exceeds the
 * CS's limit. Return the number of removed nodes.
 */
int
hicn_cs_lru_trim (hicn_pit_cs_t * pit, u32 * node_list, int sz,
		  hicn_cs_policy_t * lru)
{
  hicn_hash_node_t *lrunode;
  hicn_pcs_entry_t *lrupcs;
  u32 idx;
  int i;

  idx = lru->tail;

  for (i = 0; i < sz; i++)
    {

      if (idx == 0)
	{
	  break;
	}
      lrunode = hicn_hashtb_node_from_idx (pit->pcs_table, idx);
      lrupcs = hicn_pit_get_data (lrunode);

      node_list[i] = idx;

      idx = lrupcs->u.cs.cs_lru_prev;
      lrupcs->u.cs.cs_lru_prev = 0;
      lrupcs->u.cs.cs_lru_next = 0;
    }

  lru->count -= i;

  lru->tail = idx;
  if (idx != 0)
    {
      lrunode = hicn_hashtb_node_from_idx (pit->pcs_table, idx);
      lrupcs = hicn_pit_get_data (lrunode);

      lrupcs->u.cs.cs_lru_next = 0;
    }
  else
    {
      /* If the tail is empty, the whole lru is empty */
      lru->head = 0;
    }

  return (i);
}

int
hicn_cs_lru_flush (vlib_main_t * vm, struct hicn_pit_cs_s *pitcs,
		   hicn_cs_policy_t * state)
{
  if (state->head == 0 && state->tail == 0)
    return 0;

  hicn_hash_node_t *lrunode;
  hicn_pcs_entry_t *lrupcs;
  u32 idx;
  int i = 0;

  idx = state->tail;

  while (idx != 0)
    {
      lrunode = hicn_hashtb_node_from_idx (pitcs->pcs_table, idx);
      lrupcs = hicn_pit_get_data (lrunode);

      u64 hashval = 0;
      hicn_hashtb_fullhash ((u8 *) & (lrunode->hn_key.ks.key),
			    lrunode->hn_keysize, &hashval);
      hicn_hash_bucket_t *bucket = NULL;
      if ((hashval & (pitcs->pcs_table->ht_bucket_count - 1)) ==
	  lrunode->bucket_id)
	{
	  //The bucket is in the non overflown
	  bucket =
	    pool_elt_at_index (pitcs->pcs_table->ht_buckets,
			       lrunode->bucket_id);
	}
      else
	{
	  bucket =
	    pool_elt_at_index (pitcs->pcs_table->ht_overflow_buckets,
			       lrunode->bucket_id);
	}
      hicn_hash_entry_t *hash_entry =
	&(bucket->hb_entries[lrunode->entry_idx]);
      hash_entry->locks++;
      hicn_pcs_cs_delete (vm, pitcs, &lrupcs, &lrunode, hash_entry, NULL,
			  NULL);
      idx = state->tail;
      i++;
    }

  return (i);

}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables: eval: (c-set-style "gnu") End:
 */