SIP Witch 1.9.15
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stats.cpp
Go to the documentation of this file.
1 // Copyright (C) 2009-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #include <sipwitch-config.h>
18 #include <ucommon/ucommon.h>
19 #include <ucommon/export.h>
20 #include <sipwitch/stats.h>
21 #include <sipwitch/control.h>
22 
23 namespace sipwitch {
24 
25 static unsigned used = 0, total = 7;
26 static stats *base = NULL;
27 
28 static class __LOCAL sta : public mapped_array<stats>
29 {
30 public:
31  sta();
32 
33  void init(void);
34 } shm;
35 
36 sta::sta() : mapped_array<stats>()
37 {
38 }
39 
40 void sta::init(void)
41 {
42  const char *statmap = control::env("statmap");
43  ::remove(statmap);
44  create(statmap, total);
45 }
46 
48 {
49  shm.init();
50  base = request("system");
51  request("extension");
52  request("service");
53  request("gateway");
54  request("external");
55  request("other");
56  return base;
57 }
58 
59 stats *stats::request(const char *id)
60 {
61  assert(id && *id);
62 
63  if(used >= total)
64  return NULL;
65 
66  stats *node = shm(used++);
67  snprintf(node->id, sizeof(node->id), "%s", id);
68  return node;
69 }
70 
71 void stats::allocate(unsigned count)
72 {
73  assert(count > 0);
74 
75  total += count;
76 }
77 
78 unsigned stats::active(void) const
79 {
80  return data[0].current + data[1].current;
81 }
82 
83 void stats::assign(stat_t entry)
84 {
85  Mutex::protect(this);
86  ++data[entry].period;
87  ++data[entry].total;
88  ++data[entry].current;
89  if(data[entry].current > data[entry].peak)
90  data[entry].peak = data[entry].current;
91  if(data[entry].current > data[entry].max)
92  data[entry].max = data[entry].current;
93  Mutex::release(this);
94  if(this != base)
95  base->assign(entry);
96 }
97 
98 void stats::release(void)
99 {
100  shm.release();
101  shm.remove(control::env("statmap"));
102 }
103 
105 {
106  Mutex::protect(this);
107  if(active() == 1)
108  time(&lastcall);
109  --data[entry].current;
110  if(data[entry].current < data[entry].min)
111  data[entry].min = data[entry].current;
112  Mutex::release(this);
113  if(this != base)
114  base->release(entry);
115 }
116 
117 void stats::period(FILE *fp)
118 {
119  unsigned pos = 0;
120  char text[80];
121  size_t len;
122  time_t last;
123 
124  while(pos < total) {
125  stats *node = shm(pos++);
126  if(!node->id[0])
127  continue;
128 
129  if(fp) {
130  snprintf(text, sizeof(text), " %-12s", node->id);
131  len = strlen(text);
132  }
133  else
134  len = 0;
135 
136  Mutex::protect(node);
137  for(unsigned entry = 0; entry < 2; ++entry) {
138  if(fp) {
139  snprintf(text + len, sizeof(text) - len, " %09lu %05hu %05hu",
140  node->data[entry].period, node->data[entry].min, node->data[entry].max);
141  len = strlen(text);
142  }
143  node->data[entry].pperiod = node->data[entry].period;
144  node->data[entry].pmin = node->data[entry].min;
145  node->data[entry].pmax = node->data[entry].max;
146  node->data[entry].min = node->data[entry].max = node->data[entry].current;
147  node->data[entry].period = 0;
148  }
149  last = node->lastcall;
150  Mutex::release(node);
151  if(fp)
152  fprintf(fp, "%s %ld\n", text, (long)last);
153  }
154 }
155 
156 } // end namespace
unsigned long total
Definition: stats.h:69
A stat element of call traffic.
Definition: stats.h:57
unsigned count
Definition: cgiserver.cpp:92
static stats * create(void)
Create stats in shared memory pool.
Definition: stats.cpp:47
unsigned short min
Definition: stats.h:70
void assign(stat_t element)
Assign a call to inbound or outbound statistic for this stat node.
Definition: stats.cpp:83
unsigned short current
Definition: stats.h:70
char id[12]
Definition: stats.h:60
unsigned short peak
Definition: stats.h:70
Basic server call statistics.
unsigned short pmin
Definition: stats.h:70
time_t lastcall
Definition: stats.h:73
unsigned short pmax
Definition: stats.h:70
unsigned active(void) const
Total number of active calls in the server at the moment.
Definition: stats.cpp:78
static stats * request(const char *id)
Request a stat node from the memory pool by id.
Definition: stats.cpp:59
unsigned long period
Definition: stats.h:69
void release(stat_t element)
Release a call from inbound or outbound stastic for this stat node.
Definition: stats.cpp:104
unsigned short max
Definition: stats.h:70
static void release(void)
Release stat nodes shared memory segment.
Definition: stats.cpp:98
Manage control interface.
struct sipwitch::stats::@9 data[2]
We have stats for both incoming and outgoing traffic of various kinds.
unsigned long pperiod
Definition: stats.h:69
static void allocate(unsigned count)
Server allocate x number of stat nodes at startup.
Definition: stats.cpp:71
static const char * env(const char *id)
Return the value of a server environment variable.
Definition: control.h:131