Ice 3.7 C++98 API Reference
Loading...
Searching...
No Matches
MetricsAdminI.h
Go to the documentation of this file.
1//
2// Copyright (c) ZeroC, Inc. All rights reserved.
3//
4
5#ifndef ICE_METRICSADMIN_I_H
6#define ICE_METRICSADMIN_I_H
7
8#include <Ice/Properties.h>
10#include <Ice/Initialize.h>
11
12#include <Ice/Metrics.h>
13
14#ifdef _MSC_VER
15# define ICE_CPP11_COMPILER_REGEXP
16#endif
17
18#ifdef __MINGW32__
19 //
20 // No regexp support with MinGW, when MinGW C++11 mode is not experimental
21 // we can use std::regex.
22 //
23#elif defined(ICE_CPP11_COMPILER_REGEXP)
24# include <regex>
25#else
26# include <regex.h>
27#endif
28
29#include <list>
30
31namespace IceMX
32{
33
35class Updater;
36template<typename T> class MetricsHelperT;
38
39}
40
41namespace IceInternal
42{
43
44class ICE_API MetricsMapI;
45ICE_DEFINE_PTR(MetricsMapIPtr, MetricsMapI);
46
47class ICE_API MetricsMapI :
48#ifdef ICE_CPP11_MAPPING
49 public std::enable_shared_from_this<MetricsMapI>
50#else
51 public virtual IceUtil::Shared
52#endif
53{
54public:
55
56 class ICE_API RegExp : public IceUtil::Shared
57 {
58 public:
59
60 RegExp(const std::string&, const std::string&);
61 ~RegExp();
62
63 template<typename T> bool
64 match(const IceMX::MetricsHelperT<T>& helper, bool reject)
65 {
66 std::string value;
67 try
68 {
69 value = helper(_attribute);
70 }
71 catch(const std::exception&)
72 {
73 return !reject;
74 }
75 return match(value);
76 }
77
78 private:
79
80 bool match(const std::string&);
81
82 const std::string _attribute;
83
84#ifdef __MINGW32__
85 //
86 // No regexp support with MinGW, when MinGW C++11 mode is not experimental
87 // we can use std::regex.
88 //
89#elif defined(ICE_CPP11_COMPILER_REGEXP)
90# if _MSC_VER < 1600
91 std::tr1::regex _regex;
92# else
93 std::regex _regex;
94# endif
95#else
96 regex_t _preg;
97#endif
98 };
99 ICE_DEFINE_PTR(RegExpPtr, RegExp);
100
101 virtual ~MetricsMapI();
102
103 MetricsMapI(const std::string&, const Ice::PropertiesPtr&);
104 MetricsMapI(const MetricsMapI&);
105
106 virtual void destroy() = 0;
107
108 virtual IceMX::MetricsFailuresSeq getFailures() = 0;
109 virtual IceMX::MetricsFailures getFailures(const std::string&) = 0;
110 virtual IceMX::MetricsMap getMetrics() const = 0;
111
112 virtual MetricsMapIPtr clone() const = 0;
113
114 const Ice::PropertyDict& getProperties() const;
115
116protected:
117
118 const Ice::PropertyDict _properties;
119 const std::vector<std::string> _groupByAttributes;
120 const std::vector<std::string> _groupBySeparators;
121 const int _retain;
122 const std::vector<RegExpPtr> _accept;
123 const std::vector<RegExpPtr> _reject;
124};
125
126class ICE_API MetricsMapFactory
127#ifndef ICE_CPP11_MAPPING
128 : public Ice::LocalObject
129#endif
130{
131public:
132
133 virtual ~MetricsMapFactory();
134
135 MetricsMapFactory(IceMX::Updater*);
136
137 virtual MetricsMapIPtr create(const std::string&, const Ice::PropertiesPtr&) = 0;
138
139 void update();
140
141private:
142
143 IceMX::Updater* _updater;
144};
145ICE_DEFINE_PTR(MetricsMapFactoryPtr, MetricsMapFactory);
146
147template<class MetricsType> class MetricsMapT : public MetricsMapI, private IceUtil::Mutex
148{
149public:
150
151 typedef MetricsType T;
153
154 ICE_DEFINE_PTR(MetricsMapTPtr, MetricsMapT);
155
156 typedef IceMX::MetricsMap MetricsType::* SubMapMember;
157
158 class EntryT;
159 ICE_DEFINE_PTR(EntryTPtr, EntryT);
160
161 class EntryT :
162#ifdef ICE_CPP11_MAPPING
163 public std::enable_shared_from_this<EntryT>
164#else
165 public Ice::LocalObject
166#endif
167 {
168 public:
169
170 EntryT(MetricsMapTPtr map, const TPtr& object, const typename std::list<EntryTPtr>::iterator& p) :
171 _map(map), _object(object), _detachedPos(p)
172 {
173 }
174
175 ~EntryT()
176 {
177 assert(_object->total > 0);
178 for(typename std::map<std::string, std::pair<MetricsMapIPtr, SubMapMember> >::const_iterator p =
179 _subMaps.begin(); p != _subMaps.end(); ++p)
180 {
181 p->second.first->destroy(); // Break cyclic reference counts.
182 }
183 }
184
185 void
186 failed(const std::string& exceptionName)
187 {
188 IceUtil::Mutex::Lock sync(*_map);
189 ++_object->failures;
190 ++_failures[exceptionName];
191 }
192
193 template<typename MemberMetricsType> typename MetricsMapT<MemberMetricsType>::EntryTPtr
194 getMatching(const std::string& mapName, const IceMX::MetricsHelperT<MemberMetricsType>& helper)
195 {
196 MetricsMapIPtr m;
197 {
198 IceUtil::Mutex::Lock sync(*_map);
199 typename std::map<std::string, std::pair<MetricsMapIPtr, SubMapMember> >::iterator p =
200 _subMaps.find(mapName);
201 if(p == _subMaps.end())
202 {
203 std::pair<MetricsMapIPtr, SubMapMember> map = _map->createSubMap(mapName);
204 if(map.first)
205 {
206 p = _subMaps.insert(make_pair(mapName, map)).first;
207 }
208 }
209 if(p == _subMaps.end())
210 {
211 return 0;
212 }
213 m = p->second.first;
214 }
215
216 MetricsMapT<MemberMetricsType>* map = dynamic_cast<MetricsMapT<MemberMetricsType>*>(m.get());
217 assert(map);
218 return map->getMatching(helper);
219 }
220
221 void
222 detach(Ice::Long lifetime)
223 {
224 IceUtil::Mutex::Lock sync(*_map);
225 _object->totalLifetime += lifetime;
226 if(--_object->current == 0)
227 {
228#ifdef ICE_CPP11_MAPPING
229 _map->detached(this->shared_from_this());
230#else
231 _map->detached(this);
232#endif
233 }
234 }
235
236 template<typename Function> void
237 execute(Function func)
238 {
239 IceUtil::Mutex::Lock sync(*_map);
240 func(_object);
241 }
242
243 MetricsMapT*
244 getMap()
245 {
246 return _map.get();
247 }
248
249 private:
250
251 IceMX::MetricsFailures
252 getFailures() const
253 {
254 IceMX::MetricsFailures f;
255 f.id = _object->id;
256 f.failures = _failures;
257 return f;
258 }
259
261 clone() const
262 {
263 TPtr metrics = ICE_DYNAMIC_CAST(T, _object->ice_clone());
264 for(typename std::map<std::string, std::pair<MetricsMapIPtr, SubMapMember> >::const_iterator p =
265 _subMaps.begin(); p != _subMaps.end(); ++p)
266 {
267 metrics.get()->*p->second.second = p->second.first->getMetrics();
268 }
269 return metrics;
270 }
271
272 bool
273 isDetached() const
274 {
275 return _object->current == 0;
276 }
277
278 void
279 attach(const IceMX::MetricsHelperT<T>& helper)
280 {
281 ++_object->total;
282 ++_object->current;
283 helper.initMetrics(_object);
284 }
285
286 friend class MetricsMapT;
287 MetricsMapTPtr _map;
288 TPtr _object;
289 IceMX::StringIntDict _failures;
290 std::map<std::string, std::pair<MetricsMapIPtr, SubMapMember> > _subMaps;
291 typename std::list<EntryTPtr>::iterator _detachedPos;
292 };
293
294 MetricsMapT(const std::string& mapPrefix,
295 const Ice::PropertiesPtr& properties,
296 const std::map<std::string, std::pair<SubMapMember, MetricsMapFactoryPtr> >& subMaps) :
297 MetricsMapI(mapPrefix, properties), _destroyed(false)
298 {
299 std::vector<std::string> subMapNames;
300 typename std::map<std::string, std::pair<SubMapMember, MetricsMapFactoryPtr> >::const_iterator p;
301 for(p = subMaps.begin(); p != subMaps.end(); ++p)
302 {
303 subMapNames.push_back(p->first);
304 const std::string subMapsPrefix = mapPrefix + "Map.";
305 std::string subMapPrefix = subMapsPrefix + p->first + '.';
306 if(properties->getPropertiesForPrefix(subMapPrefix).empty())
307 {
308 if(properties->getPropertiesForPrefix(subMapsPrefix).empty())
309 {
310 subMapPrefix = mapPrefix;
311 }
312 else
313 {
314 continue; // This sub-map isn't configured.
315 }
316 }
317 _subMaps.insert(std::make_pair(p->first,
318 std::make_pair(p->second.first,
319 p->second.second->create(subMapPrefix, properties))));
320 }
321 }
322
323 MetricsMapT(const MetricsMapT& other)
324 :
325#ifndef ICE_CPP11_MAPPING
326 IceUtil::Shared(),
327#endif
328 MetricsMapI(other),
329 IceUtil::Mutex(),
330 _destroyed(false)
331 {
332 }
333
334#ifdef ICE_CPP11_MAPPING
335 std::shared_ptr<MetricsMapT> shared_from_this()
336 {
337 return std::static_pointer_cast<MetricsMapT>(MetricsMapI::shared_from_this());
338 }
339#endif
340
341 virtual void
342 destroy()
343 {
344 Lock sync(*this);
345 _destroyed = true;
346 _objects.clear(); // Break cyclic reference counts
347 _detachedQueue.clear(); // Break cyclic reference counts
348 }
349
350 virtual IceMX::MetricsMap
351 getMetrics() const
352 {
353 IceMX::MetricsMap objects;
354
355 Lock sync(*this);
356 for(typename std::map<std::string, EntryTPtr>::const_iterator p = _objects.begin(); p != _objects.end(); ++p)
357 {
358 objects.push_back(p->second->clone());
359 }
360 return objects;
361 }
362
364 getFailures()
365 {
367
368 Lock sync(*this);
369 for(typename std::map<std::string, EntryTPtr>::const_iterator p = _objects.begin(); p != _objects.end(); ++p)
370 {
371 IceMX::MetricsFailures f = p->second->getFailures();
372 if(!f.failures.empty())
373 {
374 failures.push_back(f);
375 }
376 }
377 return failures;
378 }
379
380 virtual IceMX::MetricsFailures
381 getFailures(const std::string& id)
382 {
383 Lock sync(*this);
384 typename std::map<std::string, EntryTPtr>::const_iterator p = _objects.find(id);
385 if(p != _objects.end())
386 {
387 return p->second->getFailures();
388 }
389 return IceMX::MetricsFailures();
390 }
391
392 std::pair<MetricsMapIPtr, SubMapMember>
393 createSubMap(const std::string& subMapName)
394 {
395 typename std::map<std::string, std::pair<SubMapMember, MetricsMapIPtr> >::const_iterator p =
396 _subMaps.find(subMapName);
397 if(p != _subMaps.end())
398 {
399 return std::pair<MetricsMapIPtr, SubMapMember>(ICE_GET_SHARED_FROM_THIS(p->second.second->clone()), p->second.first);
400 }
401 return std::pair<MetricsMapIPtr, SubMapMember>(MetricsMapIPtr(ICE_NULLPTR), static_cast<SubMapMember>(0));
402 }
403
404 EntryTPtr
405 getMatching(const IceMX::MetricsHelperT<T>& helper, const EntryTPtr& previous = EntryTPtr())
406 {
407 //
408 // Check the accept and reject filters.
409 //
410 for(std::vector<RegExpPtr>::const_iterator p = _accept.begin(); p != _accept.end(); ++p)
411 {
412 if(!(*p)->match(helper, false))
413 {
414 return ICE_NULLPTR;
415 }
416 }
417
418 for(std::vector<RegExpPtr>::const_iterator p = _reject.begin(); p != _reject.end(); ++p)
419 {
420 if((*p)->match(helper, true))
421 {
422 return ICE_NULLPTR;
423 }
424 }
425
426 //
427 // Compute the key from the GroupBy property.
428 //
429 std::string key;
430 try
431 {
432 if(_groupByAttributes.size() == 1)
433 {
434 key = helper(_groupByAttributes.front());
435 }
436 else
437 {
438 std::ostringstream os;
439 std::vector<std::string>::const_iterator q = _groupBySeparators.begin();
440 for(std::vector<std::string>::const_iterator p = _groupByAttributes.begin();
441 p != _groupByAttributes.end(); ++p)
442 {
443 os << helper(*p);
444 if(q != _groupBySeparators.end())
445 {
446 os << *q++;
447 }
448 }
449 key = os.str();
450 }
451 }
452 catch(const std::exception&)
453 {
454 return ICE_NULLPTR;
455 }
456
457 //
458 // Lookup the metrics object.
459 //
460 Lock sync(*this);
461 if(_destroyed)
462 {
463 return ICE_NULLPTR;
464 }
465
466 if(previous && previous->_object->id == key)
467 {
468 assert(_objects[key] == previous);
469 return previous;
470 }
471
472 typename std::map<std::string, EntryTPtr>::const_iterator p = _objects.find(key);
473 if(p == _objects.end())
474 {
475 TPtr t = ICE_MAKE_SHARED(T);
476 t->id = key;
477
478#ifdef ICE_CPP11_MAPPING
479 p = _objects.insert(typename std::map<std::string, EntryTPtr>::value_type(
480 key, std::make_shared<EntryT>(shared_from_this(), t, _detachedQueue.end()))).first;
481#else
482 p = _objects.insert(typename std::map<std::string, EntryTPtr>::value_type(
483 key, new EntryT(this, t, _detachedQueue.end()))).first;
484#endif
485
486 }
487 p->second->attach(helper);
488 return p->second;
489 }
490
491private:
492
493 virtual MetricsMapIPtr clone() const
494 {
495 return ICE_MAKE_SHARED(MetricsMapT<MetricsType>, *this);
496 }
497
498 void detached(EntryTPtr entry)
499 {
500 // This is called with the map mutex locked.
501
502 if(_retain == 0 || _destroyed)
503 {
504 return;
505 }
506
507 assert(static_cast<int>(_detachedQueue.size()) <= _retain);
508
509 // If the entry is already detached and in the queue, just move it to the back.
510 if(entry->_detachedPos != _detachedQueue.end())
511 {
512 if(entry->_detachedPos != --_detachedQueue.end()) // If not already at the end
513 {
514 _detachedQueue.splice(_detachedQueue.end(), _detachedQueue, entry->_detachedPos);
515 entry->_detachedPos = --_detachedQueue.end();
516 }
517 return;
518 }
519
520 // Otherwise, compress the queue by removing entries which are no longer detached.
521 if(static_cast<int>(_detachedQueue.size()) == _retain)
522 {
523 // Remove entries which are no longer detached
524 typename std::list<EntryTPtr>::iterator p = _detachedQueue.begin();
525 while(p != _detachedQueue.end())
526 {
527 if(!(*p)->isDetached())
528 {
529 (*p)->_detachedPos = _detachedQueue.end();
530 p = _detachedQueue.erase(p);
531 }
532 else
533 {
534 ++p;
535 }
536 }
537 }
538
539 // If there's still no room, remove the oldest entry (at the front).
540 if(static_cast<int>(_detachedQueue.size()) == _retain)
541 {
542 _objects.erase(_detachedQueue.front()->_object->id);
543 _detachedQueue.pop_front();
544 }
545
546 // Add the entry at the back of the queue.
547 entry->_detachedPos = _detachedQueue.insert(_detachedQueue.end(), entry);
548 assert(entry->_detachedPos != _detachedQueue.end());
549 return;
550 }
551
552 friend class EntryT;
553
554 bool _destroyed;
555 std::map<std::string, EntryTPtr> _objects;
556 std::list<EntryTPtr> _detachedQueue;
557 std::map<std::string, std::pair<SubMapMember, MetricsMapIPtr> > _subMaps;
558};
559
560template<class MetricsType> class MetricsMapFactoryT : public MetricsMapFactory
561{
562public:
563
564 MetricsMapFactoryT(IceMX::Updater* updater) : MetricsMapFactory(updater)
565 {
566 }
567
568 virtual MetricsMapIPtr
569 create(const std::string& mapPrefix, const Ice::PropertiesPtr& properties)
570 {
571 return ICE_MAKE_SHARED(MetricsMapT<MetricsType>, mapPrefix, properties, _subMaps);
572 }
573
574 template<class SubMapMetricsType> void
575 registerSubMap(const std::string& subMap, IceMX::MetricsMap MetricsType::* member)
576 {
577 _subMaps[subMap] = std::pair<IceMX::MetricsMap MetricsType::*,
578 MetricsMapFactoryPtr>(member, ICE_MAKE_SHARED(MetricsMapFactoryT<SubMapMetricsType>, ICE_NULLPTR));
579 }
580
581private:
582
583 std::map<std::string, std::pair<IceMX::MetricsMap MetricsType::*, MetricsMapFactoryPtr> > _subMaps;
584};
585
586class MetricsViewI : public IceUtil::Shared
587{
588public:
589
590 MetricsViewI(const std::string&);
591
592 void destroy();
593
594 bool addOrUpdateMap(const Ice::PropertiesPtr&, const std::string&, const MetricsMapFactoryPtr&,
595 const Ice::LoggerPtr&);
596 bool removeMap(const std::string&);
597
598 IceMX::MetricsView getMetrics();
599 IceMX::MetricsFailuresSeq getFailures(const std::string&);
600 IceMX::MetricsFailures getFailures(const std::string&, const std::string&);
601
602 std::vector<std::string> getMaps() const;
603
604 MetricsMapIPtr getMap(const std::string&) const;
605
606private:
607
608 const std::string _name;
609 std::map<std::string, MetricsMapIPtr> _maps;
610};
611ICE_DEFINE_PTR(MetricsViewIPtr, MetricsViewI);
612
613class ICE_API MetricsAdminI : public IceMX::MetricsAdmin,
614#ifndef ICE_CPP11_MAPPING
616#endif
617 private IceUtil::Mutex
618{
619public:
620
621 MetricsAdminI(const ::Ice::PropertiesPtr&, const Ice::LoggerPtr&);
622 ~MetricsAdminI();
623
624 void destroy();
625
626 void updateViews();
627
628 template<class MetricsType> void
629 registerMap(const std::string& map, IceMX::Updater* updater)
630 {
631 bool updated;
632 MetricsMapFactoryPtr factory;
633 {
634 Lock sync(*this);
635 factory = ICE_MAKE_SHARED(MetricsMapFactoryT<MetricsType>, updater);
636 _factories[map] = factory;
637 updated = addOrUpdateMap(map, factory);
638 }
639 if(updated)
640 {
641 factory->update();
642 }
643 }
644
645 template<class MemberMetricsType, class MetricsType> void
646 registerSubMap(const std::string& map, const std::string& subMap, IceMX::MetricsMap MetricsType::* member)
647 {
648 bool updated;
650 {
651 Lock sync(*this);
652 std::map<std::string, MetricsMapFactoryPtr>::const_iterator p = _factories.find(map);
653 if(p == _factories.end())
654 {
655 return;
656 }
657#ifdef ICE_CPP11_MAPPING
658 factory = ::std::dynamic_pointer_cast<MetricsMapFactoryT<MetricsType>>(p->second);
659#else
660 factory = dynamic_cast<MetricsMapFactoryT<MetricsType>*>(p->second.get());
661#endif
662 factory->template registerSubMap<MemberMetricsType>(subMap, member);
663 removeMap(map);
664 updated = addOrUpdateMap(map, factory);
665 }
666 if(updated)
667 {
668 factory->update();
669 }
670 }
671
672 void unregisterMap(const std::string&);
673
674 virtual Ice::StringSeq getMetricsViewNames(Ice::StringSeq&, const ::Ice::Current&);
675
676 void updated(const Ice::PropertyDict&);
677
678#ifdef ICE_CPP11_MAPPING
679 virtual void enableMetricsView(std::string, const ::Ice::Current&);
680 virtual void disableMetricsView(std::string, const ::Ice::Current&);
681 virtual IceMX::MetricsView getMetricsView(std::string, Ice::Long&, const ::Ice::Current&);
682 virtual IceMX::MetricsFailuresSeq getMapMetricsFailures(std::string, std::string, const ::Ice::Current&);
683 virtual IceMX::MetricsFailures getMetricsFailures(std::string, std::string, std::string, const ::Ice::Current&);
684#else
685 virtual void enableMetricsView(const std::string&, const ::Ice::Current&);
686 virtual void disableMetricsView(const std::string&, const ::Ice::Current&);
687 virtual IceMX::MetricsView getMetricsView(const std::string&, Ice::Long&, const ::Ice::Current&);
688 virtual IceMX::MetricsFailuresSeq getMapMetricsFailures(const std::string&, const std::string&,
689 const ::Ice::Current&);
690 virtual IceMX::MetricsFailures getMetricsFailures(const std::string&, const std::string&, const std::string&,
691 const ::Ice::Current&);
692#endif
693 std::vector<MetricsMapIPtr> getMaps(const std::string&) const;
694
695 const Ice::LoggerPtr& getLogger() const;
696
697private:
698
699 MetricsViewIPtr getMetricsView(const std::string&);
700
701 bool addOrUpdateMap(const std::string&, const MetricsMapFactoryPtr&);
702 bool removeMap(const std::string&);
703
704 std::map<std::string, MetricsViewIPtr> _views;
705 std::set<std::string> _disabledViews;
706 std::map<std::string, MetricsMapFactoryPtr> _factories;
707
708 const Ice::LoggerPtr _logger;
709 Ice::PropertiesPtr _properties;
710};
711ICE_DEFINE_PTR(MetricsAdminIPtr, MetricsAdminI);
712
713};
714
715#endif
#define ICE_DEFINE_PTR(TPtr, T)
Definition Config.h:377
#define ICE_MAKE_SHARED(T,...)
Definition Config.h:376
#define ICE_GET_SHARED_FROM_THIS(p)
Definition Config.h:384
#define ICE_HANDLE
Definition Config.h:373
#define ICE_API
Definition Config.h:197
#define ICE_NULLPTR
Definition Config.h:380
#define ICE_DYNAMIC_CAST(T, V)
Definition Config.h:381
#define ICE_INTERNAL_HANDLE
Definition Config.h:374
The metrics administrative facet interface.
Definition Metrics.h:2586
Definition Mutex.h:33
LockT< Mutex > Lock
Definition Mutex.h:39
Definition Shared.h:78
An application can be notified when its configuration properties are modified via the Properties admi...
Definition NativePropertiesAdmin.h:35
Definition Metrics.h:227
::std::vector< MetricsPtr > MetricsMap
A metrics map is a sequence of metrics.
Definition Metrics.h:1594
::std::vector< MetricsFailures > MetricsFailuresSeq
A sequence of MetricsFailures.
Definition Metrics.h:1586
::std::map< ::std::string, ::Ice::Int > StringIntDict
A dictionary of strings to integers.
Definition Metrics.h:1563
::IceInternal::Handle< Metrics > MetricsPtr
Definition Metrics.h:1460
::std::map< ::std::string, MetricsMap > MetricsView
A metrics view is a dictionary of metrics map.
Definition Metrics.h:1600
::std::vector< ::std::string > StringSeq
A sequence of strings.
Definition BuiltinSequences.h:153
IceUtil::Int64 Long
The mapping for the Slice long type.
Definition Config.h:60
::std::map< ::std::string, ::std::string > PropertyDict
A simple collection of properties, represented as a dictionary of key/value pairs.
Definition PropertiesAdmin.h:420
::IceInternal::Handle< Properties > PropertiesPtr
Definition Properties.h:256
::IceInternal::Handle< Logger > LoggerPtr
Definition Logger.h:145
::IceMX::StringIntDict failures
The failures observed for this metrics.
Definition Metrics.h:1580
::std::string id
The identifier of the metrics object associated to the failures.
Definition Metrics.h:1576