XRootD
Loading...
Searching...
No Matches
BufferUtils.cc
Go to the documentation of this file.
1
2#include "BufferUtils.hh"
3#include <algorithm> // std::max
4
5using namespace XrdCephBuffer;
6
7#ifdef CEPHBUFDEBUG
8// to synchronise logging statements
9 std::mutex cephbuf_iolock;
10#endif
11
12// ------------------------------------------------------ //
13// Extent //
14
15bool Extent::in_extent(off_t pos) const
16{
17 return ((pos > begin()) && (pos < end()));
18}
19
20bool Extent::isContiguous(const Extent &rhs) const
21{
22 // does the rhs connect directly to the end of the first
23 if (end() != rhs.begin())
24 return false;
25 return true;
26}
27
28bool Extent::allInExtent(off_t pos, size_t len) const
29{
30 // is all the range in this extent
31 if ((pos < begin()) || (pos >= end()))
32 return false;
33
34 if (off_t(pos + len) > end())
35 return false;
36 return true;
37}
38bool Extent::someInExtent(off_t pos, size_t len) const
39{ // is some of the range in this extent
40 if ((off_t(pos + len) < begin()) || (pos >= end()))
41 return false;
42 return true;
43}
44
45Extent Extent::containedExtent(off_t pos, size_t len) const
46{
47 // return the subset of input range that is in this extent
48 off_t subbeg = std::max(begin(), pos);
49 off_t subend = std::min(end(), off_t(pos + len));
50
51 return Extent(subbeg, subend - subbeg);
52}
54{
55 return containedExtent(rhs.begin(), rhs.len());
56}
57
58bool Extent::operator<(const Extent &rhs) const
59{
60 // comparison primarily on begin values
61 // use end values if begin values are equal.
62
63 if (begin() > rhs.begin()) return false;
64 if (begin() < rhs.begin()) return true;
65 if (end() < rhs.end() ) return true;
66 return false;
67}
68bool Extent::operator==(const Extent &rhs) const
69{
70 // equivalence based only on start and end
71 if (begin() != rhs.begin())
72 return false;
73 if (end() != rhs.end())
74 return false;
75 return true;
76}
77
78// ------------------------------------------------------ //
79// ExtentHolder //
80
82
84{
85 m_extents.reserve(elements);
86}
87
89{
90 m_extents.reserve(extents.size());
91 for (ExtentContainer::const_iterator vit = m_extents.cbegin(); vit != m_extents.cend(); ++vit) {
92 push_back(*vit);
93 }
94
95}
100
102 if (size()) {
103 m_begin = std::min(m_begin, in.begin());
104 m_end = std::max(m_end, in.end());
105 } else {
106 m_begin = in.begin();
107 m_end = in.end();
108 }
109 return m_extents.push_back(in);
110}
111
112
113
115 // if (!size()) return Extent(0,0);
116 // ExtentContainer se = getSortedExtents();
117 // off_t b = se.front().begin();
118 // off_t e = se.back().end();
119
120 return Extent(m_begin, m_end-m_begin);
121
122}
123
125 size_t nbytes{0};
126 for (ExtentContainer::const_iterator vit = m_extents.cbegin(); vit != m_extents.cend(); ++vit) {
127 nbytes += vit->len();
128 }
129 return nbytes;
130}
131
133 size_t bytesUsed = bytesContained();
134 size_t totalRange = asExtent().len(); //might be expensive to call
135 return totalRange - bytesUsed;
136}
137
138
140 std::sort(m_extents.begin(), m_extents.end());
141}
142
143
146 v.assign(m_extents.begin(), m_extents.end() );
147 std::sort(v.begin(), v.end());
148 return v;
149}
150
153 v.assign(m_extents.begin(), m_extents.end() );
154 return v;
155}
156
157// ------------------------------------------------------ //
158// Timer ns //
159
160Timer_ns::Timer_ns(long &output) : m_output_val(output)
161{
162 m_start = std::chrono::steady_clock::now();
163}
164
166{
167 auto end = std::chrono::steady_clock::now();
168 m_output_val = std::chrono::duration_cast<std::chrono::nanoseconds>(end - m_start).count();
169}
std::mutex cephbuf_iolock
Definition BufferUtils.cc:9
const ExtentContainer & extents() const
ExtentContainer getExtents() const
size_t size() const
number of extent elements
ExtentContainer getSortedExtents() const
void push_back(const Extent &in)
void sort()
inplace sort by offset of contained extents
off_t end() const
similar to stl vector end.
Extent containedExtent(off_t pos, size_t len) const
return the subset of range that is in this extent
off_t begin() const
Same as offset, but a bit more stl container like.
bool isContiguous(const Extent &rhs) const
bool operator==(const Extent &rhs) const
size_t len() const
bool operator<(const Extent &rhs) const
bool allInExtent(off_t pos, size_t len) const
is all the range in this extent
bool in_extent(off_t pos) const
is this position within the range of this extent
bool someInExtent(off_t pos, size_t len) const
is some of the range in this extent
Timer_ns(long &output_ns)
RAII based timer information outputing a long value of ns Almost trivial class to time something and ...
is a simple implementation of IXrdCephBufferData using std::vector<char> representation for the buffe...
std::vector< Extent > ExtentContainer
Container defintion for Extents Typedef to provide a container of extents as a simple stl vector cont...