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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
/*
* Copyright (c) 2009, Rambler media
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY Rambler media ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Rambler BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Winnow classifier
*/
#include "classifiers.h"
#include "../main.h"
#include "../filter.h"
#include "../cfg_file.h"
#ifdef WITH_LUA
#include "../lua/lua_common.h"
#endif
#define WINNOW_PROMOTION 1.23
#define WINNOW_DEMOTION 0.83
struct winnow_callback_data {
statfile_pool_t *pool;
struct classifier_ctx *ctx;
stat_file_t *file;
double sum;
int count;
int in_class;
time_t now;
};
static gboolean
classify_callback (gpointer key, gpointer value, gpointer data)
{
token_node_t *node = key;
struct winnow_callback_data *cd = data;
double v;
/* Consider that not found blocks have value 1 */
v = statfile_pool_get_block (cd->pool, cd->file, node->h1, node->h2, cd->now);
if (fabs (v) > 0.00001) {
if (cd->sum + v > G_MAXDOUBLE / 2.) {
cd->sum = G_MAXDOUBLE / 2.;
}
else {
cd->sum += v;
}
cd->in_class++;
}
cd->count++;
return FALSE;
}
static gboolean
learn_callback (gpointer key, gpointer value, gpointer data)
{
token_node_t *node = key;
struct winnow_callback_data *cd = data;
double v, c;
c = (cd->in_class) ? WINNOW_PROMOTION : WINNOW_DEMOTION;
/* Consider that not found blocks have value 1 */
v = statfile_pool_get_block (cd->pool, cd->file, node->h1, node->h2, cd->now);
if (fabs (v) < 0.00001) {
statfile_pool_set_block (cd->pool, cd->file, node->h1, node->h2, cd->now, c);
node->value = c;
}
else {
statfile_pool_set_block (cd->pool, cd->file, node->h1, node->h2, cd->now, v * c);
/* Set some limit on growing */
if (v > G_MAXDOUBLE / 2.) {
node->value = v;
}
else {
node->value = v * c;
}
}
if (cd->sum + node->value > G_MAXDOUBLE / 2.) {
cd->sum = G_MAXDOUBLE / 2.;
}
else {
cd->sum += node->value;
}
cd->count++;
return FALSE;
}
struct classifier_ctx *
winnow_init (memory_pool_t * pool, struct classifier_config *cfg)
{
struct classifier_ctx *ctx = memory_pool_alloc (pool, sizeof (struct classifier_ctx));
ctx->pool = pool;
ctx->cfg = cfg;
return ctx;
}
void
winnow_classify (struct classifier_ctx *ctx, statfile_pool_t * pool, GTree * input, struct worker_task *task)
{
struct winnow_callback_data data;
char *sumbuf;
double res = 0., max = 0.;
GList *cur;
struct statfile *st, *sel = NULL;
g_assert (pool != NULL);
g_assert (ctx != NULL);
data.pool = pool;
data.sum = 0;
data.count = 0;
data.now = time (NULL);
data.ctx = ctx;
if (ctx->cfg->pre_callbacks) {
#ifdef WITH_LUA
cur = call_classifier_pre_callbacks (ctx->cfg, task);
if (cur) {
memory_pool_add_destructor (task->task_pool, (pool_destruct_func)g_list_free, cur);
}
#else
cur = ctx->cfg->statfiles;
#endif
}
else {
cur = ctx->cfg->statfiles;
}
while (cur) {
st = cur->data;
if ((data.file = statfile_pool_is_open (pool, st->path)) == NULL) {
if ((data.file = statfile_pool_open (pool, st->path, st->size, FALSE)) == NULL) {
msg_warn ("cannot open %s, skip it", st->path);
cur = g_list_next (cur);
continue;
}
}
if (data.file != NULL) {
statfile_pool_lock_file (pool, data.file);
g_tree_foreach (input, classify_callback, &data);
statfile_pool_unlock_file (pool, data.file);
}
if (data.count != 0) {
res = data.sum / data.count;
}
else {
res = 0;
}
if (res > max) {
max = res;
sel = st;
}
cur = g_list_next (cur);
}
if (sel != NULL) {
sumbuf = memory_pool_alloc (task->task_pool, 32);
snprintf (sumbuf, 32, "%.2f", max);
cur = g_list_prepend (NULL, sumbuf);
#ifdef WITH_LUA
max = call_classifier_post_callbacks (ctx->cfg, task, max);
#endif
insert_result (task, ctx->cfg->metric, sel->symbol, max, cur);
}
}
void
winnow_learn (struct classifier_ctx *ctx, statfile_pool_t *pool, stat_file_t *file, GTree * input, int in_class, double *sum)
{
struct winnow_callback_data data = {
.file = NULL,
.sum = 0,
.count = 0,
};
g_assert (pool != NULL);
g_assert (ctx != NULL);
data.pool = pool;
data.in_class = in_class;
data.now = time (NULL);
data.ctx = ctx;
data.file = file;
if (data.file != NULL) {
statfile_pool_lock_file (pool, data.file);
g_tree_foreach (input, learn_callback, &data);
statfile_pool_unlock_file (pool, data.file);
}
if (sum) {
if (data.count != 0) {
*sum = data.sum / data.count;
}
else {
*sum = 0;
}
}
}
|