Marine Library  1.0
C++ library for Linux Networking Development
Macros | Functions
attr_stats.hh File Reference

Program statistics and profiling. More...

#include "impl/attr_stats_impl.hh"

Go to the source code of this file.

Macros

#define ATTR_ADD(attr, val)
 Increase an attribute atomically. More...
 
#define ATTR_SET(attr, val)
 Modify an attribute atomically. More...
 
#define ATTR_SET_EX(attr, val, old)
 Get an attribute and then modify it atomically. More...
 

Functions

bool ATTR_INIT (size_t capacity=1000)
 Initialize statistics APIs. More...
 
bool ATTR_ADD_SLOW (int attr, uint64_t val)
 Increase an attribute atomically. More...
 
bool ATTR_SET_SLOW (int attr, uint64_t val, uint64_t *old=NULL)
 Get an attribute and then modify it atomically. More...
 
template<class Op >
void ATTR_ITERATE (Op op)
 Iterate and apply an operation to all attributes. More...
 

Detailed Description

Program statistics and profiling.

APIs for profiling program based on lock-free, thread-safe counters.
Each counter is called an attribute, identified by an positive integer. You can put as many attributes in program as you want.

Author
Zhao DAI

Macro Definition Documentation

#define ATTR_ADD (   attr,
  val 
)

Increase an attribute atomically.

An example usage shows below:

// init statistics APIs before use
...
...
// attributes 100, 101, 102 and 103 are used to save number
// of commands for Login, Logout, Query and others, respectively.
void process(Command & cmd){
switch(cmd.type){
case Login: ATTR_ADD(100, 1); break;
case Logout:ATTR_ADD(101, 1); break;
case Query: ATTR_ADD(102, 1); break;
default: ATTR_ADD(103, 1); break;
}
}
Parameters
attrA positive number identifying the attribute
valA non-negative number to add to the attribute
Note
For performance reason, attr must be a constant expression, i.e. can be evaluated at compile time. And this is the recommended way of usage.
But if that is not the case, please use ATTR_ADD_SLOW instead.
See also
ATTR_ADD_SLOW
#define ATTR_SET (   attr,
  val 
)

Modify an attribute atomically.

An example usage shows below:

// init statistics APIs before use
...
// a message queue shared between threads or processes
MessageQueue queue;
...
// attributes 104 is used to monitor the length of 'queue' in a dedicated thread
void monitorThread(){
for(;;sleep(1)){
ATTR_SET(104, queue.length());
}
}
Parameters
attrA positive number identifying the attribute
valA non-negative number
Note
For performance reason, attr must be a constant expression, i.e. can be evaluated at compile time. And this is the recommended way of usage.
But if that is not the case, please use ATTR_SET_SLOW instead.
See also
ATTR_SET_SLOW
#define ATTR_SET_EX (   attr,
  val,
  old 
)

Get an attribute and then modify it atomically.

Parameters
[in]attrA positive number identifying the attribute
[in]valA non-negative number
[out]oldPointer to a variable to receive the value of attribute before modification; or NULL if not interested
Note
For performance reason, attr must be a constant expression, i.e. can be evaluated at compile time. And this is the recommended way of usage.
But if that is not the case, please use ATTR_SET_SLOW instead.
See also
ATTR_SET_SLOW

Function Documentation

bool ATTR_ADD_SLOW ( int  attr,
uint64_t  val 
)
inline

Increase an attribute atomically.

Parameters
attrA positive number identifying the attribute
valA non-negative number to add to the attribute
Returns
true if succeeded; otherwise false
See also
ATTR_ADD
bool ATTR_INIT ( size_t  capacity = 1000)
inline

Initialize statistics APIs.

This function must be called once only before any other statistics APIs.

Parameters
capacityThe maximum number of attributes in the program
Returns
true if succeeded; otherwise false
template<class Op >
void ATTR_ITERATE ( Op  op)
inline

Iterate and apply an operation to all attributes.

Parameters
opOperation to apply to all attributes, should implement:
void operator ()(int attr, uint64_t value) const;
In which:
  • attr is a positive number identifying the attribute
  • value is the current value of the attribute
Note
After the operation, each attribute will be reset to 0 for a new turn of counting.
bool ATTR_SET_SLOW ( int  attr,
uint64_t  val,
uint64_t *  old = NULL 
)
inline

Get an attribute and then modify it atomically.

Parameters
[in]attrA positive number identifying the attribute
[in]valA non-negative number
[out]oldPointer to a variable to receive the value of attribute before modification; or NULL if not interested
See also
ATTR_SET ATTR_SET_EX