XRootD
XrdMonRoll.hh
Go to the documentation of this file.
1 #ifndef __XRDMONROLL__
2 #define __XRDMONROLL__
3 /******************************************************************************/
4 /* */
5 /* X r d M o n R o l l . h h */
6 /* */
7 /* (c) 2024 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* All Rights Reserved */
9 /* Produced by Andrew Hanushevsky for Stanford University under contract */
10 /* DE-AC02-76-SFO0515 with the Department of Energy */
11 /* */
12 /* This file is part of the XRootD software suite. */
13 /* */
14 /* XRootD is free software: you can redistribute it and/or modify it under */
15 /* the terms of the GNU Lesser General Public License as published by the */
16 /* Free Software Foundation, either version 3 of the License, or (at your */
17 /* option) any later version. */
18 /* */
19 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
20 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
21 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
22 /* License for more details. */
23 /* */
24 /* You should have received a copy of the GNU Lesser General Public License */
25 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
26 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
27 /* */
28 /* The copyright holder's institutional names and contributor's names may not */
29 /* be used to endorse or promote products derived from this software without */
30 /* specific prior written permission of the institution or contributor. */
31 /******************************************************************************/
32 
33 #include <string>
34 #include <variant>
35 #include <vector>
36 
37 #include "XrdSys/XrdSysRAtomic.hh"
38 
39 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
47 
48 class XrdMonitor;
49 class XrdSysMutex;
50 
52 {
53 public:
54 
55 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
71 
72 class Item
73 {
74 public:
75 friend class XrdMonitor;
76 
77 //-----------------------------------------------------------------------------c
93 //-----------------------------------------------------------------------------c
94 
95 enum class Schema : char {unk=0, begArray, endArray, begObject, endObject};
96 
97  Item(const char* keyN, double& valU) : keyP(keyN), dblP(&valU),
98  Kind(Family::isBinary), Clan(Trait::isDouble) {}
99 
100  Item(const char* keyN, float& valU) : keyP(keyN), fltP(&valU),
101  Kind(Family::isBinary), Clan(Trait::isFloat) {}
102 
103  Item(const char* keyN, const char* valU) : keyP(keyN), chrP(valU),
104  Kind(Family::isText), Clan(Trait::isChar) {}
105 
106  Item(const char* keyN, std::string& valU) : keyP(keyN), strP(&valU),
107  Kind(Family::isText), Clan(Trait::isString) {}
108 
109  Item(const char* keyN, Schema valU) : keyP(keyN), Plan(valU),
110  Kind(Family::isSchema) {}
111 
112  Item(bool Lock, XrdSysMutex& valU) : keyP(0), mtxP(&valU),
113  Kind(Family::isMutex), doLK(Lock) {}
114 
115  Item(const char* keyN, RAtomic_int8_t& valU) : keyP(keyN), rbtV(&valU),
116  Kind(Family::isBinary), Clan(Trait::isBtomic) {}
117 
118  Item(const char* keyN, RAtomic_uint8_t& valU) : keyP(keyN), rbtV(&valU),
119  Kind(Family::isBinary), Clan(Trait::isBtomic) {}
120 
121  Item(const char* keyN, RAtomic_int16_t& valU) : keyP(keyN), rbtV(&valU),
122  Kind(Family::isBinary), Clan(Trait::isBtomic) {}
123 
124  Item(const char* keyN, RAtomic_uint16_t& valU) : keyP(keyN), rbtV(&valU),
125  Kind(Family::isBinary), Clan(Trait::isBtomic) {}
126 
127  Item(const char* keyN, RAtomic_int32_t& valU) : keyP(keyN), rbtV(&valU),
128  Kind(Family::isBinary), Clan(Trait::isBtomic) {}
129 
130  Item(const char* keyN, RAtomic_uint32_t& valU) : keyP(keyN), rbtV(&valU),
131  Kind(Family::isBinary), Clan(Trait::isBtomic) {}
132 
133  Item(const char* keyN, RAtomic_int64_t& valU) : keyP(keyN), rbtV(&valU),
134  Kind(Family::isBinary), Clan(Trait::isBtomic) {}
135 
136  Item(const char* keyN, RAtomic_uint64_t& valU) : keyP(keyN), rbtV(&valU),
137  Kind(Family::isBinary), Clan(Trait::isBtomic) {}
138 
139  template<typename T> // This constructor only accepts RAtomic
140  Item(const char* keyN, T& valU) : keyP(keyN), ratV(&valU),
141  Kind(Family::isBinary), Clan(Trait::isAtomic) {}
142 
143 private:
144 friend class XrdMonitor;
145 
146 const char* keyP;
147 
148 // This variant is used for native RAtomic values. It omits the bit specific
149 // types as they duplicate the native types (e.g. uint64_t maps to ullong, etc)
150 // and the compiler cannot determine which one it should actually use. C++20
151 // handles this better but we need C++17 to work as well.
152 //
153 using RATVariant = std::variant<RAtomic_ullong*, RAtomic_llong*,
159 
160 // This variant is used for bit specific RAtomic values. It is an exceptional
161 // variant that we use when handling bit specific RAtomics. Until both variants
162 // can be combined, some time in the future, we need to special case these.
163 //
164 using RBTVariant = std::variant<RAtomic_int8_t*, RAtomic_uint8_t*,
168 
169 // We use an anonymous union where Family::Kind tells us what to use. We
170 // can't include the variant in the union as it has a constructor/destructor.
171 //
172  RATVariant ratV;
173  RBTVariant rbtV; // Future: variant will be combined with ratV
174 union {float* fltP;
175  double* dblP;
176  const char* chrP;
177  std::string* strP;
178  XrdSysMutex* mtxP;
179  Schema Plan;
180  };
181 
182 enum class Family : char {isBinary = 0, isMutex, isSchema, isText};
183 
184 enum class Trait : char {isNone=0, isAtomic, isBtomic, isFloat, isDouble,
185  isChar, isString};
186 
187 short iNum = 0; // Filled in by Monitor
188 Family Kind;
189 Trait Clan = Trait::isNone;
190 bool Array = false; // Reset by Monitor to true if item is in a list
191 bool doLK = false; // Reset by constructor for mutex item
192 };
193 
194 static const int ItemSize = sizeof(Item);
195 
196 //-----------------------------------------------------------------------------c
199 //-----------------------------------------------------------------------------c
200 
202 
203 //-----------------------------------------------------------------------------c
223 //-----------------------------------------------------------------------------c
224 
225 bool Register(rollType setType, const char* setName, std::vector<Item>& iVec);
226 
227 //-----------------------------------------------------------------------------c
228 // The following are deprecated and should not be used in new code. The above
229 // definition should be used as they are a superset of what is below.
230 //-----------------------------------------------------------------------------c
231 
232 //-----------------------------------------------------------------------------c
240 //-----------------------------------------------------------------------------
241 
242 struct setMember {const char* varName; RAtomic_uint& varValu;};
243 
244 //-----------------------------------------------------------------------------
260 //-----------------------------------------------------------------------------
261 
262 static RAtomic_uint EOV; // Variable at the end of the setVec.
263 
264 bool Register(rollType setType, const char* setName, setMember setVec[]);
265 
266  XrdMonRoll(XrdMonitor& xMon);
268 
269 private:
270 XrdMonitor& XrdMon;
271 void* rsvd[3];
272 };
273 
274 /******************************************************************************/
275 /* E x a m p l e s */
276 /******************************************************************************/
277 
278 /* Lets say you have RAtomic_int variable cntr1 and cntr2 and wish to export
279  them in the summary stream as: {"keys":{"key1":cntr1, "key2":cntr2}} the
280  following Item list would produce the export:
281 
282  Item iList[] = {Item("keys", Schema::begObject),
283  Item("key1", cntr1), Item("key2", cntr2),
284  Item("keys", Schema::endObject)
285  };
286 
287  Alternatively, assume you want to export the variables as
288  {"keyvals" : [cntr1, cntr2]} then the following Item list would suffice:
289 
290  Item iList[] = {Item("keyvals", Schema::begArray),
291  Item(0, cntr1), Item(0, cntr2),
292  Item("keyvals", Schema::endArray)
293  |;
294 
295  The XML version of each would be:
296  a) <keys><key1>cntr1</key1><key2>cntr2</key2></keys>
297  b) <keyvals><item>cntr1</item><item> cntr2</item></keyvals>
298 */
299 #endif
Item(const char *keyN, RAtomic_int32_t &valU)
Definition: XrdMonRoll.hh:127
Item(const char *keyN, double &valU)
Definition: XrdMonRoll.hh:97
Item(const char *keyN, T &valU)
Definition: XrdMonRoll.hh:140
Item(const char *keyN, RAtomic_uint16_t &valU)
Definition: XrdMonRoll.hh:124
Item(const char *keyN, RAtomic_int8_t &valU)
Definition: XrdMonRoll.hh:115
Item(const char *keyN, const char *valU)
Definition: XrdMonRoll.hh:103
Item(const char *keyN, std::string &valU)
Definition: XrdMonRoll.hh:106
Item(const char *keyN, RAtomic_uint8_t &valU)
Definition: XrdMonRoll.hh:118
Item(const char *keyN, float &valU)
Definition: XrdMonRoll.hh:100
Item(const char *keyN, RAtomic_int64_t &valU)
Definition: XrdMonRoll.hh:133
Item(const char *keyN, Schema valU)
Definition: XrdMonRoll.hh:109
Item(const char *keyN, RAtomic_uint64_t &valU)
Definition: XrdMonRoll.hh:136
Item(const char *keyN, RAtomic_int16_t &valU)
Definition: XrdMonRoll.hh:121
Item(bool Lock, XrdSysMutex &valU)
Definition: XrdMonRoll.hh:112
Item(const char *keyN, RAtomic_uint32_t &valU)
Definition: XrdMonRoll.hh:130
static const int ItemSize
Definition: XrdMonRoll.hh:194
const char * varName
Definition: XrdMonRoll.hh:242
XrdMonRoll(XrdMonitor &xMon)
Definition: XrdMonRoll.cc:46
bool Register(rollType setType, const char *setName, std::vector< Item > &iVec)
Definition: XrdMonRoll.cc:53
RAtomic_uint & varValu
Definition: XrdMonRoll.hh:242
static RAtomic_uint EOV
Definition: XrdMonRoll.hh:262