dnxStats.c File Reference

Implements statistics management functionality. More...

#include "dnxStats.h"
#include "dnxDebug.h"
#include "dnxError.h"
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

Go to the source code of this file.

Data Structures

struct  DnxNode
 The structure for managing stats for one client node, by IP address. More...

Defines

#define elemcount(x)   (sizeof(x)/sizeof(*(x)))

Functions

static unsigned hashFunction (struct sockaddr *addr)
 Return a 1-byte hash code for the specified address.
static int dnxAddrCompare (struct sockaddr *addr_a, struct sockaddr *addr_b)
 Compare two addresses.
static DnxNodednxCreateNodeAt (DnxNode **npp, struct sockaddr *saddr, unsigned hashCode)
 Create, insert, and return a new node with a specified address and hash.
static DnxNodednxGetNode (struct sockaddr *saddr)
 Return a node matching an address.
void dnxStatsGetServerStats (unsigned *statsbuf)
 Return a current snapshot of the server stats array.
void dnxStatsResetServerStats (void)
 Reset all server stats to zero.
void dnxStatsInc (char *addr, DnxStatsIndex member)
 Increment a member value from a single node.
int dnxStatsForEachNode (DnxStatsNodeCB *cb, void *data)
 For each node in the list, call a method and pass some data.
int dnxStatsForNodeByAddrStr (char *addrstr, DnxStatsNodeCB *cb, void *data)
 For the specified node, call a method and pass some data.
void dnxStatsCleanup ()
 A destructor for the stats management module.

Variables

static DnxNodes_nodeHashTable [256]
 The global node hash table.
static unsigned s_serverStats [STATS_COUNT]
 The global server stats table.


Detailed Description

Implements statistics management functionality.

This implementation uses no locking in order to reduce contention. Since no nodes are ever deleted, this shouldn't be a problem, as all access is read-only. When adding nodes, there is a possibility that the same node will be added multiple times by multiple threads. To avoid this issue, ensure that the first stat increment of each new node is always done by a single thread.

This implementation is based on a hash table of fixed size containing linked bucket lists. Because the lists are singly-linked, insertion is atomic. A reader will either see a node or not, depending on if the read happened to take place after or before the atomic write of the previous nodes "next" field.

Author:
Steven D. Morrey (dnx-devel@lists.sourceforge.net)

John Calcote (dnx-devel@lists.sourceforge.net)

Attention:
Please submit patches to http://dnx.sourceforge.net

Definition in file dnxStats.c.


Define Documentation

#define elemcount (  )     (sizeof(x)/sizeof(*(x)))

Definition at line 51 of file dnxStats.c.


Function Documentation

static int dnxAddrCompare ( struct sockaddr *  addr_a,
struct sockaddr *  addr_b 
) [static]

Compare two addresses.

Only the network address portion of the address is compared; port numbers are ignored.

Parameters:
[in] addr_a - the first address to compare
[in] addr_b - the second address to compare
Returns:
A value less than zero if addr_a is less than addr_b. Zero if addr_a is equal to addr_b. A value greater than zero if addr_a is greater than addr_b.

Definition at line 115 of file dnxStats.c.

Referenced by dnxGetNode().

static DnxNode* dnxCreateNodeAt ( DnxNode **  npp,
struct sockaddr *  saddr,
unsigned  hashCode 
) [static]

Create, insert, and return a new node with a specified address and hash.

The new node is inserted at the specified location in the hash table.

Parameters:
[in] npp - the location at which to store the new node.
[in] saddr - the address for which a new node should be created.
[in] hashCode - the previously calculated hash code for addr.
Returns:
the new node, or zero on memory allocation failure.

Definition at line 147 of file dnxStats.c.

References DnxNodeData::address, DnxNodeData::addrstr, DnxNode::data, dnxNtop(), DnxNode::saddr, and xmalloc.

Referenced by dnxGetNode().

static DnxNode* dnxGetNode ( struct sockaddr *  saddr  )  [static]

Return a node matching an address.

NO LOCKS: Ensure caller is a single thread. (At least for the create case.)

Insert a new node and return it if missing.

Parameters:
[in] saddr - the address of the node to be returned.
Returns:
a (possibly new) node matching the specified address.

Definition at line 176 of file dnxStats.c.

References dnxAddrCompare(), dnxCreateNodeAt(), hashFunction(), DnxNode::next, and DnxNode::saddr.

Referenced by dnxStatsForNodeByAddrStr(), and dnxStatsInc().

void dnxStatsCleanup (  ) 

A destructor for the stats management module.

This function can be used to cleanup any resources allocated by this module before shutdown.

Definition at line 256 of file dnxStats.c.

Referenced by dnxServerDeInit().

int dnxStatsForEachNode ( DnxStatsNodeCB cb,
void *  data 
)

For each node in the list, call a method and pass some data.

Parameters:
[in] cb - the node processing function to be called.
[in] data - opaque data pointer passed to nodeProc.
Returns:
zero on success or callback-cancelled, or a non-zero error code returned by nodeProc.

Definition at line 219 of file dnxStats.c.

Referenced by buildMgmtNodeListReply(), buildMgmtStatsReply(), and dnxAgentServer().

int dnxStatsForNodeByAddrStr ( char *  addrstr,
DnxStatsNodeCB cb,
void *  data 
)

For the specified node, call a method and pass some data.

Parameters:
[in] addrstr - the DNS or dotted decimal address of the node.
[in] cb - the node processing function to be called.
[in] data - opaque data pointer passed to nodeProc.
Returns:
zero on success or callback-cancelled, or a non-zero error code returned by nodeProc.

Definition at line 234 of file dnxStats.c.

Referenced by buildMgmtNodeStatsReply().

void dnxStatsGetServerStats ( unsigned *  statsbuf  ) 

Return a current snapshot of the server stats array.

The statsbuf array should be at least STATS_COUNT elements in length.

Parameters:
[out] statsbuf - the address of storage for all server stats.

Definition at line 192 of file dnxStats.c.

Referenced by buildMgmtStatsReply().

void dnxStatsInc ( char *  addr,
DnxStatsIndex  member 
)

Increment a member value from a single node.

Parameters:
[in] addr - the address of the node for which a stat should be incremented. This is the binary address, not the presentation address.
[in] member - the stat to be incremented.

Definition at line 206 of file dnxStats.c.

Referenced by dnxDispatcher(), dnxGetNodeRequest(), dnxPostNewJob(), dnxPostResult(), dnxRegisterNode(), and dnxTimer().

void dnxStatsResetServerStats ( void   ) 

Reset all server stats to zero.

Definition at line 199 of file dnxStats.c.

Referenced by dnxAgentServer().

static unsigned hashFunction ( struct sockaddr *  addr  )  [static]

Return a 1-byte hash code for the specified address.

Parameters:
[in] addr - the address to be hashed.
Returns:
an unsigned hash code with a maximum value of 255.
Note:
: This hash function assumes that addr is at LEAST 4 bytes long.

Definition at line 77 of file dnxStats.c.

Referenced by dnxGetNode().


Variable Documentation

DnxNode* s_nodeHashTable[256] [static]

The global node hash table.

Definition at line 62 of file dnxStats.c.

unsigned s_serverStats[STATS_COUNT] [static]

The global server stats table.

Definition at line 63 of file dnxStats.c.

Referenced by dnxStatsGetServerStats(), dnxStatsInc(), and dnxStatsResetServerStats().


Generated on Tue Apr 13 15:48:08 2010 for DNX by  doxygen 1.5.6