5 #ifndef ICE_METRICSADMIN_I_H
6 #define ICE_METRICSADMIN_I_H
15 # define ICE_CPP11_COMPILER_REGEXP
23 #elif defined(ICE_CPP11_COMPILER_REGEXP)
36 template<
typename T>
class MetricsHelperT;
48 #ifdef ICE_CPP11_MAPPING
49 public std::enable_shared_from_this<MetricsMapI>
60 RegExp(
const std::string&,
const std::string&);
63 template<
typename T>
bool
64 match(
const IceMX::MetricsHelperT<T>& helper,
bool reject)
69 value = helper(_attribute);
71 catch(
const std::exception&)
80 bool match(
const std::string&);
82 const std::string _attribute;
89 #elif defined(ICE_CPP11_COMPILER_REGEXP)
91 std::tr1::regex _regex;
101 virtual ~MetricsMapI();
103 MetricsMapI(
const std::string&,
const Ice::PropertiesPtr&);
104 MetricsMapI(
const MetricsMapI&);
106 virtual void destroy() = 0;
112 virtual MetricsMapIPtr clone()
const = 0;
119 const std::vector<std::string> _groupByAttributes;
120 const std::vector<std::string> _groupBySeparators;
122 const std::vector<RegExpPtr> _accept;
123 const std::vector<RegExpPtr> _reject;
126 class ICE_API MetricsMapFactory
127 #ifndef ICE_CPP11_MAPPING
128 :
public Ice::LocalObject
133 virtual ~MetricsMapFactory();
135 MetricsMapFactory(IceMX::Updater*);
137 virtual MetricsMapIPtr create(
const std::string&,
const Ice::PropertiesPtr&) = 0;
143 IceMX::Updater* _updater;
147 template<
class MetricsType>
class MetricsMapT :
public MetricsMapI,
private IceUtil::Mutex
151 typedef MetricsType T;
152 typedef ICE_INTERNAL_HANDLE<MetricsType> TPtr;
162 #ifdef ICE_CPP11_MAPPING
163 public std::enable_shared_from_this<EntryT>
165 public Ice::LocalObject
170 EntryT(MetricsMapTPtr map,
const TPtr&
object,
const typename std::list<EntryTPtr>::iterator& p) :
171 _map(map), _object(object), _detachedPos(p)
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)
181 p->second.first->destroy();
186 failed(
const std::string& exceptionName)
190 ++_failures[exceptionName];
193 template<
typename MemberMetricsType>
typename MetricsMapT<MemberMetricsType>::EntryTPtr
194 getMatching(
const std::string& mapName,
const IceMX::MetricsHelperT<MemberMetricsType>& helper)
199 typename std::map<std::string, std::pair<MetricsMapIPtr, SubMapMember> >::iterator p =
200 _subMaps.find(mapName);
201 if(p == _subMaps.end())
203 std::pair<MetricsMapIPtr, SubMapMember> map = _map->createSubMap(mapName);
206 p = _subMaps.insert(make_pair(mapName, map)).first;
209 if(p == _subMaps.end())
216 MetricsMapT<MemberMetricsType>* map =
dynamic_cast<MetricsMapT<MemberMetricsType>*
>(m.get());
218 return map->getMatching(helper);
225 _object->totalLifetime += lifetime;
226 if(--_object->current == 0)
228 #ifdef ICE_CPP11_MAPPING
229 _map->detached(this->shared_from_this());
231 _map->detached(
this);
236 template<
typename Function>
void
237 execute(Function func)
264 for(
typename std::map<std::string, std::pair<MetricsMapIPtr, SubMapMember> >::const_iterator p =
265 _subMaps.begin(); p != _subMaps.end(); ++p)
267 metrics.get()->*p->second.second = p->second.first->getMetrics();
275 return _object->current == 0;
279 attach(
const IceMX::MetricsHelperT<T>& helper)
283 helper.initMetrics(_object);
286 friend class MetricsMapT;
290 std::map<std::string, std::pair<MetricsMapIPtr, SubMapMember> > _subMaps;
291 typename std::list<EntryTPtr>::iterator _detachedPos;
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)
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)
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())
308 if(properties->getPropertiesForPrefix(subMapsPrefix).empty())
310 subMapPrefix = mapPrefix;
317 _subMaps.insert(std::make_pair(p->first,
318 std::make_pair(p->second.first,
319 p->second.second->create(subMapPrefix, properties))));
323 MetricsMapT(
const MetricsMapT& other)
325 #ifndef ICE_CPP11_MAPPING
334 #ifdef ICE_CPP11_MAPPING
335 std::shared_ptr<MetricsMapT> shared_from_this()
337 return std::static_pointer_cast<MetricsMapT>(MetricsMapI::shared_from_this());
347 _detachedQueue.clear();
356 for(
typename std::map<std::string, EntryTPtr>::const_iterator p = _objects.begin(); p != _objects.end(); ++p)
358 objects.push_back(p->second->clone());
369 for(
typename std::map<std::string, EntryTPtr>::const_iterator p = _objects.begin(); p != _objects.end(); ++p)
374 failures.push_back(f);
381 getFailures(
const std::string&
id)
384 typename std::map<std::string, EntryTPtr>::const_iterator p = _objects.find(
id);
385 if(p != _objects.end())
387 return p->second->getFailures();
392 std::pair<MetricsMapIPtr, SubMapMember>
393 createSubMap(
const std::string& subMapName)
395 typename std::map<std::string, std::pair<SubMapMember, MetricsMapIPtr> >::const_iterator p =
396 _subMaps.find(subMapName);
397 if(p != _subMaps.end())
399 return std::pair<MetricsMapIPtr, SubMapMember>(
ICE_GET_SHARED_FROM_THIS(p->second.second->clone()), p->second.first);
401 return std::pair<MetricsMapIPtr, SubMapMember>(MetricsMapIPtr(
ICE_NULLPTR),
static_cast<SubMapMember
>(0));
405 getMatching(
const IceMX::MetricsHelperT<T>& helper,
const EntryTPtr& previous = EntryTPtr())
410 for(std::vector<RegExpPtr>::const_iterator p = _accept.begin(); p != _accept.end(); ++p)
412 if(!(*p)->match(helper,
false))
418 for(std::vector<RegExpPtr>::const_iterator p = _reject.begin(); p != _reject.end(); ++p)
420 if((*p)->match(helper,
true))
432 if(_groupByAttributes.size() == 1)
434 key = helper(_groupByAttributes.front());
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)
444 if(q != _groupBySeparators.end())
452 catch(
const std::exception&)
466 if(previous && previous->_object->id == key)
468 assert(_objects[key] == previous);
472 typename std::map<std::string, EntryTPtr>::const_iterator p = _objects.find(key);
473 if(p == _objects.end())
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;
482 p = _objects.insert(
typename std::map<std::string, EntryTPtr>::value_type(
483 key,
new EntryT(
this, t, _detachedQueue.end()))).first;
487 p->second->attach(helper);
493 virtual MetricsMapIPtr clone()
const
498 void detached(EntryTPtr entry)
502 if(_retain == 0 || _destroyed)
507 assert(
static_cast<int>(_detachedQueue.size()) <= _retain);
510 if(entry->_detachedPos != _detachedQueue.end())
512 if(entry->_detachedPos != --_detachedQueue.end())
514 _detachedQueue.splice(_detachedQueue.end(), _detachedQueue, entry->_detachedPos);
515 entry->_detachedPos = --_detachedQueue.end();
521 if(
static_cast<int>(_detachedQueue.size()) == _retain)
524 typename std::list<EntryTPtr>::iterator p = _detachedQueue.begin();
525 while(p != _detachedQueue.end())
527 if(!(*p)->isDetached())
529 (*p)->_detachedPos = _detachedQueue.end();
530 p = _detachedQueue.erase(p);
540 if(
static_cast<int>(_detachedQueue.size()) == _retain)
542 _objects.erase(_detachedQueue.front()->_object->id);
543 _detachedQueue.pop_front();
547 entry->_detachedPos = _detachedQueue.insert(_detachedQueue.end(), entry);
548 assert(entry->_detachedPos != _detachedQueue.end());
555 std::map<std::string, EntryTPtr> _objects;
556 std::list<EntryTPtr> _detachedQueue;
557 std::map<std::string, std::pair<SubMapMember, MetricsMapIPtr> > _subMaps;
560 template<
class MetricsType>
class MetricsMapFactoryT :
public MetricsMapFactory
564 MetricsMapFactoryT(IceMX::Updater* updater) : MetricsMapFactory(updater)
568 virtual MetricsMapIPtr
569 create(
const std::string& mapPrefix,
const Ice::PropertiesPtr& properties)
571 return ICE_MAKE_SHARED(MetricsMapT<MetricsType>, mapPrefix, properties, _subMaps);
574 template<
class SubMapMetricsType>
void
575 registerSubMap(
const std::string& subMap,
IceMX::MetricsMap MetricsType::* member)
583 std::map<std::string, std::pair<IceMX::MetricsMap MetricsType::*, MetricsMapFactoryPtr> > _subMaps;
590 MetricsViewI(
const std::string&);
594 bool addOrUpdateMap(
const Ice::PropertiesPtr&,
const std::string&,
const MetricsMapFactoryPtr&,
595 const Ice::LoggerPtr&);
596 bool removeMap(
const std::string&);
602 std::vector<std::string> getMaps()
const;
604 MetricsMapIPtr getMap(
const std::string&)
const;
608 const std::string _name;
609 std::map<std::string, MetricsMapIPtr> _maps;
614 #ifndef ICE_CPP11_MAPPING
615 public Ice::PropertiesAdminUpdateCallback,
621 MetricsAdminI(const ::Ice::PropertiesPtr&,
const Ice::LoggerPtr&);
628 template<
class MetricsType>
void
629 registerMap(
const std::string& map, IceMX::Updater* updater)
632 MetricsMapFactoryPtr factory;
636 _factories[map] = factory;
637 updated = addOrUpdateMap(map, factory);
645 template<
class MemberMetricsType,
class MetricsType>
void
646 registerSubMap(
const std::string& map,
const std::string& subMap,
IceMX::MetricsMap MetricsType::* member)
649 ICE_HANDLE<MetricsMapFactoryT<MetricsType> > factory;
652 std::map<std::string, MetricsMapFactoryPtr>::const_iterator p = _factories.find(map);
653 if(p == _factories.end())
657 #ifdef ICE_CPP11_MAPPING
658 factory = ::std::dynamic_pointer_cast<MetricsMapFactoryT<MetricsType>>(p->second);
660 factory =
dynamic_cast<MetricsMapFactoryT<MetricsType>*
>(p->second.get());
662 factory->template registerSubMap<MemberMetricsType>(subMap, member);
664 updated = addOrUpdateMap(map, factory);
672 void unregisterMap(
const std::string&);
678 #ifdef ICE_CPP11_MAPPING
679 virtual void enableMetricsView(std::string, const ::Ice::Current&);
680 virtual void disableMetricsView(std::string, const ::Ice::Current&);
683 virtual IceMX::MetricsFailures getMetricsFailures(std::string, std::string, std::string, const ::Ice::Current&);
685 virtual void enableMetricsView(
const std::string&, const ::Ice::Current&);
686 virtual void disableMetricsView(
const std::string&, const ::Ice::Current&);
689 const ::Ice::Current&);
690 virtual IceMX::MetricsFailures getMetricsFailures(
const std::string&,
const std::string&,
const std::string&,
691 const ::Ice::Current&);
693 std::vector<MetricsMapIPtr> getMaps(
const std::string&)
const;
695 const Ice::LoggerPtr& getLogger()
const;
699 MetricsViewIPtr getMetricsView(
const std::string&);
701 bool addOrUpdateMap(
const std::string&,
const MetricsMapFactoryPtr&);
702 bool removeMap(
const std::string&);
704 std::map<std::string, MetricsViewIPtr> _views;
705 std::set<std::string> _disabledViews;
706 std::map<std::string, MetricsMapFactoryPtr> _factories;
708 const Ice::LoggerPtr _logger;
709 Ice::PropertiesPtr _properties;