forked from kaltura/nginx-vod-module
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ngx_http_vod_status.c
269 lines (231 loc) · 7.36 KB
/
ngx_http_vod_status.c
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// includes
#include "ngx_http_vod_status.h"
#include "ngx_http_vod_module.h"
#include "ngx_http_vod_utils.h"
#include "ngx_http_vod_conf.h"
#include "ngx_perf_counters.h"
#include "ngx_buffer_cache.h"
// macros
#define DEFINE_STAT(x) { #x, sizeof(#x) - 1, offsetof(ngx_buffer_cache_stats_t, x) }
// constants
#define PATH_PERF_COUNTERS_OPEN "<performance_counters>\r\n"
#define PATH_PERF_COUNTERS_CLOSE "</performance_counters>\r\n"
#define PERF_COUNTER_FORMAT "<sum>%uA</sum>\r\n<count>%uA</count>\r\n<max>%uA</max>\r\n<max_time>%uA</max_time>\r\n<max_pid>%uA</max_pid>\r\n"
// typedefs
typedef struct {
int conf_offset;
ngx_str_t open_tag;
ngx_str_t close_tag;
} ngx_http_vod_cache_info_t;
typedef struct {
const char* name;
int name_len;
int offset;
} ngx_http_vod_stat_def_t;
// constants
static const u_char status_prefix[] =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
"<vod>\r\n"
"<version>" NGINX_VOD_VERSION "</version>\r\n"
"<built>" __DATE__ " " __TIME__ "</built>\r\n";
static const u_char status_postfix[] = "</vod>\r\n";
static ngx_str_t xml_content_type = ngx_string("text/xml");
static ngx_str_t text_content_type = ngx_string("text/plain");
static ngx_str_t reset_response = ngx_string("OK\r\n");
static ngx_http_vod_stat_def_t buffer_cache_stat_defs[] = {
DEFINE_STAT(store_ok),
DEFINE_STAT(store_bytes),
DEFINE_STAT(store_err),
DEFINE_STAT(store_exists),
DEFINE_STAT(fetch_hit),
DEFINE_STAT(fetch_bytes),
DEFINE_STAT(fetch_miss),
DEFINE_STAT(evicted),
DEFINE_STAT(evicted_bytes),
DEFINE_STAT(reset),
DEFINE_STAT(entries),
DEFINE_STAT(data_size),
{ NULL, 0, 0 }
};
static ngx_http_vod_cache_info_t cache_infos[] = {
{
offsetof(ngx_http_vod_loc_conf_t, metadata_cache),
ngx_string("<metadata_cache>\r\n"),
ngx_string("</metadata_cache>\r\n"),
},
{
offsetof(ngx_http_vod_loc_conf_t, response_cache[CACHE_TYPE_VOD]),
ngx_string("<response_cache>\r\n"),
ngx_string("</response_cache>\r\n"),
},
{
offsetof(ngx_http_vod_loc_conf_t, response_cache[CACHE_TYPE_LIVE]),
ngx_string("<live_response_cache>\r\n"),
ngx_string("</live_response_cache>\r\n"),
},
{
offsetof(ngx_http_vod_loc_conf_t, mapping_cache[CACHE_TYPE_VOD]),
ngx_string("<mapping_cache>\r\n"),
ngx_string("</mapping_cache>\r\n"),
},
{
offsetof(ngx_http_vod_loc_conf_t, mapping_cache[CACHE_TYPE_LIVE]),
ngx_string("<live_mapping_cache>\r\n"),
ngx_string("</live_mapping_cache>\r\n"),
},
{
offsetof(ngx_http_vod_loc_conf_t, drm_info_cache),
ngx_string("<drm_info_cache>\r\n"),
ngx_string("</drm_info_cache>\r\n"),
},
};
static u_char*
ngx_http_vod_append_cache_stats(u_char* p, ngx_buffer_cache_stats_t* stats)
{
ngx_http_vod_stat_def_t* cur_stat;
for (cur_stat = buffer_cache_stat_defs; cur_stat->name != NULL; cur_stat++)
{
// opening tag
*p++ = '<';
p = ngx_copy(p, cur_stat->name, cur_stat->name_len);
*p++ = '>';
// value
p = ngx_sprintf(p, "%uA", *(ngx_atomic_t*)((u_char*)stats + cur_stat->offset));
// closing tag
*p++ = '<';
*p++ = '/';
p = ngx_copy(p, cur_stat->name, cur_stat->name_len);
*p++ = '>';
// newline
*p++ = CR;
*p++ = LF;
}
return p;
}
static ngx_int_t
ngx_http_vod_status_reset(ngx_http_request_t *r)
{
ngx_perf_counters_t* perf_counters;
ngx_http_vod_loc_conf_t *conf;
ngx_buffer_cache_t *cur_cache;
unsigned i;
conf = ngx_http_get_module_loc_conf(r, ngx_http_vod_module);
perf_counters = ngx_perf_counter_get_state(conf->perf_counters_zone);
for (i = 0; i < sizeof(cache_infos) / sizeof(cache_infos[0]); i++)
{
cur_cache = *(ngx_buffer_cache_t **)((u_char*)conf + cache_infos[i].conf_offset);
if (cur_cache == NULL)
{
continue;
}
ngx_buffer_cache_reset_stats(cur_cache);
}
if (perf_counters != NULL)
{
for (i = 0; i < PC_COUNT; i++)
{
perf_counters->counters[i].sum = 0;
perf_counters->counters[i].count = 0;
perf_counters->counters[i].max = 0;
perf_counters->counters[i].max_time = 0;
perf_counters->counters[i].max_pid = 0;
}
}
return ngx_http_vod_send_response(r, &reset_response, &text_content_type);
}
ngx_int_t
ngx_http_vod_status_handler(ngx_http_request_t *r)
{
ngx_perf_counters_t* perf_counters;
ngx_buffer_cache_stats_t stats;
ngx_http_vod_loc_conf_t *conf;
ngx_http_vod_stat_def_t* cur_stat;
ngx_buffer_cache_t *cur_cache;
ngx_str_t response;
ngx_str_t reset;
u_char* p;
size_t cache_stats_len = 0;
size_t result_size;
unsigned i;
if (ngx_http_arg(r, (u_char *) "reset", sizeof("reset") - 1, &reset) == NGX_OK &&
reset.len == 1 &&
reset.data[0] == '1')
{
return ngx_http_vod_status_reset(r);
}
conf = ngx_http_get_module_loc_conf(r, ngx_http_vod_module);
perf_counters = ngx_perf_counter_get_state(conf->perf_counters_zone);
// calculate the buffer size
for (cur_stat = buffer_cache_stat_defs; cur_stat->name != NULL; cur_stat++)
{
cache_stats_len += sizeof("<></>\r\n") - 1 + 2 * cur_stat->name_len + NGX_ATOMIC_T_LEN;
}
result_size = sizeof(status_prefix) - 1;
for (i = 0; i < sizeof(cache_infos) / sizeof(cache_infos[0]); i++)
{
cur_cache = *(ngx_buffer_cache_t **)((u_char*)conf + cache_infos[i].conf_offset);
if (cur_cache == NULL)
{
continue;
}
result_size += cache_infos[i].open_tag.len + cache_stats_len + cache_infos[i].close_tag.len;
}
if (perf_counters != NULL)
{
result_size += sizeof(PATH_PERF_COUNTERS_OPEN);
for (i = 0; i < PC_COUNT; i++)
{
result_size += perf_counters_open_tags[i].len + sizeof(PERF_COUNTER_FORMAT) + 5 * NGX_ATOMIC_T_LEN + perf_counters_close_tags[i].len;
}
result_size += sizeof(PATH_PERF_COUNTERS_CLOSE);
}
result_size += sizeof(status_postfix);
// allocate the buffer
response.data = ngx_palloc(r->pool, result_size);
if (response.data == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_status_handler: ngx_palloc failed");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
// populate the buffer
p = ngx_copy(response.data, status_prefix, sizeof(status_prefix) - 1);
for (i = 0; i < sizeof(cache_infos) / sizeof(cache_infos[0]); i++)
{
cur_cache = *(ngx_buffer_cache_t **)((u_char*)conf + cache_infos[i].conf_offset);
if (cur_cache == NULL)
{
continue;
}
ngx_buffer_cache_get_stats(cur_cache, &stats);
p = ngx_copy(p, cache_infos[i].open_tag.data, cache_infos[i].open_tag.len);
p = ngx_http_vod_append_cache_stats(p, &stats);
p = ngx_copy(p, cache_infos[i].close_tag.data, cache_infos[i].close_tag.len);
}
if (perf_counters != NULL)
{
p = ngx_copy(p, PATH_PERF_COUNTERS_OPEN, sizeof(PATH_PERF_COUNTERS_OPEN) - 1);
for (i = 0; i < PC_COUNT; i++)
{
p = ngx_copy(p, perf_counters_open_tags[i].data, perf_counters_open_tags[i].len);
p = ngx_sprintf(p, PERF_COUNTER_FORMAT,
perf_counters->counters[i].sum,
perf_counters->counters[i].count,
perf_counters->counters[i].max,
perf_counters->counters[i].max_time,
perf_counters->counters[i].max_pid);
p = ngx_copy(p, perf_counters_close_tags[i].data, perf_counters_close_tags[i].len);
}
p = ngx_copy(p, PATH_PERF_COUNTERS_CLOSE, sizeof(PATH_PERF_COUNTERS_CLOSE) - 1);
}
p = ngx_copy(p, status_postfix, sizeof(status_postfix) - 1);
response.len = p - response.data;
if (response.len > result_size)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_status_handler: response length %uz exceeded allocated length %uz",
response.len, result_size);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
return ngx_http_vod_send_response(r, &response, &xml_content_type);
}