XRootD
Loading...
Searching...
No Matches
XrdCephBufferDataSimple.cc
Go to the documentation of this file.
1//------------------------------------------------------------------------------
3//------------------------------------------------------------------------------
4
6#include "BufferUtils.hh"
7//#include "XrdCeph/XrdCephBuffers/IXrdCephBufferData.hh"
8#include <errno.h>
9#include <memory.h>
10#include <stdio.h>
11#include <string.h>
12#include <algorithm>
13#include <iostream>
14
15
16using namespace XrdCephBuffer;
17
20
21
22
24 m_bufferSize(bufCapacity), m_buffer(bufCapacity,0), m_externalOffset(0),m_bufLength(0) {
25 m_valid = true;
26
27 // update global statistics
28 m_total_memory_used.fetch_add(bufCapacity);
30 BUFLOG("XrdCephBufferDataSimple: Global: " << m_total_memory_nbuffers.load() << " " << m_total_memory_used.load());
31}
32
34 m_valid = false;
35 // obtain the actual capacity here, as this is the real number of bytes to be released
36 auto cap = m_buffer.capacity();
37 m_buffer.clear();
38 m_buffer.reserve(0); // just to be paranoid and realse memory immediately
39
40 // update global statistics
41 m_total_memory_used.fetch_add(-cap);
43 BUFLOG("XrdCephBufferDataSimple~: Global: " << m_total_memory_nbuffers.load() << " " << m_total_memory_used.load());
44
45}
46
47
49 // return defined buffered size, which might in principle be different
50 // to the actual size of the buffer allocated in memory
51 return m_bufferSize;
52}
53
55 return m_bufLength;
56}
58 m_bufLength = len;
59}
61 return m_valid;
62}
65}
66
67
72 m_externalOffset = offset;
73 return m_externalOffset;
74}
75
78 m_bufLength = 0;
79 m_valid = false;
80 //m_buffer.clear(); // do we really need to clear the elements ?
81 return 0;
82}
83
84
85
86ssize_t XrdCephBufferDataSimple::readBuffer(void* buf, off_t offset, size_t blen) const {
87 // read from the internal buffer to buf (at pos 0), from offset for blen, or max length possible
88 // returns -ve value on error, else the actual number of bytes read
89
90 if (!m_valid) {
91 return -EINVAL;
92 }
93 if (offset < 0) {
94 return -EINVAL;
95 }
96 if (offset > (ssize_t) m_bufLength) {
97 return 0;
98 }
99 ssize_t readlength = blen;
100 if (offset + blen > m_bufLength) {
101 readlength = m_bufLength - offset;
102 }
103 //std::cout << readlength << " " << blen << " " << m_bufLength << " " << offset << std::endl;
104 if (readlength <0) {
105 return -EINVAL;
106 }
107
108 if (readlength == 0) {
109 return 0;
110 }
111
112 const char* rawbufstart = m_buffer.data();
113
114 long int_ns{0};
115 {auto t = Timer_ns(int_ns);
116 // std::copy(rawbufstart + offset, rawbufstart+offset+readlength, reinterpret_cast<char*>(buf) );
117 memcpy(reinterpret_cast<char*>(buf), rawbufstart + offset, readlength);
118 } // end Timer
119 // BUFLOG("XrdCephBufferDataSimple::readBuffer: " << offset << " " << readlength << " " << int_ns );
120
121 return readlength;
122}
123
124
125ssize_t XrdCephBufferDataSimple::writeBuffer(const void* buf, off_t offset, size_t blen, off_t externalOffset) {
126 // write data from buf (from pos 0), with length blen, into the buffer at position offset (local to the internal buffer)
127
128 // #TODO Add test to see if it's in use
129 //invalidate();
130
131 if (offset < 0) {
132 BUFLOG("XrdCephBufferDataSimple::writeBuffer: offset <0");
133 return -EINVAL;
134 }
135
136 ssize_t cap = capacity();
137 if ((ssize_t)blen > cap) {
138 BUFLOG("XrdCephBufferDataSimple::writeBuffer: blen > cap:" << blen << " > " << cap);
139 return -EINVAL;
140 }
141 if ((ssize_t)offset > cap) {
142 BUFLOG("XrdCephBufferDataSimple::writeBuffer: offset > cap:" << offset << " > " << cap);
143 return -EINVAL;
144 }
145 if (ssize_t(offset + blen) > cap) {
146 BUFLOG("XrdCephBufferDataSimple::writeBuffer: (offset + blen) > cap: (" << offset << " + " << blen << ") >" << cap);
147 return -EINVAL;
148 }
149
150 // std::vector<char>::iterator itstart = m_buffer.begin();
151 size_t readBytes = blen;
152 char* rawbufstart = m_buffer.data();
153
154
155 long int_ns{0};
156 {auto t = Timer_ns(int_ns); // brace for timer start/stop scoping
157 //std::copy((char*)buf, (char*)buf +readBytes ,itstart + offset );
158 memcpy(rawbufstart + offset, buf, readBytes);
159
160 } // end Timer
161
162 // BUFLOG("XrdCephBufferDataSimple::writeBuffer: " << offset << " " << readBytes << " " << int_ns);
163
164
165
166 m_externalOffset = externalOffset;
167 // Decide to set the length of the maximum value that has be written
168 // note; unless invalidate is called, then this value may not be correctly set ...
169 m_bufLength = std::max(offset+blen, m_bufLength);
170 m_valid = true;
171
172
173 return readBytes;
174}
#define BUFLOG(x)
virtual ssize_t readBuffer(void *buf, off_t offset, size_t blen) const override
set cache into an invalid state
virtual off_t setStartingOffset(off_t offset) override
virtual bool isValid() const override
Currently occupied and valid space, which may be less than capacity.
virtual off_t startingOffset() const override
size_t m_bufLength
what does the first byte of the buffer map to for external offsets
virtual ssize_t invalidate() override
copy data from the internal buffer to buf
virtual void setValid(bool isValid) override
virtual ssize_t writeBuffer(const void *buf, off_t offset, size_t blen, off_t externalOffset=0) override
set cache into an invalid state; do this before writes to be consistent
static std::atomic< long > m_total_memory_nbuffers
total number of buffers actively open
static std::atomic< long > m_total_memory_used
total memory of all these buffers
virtual size_t length() const override
total available space
virtual void setLength(size_t len) override
Currently occupied and valid space, which may be less than capacity.
is a simple implementation of IXrdCephBufferData using std::vector<char> representation for the buffe...