3
Record utils definitions
5
@section license License
7
Licensed to the Apache Software Foundation (ASF) under one
8
or more contributor license agreements. See the NOTICE file
9
distributed with this work for additional information
10
regarding copyright ownership. The ASF licenses this file
11
to you under the Apache License, Version 2.0 (the
12
"License"); you may not use this file except in compliance
13
with the License. You may obtain a copy of the License at
15
http://www.apache.org/licenses/LICENSE-2.0
17
Unless required by applicable law or agreed to in writing, software
18
distributed under the License is distributed on an "AS IS" BASIS,
19
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
See the License for the specific language governing permissions and
21
limitations under the License.
25
#include "P_RecUtils.h"
26
#include "P_RecCore.h"
27
#include "P_RecTree.h"
29
// diags defined in RecCore.cc
30
extern Diags *g_diags;
32
//-------------------------------------------------------------------------
34
//-------------------------------------------------------------------------
36
RecAlloc(RecT rec_type, const char *name, RecDataT data_type)
38
if (g_num_records >= REC_MAX_RECORDS) {
39
Warning("too many stats/configs, please increase REC_MAX_RECORDS or rebuild with --with_max_api_stats=<n>");
43
int i = ink_atomic_increment(&g_num_records, 1);
44
RecRecord *r = &(g_records[i]);
45
// Note: record should already be memset to 0 from RecCoreInit()
46
r->rec_type = rec_type;
47
r->name = xstrdup(name);
49
r->data_type = data_type;
50
rec_mutex_init(&(r->lock), NULL);
52
g_records_tree->rec_tree_insert(r->name);
58
//-------------------------------------------------------------------------
60
//-------------------------------------------------------------------------
62
RecDataClear(RecDataT data_type, RecData * data)
64
if ((data_type == RECD_STRING) && (data->rec_string)) {
65
xfree(data->rec_string);
67
memset(data, 0, sizeof(RecData));
71
//-------------------------------------------------------------------------
73
//-------------------------------------------------------------------------
75
RecDataSet(RecDataT data_type, RecData * data_dst, RecData * data_src)
81
if (data_src->rec_string == NULL) {
82
if (data_dst->rec_string != NULL) {
83
xfree(data_dst->rec_string);
84
data_dst->rec_string = NULL;
87
} else if (((data_dst->rec_string) && (strcmp(data_dst->rec_string, data_src->rec_string) != 0)) ||
88
((data_dst->rec_string == NULL) && (data_src->rec_string != NULL))) {
89
if (data_dst->rec_string) {
90
xfree(data_dst->rec_string);
92
data_dst->rec_string = xstrdup(data_src->rec_string);
97
if (data_dst->rec_int != data_src->rec_int) {
98
data_dst->rec_int = data_src->rec_int;
103
if (data_dst->rec_float != data_src->rec_float) {
104
data_dst->rec_float = data_src->rec_float;
109
if (data_dst->rec_counter != data_src->rec_counter) {
110
data_dst->rec_counter = data_src->rec_counter;
115
ink_assert(!"Wrong RECD type!");
122
//-------------------------------------------------------------------------
123
// RecDataSetFromInk64
124
//-------------------------------------------------------------------------
126
RecDataSetFromInk64(RecDataT data_type, RecData * data_dst, int64_t data_int64)
130
data_dst->rec_int = data_int64;
133
data_dst->rec_float = (float) (data_int64);
138
if (data_dst->rec_string) {
139
xfree(data_dst->rec_string);
141
snprintf(buf, 32, "%" PRId64 "", data_int64);
142
data_dst->rec_string = xstrdup(buf);
146
data_dst->rec_counter = data_int64;
149
ink_debug_assert(!"Unexpected RecD type");
157
//-------------------------------------------------------------------------
158
// RecDataSetFromFloat
159
//-------------------------------------------------------------------------
161
RecDataSetFromFloat(RecDataT data_type, RecData * data_dst, float data_float)
165
data_dst->rec_int = (RecInt) data_float;
168
data_dst->rec_float = (float) (data_float);
173
if (data_dst->rec_string) {
174
xfree(data_dst->rec_string);
176
snprintf(buf, 32, "%f", data_float);
177
data_dst->rec_string = xstrdup(buf);
181
data_dst->rec_counter = (RecCounter) data_float;
184
ink_debug_assert(!"Unexpected RecD type");
192
//-------------------------------------------------------------------------
193
// RecDataSetFromString
194
//-------------------------------------------------------------------------
196
RecDataSetFromString(RecDataT data_type, RecData * data_dst, char *data_string)
203
data_src.rec_int = ink_atoi64(data_string);
206
data_src.rec_float = atof(data_string);
209
if (strcmp((data_string), "NULL") == 0)
210
data_src.rec_string = NULL;
212
data_src.rec_string = data_string;
215
data_src.rec_counter = ink_atoi64(data_string);
218
ink_debug_assert(!"Unexpected RecD type");
221
rec_set = RecDataSet(data_type, data_dst, &data_src);
227
//-------------------------------------------------------------------------
229
//-------------------------------------------------------------------------
231
RecLog(DiagsLevel dl, const char *format_string, ...)
235
va_start(ap, format_string);
237
g_diags->log_va(NULL, dl, NULL, NULL, format_string, ap);
243
//-------------------------------------------------------------------------
245
//-------------------------------------------------------------------------
247
RecDebug(DiagsLevel dl, const char *format_string, ...)
251
va_start(ap, format_string);
253
g_diags->log_va("rec", dl, NULL, NULL, format_string, ap);
259
//-------------------------------------------------------------------------
261
//-------------------------------------------------------------------------