1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
21 #include "include/compat.h"
22 #include "include/mempool.h"
24 #include "common/environment.h"
25 #include "common/errno.h"
26 #include "common/safe_io.h"
27 #include "common/simple_spin.h"
28 #include "common/strtol.h"
29 #include "common/likely.h"
30 #include "common/valgrind.h"
31 #include "common/deleter.h"
32 #include "common/RWLock.h"
33 #include "include/types.h"
34 #include "include/scope_guard.h"
37 #include "msg/xio/XioMsg.h"
42 #define CEPH_BUFFER_ALLOC_UNIT (MIN(CEPH_PAGE_SIZE, 4096))
43 #define CEPH_BUFFER_APPEND_SIZE (CEPH_BUFFER_ALLOC_UNIT - sizeof(raw_combined))
46 static std::atomic_flag buffer_debug_lock
= ATOMIC_FLAG_INIT
;
47 # define bdout { simple_spin_lock(&buffer_debug_lock); std::cout
48 # define bendl std::endl; simple_spin_unlock(&buffer_debug_lock); }
50 # define bdout if (0) { std::cout
51 # define bendl std::endl; }
54 static std::atomic
<uint64_t> buffer_total_alloc
{ 0 };
55 static std::atomic
<uint64_t> buffer_history_alloc_bytes
{ 0 };
56 static std::atomic
<uint64_t> buffer_history_alloc_num
{ 0 };
58 const bool buffer_track_alloc
= get_env_bool("CEPH_BUFFER_TRACK");
61 void inc_total_alloc(unsigned len
) {
62 if (buffer_track_alloc
)
63 buffer_total_alloc
+= len
;
66 void dec_total_alloc(unsigned len
) {
67 if (buffer_track_alloc
)
68 buffer_total_alloc
-= len
;
71 void inc_history_alloc(uint64_t len
) {
72 if (buffer_track_alloc
) {
73 buffer_history_alloc_bytes
+= len
;
74 buffer_history_alloc_num
++;
79 int buffer::get_total_alloc() {
80 return buffer_total_alloc
;
82 uint64_t buffer::get_history_alloc_bytes() {
83 return buffer_history_alloc_bytes
;
85 uint64_t buffer::get_history_alloc_num() {
86 return buffer_history_alloc_num
;
89 static std::atomic
<unsigned> buffer_cached_crc
{ 0 };
90 static std::atomic
<unsigned> buffer_cached_crc_adjusted
{ 0 };
91 static std::atomic
<unsigned> buffer_missed_crc
{ 0 };
93 static bool buffer_track_crc
= get_env_bool("CEPH_BUFFER_TRACK");
95 void buffer::track_cached_crc(bool b
) {
98 int buffer::get_cached_crc() {
99 return buffer_cached_crc
;
101 int buffer::get_cached_crc_adjusted() {
102 return buffer_cached_crc_adjusted
;
105 int buffer::get_missed_crc() {
106 return buffer_missed_crc
;
109 static std::atomic
<unsigned> buffer_c_str_accesses
{ 0 };
111 static bool buffer_track_c_str
= get_env_bool("CEPH_BUFFER_TRACK");
113 void buffer::track_c_str(bool b
) {
114 buffer_track_c_str
= b
;
116 int buffer::get_c_str_accesses() {
117 return buffer_c_str_accesses
;
120 static std::atomic
<unsigned> buffer_max_pipe_size
{ 0 };
121 int update_max_pipe_size() {
122 #ifdef CEPH_HAVE_SETPIPE_SZ
126 struct stat stat_result
;
127 if (::stat(PROCPREFIX
"/proc/sys/fs/pipe-max-size", &stat_result
) == -1)
129 r
= safe_read_file(PROCPREFIX
"/proc/sys/fs/", "pipe-max-size",
130 buf
, sizeof(buf
) - 1);
134 size_t size
= strict_strtol(buf
, 10, &err
);
137 buffer_max_pipe_size
= size
;
142 size_t get_max_pipe_size() {
143 #ifdef CEPH_HAVE_SETPIPE_SZ
144 size_t size
= buffer_max_pipe_size
;
147 if (update_max_pipe_size() == 0)
148 return buffer_max_pipe_size
;
150 // this is the max size hardcoded in linux before 2.6.35
154 const char * buffer::error::what() const throw () {
155 return "buffer::exception";
157 const char * buffer::bad_alloc::what() const throw () {
158 return "buffer::bad_alloc";
160 const char * buffer::end_of_buffer::what() const throw () {
161 return "buffer::end_of_buffer";
163 const char * buffer::malformed_input::what() const throw () {
166 buffer::error_code::error_code(int error
) :
167 buffer::malformed_input(cpp_strerror(error
).c_str()), code(error
) {}
173 std::atomic
<unsigned> nref
{ 0 };
174 int mempool
= mempool::mempool_buffer_anon
;
176 mutable std::atomic_flag crc_spinlock
= ATOMIC_FLAG_INIT
;
177 map
<pair
<size_t, size_t>, pair
<uint32_t, uint32_t> > crc_map
;
179 explicit raw(unsigned l
)
180 : data(NULL
), len(l
), nref(0) {
181 mempool::get_pool(mempool::pool_index_t(mempool
)).adjust_count(1, len
);
183 raw(char *c
, unsigned l
)
184 : data(c
), len(l
), nref(0) {
185 mempool::get_pool(mempool::pool_index_t(mempool
)).adjust_count(1, len
);
188 mempool::get_pool(mempool::pool_index_t(mempool
)).adjust_count(
192 void _set_len(unsigned l
) {
193 mempool::get_pool(mempool::pool_index_t(mempool
)).adjust_count(
196 mempool::get_pool(mempool::pool_index_t(mempool
)).adjust_count(1, len
);
199 void reassign_to_mempool(int pool
) {
200 if (pool
== mempool
) {
203 mempool::get_pool(mempool::pool_index_t(mempool
)).adjust_count(
206 mempool::get_pool(mempool::pool_index_t(pool
)).adjust_count(1, len
);
209 void try_assign_to_mempool(int pool
) {
210 if (mempool
== mempool::mempool_buffer_anon
) {
211 reassign_to_mempool(pool
);
216 // cppcheck-suppress noExplicitConstructor
217 raw(const raw
&other
);
218 const raw
& operator=(const raw
&other
);
220 virtual char *get_data() {
223 virtual raw
* clone_empty() = 0;
225 raw
*c
= clone_empty();
226 memcpy(c
->data
, data
, len
);
229 virtual bool can_zero_copy() const {
232 virtual int zero_copy_to_fd(int fd
, loff_t
*offset
) {
235 virtual bool is_page_aligned() {
236 return ((long)data
& ~CEPH_PAGE_MASK
) == 0;
238 bool is_n_page_sized() {
239 return (len
& ~CEPH_PAGE_MASK
) == 0;
241 virtual bool is_shareable() {
242 // true if safe to reference/share the existing buffer copy
243 // false if it is not safe to share the buffer, e.g., due to special
244 // and/or registered memory that is scarce
247 bool get_crc(const pair
<size_t, size_t> &fromto
,
248 pair
<uint32_t, uint32_t> *crc
) const {
249 simple_spin_lock(&crc_spinlock
);
250 map
<pair
<size_t, size_t>, pair
<uint32_t, uint32_t> >::const_iterator i
=
251 crc_map
.find(fromto
);
252 if (i
== crc_map
.end()) {
253 simple_spin_unlock(&crc_spinlock
);
257 simple_spin_unlock(&crc_spinlock
);
260 void set_crc(const pair
<size_t, size_t> &fromto
,
261 const pair
<uint32_t, uint32_t> &crc
) {
262 simple_spin_lock(&crc_spinlock
);
263 crc_map
[fromto
] = crc
;
264 simple_spin_unlock(&crc_spinlock
);
266 void invalidate_crc() {
267 simple_spin_lock(&crc_spinlock
);
268 if (crc_map
.size() != 0) {
271 simple_spin_unlock(&crc_spinlock
);
276 * raw_combined is always placed within a single allocation along
277 * with the data buffer. the data goes at the beginning, and
278 * raw_combined at the end.
280 class buffer::raw_combined
: public buffer::raw
{
283 raw_combined(char *dataptr
, unsigned l
, unsigned align
=0)
286 inc_total_alloc(len
);
287 inc_history_alloc(len
);
289 ~raw_combined() override
{
290 dec_total_alloc(len
);
292 raw
* clone_empty() override
{
293 return create(len
, alignment
);
296 static raw_combined
*create(unsigned len
, unsigned align
=0) {
298 align
= sizeof(size_t);
299 size_t rawlen
= ROUND_UP_TO(sizeof(buffer::raw_combined
),
300 alignof(buffer::raw_combined
));
301 size_t datalen
= ROUND_UP_TO(len
, alignof(buffer::raw_combined
));
304 char *ptr
= (char *) valloc(rawlen
+ datalen
);
307 int r
= ::posix_memalign((void**)(void*)&ptr
, align
, rawlen
+ datalen
);
314 // actual data first, since it has presumably larger alignment restriction
315 // then put the raw_combined at the end
316 return new (ptr
+ datalen
) raw_combined(ptr
, len
, align
);
319 static void operator delete(void *ptr
) {
320 raw_combined
*raw
= (raw_combined
*)ptr
;
321 ::free((void *)raw
->data
);
325 class buffer::raw_malloc
: public buffer::raw
{
327 MEMPOOL_CLASS_HELPERS();
329 explicit raw_malloc(unsigned l
) : raw(l
) {
331 data
= (char *)malloc(len
);
337 inc_total_alloc(len
);
338 inc_history_alloc(len
);
339 bdout
<< "raw_malloc " << this << " alloc " << (void *)data
<< " " << l
<< " " << buffer::get_total_alloc() << bendl
;
341 raw_malloc(unsigned l
, char *b
) : raw(b
, l
) {
342 inc_total_alloc(len
);
343 bdout
<< "raw_malloc " << this << " alloc " << (void *)data
<< " " << l
<< " " << buffer::get_total_alloc() << bendl
;
345 ~raw_malloc() override
{
347 dec_total_alloc(len
);
348 bdout
<< "raw_malloc " << this << " free " << (void *)data
<< " " << buffer::get_total_alloc() << bendl
;
350 raw
* clone_empty() override
{
351 return new raw_malloc(len
);
356 class buffer::raw_mmap_pages
: public buffer::raw
{
358 MEMPOOL_CLASS_HELPERS();
360 explicit raw_mmap_pages(unsigned l
) : raw(l
) {
361 data
= (char*)::mmap(NULL
, len
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
|MAP_ANON
, -1, 0);
364 inc_total_alloc(len
);
365 inc_history_alloc(len
);
366 bdout
<< "raw_mmap " << this << " alloc " << (void *)data
<< " " << l
<< " " << buffer::get_total_alloc() << bendl
;
368 ~raw_mmap_pages() override
{
370 dec_total_alloc(len
);
371 bdout
<< "raw_mmap " << this << " free " << (void *)data
<< " " << buffer::get_total_alloc() << bendl
;
373 raw
* clone_empty() override
{
374 return new raw_mmap_pages(len
);
378 class buffer::raw_posix_aligned
: public buffer::raw
{
381 MEMPOOL_CLASS_HELPERS();
383 raw_posix_aligned(unsigned l
, unsigned _align
) : raw(l
) {
385 assert((align
>= sizeof(void *)) && (align
& (align
- 1)) == 0);
387 data
= (char *) valloc(len
);
389 int r
= ::posix_memalign((void**)(void*)&data
, align
, len
);
395 inc_total_alloc(len
);
396 inc_history_alloc(len
);
397 bdout
<< "raw_posix_aligned " << this << " alloc " << (void *)data
<< " l=" << l
<< ", align=" << align
<< " total_alloc=" << buffer::get_total_alloc() << bendl
;
399 ~raw_posix_aligned() override
{
401 dec_total_alloc(len
);
402 bdout
<< "raw_posix_aligned " << this << " free " << (void *)data
<< " " << buffer::get_total_alloc() << bendl
;
404 raw
* clone_empty() override
{
405 return new raw_posix_aligned(len
, align
);
411 class buffer::raw_hack_aligned
: public buffer::raw
{
415 raw_hack_aligned(unsigned l
, unsigned _align
) : raw(l
) {
417 realdata
= new char[len
+align
-1];
418 unsigned off
= ((unsigned)realdata
) & (align
-1);
420 data
= realdata
+ align
- off
;
423 inc_total_alloc(len
+align
-1);
424 inc_history_alloc(len
+align
-1);
425 //cout << "hack aligned " << (unsigned)data
426 //<< " in raw " << (unsigned)realdata
427 //<< " off " << off << std::endl;
428 assert(((unsigned)data
& (align
-1)) == 0);
430 ~raw_hack_aligned() {
432 dec_total_alloc(len
+align
-1);
435 return new raw_hack_aligned(len
, align
);
440 #ifdef CEPH_HAVE_SPLICE
441 class buffer::raw_pipe
: public buffer::raw
{
443 MEMPOOL_CLASS_HELPERS();
445 explicit raw_pipe(unsigned len
) : raw(len
), source_consumed(false) {
446 size_t max
= get_max_pipe_size();
448 bdout
<< "raw_pipe: requested length " << len
449 << " > max length " << max
<< bendl
;
450 throw malformed_input("length larger than max pipe size");
456 if (::pipe(pipefds
) == -1) {
458 bdout
<< "raw_pipe: error creating pipe: " << cpp_strerror(r
) << bendl
;
462 r
= set_nonblocking(pipefds
);
464 bdout
<< "raw_pipe: error setting nonblocking flag on temp pipe: "
465 << cpp_strerror(r
) << bendl
;
469 r
= set_pipe_size(pipefds
, len
);
471 bdout
<< "raw_pipe: could not set pipe size" << bendl
;
472 // continue, since the pipe should become large enough as needed
475 inc_total_alloc(len
);
476 inc_history_alloc(len
);
477 bdout
<< "raw_pipe " << this << " alloc " << len
<< " "
478 << buffer::get_total_alloc() << bendl
;
481 ~raw_pipe() override
{
485 dec_total_alloc(len
);
486 bdout
<< "raw_pipe " << this << " free " << (void *)data
<< " "
487 << buffer::get_total_alloc() << bendl
;
490 bool can_zero_copy() const override
{
494 int set_source(int fd
, loff_t
*off
) {
495 int flags
= SPLICE_F_NONBLOCK
;
496 ssize_t r
= safe_splice(fd
, off
, pipefds
[1], NULL
, len
, flags
);
498 bdout
<< "raw_pipe: error splicing into pipe: " << cpp_strerror(r
)
502 // update length with actual amount read
507 int zero_copy_to_fd(int fd
, loff_t
*offset
) override
{
508 assert(!source_consumed
);
509 int flags
= SPLICE_F_NONBLOCK
;
510 ssize_t r
= safe_splice_exact(pipefds
[0], NULL
, fd
, offset
, len
, flags
);
512 bdout
<< "raw_pipe: error splicing from pipe to fd: "
513 << cpp_strerror(r
) << bendl
;
516 source_consumed
= true;
520 buffer::raw
* clone_empty() override
{
521 // cloning doesn't make sense for pipe-based buffers,
522 // and is only used by unit tests for other types of buffers
526 char *get_data() override
{
529 return copy_pipe(pipefds
);
533 int set_pipe_size(int *fds
, long length
) {
534 #ifdef CEPH_HAVE_SETPIPE_SZ
535 if (::fcntl(fds
[1], F_SETPIPE_SZ
, length
) == -1) {
538 // pipe limit must have changed - EPERM means we requested
539 // more than the maximum size as an unprivileged user
540 update_max_pipe_size();
541 throw malformed_input("length larger than new max pipe size");
549 int set_nonblocking(int *fds
) {
550 if (::fcntl(fds
[0], F_SETFL
, O_NONBLOCK
) == -1)
552 if (::fcntl(fds
[1], F_SETFL
, O_NONBLOCK
) == -1)
557 static void close_pipe(const int *fds
) {
559 VOID_TEMP_FAILURE_RETRY(::close(fds
[0]));
561 VOID_TEMP_FAILURE_RETRY(::close(fds
[1]));
563 char *copy_pipe(int *fds
) {
564 /* preserve original pipe contents by copying into a temporary
565 * pipe before reading.
570 assert(!source_consumed
);
573 if (::pipe(tmpfd
) == -1) {
575 bdout
<< "raw_pipe: error creating temp pipe: " << cpp_strerror(r
)
579 auto sg
= make_scope_guard([=] { close_pipe(tmpfd
); });
580 r
= set_nonblocking(tmpfd
);
582 bdout
<< "raw_pipe: error setting nonblocking flag on temp pipe: "
583 << cpp_strerror(r
) << bendl
;
586 r
= set_pipe_size(tmpfd
, len
);
588 bdout
<< "raw_pipe: error setting pipe size on temp pipe: "
589 << cpp_strerror(r
) << bendl
;
591 int flags
= SPLICE_F_NONBLOCK
;
592 if (::tee(fds
[0], tmpfd
[1], len
, flags
) == -1) {
594 bdout
<< "raw_pipe: error tee'ing into temp pipe: " << cpp_strerror(r
)
598 data
= (char *)malloc(len
);
602 r
= safe_read(tmpfd
[0], data
, len
);
603 if (r
< (ssize_t
)len
) {
604 bdout
<< "raw_pipe: error reading from temp pipe:" << cpp_strerror(r
)
612 bool source_consumed
;
615 #endif // CEPH_HAVE_SPLICE
618 * primitive buffer types
620 class buffer::raw_char
: public buffer::raw
{
622 MEMPOOL_CLASS_HELPERS();
624 explicit raw_char(unsigned l
) : raw(l
) {
626 data
= new char[len
];
629 inc_total_alloc(len
);
630 inc_history_alloc(len
);
631 bdout
<< "raw_char " << this << " alloc " << (void *)data
<< " " << l
<< " " << buffer::get_total_alloc() << bendl
;
633 raw_char(unsigned l
, char *b
) : raw(b
, l
) {
634 inc_total_alloc(len
);
635 bdout
<< "raw_char " << this << " alloc " << (void *)data
<< " " << l
<< " " << buffer::get_total_alloc() << bendl
;
637 ~raw_char() override
{
639 dec_total_alloc(len
);
640 bdout
<< "raw_char " << this << " free " << (void *)data
<< " " << buffer::get_total_alloc() << bendl
;
642 raw
* clone_empty() override
{
643 return new raw_char(len
);
647 class buffer::raw_claimed_char
: public buffer::raw
{
649 MEMPOOL_CLASS_HELPERS();
651 explicit raw_claimed_char(unsigned l
, char *b
) : raw(b
, l
) {
652 inc_total_alloc(len
);
653 bdout
<< "raw_claimed_char " << this << " alloc " << (void *)data
654 << " " << l
<< " " << buffer::get_total_alloc() << bendl
;
656 ~raw_claimed_char() override
{
657 dec_total_alloc(len
);
658 bdout
<< "raw_claimed_char " << this << " free " << (void *)data
659 << " " << buffer::get_total_alloc() << bendl
;
661 raw
* clone_empty() override
{
662 return new raw_char(len
);
666 class buffer::raw_unshareable
: public buffer::raw
{
668 MEMPOOL_CLASS_HELPERS();
670 explicit raw_unshareable(unsigned l
) : raw(l
) {
672 data
= new char[len
];
676 raw_unshareable(unsigned l
, char *b
) : raw(b
, l
) {
678 raw
* clone_empty() override
{
679 return new raw_char(len
);
681 bool is_shareable() override
{
682 return false; // !shareable, will force make_shareable()
684 ~raw_unshareable() override
{
689 class buffer::raw_static
: public buffer::raw
{
691 MEMPOOL_CLASS_HELPERS();
693 raw_static(const char *d
, unsigned l
) : raw((char*)d
, l
) { }
694 ~raw_static() override
{}
695 raw
* clone_empty() override
{
696 return new buffer::raw_char(len
);
700 class buffer::raw_claim_buffer
: public buffer::raw
{
703 raw_claim_buffer(const char *b
, unsigned l
, deleter d
)
704 : raw((char*)b
, l
), del(std::move(d
)) { }
705 ~raw_claim_buffer() override
{}
706 raw
* clone_empty() override
{
707 return new buffer::raw_char(len
);
711 #if defined(HAVE_XIO)
712 class buffer::xio_msg_buffer
: public buffer::raw
{
714 XioDispatchHook
* m_hook
;
716 xio_msg_buffer(XioDispatchHook
* _m_hook
, const char *d
,
718 raw((char*)d
, l
), m_hook(_m_hook
->get()) {}
720 bool is_shareable() { return false; }
721 static void operator delete(void *p
)
723 xio_msg_buffer
*buf
= static_cast<xio_msg_buffer
*>(p
);
724 // return hook ref (counts against pool); it appears illegal
725 // to do this in our dtor, because this fires after that
729 return new buffer::raw_char(len
);
733 class buffer::xio_mempool
: public buffer::raw
{
735 struct xio_reg_mem
*mp
;
736 xio_mempool(struct xio_reg_mem
*_mp
, unsigned l
) :
737 raw((char*)_mp
->addr
, l
), mp(_mp
)
741 return new buffer::raw_char(len
);
745 struct xio_reg_mem
* get_xio_mp(const buffer::ptr
& bp
)
747 buffer::xio_mempool
*mb
= dynamic_cast<buffer::xio_mempool
*>(bp
.get_raw());
754 buffer::raw
* buffer::create_msg(
755 unsigned len
, char *buf
, XioDispatchHook
* m_hook
) {
756 XioPool
& pool
= m_hook
->get_pool();
758 static_cast<buffer::raw
*>(pool
.alloc(sizeof(xio_msg_buffer
)));
759 new (bp
) xio_msg_buffer(m_hook
, buf
, len
);
762 #endif /* HAVE_XIO */
764 buffer::raw
* buffer::copy(const char *c
, unsigned len
) {
765 raw
* r
= buffer::create_aligned(len
, sizeof(size_t));
766 memcpy(r
->data
, c
, len
);
770 buffer::raw
* buffer::create(unsigned len
) {
771 return buffer::create_aligned(len
, sizeof(size_t));
773 buffer::raw
* buffer::claim_char(unsigned len
, char *buf
) {
774 return new raw_claimed_char(len
, buf
);
776 buffer::raw
* buffer::create_malloc(unsigned len
) {
777 return new raw_malloc(len
);
779 buffer::raw
* buffer::claim_malloc(unsigned len
, char *buf
) {
780 return new raw_malloc(len
, buf
);
782 buffer::raw
* buffer::create_static(unsigned len
, char *buf
) {
783 return new raw_static(buf
, len
);
785 buffer::raw
* buffer::claim_buffer(unsigned len
, char *buf
, deleter del
) {
786 return new raw_claim_buffer(buf
, len
, std::move(del
));
789 buffer::raw
* buffer::create_aligned(unsigned len
, unsigned align
) {
790 // If alignment is a page multiple, use a separate buffer::raw to
791 // avoid fragmenting the heap.
793 // Somewhat unexpectedly, I see consistently better performance
794 // from raw_combined than from raw even when the allocation size is
795 // a page multiple (but alignment is not).
797 // I also see better performance from a separate buffer::raw once the
799 if ((align
& ~CEPH_PAGE_MASK
) == 0 ||
800 len
>= CEPH_PAGE_SIZE
* 2) {
802 return new raw_posix_aligned(len
, align
);
804 return new raw_hack_aligned(len
, align
);
807 return raw_combined::create(len
, align
);
810 buffer::raw
* buffer::create_page_aligned(unsigned len
) {
811 return create_aligned(len
, CEPH_PAGE_SIZE
);
814 buffer::raw
* buffer::create_zero_copy(unsigned len
, int fd
, int64_t *offset
) {
815 #ifdef CEPH_HAVE_SPLICE
816 buffer::raw_pipe
* buf
= new raw_pipe(len
);
817 int r
= buf
->set_source(fd
, (loff_t
*)offset
);
824 throw error_code(-ENOTSUP
);
828 buffer::raw
* buffer::create_unshareable(unsigned len
) {
829 return new raw_unshareable(len
);
832 buffer::ptr::ptr(raw
*r
) : _raw(r
), _off(0), _len(r
->len
) // no lock needed; this is an unref raw.
835 bdout
<< "ptr " << this << " get " << _raw
<< bendl
;
837 buffer::ptr::ptr(unsigned l
) : _off(0), _len(l
)
841 bdout
<< "ptr " << this << " get " << _raw
<< bendl
;
843 buffer::ptr::ptr(const char *d
, unsigned l
) : _off(0), _len(l
) // ditto.
847 bdout
<< "ptr " << this << " get " << _raw
<< bendl
;
849 buffer::ptr::ptr(const ptr
& p
) : _raw(p
._raw
), _off(p
._off
), _len(p
._len
)
853 bdout
<< "ptr " << this << " get " << _raw
<< bendl
;
856 buffer::ptr::ptr(ptr
&& p
) noexcept
: _raw(p
._raw
), _off(p
._off
), _len(p
._len
)
861 buffer::ptr::ptr(const ptr
& p
, unsigned o
, unsigned l
)
862 : _raw(p
._raw
), _off(p
._off
+ o
), _len(l
)
864 assert(o
+l
<= p
._len
);
867 bdout
<< "ptr " << this << " get " << _raw
<< bendl
;
869 buffer::ptr
& buffer::ptr::operator= (const ptr
& p
)
873 bdout
<< "ptr " << this << " get " << _raw
<< bendl
;
875 buffer::raw
*raw
= p
._raw
;
886 buffer::ptr
& buffer::ptr::operator= (ptr
&& p
) noexcept
889 buffer::raw
*raw
= p
._raw
;
902 buffer::raw
*buffer::ptr::clone()
904 return _raw
->clone();
907 buffer::ptr
& buffer::ptr::make_shareable() {
908 if (_raw
&& !_raw
->is_shareable()) {
909 buffer::raw
*tr
= _raw
;
912 if (unlikely(--tr
->nref
== 0)) {
913 ANNOTATE_HAPPENS_AFTER(&tr
->nref
);
914 ANNOTATE_HAPPENS_BEFORE_FORGET_ALL(&tr
->nref
);
917 ANNOTATE_HAPPENS_BEFORE(&tr
->nref
);
923 void buffer::ptr::swap(ptr
& other
)
936 void buffer::ptr::release()
939 bdout
<< "ptr " << this << " release " << _raw
<< bendl
;
940 if (--_raw
->nref
== 0) {
941 //cout << "hosing raw " << (void*)_raw << " len " << _raw->len << std::endl;
942 ANNOTATE_HAPPENS_AFTER(&_raw
->nref
);
943 ANNOTATE_HAPPENS_BEFORE_FORGET_ALL(&_raw
->nref
);
944 delete _raw
; // dealloc old (if any)
946 ANNOTATE_HAPPENS_BEFORE(&_raw
->nref
);
952 bool buffer::ptr::at_buffer_tail() const { return _off
+ _len
== _raw
->len
; }
954 const char *buffer::ptr::c_str() const {
956 if (buffer_track_c_str
)
957 buffer_c_str_accesses
++;
958 return _raw
->get_data() + _off
;
960 char *buffer::ptr::c_str() {
962 if (buffer_track_c_str
)
963 buffer_c_str_accesses
++;
964 return _raw
->get_data() + _off
;
966 const char *buffer::ptr::end_c_str() const {
968 if (buffer_track_c_str
)
969 buffer_c_str_accesses
++;
970 return _raw
->get_data() + _off
+ _len
;
972 char *buffer::ptr::end_c_str() {
974 if (buffer_track_c_str
)
975 buffer_c_str_accesses
++;
976 return _raw
->get_data() + _off
+ _len
;
979 unsigned buffer::ptr::unused_tail_length() const
982 return _raw
->len
- (_off
+_len
);
986 const char& buffer::ptr::operator[](unsigned n
) const
990 return _raw
->get_data()[_off
+ n
];
992 char& buffer::ptr::operator[](unsigned n
)
996 return _raw
->get_data()[_off
+ n
];
999 const char *buffer::ptr::raw_c_str() const { assert(_raw
); return _raw
->data
; }
1000 unsigned buffer::ptr::raw_length() const { assert(_raw
); return _raw
->len
; }
1001 int buffer::ptr::raw_nref() const { assert(_raw
); return _raw
->nref
; }
1003 void buffer::ptr::copy_out(unsigned o
, unsigned l
, char *dest
) const {
1006 throw end_of_buffer();
1007 char* src
= _raw
->data
+ _off
+ o
;
1008 maybe_inline_memcpy(dest
, src
, l
, 8);
1011 unsigned buffer::ptr::wasted() const
1013 return _raw
->len
- _len
;
1016 int buffer::ptr::cmp(const ptr
& o
) const
1018 int l
= _len
< o
._len
? _len
: o
._len
;
1020 int r
= memcmp(c_str(), o
.c_str(), l
);
1031 bool buffer::ptr::is_zero() const
1033 return mem_is_zero(c_str(), _len
);
1036 unsigned buffer::ptr::append(char c
)
1039 assert(1 <= unused_tail_length());
1040 char* ptr
= _raw
->data
+ _off
+ _len
;
1046 unsigned buffer::ptr::append(const char *p
, unsigned l
)
1049 assert(l
<= unused_tail_length());
1050 char* c
= _raw
->data
+ _off
+ _len
;
1051 maybe_inline_memcpy(c
, p
, l
, 32);
1056 void buffer::ptr::copy_in(unsigned o
, unsigned l
, const char *src
)
1058 copy_in(o
, l
, src
, true);
1061 void buffer::ptr::copy_in(unsigned o
, unsigned l
, const char *src
, bool crc_reset
)
1065 assert(o
+l
<= _len
);
1066 char* dest
= _raw
->data
+ _off
+ o
;
1068 _raw
->invalidate_crc();
1069 maybe_inline_memcpy(dest
, src
, l
, 64);
1072 void buffer::ptr::zero()
1077 void buffer::ptr::zero(bool crc_reset
)
1080 _raw
->invalidate_crc();
1081 memset(c_str(), 0, _len
);
1084 void buffer::ptr::zero(unsigned o
, unsigned l
)
1089 void buffer::ptr::zero(unsigned o
, unsigned l
, bool crc_reset
)
1091 assert(o
+l
<= _len
);
1093 _raw
->invalidate_crc();
1094 memset(c_str()+o
, 0, l
);
1096 bool buffer::ptr::can_zero_copy() const
1098 return _raw
->can_zero_copy();
1101 int buffer::ptr::zero_copy_to_fd(int fd
, int64_t *offset
) const
1103 return _raw
->zero_copy_to_fd(fd
, (loff_t
*)offset
);
1106 // -- buffer::list::iterator --
1108 buffer::list::iterator operator=(const buffer::list::iterator& other)
1110 if (this != &other) {
1115 p_off = other.p_off;
1120 template<bool is_const
>
1121 buffer::list::iterator_impl
<is_const
>::iterator_impl(bl_t
*l
, unsigned o
)
1122 : bl(l
), ls(&bl
->_buffers
), off(0), p(ls
->begin()), p_off(0)
1127 template<bool is_const
>
1128 buffer::list::iterator_impl
<is_const
>::iterator_impl(const buffer::list::iterator
& i
)
1129 : iterator_impl
<is_const
>(i
.bl
, i
.off
, i
.p
, i
.p_off
) {}
1131 template<bool is_const
>
1132 void buffer::list::iterator_impl
<is_const
>::advance(int o
)
1134 //cout << this << " advance " << o << " from " << off << " (p_off " << p_off << " in " << p->length() << ")" << std::endl;
1139 throw end_of_buffer();
1140 if (p_off
>= p
->length()) {
1142 p_off
-= p
->length();
1145 // somewhere in this buffer!
1160 } else if (off
> 0) {
1161 assert(p
!= ls
->begin());
1163 p_off
= p
->length();
1165 throw end_of_buffer();
1170 template<bool is_const
>
1171 void buffer::list::iterator_impl
<is_const
>::seek(unsigned o
)
1178 template<bool is_const
>
1179 char buffer::list::iterator_impl
<is_const
>::operator*() const
1182 throw end_of_buffer();
1186 template<bool is_const
>
1187 buffer::list::iterator_impl
<is_const
>&
1188 buffer::list::iterator_impl
<is_const
>::operator++()
1191 throw end_of_buffer();
1196 template<bool is_const
>
1197 buffer::ptr
buffer::list::iterator_impl
<is_const
>::get_current_ptr() const
1200 throw end_of_buffer();
1201 return ptr(*p
, p_off
, p
->length() - p_off
);
1205 // note that these all _append_ to dest!
1206 template<bool is_const
>
1207 void buffer::list::iterator_impl
<is_const
>::copy(unsigned len
, char *dest
)
1209 if (p
== ls
->end()) seek(off
);
1212 throw end_of_buffer();
1213 assert(p
->length() > 0);
1215 unsigned howmuch
= p
->length() - p_off
;
1216 if (len
< howmuch
) howmuch
= len
;
1217 p
->copy_out(p_off
, howmuch
, dest
);
1225 template<bool is_const
>
1226 void buffer::list::iterator_impl
<is_const
>::copy(unsigned len
, ptr
&dest
)
1228 copy_deep(len
, dest
);
1231 template<bool is_const
>
1232 void buffer::list::iterator_impl
<is_const
>::copy_deep(unsigned len
, ptr
&dest
)
1238 throw end_of_buffer();
1239 assert(p
->length() > 0);
1241 copy(len
, dest
.c_str());
1243 template<bool is_const
>
1244 void buffer::list::iterator_impl
<is_const
>::copy_shallow(unsigned len
,
1251 throw end_of_buffer();
1252 assert(p
->length() > 0);
1253 unsigned howmuch
= p
->length() - p_off
;
1254 if (howmuch
< len
) {
1256 copy(len
, dest
.c_str());
1258 dest
= ptr(*p
, p_off
, len
);
1263 template<bool is_const
>
1264 void buffer::list::iterator_impl
<is_const
>::copy(unsigned len
, list
&dest
)
1270 throw end_of_buffer();
1272 unsigned howmuch
= p
->length() - p_off
;
1275 dest
.append(*p
, p_off
, howmuch
);
1282 template<bool is_const
>
1283 void buffer::list::iterator_impl
<is_const
>::copy(unsigned len
, std::string
&dest
)
1289 throw end_of_buffer();
1291 unsigned howmuch
= p
->length() - p_off
;
1292 const char *c_str
= p
->c_str();
1295 dest
.append(c_str
+ p_off
, howmuch
);
1302 template<bool is_const
>
1303 void buffer::list::iterator_impl
<is_const
>::copy_all(list
&dest
)
1310 assert(p
->length() > 0);
1312 unsigned howmuch
= p
->length() - p_off
;
1313 const char *c_str
= p
->c_str();
1314 dest
.append(c_str
+ p_off
, howmuch
);
1320 template<bool is_const
>
1321 size_t buffer::list::iterator_impl
<is_const
>::get_ptr_and_advance(
1322 size_t want
, const char **data
)
1324 if (p
== ls
->end()) {
1326 if (p
== ls
->end()) {
1330 *data
= p
->c_str() + p_off
;
1331 size_t l
= MIN(p
->length() - p_off
, want
);
1333 if (p_off
== p
->length()) {
1341 template<bool is_const
>
1342 uint32_t buffer::list::iterator_impl
<is_const
>::crc32c(
1343 size_t length
, uint32_t crc
)
1345 length
= MIN( length
, get_remaining());
1346 while (length
> 0) {
1348 size_t l
= get_ptr_and_advance(length
, &p
);
1349 crc
= ceph_crc32c(crc
, (unsigned char*)p
, l
);
1355 // explicitly instantiate only the iterator types we need, so we can hide the
1356 // details in this compilation unit without introducing unnecessary link time
1358 template class buffer::list::iterator_impl
<true>;
1359 template class buffer::list::iterator_impl
<false>;
1361 buffer::list::iterator::iterator(bl_t
*l
, unsigned o
)
1362 : iterator_impl(l
, o
)
1365 buffer::list::iterator::iterator(bl_t
*l
, unsigned o
, list_iter_t ip
, unsigned po
)
1366 : iterator_impl(l
, o
, ip
, po
)
1369 void buffer::list::iterator::advance(int o
)
1371 buffer::list::iterator_impl
<false>::advance(o
);
1374 void buffer::list::iterator::seek(unsigned o
)
1376 buffer::list::iterator_impl
<false>::seek(o
);
1379 char buffer::list::iterator::operator*()
1381 if (p
== ls
->end()) {
1382 throw end_of_buffer();
1387 buffer::list::iterator
& buffer::list::iterator::operator++()
1389 buffer::list::iterator_impl
<false>::operator++();
1393 buffer::ptr
buffer::list::iterator::get_current_ptr()
1395 if (p
== ls
->end()) {
1396 throw end_of_buffer();
1398 return ptr(*p
, p_off
, p
->length() - p_off
);
1401 void buffer::list::iterator::copy(unsigned len
, char *dest
)
1403 return buffer::list::iterator_impl
<false>::copy(len
, dest
);
1406 void buffer::list::iterator::copy(unsigned len
, ptr
&dest
)
1408 return buffer::list::iterator_impl
<false>::copy_deep(len
, dest
);
1411 void buffer::list::iterator::copy_deep(unsigned len
, ptr
&dest
)
1413 buffer::list::iterator_impl
<false>::copy_deep(len
, dest
);
1416 void buffer::list::iterator::copy_shallow(unsigned len
, ptr
&dest
)
1418 buffer::list::iterator_impl
<false>::copy_shallow(len
, dest
);
1421 void buffer::list::iterator::copy(unsigned len
, list
&dest
)
1423 buffer::list::iterator_impl
<false>::copy(len
, dest
);
1426 void buffer::list::iterator::copy(unsigned len
, std::string
&dest
)
1428 buffer::list::iterator_impl
<false>::copy(len
, dest
);
1431 void buffer::list::iterator::copy_all(list
&dest
)
1433 buffer::list::iterator_impl
<false>::copy_all(dest
);
1436 void buffer::list::iterator::copy_in(unsigned len
, const char *src
)
1438 copy_in(len
, src
, true);
1442 void buffer::list::iterator::copy_in(unsigned len
, const char *src
, bool crc_reset
)
1449 throw end_of_buffer();
1451 unsigned howmuch
= p
->length() - p_off
;
1454 p
->copy_in(p_off
, howmuch
, src
, crc_reset
);
1462 void buffer::list::iterator::copy_in(unsigned len
, const list
& otherl
)
1466 unsigned left
= len
;
1467 for (std::list
<ptr
>::const_iterator i
= otherl
._buffers
.begin();
1468 i
!= otherl
._buffers
.end();
1470 unsigned l
= (*i
).length();
1473 copy_in(l
, i
->c_str());
1480 // -- buffer::list --
1482 buffer::list::list(list
&& other
)
1483 : _buffers(std::move(other
._buffers
)),
1485 _memcopy_count(other
._memcopy_count
),
1487 append_buffer
.swap(other
.append_buffer
);
1491 void buffer::list::swap(list
& other
)
1493 std::swap(_len
, other
._len
);
1494 std::swap(_memcopy_count
, other
._memcopy_count
);
1495 std::swap(_mempool
, other
._mempool
);
1496 _buffers
.swap(other
._buffers
);
1497 append_buffer
.swap(other
.append_buffer
);
1498 //last_p.swap(other.last_p);
1500 other
.last_p
= other
.begin();
1503 bool buffer::list::contents_equal(buffer::list
& other
)
1505 return static_cast<const buffer::list
*>(this)->contents_equal(other
);
1508 bool buffer::list::contents_equal(const ceph::buffer::list
& other
) const
1510 if (length() != other
.length())
1513 // buffer-wise comparison
1515 std::list
<ptr
>::const_iterator a
= _buffers
.begin();
1516 std::list
<ptr
>::const_iterator b
= other
._buffers
.begin();
1517 unsigned aoff
= 0, boff
= 0;
1518 while (a
!= _buffers
.end()) {
1519 unsigned len
= a
->length() - aoff
;
1520 if (len
> b
->length() - boff
)
1521 len
= b
->length() - boff
;
1522 if (memcmp(a
->c_str() + aoff
, b
->c_str() + boff
, len
) != 0)
1525 if (aoff
== a
->length()) {
1530 if (boff
== b
->length()) {
1535 assert(b
== other
._buffers
.end());
1539 // byte-wise comparison
1541 bufferlist::const_iterator me
= begin();
1542 bufferlist::const_iterator him
= other
.begin();
1553 bool buffer::list::can_zero_copy() const
1555 for (std::list
<ptr
>::const_iterator it
= _buffers
.begin();
1556 it
!= _buffers
.end();
1558 if (!it
->can_zero_copy())
1563 bool buffer::list::is_provided_buffer(const char *dst
) const
1565 if (_buffers
.empty())
1567 return (is_contiguous() && (_buffers
.front().c_str() == dst
));
1570 bool buffer::list::is_aligned(unsigned align
) const
1572 for (std::list
<ptr
>::const_iterator it
= _buffers
.begin();
1573 it
!= _buffers
.end();
1575 if (!it
->is_aligned(align
))
1580 bool buffer::list::is_n_align_sized(unsigned align
) const
1582 for (std::list
<ptr
>::const_iterator it
= _buffers
.begin();
1583 it
!= _buffers
.end();
1585 if (!it
->is_n_align_sized(align
))
1590 bool buffer::list::is_aligned_size_and_memory(unsigned align_size
,
1591 unsigned align_memory
) const
1593 for (std::list
<ptr
>::const_iterator it
= _buffers
.begin();
1594 it
!= _buffers
.end();
1596 if (!it
->is_aligned(align_memory
) || !it
->is_n_align_sized(align_size
))
1602 bool buffer::list::is_zero() const {
1603 for (std::list
<ptr
>::const_iterator it
= _buffers
.begin();
1604 it
!= _buffers
.end();
1606 if (!it
->is_zero()) {
1613 void buffer::list::zero()
1615 for (std::list
<ptr
>::iterator it
= _buffers
.begin();
1616 it
!= _buffers
.end();
1621 void buffer::list::zero(unsigned o
, unsigned l
)
1623 assert(o
+l
<= _len
);
1625 for (std::list
<ptr
>::iterator it
= _buffers
.begin();
1626 it
!= _buffers
.end();
1628 if (p
+ it
->length() > o
) {
1629 if (p
>= o
&& p
+it
->length() <= o
+l
) {
1630 // 'o'------------- l -----------|
1631 // 'p'-- it->length() --|
1633 } else if (p
>= o
) {
1634 // 'o'------------- l -----------|
1635 // 'p'------- it->length() -------|
1637 } else if (p
+ it
->length() <= o
+l
) {
1638 // 'o'------------- l -----------|
1639 // 'p'------- it->length() -------|
1640 it
->zero(o
-p
, it
->length()-(o
-p
));
1642 // 'o'----------- l -----------|
1643 // 'p'---------- it->length() ----------|
1653 bool buffer::list::is_contiguous() const
1655 return &(*_buffers
.begin()) == &(*_buffers
.rbegin());
1658 bool buffer::list::is_n_page_sized() const
1660 return is_n_align_sized(CEPH_PAGE_SIZE
);
1663 bool buffer::list::is_page_aligned() const
1665 return is_aligned(CEPH_PAGE_SIZE
);
1668 void buffer::list::reassign_to_mempool(int pool
)
1671 if (append_buffer
.get_raw()) {
1672 append_buffer
.get_raw()->reassign_to_mempool(pool
);
1674 for (auto& p
: _buffers
) {
1675 p
.get_raw()->reassign_to_mempool(pool
);
1679 void buffer::list::try_assign_to_mempool(int pool
)
1682 if (append_buffer
.get_raw()) {
1683 append_buffer
.get_raw()->try_assign_to_mempool(pool
);
1685 for (auto& p
: _buffers
) {
1686 p
.get_raw()->try_assign_to_mempool(pool
);
1690 void buffer::list::rebuild()
1697 if ((_len
& ~CEPH_PAGE_MASK
) == 0)
1698 nb
= buffer::create_page_aligned(_len
);
1700 nb
= buffer::create(_len
);
1704 void buffer::list::rebuild(ptr
& nb
)
1707 for (std::list
<ptr
>::iterator it
= _buffers
.begin();
1708 it
!= _buffers
.end();
1710 nb
.copy_in(pos
, it
->length(), it
->c_str(), false);
1711 pos
+= it
->length();
1713 _memcopy_count
+= pos
;
1716 _buffers
.push_back(nb
);
1721 bool buffer::list::rebuild_aligned(unsigned align
)
1723 return rebuild_aligned_size_and_memory(align
, align
);
1726 bool buffer::list::rebuild_aligned_size_and_memory(unsigned align_size
,
1727 unsigned align_memory
)
1729 unsigned old_memcopy_count
= _memcopy_count
;
1730 std::list
<ptr
>::iterator p
= _buffers
.begin();
1731 while (p
!= _buffers
.end()) {
1732 // keep anything that's already align and sized aligned
1733 if (p
->is_aligned(align_memory
) && p
->is_n_align_sized(align_size
)) {
1734 /*cout << " segment " << (void*)p->c_str()
1735 << " offset " << ((unsigned long)p->c_str() & (align - 1))
1736 << " length " << p->length()
1737 << " " << (p->length() & (align - 1)) << " ok" << std::endl;
1743 // consolidate unaligned items, until we get something that is sized+aligned
1745 unsigned offset
= 0;
1747 /*cout << " segment " << (void*)p->c_str()
1748 << " offset " << ((unsigned long)p->c_str() & (align - 1))
1749 << " length " << p->length() << " " << (p->length() & (align - 1))
1750 << " overall offset " << offset << " " << (offset & (align - 1))
1751 << " not ok" << std::endl;
1753 offset
+= p
->length();
1754 unaligned
.push_back(*p
);
1755 _buffers
.erase(p
++);
1756 } while (p
!= _buffers
.end() &&
1757 (!p
->is_aligned(align_memory
) ||
1758 !p
->is_n_align_sized(align_size
) ||
1759 (offset
% align_size
)));
1760 if (!(unaligned
.is_contiguous() && unaligned
._buffers
.front().is_aligned(align_memory
))) {
1761 ptr
nb(buffer::create_aligned(unaligned
._len
, align_memory
));
1762 unaligned
.rebuild(nb
);
1763 _memcopy_count
+= unaligned
._len
;
1765 _buffers
.insert(p
, unaligned
._buffers
.front());
1769 return (old_memcopy_count
!= _memcopy_count
);
1772 bool buffer::list::rebuild_page_aligned()
1774 return rebuild_aligned(CEPH_PAGE_SIZE
);
1777 void buffer::list::reserve(size_t prealloc
)
1779 if (append_buffer
.unused_tail_length() < prealloc
) {
1780 append_buffer
= buffer::create(prealloc
);
1781 if (_mempool
>= 0) {
1782 append_buffer
.get_raw()->reassign_to_mempool(_mempool
);
1784 append_buffer
.set_length(0); // unused, so far.
1788 // sort-of-like-assignment-op
1789 void buffer::list::claim(list
& bl
, unsigned int flags
)
1793 claim_append(bl
, flags
);
1796 void buffer::list::claim_append(list
& bl
, unsigned int flags
)
1798 // steal the other guy's buffers
1800 if (!(flags
& CLAIM_ALLOW_NONSHAREABLE
))
1801 bl
.make_shareable();
1802 _buffers
.splice(_buffers
.end(), bl
._buffers
);
1804 bl
.last_p
= bl
.begin();
1807 void buffer::list::claim_prepend(list
& bl
, unsigned int flags
)
1809 // steal the other guy's buffers
1811 if (!(flags
& CLAIM_ALLOW_NONSHAREABLE
))
1812 bl
.make_shareable();
1813 _buffers
.splice(_buffers
.begin(), bl
._buffers
);
1815 bl
.last_p
= bl
.begin();
1818 void buffer::list::claim_append_piecewise(list
& bl
)
1820 // steal the other guy's buffers
1821 for (std::list
<buffer::ptr
>::const_iterator i
= bl
.buffers().begin();
1822 i
!= bl
.buffers().end(); i
++) {
1823 append(*i
, 0, i
->length());
1828 void buffer::list::copy(unsigned off
, unsigned len
, char *dest
) const
1830 if (off
+ len
> length())
1831 throw end_of_buffer();
1832 if (last_p
.get_off() != off
)
1834 last_p
.copy(len
, dest
);
1837 void buffer::list::copy(unsigned off
, unsigned len
, list
&dest
) const
1839 if (off
+ len
> length())
1840 throw end_of_buffer();
1841 if (last_p
.get_off() != off
)
1843 last_p
.copy(len
, dest
);
1846 void buffer::list::copy(unsigned off
, unsigned len
, std::string
& dest
) const
1848 if (last_p
.get_off() != off
)
1850 return last_p
.copy(len
, dest
);
1853 void buffer::list::copy_in(unsigned off
, unsigned len
, const char *src
)
1855 copy_in(off
, len
, src
, true);
1858 void buffer::list::copy_in(unsigned off
, unsigned len
, const char *src
, bool crc_reset
)
1860 if (off
+ len
> length())
1861 throw end_of_buffer();
1863 if (last_p
.get_off() != off
)
1865 last_p
.copy_in(len
, src
, crc_reset
);
1868 void buffer::list::copy_in(unsigned off
, unsigned len
, const list
& src
)
1870 if (last_p
.get_off() != off
)
1872 last_p
.copy_in(len
, src
);
1875 void buffer::list::append(char c
)
1877 // put what we can into the existing append_buffer.
1878 unsigned gap
= append_buffer
.unused_tail_length();
1880 // make a new append_buffer!
1881 append_buffer
= raw_combined::create(CEPH_BUFFER_APPEND_SIZE
);
1882 append_buffer
.set_length(0); // unused, so far.
1883 if (_mempool
>= 0) {
1884 append_buffer
.get_raw()->reassign_to_mempool(_mempool
);
1887 append(append_buffer
, append_buffer
.append(c
) - 1, 1); // add segment to the list
1890 void buffer::list::append(const char *data
, unsigned len
)
1893 // put what we can into the existing append_buffer.
1894 unsigned gap
= append_buffer
.unused_tail_length();
1896 if (gap
> len
) gap
= len
;
1897 //cout << "append first char is " << data[0] << ", last char is " << data[len-1] << std::endl;
1898 append_buffer
.append(data
, gap
);
1899 append(append_buffer
, append_buffer
.length() - gap
, gap
); // add segment to the list
1906 // make a new append_buffer. fill out a complete page, factoring in the
1907 // raw_combined overhead.
1908 size_t need
= ROUND_UP_TO(len
, sizeof(size_t)) + sizeof(raw_combined
);
1909 size_t alen
= ROUND_UP_TO(need
, CEPH_BUFFER_ALLOC_UNIT
) -
1910 sizeof(raw_combined
);
1911 append_buffer
= raw_combined::create(alen
);
1912 append_buffer
.set_length(0); // unused, so far.
1913 if (_mempool
>= 0) {
1914 append_buffer
.get_raw()->reassign_to_mempool(_mempool
);
1919 void buffer::list::append(const ptr
& bp
)
1925 void buffer::list::append(ptr
&& bp
)
1928 push_back(std::move(bp
));
1931 void buffer::list::append(const ptr
& bp
, unsigned off
, unsigned len
)
1933 assert(len
+off
<= bp
.length());
1934 if (!_buffers
.empty()) {
1935 ptr
&l
= _buffers
.back();
1936 if (l
.get_raw() == bp
.get_raw() &&
1937 l
.end() == bp
.start() + off
) {
1938 // yay contiguous with tail bp!
1939 l
.set_length(l
.length()+len
);
1944 // add new item to list
1945 push_back(ptr(bp
, off
, len
));
1948 void buffer::list::append(const list
& bl
)
1951 for (std::list
<ptr
>::const_iterator p
= bl
._buffers
.begin();
1952 p
!= bl
._buffers
.end();
1954 _buffers
.push_back(*p
);
1957 void buffer::list::append(std::istream
& in
)
1962 append(s
.c_str(), s
.length());
1968 void buffer::list::prepend_zero(unsigned len
)
1973 _buffers
.emplace_front(std::move(bp
));
1976 void buffer::list::append_zero(unsigned len
)
1980 append(std::move(bp
));
1987 const char& buffer::list::operator[](unsigned n
) const
1990 throw end_of_buffer();
1992 for (std::list
<ptr
>::const_iterator p
= _buffers
.begin();
1993 p
!= _buffers
.end();
1995 if (n
>= p
->length()) {
2005 * return a contiguous ptr to whole bufferlist contents.
2007 char *buffer::list::c_str()
2009 if (_buffers
.empty())
2010 return 0; // no buffers
2012 std::list
<ptr
>::const_iterator iter
= _buffers
.begin();
2015 if (iter
!= _buffers
.end())
2017 return _buffers
.front().c_str(); // good, we're already contiguous.
2020 string
buffer::list::to_str() const {
2022 s
.reserve(length());
2023 for (std::list
<ptr
>::const_iterator p
= _buffers
.begin();
2024 p
!= _buffers
.end();
2027 s
.append(p
->c_str(), p
->length());
2033 char *buffer::list::get_contiguous(unsigned orig_off
, unsigned len
)
2035 if (orig_off
+ len
> length())
2036 throw end_of_buffer();
2042 unsigned off
= orig_off
;
2043 std::list
<ptr
>::iterator curbuf
= _buffers
.begin();
2044 while (off
> 0 && off
>= curbuf
->length()) {
2045 off
-= curbuf
->length();
2049 if (off
+ len
> curbuf
->length()) {
2051 unsigned l
= off
+ len
;
2054 if (l
>= curbuf
->length())
2055 l
-= curbuf
->length();
2058 tmp
.append(*curbuf
);
2059 curbuf
= _buffers
.erase(curbuf
);
2061 } while (curbuf
!= _buffers
.end() && l
> 0);
2066 _buffers
.insert(curbuf
, tmp
._buffers
.front());
2067 return tmp
.c_str() + off
;
2070 last_p
= begin(); // we modified _buffers
2072 return curbuf
->c_str() + off
;
2075 void buffer::list::substr_of(const list
& other
, unsigned off
, unsigned len
)
2077 if (off
+ len
> other
.length())
2078 throw end_of_buffer();
2083 std::list
<ptr
>::const_iterator curbuf
= other
._buffers
.begin();
2085 off
>= curbuf
->length()) {
2087 //cout << "skipping over " << *curbuf << std::endl;
2088 off
-= (*curbuf
).length();
2091 assert(len
== 0 || curbuf
!= other
._buffers
.end());
2095 if (off
+ len
< curbuf
->length()) {
2096 //cout << "copying partial of " << *curbuf << std::endl;
2097 _buffers
.push_back( ptr( *curbuf
, off
, len
) );
2103 //cout << "copying end (all?) of " << *curbuf << std::endl;
2104 unsigned howmuch
= curbuf
->length() - off
;
2105 _buffers
.push_back( ptr( *curbuf
, off
, howmuch
) );
2114 void buffer::list::splice(unsigned off
, unsigned len
, list
*claim_by
/*, bufferlist& replace_with */)
2119 if (off
>= length())
2120 throw end_of_buffer();
2123 //cout << "splice off " << off << " len " << len << " ... mylen = " << length() << std::endl;
2126 std::list
<ptr
>::iterator curbuf
= _buffers
.begin();
2128 assert(curbuf
!= _buffers
.end());
2129 if (off
>= (*curbuf
).length()) {
2131 //cout << "off = " << off << " skipping over " << *curbuf << std::endl;
2132 off
-= (*curbuf
).length();
2135 // somewhere in this buffer!
2136 //cout << "off = " << off << " somewhere in " << *curbuf << std::endl;
2142 // add a reference to the front bit
2143 // insert it before curbuf (which we'll hose)
2144 //cout << "keeping front " << off << " of " << *curbuf << std::endl;
2145 _buffers
.insert( curbuf
, ptr( *curbuf
, 0, off
) );
2151 if (off
+ len
< (*curbuf
).length()) {
2152 //cout << "keeping end of " << *curbuf << ", losing first " << off+len << std::endl;
2154 claim_by
->append( *curbuf
, off
, len
);
2155 (*curbuf
).set_offset( off
+len
+ (*curbuf
).offset() ); // ignore beginning big
2156 (*curbuf
).set_length( (*curbuf
).length() - (len
+off
) );
2158 //cout << " now " << *curbuf << std::endl;
2162 // hose though the end
2163 unsigned howmuch
= (*curbuf
).length() - off
;
2164 //cout << "discarding " << howmuch << " of " << *curbuf << std::endl;
2166 claim_by
->append( *curbuf
, off
, howmuch
);
2167 _len
-= (*curbuf
).length();
2168 _buffers
.erase( curbuf
++ );
2173 // splice in *replace (implement me later?)
2175 last_p
= begin(); // just in case we were in the removed region.
2178 void buffer::list::write(int off
, int len
, std::ostream
& out
) const
2181 s
.substr_of(*this, off
, len
);
2182 for (std::list
<ptr
>::const_iterator it
= s
._buffers
.begin();
2183 it
!= s
._buffers
.end();
2186 out
.write(it
->c_str(), it
->length());
2187 /*iterator p(this, off);
2188 while (len > 0 && !p.end()) {
2189 int l = p.left_in_this_buf();
2192 out.write(p.c_str(), l);
2197 void buffer::list::encode_base64(buffer::list
& o
)
2199 bufferptr
bp(length() * 4 / 3 + 3);
2200 int l
= ceph_armor(bp
.c_str(), bp
.c_str() + bp
.length(), c_str(), c_str() + length());
2202 o
.push_back(std::move(bp
));
2205 void buffer::list::decode_base64(buffer::list
& e
)
2207 bufferptr
bp(4 + ((e
.length() * 3) / 4));
2208 int l
= ceph_unarmor(bp
.c_str(), bp
.c_str() + bp
.length(), e
.c_str(), e
.c_str() + e
.length());
2210 std::ostringstream oss
;
2211 oss
<< "decode_base64: decoding failed:\n";
2213 throw buffer::malformed_input(oss
.str().c_str());
2215 assert(l
<= (int)bp
.length());
2217 push_back(std::move(bp
));
2222 int buffer::list::read_file(const char *fn
, std::string
*error
)
2224 int fd
= TEMP_FAILURE_RETRY(::open(fn
, O_RDONLY
));
2227 std::ostringstream oss
;
2228 oss
<< "can't open " << fn
<< ": " << cpp_strerror(err
);
2234 memset(&st
, 0, sizeof(st
));
2235 if (::fstat(fd
, &st
) < 0) {
2237 std::ostringstream oss
;
2238 oss
<< "bufferlist::read_file(" << fn
<< "): stat error: "
2239 << cpp_strerror(err
);
2241 VOID_TEMP_FAILURE_RETRY(::close(fd
));
2245 ssize_t ret
= read_fd(fd
, st
.st_size
);
2247 std::ostringstream oss
;
2248 oss
<< "bufferlist::read_file(" << fn
<< "): read error:"
2249 << cpp_strerror(ret
);
2251 VOID_TEMP_FAILURE_RETRY(::close(fd
));
2254 else if (ret
!= st
.st_size
) {
2256 // Perhaps the file changed between stat() and read()?
2257 std::ostringstream oss
;
2258 oss
<< "bufferlist::read_file(" << fn
<< "): warning: got premature EOF.";
2260 // not actually an error, but weird
2262 VOID_TEMP_FAILURE_RETRY(::close(fd
));
2266 ssize_t
buffer::list::read_fd(int fd
, size_t len
)
2268 // try zero copy first
2269 if (false && read_fd_zero_copy(fd
, len
) == 0) {
2270 // TODO fix callers to not require correct read size, which is not
2271 // available for raw_pipe until we actually inspect the data
2274 bufferptr bp
= buffer::create(len
);
2275 ssize_t ret
= safe_read(fd
, (void*)bp
.c_str(), len
);
2278 append(std::move(bp
));
2283 int buffer::list::read_fd_zero_copy(int fd
, size_t len
)
2285 #ifdef CEPH_HAVE_SPLICE
2287 append(buffer::create_zero_copy(len
, fd
, NULL
));
2288 } catch (buffer::error_code
&e
) {
2290 } catch (buffer::malformed_input
&e
) {
2299 int buffer::list::write_file(const char *fn
, int mode
)
2301 int fd
= TEMP_FAILURE_RETRY(::open(fn
, O_WRONLY
|O_CREAT
|O_TRUNC
, mode
));
2304 cerr
<< "bufferlist::write_file(" << fn
<< "): failed to open file: "
2305 << cpp_strerror(err
) << std::endl
;
2308 int ret
= write_fd(fd
);
2310 cerr
<< "bufferlist::write_fd(" << fn
<< "): write_fd error: "
2311 << cpp_strerror(ret
) << std::endl
;
2312 VOID_TEMP_FAILURE_RETRY(::close(fd
));
2315 if (TEMP_FAILURE_RETRY(::close(fd
))) {
2317 cerr
<< "bufferlist::write_file(" << fn
<< "): close error: "
2318 << cpp_strerror(err
) << std::endl
;
2324 static int do_writev(int fd
, struct iovec
*vec
, uint64_t offset
, unsigned veclen
, unsigned bytes
)
2329 r
= ::pwritev(fd
, vec
, veclen
, offset
);
2331 r
= ::lseek64(fd
, offset
, SEEK_SET
);
2336 r
= ::writev(fd
, vec
, veclen
);
2346 if (bytes
== 0) break;
2349 if (vec
[0].iov_len
<= (size_t)r
) {
2350 // drain this whole item
2351 r
-= vec
[0].iov_len
;
2355 vec
[0].iov_base
= (char *)vec
[0].iov_base
+ r
;
2356 vec
[0].iov_len
-= r
;
2364 int buffer::list::write_fd(int fd
) const
2366 if (can_zero_copy())
2367 return write_fd_zero_copy(fd
);
2374 std::list
<ptr
>::const_iterator p
= _buffers
.begin();
2375 while (p
!= _buffers
.end()) {
2376 if (p
->length() > 0) {
2377 iov
[iovlen
].iov_base
= (void *)p
->c_str();
2378 iov
[iovlen
].iov_len
= p
->length();
2379 bytes
+= p
->length();
2384 if (iovlen
== IOV_MAX
-1 ||
2385 p
== _buffers
.end()) {
2390 wrote
= ::writev(fd
, start
, num
);
2397 if (wrote
< bytes
) {
2398 // partial write, recover!
2399 while ((size_t)wrote
>= start
[0].iov_len
) {
2400 wrote
-= start
[0].iov_len
;
2401 bytes
-= start
[0].iov_len
;
2406 start
[0].iov_len
-= wrote
;
2407 start
[0].iov_base
= (char *)start
[0].iov_base
+ wrote
;
2419 int buffer::list::write_fd(int fd
, uint64_t offset
) const
2423 std::list
<ptr
>::const_iterator p
= _buffers
.begin();
2424 uint64_t left_pbrs
= _buffers
.size();
2427 unsigned iovlen
= 0;
2428 uint64_t size
= MIN(left_pbrs
, IOV_MAX
);
2431 iov
[iovlen
].iov_base
= (void *)p
->c_str();
2432 iov
[iovlen
].iov_len
= p
->length();
2434 bytes
+= p
->length();
2439 int r
= do_writev(fd
, iov
, offset
, iovlen
, bytes
);
2447 int buffer::list::write_fd_zero_copy(int fd
) const
2449 if (!can_zero_copy())
2451 /* pass offset to each call to avoid races updating the fd seek
2452 * position, since the I/O may be non-blocking
2454 int64_t offset
= ::lseek(fd
, 0, SEEK_CUR
);
2455 int64_t *off_p
= &offset
;
2456 if (offset
< 0 && errno
!= ESPIPE
)
2458 if (errno
== ESPIPE
)
2460 for (std::list
<ptr
>::const_iterator it
= _buffers
.begin();
2461 it
!= _buffers
.end(); ++it
) {
2462 int r
= it
->zero_copy_to_fd(fd
, off_p
);
2466 offset
+= it
->length();
2471 __u32
buffer::list::crc32c(__u32 crc
) const
2473 for (std::list
<ptr
>::const_iterator it
= _buffers
.begin();
2474 it
!= _buffers
.end();
2477 raw
*r
= it
->get_raw();
2478 pair
<size_t, size_t> ofs(it
->offset(), it
->offset() + it
->length());
2479 pair
<uint32_t, uint32_t> ccrc
;
2480 if (r
->get_crc(ofs
, &ccrc
)) {
2481 if (ccrc
.first
== crc
) {
2484 if (buffer_track_crc
)
2485 buffer_cached_crc
++;
2487 /* If we have cached crc32c(buf, v) for initial value v,
2488 * we can convert this to a different initial value v' by:
2489 * crc32c(buf, v') = crc32c(buf, v) ^ adjustment
2490 * where adjustment = crc32c(0*len(buf), v ^ v')
2492 * http://crcutil.googlecode.com/files/crc-doc.1.0.pdf
2493 * note, u for our crc32c implementation is 0
2495 crc
= ccrc
.second
^ ceph_crc32c(ccrc
.first
^ crc
, NULL
, it
->length());
2496 if (buffer_track_crc
)
2497 buffer_cached_crc_adjusted
++;
2500 if (buffer_track_crc
)
2501 buffer_missed_crc
++;
2502 uint32_t base
= crc
;
2503 crc
= ceph_crc32c(crc
, (unsigned char*)it
->c_str(), it
->length());
2504 r
->set_crc(ofs
, make_pair(base
, crc
));
2511 void buffer::list::invalidate_crc()
2513 for (std::list
<ptr
>::const_iterator p
= _buffers
.begin(); p
!= _buffers
.end(); ++p
) {
2514 raw
*r
= p
->get_raw();
2516 r
->invalidate_crc();
2522 * Binary write all contents to a C++ stream
2524 void buffer::list::write_stream(std::ostream
&out
) const
2526 for (std::list
<ptr
>::const_iterator p
= _buffers
.begin(); p
!= _buffers
.end(); ++p
) {
2527 if (p
->length() > 0) {
2528 out
.write(p
->c_str(), p
->length());
2534 void buffer::list::hexdump(std::ostream
&out
, bool trailing_newline
) const
2539 std::ios_base::fmtflags original_flags
= out
.flags();
2541 // do our best to match the output of hexdump -C, for better
2544 out
.setf(std::ios::right
);
2548 bool was_zeros
= false, did_star
= false;
2549 for (unsigned o
=0; o
<length(); o
+= per
) {
2550 bool row_is_zeros
= false;
2551 if (o
+ per
< length()) {
2552 row_is_zeros
= true;
2553 for (unsigned i
=0; i
<per
&& o
+i
<length(); i
++) {
2555 row_is_zeros
= false;
2574 out
<< std::hex
<< std::setw(8) << o
<< " ";
2577 for (i
=0; i
<per
&& o
+i
<length(); i
++) {
2580 out
<< " " << std::setw(2) << ((unsigned)(*this)[o
+i
] & 0xff);
2582 for (; i
<per
; i
++) {
2589 for (i
=0; i
<per
&& o
+i
<length(); i
++) {
2590 char c
= (*this)[o
+i
];
2591 if (isupper(c
) || islower(c
) || isdigit(c
) || c
== ' ' || ispunct(c
))
2596 out
<< '|' << std::dec
;
2598 if (trailing_newline
) {
2599 out
<< "\n" << std::hex
<< std::setw(8) << length();
2603 out
.flags(original_flags
);
2607 buffer::list
buffer::list::static_from_mem(char* c
, size_t l
) {
2609 bl
.push_back(ptr(create_static(l
, c
)));
2613 buffer::list
buffer::list::static_from_cstring(char* c
) {
2614 return static_from_mem(c
, std::strlen(c
));
2617 buffer::list
buffer::list::static_from_string(string
& s
) {
2618 // C++14 just has string::data return a char* from a non-const
2620 return static_from_mem(const_cast<char*>(s
.data()), s
.length());
2621 // But the way buffer::list mostly doesn't work in a sane way with
2622 // const makes me generally sad.
2625 std::ostream
& buffer::operator<<(std::ostream
& out
, const buffer::raw
&r
) {
2626 return out
<< "buffer::raw(" << (void*)r
.data
<< " len " << r
.len
<< " nref " << r
.nref
.load() << ")";
2629 std::ostream
& buffer::operator<<(std::ostream
& out
, const buffer::ptr
& bp
) {
2631 out
<< "buffer::ptr(" << bp
.offset() << "~" << bp
.length()
2632 << " " << (void*)bp
.c_str()
2633 << " in raw " << (void*)bp
.raw_c_str()
2634 << " len " << bp
.raw_length()
2635 << " nref " << bp
.raw_nref() << ")";
2637 out
<< "buffer:ptr(" << bp
.offset() << "~" << bp
.length() << " no raw)";
2641 std::ostream
& buffer::operator<<(std::ostream
& out
, const buffer::list
& bl
) {
2642 out
<< "buffer::list(len=" << bl
.length() << "," << std::endl
;
2644 std::list
<buffer::ptr
>::const_iterator it
= bl
.buffers().begin();
2645 while (it
!= bl
.buffers().end()) {
2647 if (++it
== bl
.buffers().end()) break;
2648 out
<< "," << std::endl
;
2650 out
<< std::endl
<< ")";
2654 std::ostream
& buffer::operator<<(std::ostream
& out
, const buffer::error
& e
)
2656 return out
<< e
.what();
2659 MEMPOOL_DEFINE_OBJECT_FACTORY(buffer::raw_malloc
, buffer_raw_malloc
,
2661 MEMPOOL_DEFINE_OBJECT_FACTORY(buffer::raw_mmap_pages
, buffer_raw_mmap_pagse
,
2663 MEMPOOL_DEFINE_OBJECT_FACTORY(buffer::raw_posix_aligned
,
2664 buffer_raw_posix_aligned
, buffer_meta
);
2665 #ifdef CEPH_HAVE_SPLICE
2666 MEMPOOL_DEFINE_OBJECT_FACTORY(buffer::raw_pipe
, buffer_raw_pipe
, buffer_meta
);
2668 MEMPOOL_DEFINE_OBJECT_FACTORY(buffer::raw_char
, buffer_raw_char
, buffer_meta
);
2669 MEMPOOL_DEFINE_OBJECT_FACTORY(buffer::raw_claimed_char
, buffer_raw_claimed_char
,
2671 MEMPOOL_DEFINE_OBJECT_FACTORY(buffer::raw_unshareable
, buffer_raw_unshareable
,
2673 MEMPOOL_DEFINE_OBJECT_FACTORY(buffer::raw_static
, buffer_raw_static
,