1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
/*
* Copyright (c) 2012 Gilles Chehade <gilles@poolp.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/param.h>
#include <event.h>
#include <imsg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "smtpd.h"
#include "log.h"
static void ramstat_init(void);
static void ramstat_close(void);
static void ramstat_increment(const char *, size_t);
static void ramstat_decrement(const char *, size_t);
static void ramstat_set(const char *, const struct stat_value *);
static int ramstat_iter(void **, char **, struct stat_value *);
struct ramstat_entry {
RB_ENTRY(ramstat_entry) entry;
char key[STAT_KEY_SIZE];
struct stat_value value;
};
RB_HEAD(stats_tree, ramstat_entry) stats;
RB_PROTOTYPE(stats_tree, ramstat_entry, entry, ramstat_entry_cmp);
struct stat_backend stat_backend_ramstat = {
ramstat_init,
ramstat_close,
ramstat_increment,
ramstat_decrement,
ramstat_set,
ramstat_iter
};
static void
ramstat_init(void)
{
log_trace(TRACE_STAT, "ramstat: init");
RB_INIT(&stats);
/* ramstat_set() should be called for each key we want
* to have displayed by smtpctl show stats at startup.
*/
ramstat_set("uptime", stat_timestamp(env->sc_uptime));
}
static void
ramstat_close(void)
{
log_trace(TRACE_STAT, "ramstat: close");
}
static void
ramstat_increment(const char *name, size_t val)
{
struct ramstat_entry *np, lk;
log_trace(TRACE_STAT, "ramstat: increment: %s", name);
strlcpy(lk.key, name, sizeof (lk.key));
np = RB_FIND(stats_tree, &stats, &lk);
if (np == NULL) {
np = xcalloc(1, sizeof *np, "ramstat_increment");
strlcpy(np->key, name, sizeof (np->key));
RB_INSERT(stats_tree, &stats, np);
}
log_trace(TRACE_STAT, "ramstat: %s (%p): %zd -> %zd",
name, name, np->value.u.counter, np->value.u.counter + val);
np->value.u.counter += val;
}
static void
ramstat_decrement(const char *name, size_t val)
{
struct ramstat_entry *np, lk;
log_trace(TRACE_STAT, "ramstat: decrement: %s", name);
strlcpy(lk.key, name, sizeof (lk.key));
np = RB_FIND(stats_tree, &stats, &lk);
if (np == NULL) {
np = xcalloc(1, sizeof *np, "ramstat_decrement");
strlcpy(np->key, name, sizeof (np->key));
RB_INSERT(stats_tree, &stats, np);
}
log_trace(TRACE_STAT, "ramstat: %s (%p): %zd -> %zd",
name, name, np->value.u.counter, np->value.u.counter - val);
np->value.u.counter -= val;
}
static void
ramstat_set(const char *name, const struct stat_value *val)
{
struct ramstat_entry *np, lk;
log_trace(TRACE_STAT, "ramstat: set: %s", name);
strlcpy(lk.key, name, sizeof (lk.key));
np = RB_FIND(stats_tree, &stats, &lk);
if (np == NULL) {
np = xcalloc(1, sizeof *np, "ramstat_set");
strlcpy(np->key, name, sizeof (np->key));
RB_INSERT(stats_tree, &stats, np);
}
log_trace(TRACE_STAT, "ramstat: %s: n/a -> n/a", name);
np->value = *val;
}
static int
ramstat_iter(void **iter, char **name, struct stat_value *val)
{
struct ramstat_entry *np;
log_trace(TRACE_STAT, "ramstat: iter");
if (RB_EMPTY(&stats))
return 0;
if (*iter == NULL)
np = RB_MIN(stats_tree, &stats);
else
np = RB_NEXT(stats_tree, &stats, *iter);
*iter = np;
if (np == NULL)
return 0;
*name = np->key;
*val = np->value;
return 1;
}
static int
ramstat_entry_cmp(struct ramstat_entry *e1, struct ramstat_entry *e2)
{
return strcmp(e1->key, e2->key);
}
RB_GENERATE(stats_tree, ramstat_entry, entry, ramstat_entry_cmp);
|