Skip to content

Commit 792f50a

Browse files
committed
CTT - add CacheRowSpan class
1 parent cc6b9b6 commit 792f50a

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed

storage/cacheRowSpan.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* \file
3+
* \author Damir Zainullin <[email protected]>
4+
* \brief CacheRowSpan implementation.
5+
*/
6+
/*
7+
* Copyright (C) 2023 CESNET
8+
*
9+
* LICENSE TERMS
10+
*
11+
* Redistribution and use in source and binary forms, with or without
12+
* modification, are permitted provided that the following conditions
13+
* are met:
14+
* 1. Redistributions of source code must retain the above copyright
15+
* notice, this list of conditions and the following disclaimer.
16+
* 2. Redistributions in binary form must reproduce the above copyright
17+
* notice, this list of conditions and the following disclaimer in
18+
* the documentation and/or other materials provided with the
19+
* distribution.
20+
* 3. Neither the name of the Company nor the names of its contributors
21+
* may be used to endorse or promote products derived from this
22+
* software without specific prior written permission.
23+
*/
24+
25+
#include "cacheRowSpan.hpp"
26+
27+
#include <algorithm>
28+
29+
#include "fragmentationCache/timevalUtils.hpp"
30+
31+
namespace ipxp {
32+
33+
CacheRowSpan::CacheRowSpan(FlowRecord** begin, size_t count) noexcept
34+
: m_begin(begin), m_count(count)
35+
{
36+
}
37+
38+
std::optional<size_t> CacheRowSpan::find_by_hash(uint64_t hash) const noexcept
39+
{
40+
auto it = std::find_if(m_begin, m_begin + m_count, [&](const FlowRecord* flow) {
41+
return flow->belongs(hash);
42+
});
43+
if (it == m_begin + m_count) {
44+
return std::nullopt;
45+
}
46+
return it - m_begin;
47+
}
48+
49+
void CacheRowSpan::advance_flow_to(size_t from, size_t to) noexcept
50+
{
51+
if (from < to) {
52+
std::rotate(m_begin + from, m_begin + from + 1, m_begin + to + 1);
53+
return;
54+
}
55+
std::rotate(m_begin + to, m_begin + from, m_begin + from + 1);
56+
}
57+
58+
void CacheRowSpan::advance_flow(size_t flow_index) noexcept
59+
{
60+
advance_flow_to(flow_index, 0);
61+
}
62+
63+
std::optional<size_t> CacheRowSpan::find_empty() const noexcept
64+
{
65+
auto it = std::find_if(m_begin, m_begin + m_count, [](const FlowRecord* flow) {
66+
return flow->is_empty();
67+
});
68+
if (it == m_begin + m_count) {
69+
return std::nullopt;
70+
}
71+
return it - m_begin;
72+
}
73+
74+
#ifdef WITH_CTT
75+
size_t CacheRowSpan::find_victim(const timeval& now) const noexcept
76+
{
77+
FlowRecord* const* victim = m_begin + m_count - 1;
78+
auto it = std::find_if(m_begin, m_begin + m_count, [&](FlowRecord* const& flow) {
79+
if (!flow->is_in_ctt) {
80+
victim = &flow;
81+
}
82+
return flow->is_waiting_for_export && now > flow->export_time;
83+
});
84+
if (it == m_begin + m_count) {
85+
return victim - m_begin;
86+
}
87+
return it - m_begin;
88+
}
89+
#endif /* WITH_CTT */
90+
91+
} // ipxp

storage/cacheRowSpan.hpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* \file
3+
* \author Damir Zainullin <[email protected]>
4+
* \brief CacheRowSpan declaration.
5+
*/
6+
/*
7+
* Copyright (C) 2023 CESNET
8+
*
9+
* LICENSE TERMS
10+
*
11+
* Redistribution and use in source and binary forms, with or without
12+
* modification, are permitted provided that the following conditions
13+
* are met:
14+
* 1. Redistributions of source code must retain the above copyright
15+
* notice, this list of conditions and the following disclaimer.
16+
* 2. Redistributions in binary form must reproduce the above copyright
17+
* notice, this list of conditions and the following disclaimer in
18+
* the documentation and/or other materials provided with the
19+
* distribution.
20+
* 3. Neither the name of the Company nor the names of its contributors
21+
* may be used to endorse or promote products derived from this
22+
* software without specific prior written permission.
23+
*/
24+
25+
#pragma once
26+
27+
#include <cstddef>
28+
#include <optional>
29+
#include "flowRecord.hpp"
30+
31+
namespace ipxp {
32+
/**
33+
* \brief Class representing a non-owning view of a row span in a cache.
34+
*/
35+
class CacheRowSpan {
36+
public:
37+
/**
38+
* \brief Construct a new CacheRowSpan object.
39+
* \param begin Pointer to the first element in the row.
40+
* \param count Number of elements in the row.
41+
*/
42+
CacheRowSpan(FlowRecord** begin, size_t count) noexcept;
43+
44+
/**
45+
* \brief Find a flow record by hash.
46+
* \param hash Hash value to search for.
47+
* \return Index of the flow record relative to row begin if found, std::nullopt otherwise.
48+
*/
49+
std::optional<size_t> find_by_hash(uint64_t hash) const noexcept;
50+
/**
51+
* \brief Move a flow record to the beginning of the row.
52+
* \param flow_index Index of the flow record to move.
53+
*/
54+
void advance_flow(size_t flow_index) noexcept;
55+
56+
/**
57+
* \brief Move a flow record to a specific position in the row.
58+
* \param from Index of the flow record to move.
59+
* \param to Index of the position to move the flow record to.
60+
*/
61+
void advance_flow_to(size_t from, size_t to) noexcept;
62+
63+
/**
64+
* \brief Find an empty flow record in the row.
65+
* \return Index of the empty flow record if found, std::nullopt otherwise.
66+
*/
67+
std::optional<size_t> find_empty() const noexcept;
68+
#ifdef WITH_CTT
69+
/**
70+
* \brief Find a flow record to be evicted.
71+
* \param now Current time.
72+
* \return Index of flow from ctt which has delayed export timeout expired if found,
73+
* last record which is not in ctt, or last record otherwise
74+
*/
75+
size_t find_victim(const timeval& now) const noexcept;
76+
#endif /* WITH_CTT */
77+
private:
78+
FlowRecord** m_begin;
79+
size_t m_count;
80+
};
81+
82+
} // ipxp

0 commit comments

Comments
 (0)