Skip to contentSkip to conversation

reducing buffer allocations

DISCUSSION Sage Weil 6 participants 9 messages Feb 4, 2016 – Feb 5, 2016

lore
Share & tools
lore

Sage Weil

author
#0 lore raw

https://github.com/ceph/ceph/pull/5534

does two things: (1) combine the buffer::raw reference count and buffer into a single allocation (ala make_shared), and (2) convert buffer::list into an intrusive_list, while embedding N (=3) buffer::ptr's in the buffer::raw so that most buffers require only 1 allocation (provided they aren't on more than N lists).

This needs a rebase, and some benchmarking. But before I pull it up, is this the right direction? I saw little benefit from (2) after doing (1), but I was doing a really simple microbenchmark. Do we want both, or just 1, or is there an alternative we should consider?

sage

#1 lore raw

On 04/02/2016, Sage Weil wrote:
[snip]

Quoted 3 lines

(2) convert buffer::list into an intrusive_list, while embedding N (=3) buffer::ptr's in the buffer::raw so that most buffers require only 1 allocation (provided they aren't on more than N lists).

[snip]

Quoted 2 lines

Do we want both, or just 1, or is there an alternative we should consider?

Do we have any places in the code where we seek into a buffer::list that has a good number of buffer::ptrs in it? If so then we could get a speedup by switching from an intrusive list to an intrusive set (or avlset).

Signature · 3 lines
Senior Software Engineer           Red Hat Storage, Ann Arbor, MI, US
IRC: Aemerson@{RedHat, OFTC, Freenode}
0x80F7544B90EDBFB9 E707 86BA 0C1B 62CC 152C  7C12 80F7 544B 90ED BFB9
author
#2 lore raw
Quoted 5 lines · Adam C. Emerson

On 04/02/2016, Sage Weil wrote:
[snip]

Quoted 3 lines

(2) convert buffer::list into an intrusive_list, while embedding N (=3) buffer::ptr's in the buffer::raw so that most buffers require only 1 allocation (provided they aren't on more than N lists).

Quoted 7 lines

[snip]

Quoted 2 lines

Do we want both, or just 1, or is there an alternative we should consider?

Do we have any places in the code where we seek into a buffer::list that has a good number of buffer::ptrs in it? If so then we could get a speedup by switching from an intrusive list to an intrusive set (or avlset).

There are a very few, but not many. They tend to be in debug code only.

Re: Allen's comment: we could use small_vector<> in combination with (1) too and probably get much of the benefit, but (1) + (2) should avoid the need for it at all...

sage

#3 lore raw
In reply to Sage Weil

Looks like another place for small_vector ;-)

Allen Samuels
Software Architect, Fellow, Systems and Software Solutions

2880 Junction Avenue, San Jose, CA 95134
T: +1 408 801 7030| M: +1 408 780 6416
[email protected]

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Sage Weil
Sent: Thursday, February 04, 2016 2:33 PM
To: [email protected]
Subject: reducing buffer allocations

https://github.com/ceph/ceph/pull/5534

does two things: (1) combine the buffer::raw reference count and buffer into a single allocation (ala make_shared), and (2) convert buffer::list into an intrusive_list, while embedding N (=3) buffer::ptr's in the buffer::raw so that most buffers require only 1 allocation (provided they aren't on more than N lists).

This needs a rebase, and some benchmarking. But before I pull it up, is this the right direction? I saw little benefit from (2) after doing (1), but I was doing a really simple microbenchmark. Do we want both, or just 1, or is there an alternative we should consider?

sage
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PLEASE NOTE: The information contained in this electronic mail message is intended only for the use of the designated recipient(s) named above. If the reader of this message is not the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify the sender by telephone or e-mail (as shown above) immediately and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies).

#4 lore raw

This is the approach that Casey and I lean toward, but if the intrusive ptr change is ready now, I'd work on merging it.

Matt

----- Original Message -----

Quoted 55 lines

From: "Allen Samuels" <[email protected]>
To: "Sage Weil" <[email protected]>, [email protected]
Sent: Thursday, February 4, 2016 5:43:36 PM
Subject: RE: reducing buffer allocations

Looks like another place for small_vector ;-)

Allen Samuels
Software Architect, Fellow, Systems and Software Solutions

2880 Junction Avenue, San Jose, CA 95134
T: +1 408 801 7030| M: +1 408 780 6416
[email protected]

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Sage Weil
Sent: Thursday, February 04, 2016 2:33 PM
To: [email protected]
Subject: reducing buffer allocations

https://github.com/ceph/ceph/pull/5534

does two things: (1) combine the buffer::raw reference count and buffer into a single allocation (ala make_shared), and (2) convert buffer::list into an intrusive_list, while embedding N (=3) buffer::ptr's in the buffer::raw so that most buffers require only 1 allocation (provided they aren't on more than N lists).

This needs a rebase, and some benchmarking. But before I pull it up, is this the right direction? I saw little benefit from (2) after doing (1), but I was doing a really simple microbenchmark. Do we want both, or just 1, or is there an alternative we should consider?

sage
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected] More majordomo info at
http://vger.kernel.org/majordomo-info.html
PLEASE NOTE: The information contained in this electronic mail message is intended only for the use of the designated recipient(s) named above. If the reader of this message is not the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify the sender by telephone or e-mail (as shown above) immediately and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies).
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 11 lines
-- 
Matt Benjamin
Red Hat, Inc.
315 West Huron Street, Suite 140A
Ann Arbor, Michigan 48103

http://www.redhat.com/en/technologies/storage

tel.  734-707-0660
fax.  734-769-8938
cel.  734-216-5309
#5 lore raw

Agreed.

Allen Samuels
Software Architect, Fellow, Systems and Software Solutions

2880 Junction Avenue, San Jose, CA 95134
T: +1 408 801 7030| M: +1 408 780 6416
[email protected]

-----Original Message-----
From: Matt Benjamin [mailto:[email protected]]
Sent: Thursday, February 04, 2016 3:49 PM
To: Allen Samuels <[email protected]>
Cc: Sage Weil <[email protected]>; [email protected]
Subject: Re: reducing buffer allocations

This is the approach that Casey and I lean toward, but if the intrusive ptr change is ready now, I'd work on merging it.

Matt

----- Original Message -----

Quoted 45 lines

From: "Allen Samuels" <[email protected]>
To: "Sage Weil" <[email protected]>, [email protected]
Sent: Thursday, February 4, 2016 5:43:36 PM
Subject: RE: reducing buffer allocations

Looks like another place for small_vector ;-)

Allen Samuels
Software Architect, Fellow, Systems and Software Solutions

2880 Junction Avenue, San Jose, CA 95134
T: +1 408 801 7030| M: +1 408 780 6416 [email protected]

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Sage Weil
Sent: Thursday, February 04, 2016 2:33 PM
To: [email protected]
Subject: reducing buffer allocations

https://github.com/ceph/ceph/pull/5534

does two things: (1) combine the buffer::raw reference count and buffer into a single allocation (ala make_shared), and (2) convert buffer::list into an intrusive_list, while embedding N (=3) buffer::ptr's in the buffer::raw so that most buffers require only 1 allocation (provided they aren't on more than N lists).

This needs a rebase, and some benchmarking. But before I pull it up, is this the right direction? I saw little benefit from (2) after doing (1), but I was doing a really simple microbenchmark. Do we want both, or just 1, or is there an alternative we should consider?

sage
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PLEASE NOTE: The information contained in this electronic mail message is intended only for the use of the designated recipient(s) named above. If the reader of this message is not the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this

Quoted 9 lines

message is strictly prohibited. If you have received this
communication in error, please notify the sender by telephone or
e-mail (as shown above) immediately and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies). --
To unsubscribe from this list: send the line "unsubscribe ceph-devel"
in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
--
Matt Benjamin
Red Hat, Inc.
315 West Huron Street, Suite 140A
Ann Arbor, Michigan 48103

http://www.redhat.com/en/technologies/storage

tel.  734-707-0660
fax.  734-769-8938
cel.  734-216-5309

PLEASE NOTE: The information contained in this electronic mail message is intended only for the use of the designated recipient(s) named above. If the reader of this message is not the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify the sender by telephone or e-mail (as shown above) immediately and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies).

author
#6 lore raw
Quoted 2 lines

This is the approach that Casey and I lean toward, but if the intrusive ptr change is ready now, I'd work on merging it.

Well, it worked well enough to pass unit tests, but it was reasonably complicated with the inline ptrs, and I didn't the big change going from 2 allocations (buffer::raw/buffer and list<ptr> node/ptr) that I expected to see.

Hmm, a small_vector<> approach means 1 allocation per buffer, and in most cases will mean no additional allocation per bufferlist. I think this will make for much simpler code...

Perhaps we should just start with (1) (combine buffer::raw with the buffer itself), since that's a simpler change anyway, and understanding any tradeoffs there around whether it is a good idea for larger buffers is enough to keep us busy for a bit anyway?

sage

Quoted 73 lines

Matt

----- Original Message -----

Quoted 45 lines

From: "Allen Samuels" <[email protected]>
To: "Sage Weil" <[email protected]>, [email protected]
Sent: Thursday, February 4, 2016 5:43:36 PM
Subject: RE: reducing buffer allocations

Looks like another place for small_vector ;-)

Allen Samuels
Software Architect, Fellow, Systems and Software Solutions

2880 Junction Avenue, San Jose, CA 95134
T: +1 408 801 7030| M: +1 408 780 6416 [email protected]

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Sage Weil
Sent: Thursday, February 04, 2016 2:33 PM
To: [email protected]
Subject: reducing buffer allocations

https://github.com/ceph/ceph/pull/5534

does two things: (1) combine the buffer::raw reference count and buffer into a single allocation (ala make_shared), and (2) convert buffer::list into an intrusive_list, while embedding N (=3) buffer::ptr's in the buffer::raw so that most buffers require only 1 allocation (provided they aren't on more than N lists).

This needs a rebase, and some benchmarking. But before I pull it up, is this the right direction? I saw little benefit from (2) after doing (1), but I was doing a really simple microbenchmark. Do we want both, or just 1, or is there an alternative we should consider?

sage
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PLEASE NOTE: The information contained in this electronic mail message is intended only for the use of the designated recipient(s) named above. If the reader of this message is not the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this

Quoted 9 lines

message is strictly prohibited. If you have received this
communication in error, please notify the sender by telephone or
e-mail (as shown above) immediately and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies). --
To unsubscribe from this list: send the line "unsubscribe ceph-devel"
in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
--
Matt Benjamin
Red Hat, Inc.
315 West Huron Street, Suite 140A
Ann Arbor, Michigan 48103

http://www.redhat.com/en/technologies/storage

tel.  734-707-0660
fax.  734-769-8938
cel.  734-216-5309

PLEASE NOTE: The information contained in this electronic mail message is intended only for the use of the designated recipient(s) named above. If the reader of this message is not the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify the sender by telephone or e-mail (as shown above) immediately and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies). N?????r??y??????X??ǧv???)޺{.n?????z?]z????ay?ʇڙ??j??f???h??????w??? ???j:+v???w????????????zZ+???????j"????i

#7 lore raw
In reply to Sage Weil
Quoted 8 lines · Sage Weil

https://github.com/ceph/ceph/pull/5534

does two things: (1) combine the buffer::raw reference count and buffer into a single allocation (ala make_shared), and (2) convert buffer::list into an intrusive_list, while embedding N (=3) buffer::ptr's in the buffer::raw so that most buffers require only 1 allocation (provided they aren't on more than N lists).

Quoted 4 lines

This needs a rebase, and some benchmarking. But before I pull it up, is this the right direction? I saw little benefit from (2) after doing (1), but I was doing a really simple microbenchmark. Do we want both, or just 1, or is there an alternative we should consider?

I think a another way is separating the owner of memory and the reference to the part of memory. Like Buffer structure in RAMCloud(https://github.com/PlatformLab/RAMCloud/blob/master/src/Buffer.h)

1. The Buffer(like bufferlist) own "vector<void*> allocations" to store all associated memory to keep them. It will be convenient for addition malloc(like getRange)
2. Chunk(like ptr) maintain *next ptr instead std::list

Hmm, it doesn't have much difference. FYI

Quoted 6 lines

sage
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw
In reply to Sage Weil
Quoted 15 lines

-----Original Message-----
From: [email protected] [mailto:ceph-devel- [email protected]] On Behalf Of Sage Weil
Sent: Thursday, February 04, 2016 11:33 PM
To: [email protected]
Subject: reducing buffer allocations

https://github.com/ceph/ceph/pull/5534

does two things: (1) combine the buffer::raw reference count and buffer into a single allocation (ala make_shared), and (2) convert buffer::list into an intrusive_list, while embedding N (=3) buffer::ptr's in the buffer::raw so that most buffers require only 1 allocation (provided they aren't on more than N lists).

Quoted 4 lines

This needs a rebase, and some benchmarking. But before I pull it up, is this the right direction? I saw little benefit from (2) after doing (1), but I was doing a really simple microbenchmark. Do we want both, or just 1, or is there an alternative we should consider?

If that adds something to discussion, I found out that during my rados bench runs, bufferlists contain *up to* 1 bufferptr 80% of time, and since bufferlists are routinely used even as a temporary storage, in many cases they're (bufferlists and bufferptrs) allocated and freed within the same function or thread, so that might be reason for odd perf results. One thing that we're missing for sure, is something that is bufferlist-compatible (so we could pass their iterators to existing encode() and decode() functions) and is not so elaborate - prime example would be key/value store code, where we have stuff like

(https://github.com/ceph/ceph/blob/master/src/kv/LevelDBStore.cc#L250)

  KeyValueDB::Iterator it = get_iterator(prefix);
  for (std::set<string>::const_iterator i = keys.begin();
       i != keys.end();
       ++i) {
    it->lower_bound(*i);
    if (it->valid() && it->key() == *i) {
      out->insert(make_pair(*i, it->value()));
    } else if (!it->valid())
      break;
  }

(https://github.com/ceph/ceph/blob/master/src/kv/LevelDBStore.h#L298)

And it->value() is:

    bufferlist value() {
      return to_bufferlist(dbiter->value());
    }

Finally, to_bufferlist() is:

(https://github.com/ceph/ceph/blob/master/src/kv/LevelDBStore.cc#L294)

bufferlist LevelDBStore::to_bufferlist(leveldb::Slice in) {
bufferlist bl;
bl.append(bufferptr(in.data(), in.size())); return bl;
}

So, in the end, we're constructing such monstrosity:

+----------------+
|   bufferlist   |
+----------------+        +-----------+
| _buffers       | ------ | bufferptr |
| _len           |        +-----------+         +--------------+
| _memcopy_count |        | raw*      |  ------ + buffer::raw  |
| append_buffer  |        | _off      |         +--------------+
+----------------+        | _len      |         | *data        | ---- { actual data }
                          +-----------+         | len          |
                                                | nref         |
                                                | crc_spinlock |
                                                | crc_map      |
                                                +--------------+

(see http://ceph.predictor.org.pl/bufferptr.txt if Outlook screws up the above ascii art)

My point here is that Ceph is using bufferlists not only in cases where it needs to store non-contiguous memory areas as logically-contiguous one, but also for single, self-contained blocks of data. But, there's no other choice yet when someone wants to get data off RocksDB/LevelDB and parse it using existing encode()/decode() functions.

Sage's idea (again, consult above link if my ascii-art won't line up):

+----------------------+
|     bufferlist       |
+----------------------+ 
| _buffers             | 
| _len                 | 
| _memcopy_count       | 
| append_buffer        | 
| +------------------+ | 
| |    bufferptr     | | 
| +------------------+ | 
| | +--------------+ | |
| | + buffer::raw  | | |
| | +--------------+ | |
| | | *data        | | |  ---- { actual data }
| | | len          | | |
| | | nref         | | |
| | | crc_spinlock | | |
| | | crc_map      | | | 
| | +--------------+ | | 
| | _len             | |
| | _off             | |
| +------------------+ | 
|                      |
| +------------------+ | 
| |    bufferptr     | | 
| +------------------+ | 
| | +--------------+ | |
| | + buffer::raw  | | |
| | +--------------+ | |
| | | *data        | | |  ---- { actual data }
| | | len          | | |
| | | nref         | | |
| | | crc_spinlock | | |
| | | crc_map      | | | 
| | +--------------+ | | 
| | _len             | |
| | _off             | |
| +------------------+ | 
|                      |
| +------------------+ | 
| |    bufferptr     | | 
| +------------------+ | 
| | +--------------+ | |
| | + buffer::raw  | | |
| | +--------------+ | |
| | | *data        | | |  ---- { actual data }
| | | len          | | |
| | | nref         | | |
| | | crc_spinlock | | |
| | | crc_map      | | | 
| | +--------------+ | | 
| | _len             | |
| | _off             | |
| +------------------+ | 
|                      |
+----------------------+

This is clearly a step forward from the previous design... and a step-back at the same time. For the case of fragmented bufferlists (like those used by messengers, with header, payload and footer being in separate bufferptrs) this reduces allocation count by six (we have already allocated all three bufferptrs and raws), but in case of K/V store, we're wasting memory for two bufferptrs and raws (around 160 bytes, if I'm calculating correctly). This starts to get interesting when you consider structures like

struct pg_log_entry_t {
[..]
// describes state for a locally-rollbackable entry ObjectModDesc mod_desc;
bufferlist snaps; // only for clone entries <------ *here*
hobject_t soid;
osd_reqid_t reqid; // caller+tid to uniquely identify request vector<pair<osd_reqid_t, version_t> > extra_reqids; eversion_t version, prior_version, reverting_to; version_t user_version; // the user version for this entry
utime_t mtime; // this is the _user_ mtime, mind you

__s32 op;
bool invalid_hash; // only when decoding sobject_t based entries bool invalid_pool; // only when decoding pool-less hobject based entries
[..]

Since we're storing tens of thousands of such things in single OSD memory, and cases where one physical server hosts just one OSD are rare, those 160 bytes starts to add up really quickly.

With best regards / Pozdrawiam
Piotr Dałek

--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html