libqi-api  2.8.7.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
manageable.hpp
Go to the documentation of this file.
1 #pragma once
2 /*
3 ** Copyright (C) 2013 Aldebaran Robotics
4 ** See COPYING for the license
5 */
6 
7 #ifndef _QITYPE_MANAGEABLE_HPP_
8 #define _QITYPE_MANAGEABLE_HPP_
9 
10 #include <memory>
11 #include <algorithm>
12 #include <boost/function.hpp>
13 
14 
15 #include <qi/stats.hpp>
16 
17 #include <qi/api.hpp>
18 #include <qi/anyfunction.hpp>
19 #include <qi/type/typeobject.hpp>
20 #include <qi/signal.hpp>
21 
22 #ifdef _MSC_VER
23 # pragma warning( push )
24 # pragma warning( disable: 4251 ) // dll interface
25 #endif
26 
27 namespace qi {
28 
29  class ManageablePrivate;
30  class ExecutionContext;
31 
34  {
39 
41  };
42 
43 
44  class EventTrace
45  {
46  public:
47  enum EventKind
48  {
53  };
55  EventTrace(unsigned int id, EventKind kind, unsigned int slotId,
58  unsigned int callerContext=0, unsigned int calleeContext=0,
60  : _id(id), _kind(kind), _slotId(slotId), _arguments(arguments),
61  _timestamp(timestamp), _postTimestamp(postTimestamp), _userUsTime(userUsTime), _systemUsTime(systemUsTime),
62  _callerContext(callerContext), _calleeContext(calleeContext)
63  {}
64 
65  // trace id, used to match call and call result
66  const unsigned int& id() const { return _id;}
67  const EventKind& kind() const { return _kind;}
68  // method or signal id
69  const unsigned int& slotId() const { return _slotId;}
70  // call or signal arguments
71  const AnyValue& arguments() const { return _arguments;}
72  const qi::os::timeval& timestamp() const { return _timestamp;}
73  const qi::os::timeval& postTimestamp() const { return _postTimestamp;}
74  const qi::int64_t& userUsTime() const { return _userUsTime;}
75  const qi::int64_t& systemUsTime() const { return _systemUsTime;}
76  const unsigned int& callerContext() const { return _callerContext;}
77  const unsigned int& calleeContext() const { return _calleeContext;}
78 
79  private:
80  unsigned int _id; // trace id, used to match call and call result
81  EventKind _kind;
82  unsigned int _slotId; // method or signal id
83  AnyValue _arguments; // call or signal arguments
84  qi::os::timeval _timestamp;
85  qi::os::timeval _postTimestamp; // timestamp of eventual post call
86  qi::int64_t _userUsTime;
87  qi::int64_t _systemUsTime;
88  unsigned int _callerContext; // context of caller function
89  unsigned int _calleeContext; // context where method runs
90  };
91 
92 }
93 
95  ("minValue", minValue),
96  ("maxValue", maxValue),
97  ("cumulatedValue", cumulatedValue));
98 
100  ("count", count),
101  ("wall", wall),
102  ("user", user),
103  ("system", system));
104 
106  ("id", id),
107  ("kind", kind),
108  ("slotId", slotId),
109  ("arguments", arguments),
110  ("timestamp", timestamp),
111  ("userUsTime", userUsTime),
112  ("systemUsTime", systemUsTime),
113  ("callerContext", callerContext),
114  ("calleeContext", calleeContext));
115 
116 QI_TYPE_STRUCT(qi::os::timeval, tv_sec, tv_usec);
117 
118 namespace qi {
119 
120  using ObjectStatistics = std::map<unsigned int, MethodStatistics>;
124  {
125  protected:
126  Manageable();
127  Manageable(const Manageable& b);
128  Manageable& operator=(const Manageable& b);
129 
130  public:
131  virtual ~Manageable();
132 
133  boost::mutex& initMutex();
134 
136  void forceExecutionContext(boost::shared_ptr<ExecutionContext> eventLoop);
138  boost::shared_ptr<ExecutionContext> executionContext() const;
139 
141 
144  bool isStatsEnabled() const;
147  void enableStats(bool enable);
149  void pushStats(int slotId, float wallTime, float userTime, float systemTime);
150  ObjectStatistics stats() const;
152  void clearStats();
153 
156 
158  bool isTraceEnabled() const;
169  void enableTrace(bool enable);
171 
173  static const uint32_t startId = 80;
175  static const uint32_t endId = 99;
176  using MethodMap = std::map<unsigned int, std::pair<AnyFunction, MetaCallType>>;
177  using SignalGetter = boost::function<SignalBase* (void*)>;
178  using SignalMap = std::map<unsigned int, SignalGetter>;
179 
180  /* Return the methods and signals defined at GenericObject level.
181  * The 'this' argument must be the Manageable*.
182  */
183  static MethodMap& manageableMmethodMap();
184  static SignalMap& manageableSignalMap();
185  static MetaObject& manageableMetaObject();
186  static void _build();
187  int _nextTraceId();
188 
189  private:
190  std::unique_ptr<ManageablePrivate> _p;
191  SignalMap signalMap;
192  };
193 }
194 
195 #ifdef _MSC_VER
196 # pragma warning( pop )
197 #endif
198 
200 #endif // _QITYPE_MANAGEABLE_HPP_
Stores min, max and sum of values fed to it.
Definition: stats.hpp:17
Store statistics about method calls.
Definition: stats.hpp:78
int64_t int64_t
Definition: types.hpp:61
const unsigned int & calleeContext() const
Definition: manageable.hpp:77
const unsigned int & id() const
Definition: manageable.hpp:66
#define QI_API
Definition: api.hpp:33
const qi::int64_t & userUsTime() const
Definition: manageable.hpp:74
uint32_t uint32_t
Definition: types.hpp:65
QI_TYPE_STRUCT_AGREGATE_CONSTRUCTOR(qi::MinMaxSum,("minValue", minValue),("maxValue", maxValue),("cumulatedValue", cumulatedValue))
const qi::os::timeval & timestamp() const
Definition: manageable.hpp:72
EventTrace(unsigned int id, EventKind kind, unsigned int slotId, const AnyValue &arguments, const qi::os::timeval timestamp, qi::int64_t userUsTime=0, qi::int64_t systemUsTime=0, unsigned int callerContext=0, unsigned int calleeContext=0, qi::os::timeval postTimestamp=qi::os::timeval())
Definition: manageable.hpp:55
std::map< unsigned int, MethodStatistics > ObjectStatistics
Definition: manageable.hpp:120
const AnyValue & arguments() const
Definition: manageable.hpp:71
const EventKind & kind() const
Definition: manageable.hpp:67
std::map< unsigned int, SignalGetter > SignalMap
Definition: manageable.hpp:178
Signal< EventTrace > traceObject
Emitted each time a call starts and finishes, and for each signal trigger.
Definition: manageable.hpp:155
const qi::int64_t & systemUsTime() const
Definition: manageable.hpp:75
dll import/export and compiler message
QI_TYPE_STRUCT(qi::os::timeval, tv_sec, tv_usec)
Object is thread safe, multiple calls can occur in different threads in parallel. ...
Definition: manageable.hpp:38
QI_TYPE_ENUM(qi::EventTrace::EventKind)
boost::function< SignalBase *(void *)> SignalGetter
Definition: manageable.hpp:177
const qi::os::timeval & postTimestamp() const
Definition: manageable.hpp:73
struct similar to POSIX timeval
Definition: os.hpp:293
const unsigned int & callerContext() const
Definition: manageable.hpp:76
const unsigned int & slotId() const
Definition: manageable.hpp:69
Object is not thread safe, all method calls must occur in the same thread.
Definition: manageable.hpp:36
int system(const char *command)
Execute a shell command.
ObjectThreadingModel
Possible thread models for an object.
Definition: manageable.hpp:33
Description of the signals and methods accessible on an ObjectTypeInterface.
Definition: metaobject.hpp:25
std::map< unsigned int, std::pair< AnyFunction, MetaCallType >> MethodMap
Definition: manageable.hpp:176