Flow-IPC 3.0.0
Flow-IPC project: Full implementation reference.
pool_arena_stats.hpp
Go to the documentation of this file.
1/* Flow-IPC: Shared Memory
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
19#pragma once
20
22#include "ipc/shm/shm_stats.hpp"
23#include "ipc/util/util.hpp"
24#include <flow/util/stat/stat_set.hpp>
25#include <cstddef>
26#include <string>
27
29{
30
31// Types.
32
33/**
34 * Cumulative stats relevant to one SHM-classic arena -- in this case single SHM-pool in shared memory, accessed
35 * via 1+ Pool_arena objects over time.
36 *
37 * @see Local_stats, which tracks stats relevant to a particular Pool_arena *object*, as opposed to Arena_stats
38 * that tracks events in the in-SHM arena regardless of Pool_arena object. Since SHM-classic is
39 * an *arena-sharing* SHM-provider, by definition multiple arena objects act as handles to a shared
40 * (pool) arena.
41 * @see Namespace ipc::shm doc header introduces SHM-provider types including *arena-sharing* (shm::classic)
42 * versus *arena-lending* (arena_lend::jemalloc).
43 *
44 * The above distinction is important and might not be obvious at first glance, since Arena_stats is available
45 * to the user via a Pool_arena accessor (but tracks data potentially from before that Pool_arena existed).
46 *
47 * ### Design ###
48 * The members of Arena_stats, as you can see, are not themselves individual stat-members but rather
49 * `structs` from ipc::shm. This should not be difficult to follow -- see their docs for details about
50 * individual stats therein.
51 *
52 * The reason for using these sub-`struct`s is that they generalize beyond this SHM-provider (SHM-classic) and
53 * more importantly beyond this SHM-provider *type (arena-sharing)*. A different SHM-provider, such as
54 * SHM-jemalloc at least, by its nature will reuse many (~most) of the same stat-members in aggregate but
55 * organized differently. We omit details here, but if you have a look at the stats-related `struct`s in
56 * shm::arena_lend::jemalloc, what we mean here should become evident.
57 */
59{
60 // Data.
61
62 /// Sub-`struct` with individual stats; see type's doc header.
64 /// Sub-`struct` with individual stats; see type's doc header.
66}; // struct Arena_stats
67
68/**
69 * Cumulative stats relevant to a given SHM-classic arena *object* -- a Pool_arena -- and excluding those
70 * in Arena_stats which track the underlying in-SHM arena (pool) itself.
71 *
72 * To put a fine point on it: A fresh (zeroed) Local_stats is created inside each new Pool_arena object;
73 * stats accumulate in it until that object is destroyed; and the two are destroyed together as well.
74 * By contrast, Arena_stats, as accessed (by ref) via any pertinent `Pool_arena`(s), lives in SHM and can outlive
75 * said `Pool_arena`(s).
76 *
77 * @see Arena_stats doc header for background; it'll also point to ipc::shm doc header which would be of interest.
78 *
79 * ### Design ###
80 * The note in Arena_stats doc header applies equally here.
81 */
83{
84 // Data.
85
86 /**
87 * Sub-`struct` with individual stats; see type's doc header.
88 *
89 * Per suggested convention in `Owner_obj_stats` doc header, we place this one first.
90 */
92 /// Sub-`struct` with individual stats; see type's doc header.
94 /// Sub-`struct` with individual stats; see type's doc header.
96}; // struct Local_stats
97
98/**
99 * Printable (with knobs in contained util::stat::Info_dump_format) bundling of stats/info pertaining to a
100 * particular SHM-classic Pool_arena, as stat-consumed at a particular point in time.
101 * - Stats/info stored by-value; can be queried in peace. They will not concurrently change nor become invalid
102 * when the source Pool_arena is destroyed.
103 * - Can be printed to an `ostream` via `ostream<<`. #m_fmt (`Info_dump_format`) has output-format knobs,
104 * adjusting which ahead of the `<<` will affect the output.
105 * - As usual: can `FLOW_LOG_...(x)` it, `boost::lexical_cast<string>(x)` it, `flow::util::ostream_op_string(x)`,
106 * where `x` is a `*this`.
107 *
108 * ### Subject matter ###
109 * One fills-out the payload by calling `arena.info_dump(&x)`, where `arena` is a `Pool_arena`; `x` is a `*this`.
110 *
111 * Since SHM-classic is an *arena-sharing* SHM-provider, a single Pool_arena plays the role of both the SHM-arena
112 * and the SHM-session. Hence -- unlike the SHM-arena-lend (at least SHM-jemalloc) analogs (which split this into
113 * `Arena_info_dump` and `Shm_session_info_dump`) -- this single bundle covers
114 * everything for a `Pool_arena`. #m_arena_stats (also #m_arena_sz_or_0, #m_arena_stat_free_sz) pertains to the
115 * underlying (shared, in-SHM) arena; #m_local_stats pertains to the specific `*this`-filling Pool_arena *object*.
116 *
117 * As of this writing there are no relevant stats/info outside either a given Pool_arena object or the
118 * `Pool_arena`-pointee SHM-pool; nothing global in other words. (Cf. SHM-jemalloc's jemalloc::stat::Arena_info_dump +
119 * jemalloc::stat::Shm_session_info_dump and their grabbers `Ipc_session::info_dump()`, `Shm_session::info_dump()`:
120 * they also contain/grab copies of certain globally maintained stats.)
121 *
122 * ### Corner case: invalid Pool_arena ###
123 * If the source `Pool_arena`'s ctor failed to attach its SHM-pool, then there are no meaningful stats; this is
124 * indicated by #m_arena_sz_or_0 being `0` (a valid pool always has positive size). In that case only #m_arena_sz_or_0
125 * (`== 0`) is meaningful; the other stat members are unspecified (not to be relied upon), and `ostream<<` emits
126 * only a short note to that effect.
127 */
129{
130 // Data.
131
132 /**
133 * Print-format knobs in effect at `ostream << *this` time.
134 *
135 * @note Info_dump_format::m_verbose has no effect (SHM-classic has no large optional output to suppress).
136 */
138
139 /**
140 * The source `Pool_arena`'s Pool_arena::arena_size(); `0` if and only if that Pool_arena was invalid
141 * (its ctor failed to attach a pool).
142 */
143 size_t m_arena_sz_or_0 = 0;
144
145 /// The source `Pool_arena`'s Pool_arena::arena_stat_free_size(); meaningless/ignorable if `m_arena_sz_or_0 == 0`.
147
148 /**
149 * The source `Pool_arena`'s (shared, in-SHM) arena stats from: Pool_arena::arena_stats();
150 * meaningless/ignorable if `m_arena_sz_or_0 == 0`.
151 */
153
154 /**
155 * The source `Pool_arena` *object*'s stats from: Pool_arena::local_stats();
156 * meaningless/ignorable if `m_arena_sz_or_0 == 0`.
157 */
159}; // struct Arena_info_dump
160
161// Template implementations.
162
163template<typename Visitor>
164void declare_stats(const flow::util::stat::Stat_name& name_prefix,
165 const Arena_stats* src_stats, Arena_stats* target_stats,
166 Visitor&& visitor)
167{
168 /* (See flow::util::stat doc header for background on declare_stats()/sub-struct composition.)
169 * The name_prefix handling below is a bit subtle. (Recall this is entirely about how stats appear in pretty-print
170 * output; that's all this affects -- but ambiguous names when consuming stats can lead to surprisingly big trouble.)
171 * - We prepend "obj." to each stat name, because we are just "Arena_stats" -- stats about the arena -- and
172 * nothing specifies that these stats are about *first-class objects* (`obj` by convention), meaning things
173 * constructed via arena.construct<T>(); not all buffers arena.[de]allocate()d, whether directly or more likely
174 * on behalf of the `.construct<T>()`s, ~T(), and the T allocator if any. Indeed we *could* have
175 * raw-allocation-related stats too; we just don't as of this writing for reasons explained elsewhere.
176 * Hence this gives a visual cue as to the distinction, whether those other stats ever are added or not.
177 * - We do not prepend "live." and "misc." (or something, respectively) for each of the composed sub-structs.
178 * (1) ~Formally speaking that's because -- as noted in Arena_stats doc header -- the sub-structs are
179 * sub-structs for technocratic impl reasons, not because there is much concern separation between them.
180 * (2) Pragmatically speaking that's because the individual stat-names in these sub-structs are sufficiently
181 * qualified as of this writing. Hopefully that will not change. */
182
183 using flow::util::stat::Stat_name;
184
185 const Stat_name obj_prefix{name_prefix, "obj."};
186 declare_stats(obj_prefix,
187 src_stats ? &src_stats->m_live_obj : nullptr, target_stats ? &target_stats->m_live_obj : nullptr,
188 visitor);
189 declare_stats(obj_prefix,
190 src_stats ? &src_stats->m_obj : nullptr, target_stats ? &target_stats->m_obj : nullptr,
191 visitor);
192}
193
194template<typename Visitor>
195void declare_stats(const flow::util::stat::Stat_name& name_prefix,
196 const Local_stats* src_stats, Local_stats* target_stats,
197 Visitor&& visitor)
198{
199 /* Some of the commentary inside declare_stats(Arena_stats) applies here; except that our sub-structs
200 * are organized in reasonably concern-separated groups, and (largely because of this) some of the
201 * stat-names would be mutually duplicate (e.g., m_disposer_count in m_{own|borrow}er_obj),
202 * certainly leading to confusion. */
203
204 using flow::util::stat::Stat_name;
205
206 const Stat_name obj_prefix{name_prefix, "obj."};
208 (Stat_name{obj_prefix, "own."},
209 src_stats ? &src_stats->m_owner_obj : nullptr, target_stats ? &target_stats->m_owner_obj : nullptr, visitor);
211 (Stat_name{obj_prefix, "lnd."},
212 src_stats ? &src_stats->m_lender_obj : nullptr, target_stats ? &target_stats->m_lender_obj : nullptr, visitor);
214 (Stat_name{obj_prefix, "brw."},
215 src_stats ? &src_stats->m_borrower_obj : nullptr, target_stats ? &target_stats->m_borrower_obj : nullptr, visitor);
216}
217
218} // namespace ipc::shm::classic::stat
Stats-related sub-namespace, for ADL segregation and general organization.
Definition: classic_fwd.hpp:53
void declare_stats(const flow::util::stat::Stat_name &name_prefix, const Arena_stats *src_stats, Arena_stats *target_stats, Visitor &&visitor)
Declares the stats for Arena_stats.
Printable (with knobs in contained util::stat::Info_dump_format) bundling of stats/info pertaining to...
Arena_stats m_arena_stats
The source Pool_arena's (shared, in-SHM) arena stats from: Pool_arena::arena_stats(); meaningless/ign...
size_t m_arena_sz_or_0
The source Pool_arena's Pool_arena::arena_size(); 0 if and only if that Pool_arena was invalid (its c...
Local_stats m_local_stats
The source Pool_arena object's stats from: Pool_arena::local_stats(); meaningless/ignorable if m_aren...
size_t m_arena_stat_free_sz
The source Pool_arena's Pool_arena::arena_stat_free_size(); meaningless/ignorable if m_arena_sz_or_0 ...
util::stat::Info_dump_format m_fmt
Print-format knobs in effect at ostream << *this time.
Cumulative stats relevant to one SHM-classic arena – in this case single SHM-pool in shared memory,...
shm::stat::Live_obj_stats m_live_obj
Sub-struct with individual stats; see type's doc header.
shm::stat::Shared_arena_obj_stats m_obj
Sub-struct with individual stats; see type's doc header.
Cumulative stats relevant to a given SHM-classic arena object – a Pool_arena – and excluding those in...
shm::stat::Borrower_obj_stats m_borrower_obj
Sub-struct with individual stats; see type's doc header.
shm::stat::Owner_obj_stats m_owner_obj
Sub-struct with individual stats; see type's doc header.
shm::stat::Lender_obj_stats m_lender_obj
Sub-struct with individual stats; see type's doc header.
Flow-IPC SHM-module: SHM-provider-agnostic stats about borrowing objects, typically across a process ...
Definition: shm_stats.hpp:335
Flow-IPC SHM-module: SHM-provider-agnostic stats about an arena if viewed as being capable or lending...
Definition: shm_stats.hpp:257
Stats struct thematically relevant to gauging outstanding (not-yet-garbage-collected) first-class obj...
Definition: shm_stats.hpp:384
Flow-IPC SHM-module: SHM-provider-agnostic stats about an arena object if viewed as solely a memory-m...
Definition: shm_stats.hpp:120
Stats struct tracking quantities applicable only to the arena-sharing SHM-provider type (at least SHM...
Definition: shm_stats.hpp:433
Knobs meant to control the cosmetics of printing (ostream <<) a bundling of stats/info for some parti...
Definition: util.hpp:39