5
/* postscreen table access wrappers
7
/* #include <postscreen.h>
9
/* int psc_addr_match_list_match(match_list, client_addr)
10
/* ADDR_MATCH_LIST *match_list;
11
/* const char *client_addr;
13
/* const char *psc_cache_lookup(DICT_CACHE *cache, const char *key)
17
/* void psc_cache_update(cache, key, value)
22
/* void psc_dict_get(dict, key)
26
/* void psc_maps_find(maps, key, flags)
31
/* This module implements wrappers around time-critical table
32
/* access functions. The functions log a warning when table
33
/* access takes a non-trivial amount of time.
35
/* psc_addr_match_list_match() is a wrapper around
36
/* addr_match_list_match().
38
/* psc_cache_lookup() and psc_cache_update() are wrappers around
39
/* the corresponding dict_cache() methods.
41
/* psc_dict_get() and psc_maps_find() are wrappers around
42
/* dict_get() and maps_find(), respectively.
46
/* The Secure Mailer license must be distributed with this software.
49
/* IBM T.J. Watson Research
51
/* Yorktown Heights, NY 10598, USA
58
/* Utility library. */
67
/* Application-specific. */
69
#include <postscreen.h>
72
* Monitor time-critical operations.
74
* XXX Averaging support was added during a stable release candidate, so it
75
* provides only the absolute minimum necessary. A complete implementation
76
* should maintain separate statistics for each table, and it should not
77
* complain when the access latency is less than the time between accesses.
79
#define PSC_GET_TIME_BEFORE_LOOKUP { \
80
struct timeval _before, _after; \
82
double _new_delta_ms; \
83
GETTIMEOFDAY(&_before);
85
#define PSC_DELTA_MS(d) ((d).dt_sec * 1000.0 + (d).dt_usec / 1000.0)
87
#define PSC_AVERAGE(new, old) (0.1 * (new) + 0.9 * (old))
89
#ifndef PSC_THRESHOLD_MS
90
#define PSC_THRESHOLD_MS 100 /* nag if latency > 100ms */
93
#ifndef PSC_WARN_LOCKOUT_S
94
#define PSC_WARN_LOCKOUT_S 60 /* don't nag for 60s */
98
* Shared warning lock, so that we don't spam the logfile when the system
101
static time_t psc_last_warn = 0;
103
#define PSC_CHECK_TIME_AFTER_LOOKUP(table, action, average) \
104
GETTIMEOFDAY(&_after); \
105
PSC_CALC_DELTA(_delta, _after, _before); \
106
_new_delta_ms = PSC_DELTA_MS(_delta); \
107
if ((average = PSC_AVERAGE(_new_delta_ms, average)) > PSC_THRESHOLD_MS \
108
&& psc_last_warn < _after.tv_sec - PSC_WARN_LOCKOUT_S) { \
109
msg_warn("%s: %s %s average delay is %.0f ms", \
110
myname, (table), (action), average); \
111
psc_last_warn = _after.tv_sec; \
115
/* psc_addr_match_list_match - time-critical address list lookup */
117
int psc_addr_match_list_match(ADDR_MATCH_LIST *addr_list,
118
const char *addr_str)
120
const char *myname = "psc_addr_match_list_match";
122
static double latency_ms;
124
PSC_GET_TIME_BEFORE_LOOKUP;
125
result = addr_match_list_match(addr_list, addr_str);
126
PSC_CHECK_TIME_AFTER_LOOKUP("address list", "lookup", latency_ms);
130
/* psc_cache_lookup - time-critical cache lookup */
132
const char *psc_cache_lookup(DICT_CACHE *cache, const char *key)
134
const char *myname = "psc_cache_lookup";
136
static double latency_ms;
138
PSC_GET_TIME_BEFORE_LOOKUP;
139
result = dict_cache_lookup(cache, key);
140
PSC_CHECK_TIME_AFTER_LOOKUP(dict_cache_name(cache), "lookup", latency_ms);
144
/* psc_cache_update - time-critical cache update */
146
void psc_cache_update(DICT_CACHE *cache, const char *key, const char *value)
148
const char *myname = "psc_cache_update";
149
static double latency_ms;
151
PSC_GET_TIME_BEFORE_LOOKUP;
152
dict_cache_update(cache, key, value);
153
PSC_CHECK_TIME_AFTER_LOOKUP(dict_cache_name(cache), "update", latency_ms);
156
/* psc_dict_get - time-critical table lookup */
158
const char *psc_dict_get(DICT *dict, const char *key)
160
const char *myname = "psc_dict_get";
162
static double latency_ms;
164
PSC_GET_TIME_BEFORE_LOOKUP;
165
result = dict_get(dict, key);
166
PSC_CHECK_TIME_AFTER_LOOKUP(dict->name, "lookup", latency_ms);
170
/* psc_maps_find - time-critical table lookup */
172
const char *psc_maps_find(MAPS *maps, const char *key, int flags)
174
const char *myname = "psc_maps_find";
176
static double latency_ms;
178
PSC_GET_TIME_BEFORE_LOOKUP;
179
result = maps_find(maps, key, flags);
180
PSC_CHECK_TIME_AFTER_LOOKUP(maps->title, "lookup", latency_ms);