The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
stats.c
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 
17 /** Map internal data structures to statistics
18  *
19  * @file src/lib/util/stats.c
20  *
21  * @copyright 2022 Network RADIUS SAS (legal@networkradius.com)
22  */
23 RCSID("$Id: ac83ec7d7fb561ab3fde64fefc12698385c2801f $")
24 
25 #include <freeradius-devel/util/stats.h>
26 
27 /** Define dictionary attributes for a given statistics structure.
28  *
29  * @param parent the parent attribute under which statistics are defined
30  * @param stats the statistics mapping structure.
31  * @return
32  * - <0 on error
33  * - 0 on success
34  */
36 {
37  fr_stats_entry_t const *entry;
38  fr_dict_t *dict;
39 
41 
42  for (entry = &stats->table[0]; entry->name != NULL; entry++) {
43  fr_dict_attr_flags_t flags = {
44  .internal = true,
45  .counter = entry->flags.counter,
46  };
47 
48  fr_assert(entry->number > 0);
49  fr_assert((entry->type == FR_TYPE_TIME_DELTA) || (fr_type_is_integer(entry->type)));
50 
51  if (fr_dict_attr_add(dict, parent, entry->name, entry->number, entry->type, &flags) < 0) return -1;
52  }
53 
54  return 0;
55 }
56 
57 /** Add statistics VPs for a particular struct / context
58  *
59  * @param parent structural vp where the children will be added
60  * @param stats structure which maps between #fr_dict_attr_t and internal stats structures
61  * @param ctx the internal structure holding the stastics
62  * @return
63  * - <0 on error
64  * - 0 on success
65  */
67 {
68  fr_stats_entry_t const *entry;
69 
71 
72  for (entry = &stats->table[0]; entry->name != NULL; entry++) {
73  fr_pair_t *vp;
74  fr_dict_attr_t const *da;
75 
76  da = fr_dict_attr_child_by_num(parent->da, entry->number);
77  if (!da) {
78  fr_strerror_printf("Unknown child %d for parent %s", entry->number, parent->da->name);
79  return -1;
80  }
81 
82  vp = fr_pair_afrom_da(parent, da);
83  if (!vp) return -1;
84 
85  if (fr_value_box_memcpy_in(&vp->data, ((uint8_t const *) ctx) + entry->offset) < 0) return -1;
86 
87  fr_pair_append(&parent->vp_group, vp);
88  }
89 
90  return 0;
91 }
static fr_dict_t * dict
Definition: fuzzer.c:46
#define RCSID(id)
Definition: build.h:481
unsigned int internal
Internal attribute, should not be received in protocol packets, should not be encoded.
Definition: dict.h:86
fr_dict_t const * fr_dict_by_da(fr_dict_attr_t const *da)
Attempt to locate the protocol dictionary containing an attribute.
Definition: dict_util.c:2606
fr_dict_t * fr_dict_unconst(fr_dict_t const *dict)
Coerce to non-const.
Definition: dict_util.c:4585
int fr_dict_attr_add(fr_dict_t *dict, fr_dict_attr_t const *parent, char const *name, unsigned int attr, fr_type_t type, fr_dict_attr_flags_t const *flags))
Add an attribute to the dictionary.
Definition: dict_util.c:1712
fr_dict_attr_t const * fr_dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr)
Check if a child attribute exists in a parent using an attribute number.
Definition: dict_util.c:3328
Values of the encryption flags.
Definition: merged_model.c:139
@ FR_TYPE_TIME_DELTA
A period of time measured in nanoseconds.
Definition: merged_model.c:113
unsigned char uint8_t
Definition: merged_model.c:30
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
Definition: pair.c:283
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition: pair.c:1345
static rc_stats_t stats
Definition: radclient-ng.c:74
fr_assert(0)
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition: pair.h:68
static fr_slen_t parent
Definition: pair.h:851
int fr_stats_pair_add(fr_pair_t *parent, fr_stats_struct_t const *stats, void const *ctx)
Add statistics VPs for a particular struct / context.
Definition: stats.c:66
int fr_stats_attr_init(fr_dict_attr_t *parent, fr_stats_struct_t const *stats)
Define dictionary attributes for a given statistics structure.
Definition: stats.c:35
fr_type_t type
data type for this statistics
Definition: stats.h:40
char const * name
Attribute name.
Definition: stats.h:39
int number
attribute number, so that numbers are consistent
Definition: stats.h:41
struct fr_stats_entry_t::@151 flags
size_t offset
from start of the structure
Definition: stats.h:47
Define a statistics mapping between dictionary attribute and a field in an internal structure.
Definition: stats.h:38
Define a statistics mapping between a public name and an entire internal structure.
Definition: stats.h:55
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition: strerror.h:64
#define fr_type_is_structural(_x)
Definition: types.h:371
#define fr_type_is_integer(_x)
Definition: types.h:360
static int fr_value_box_memcpy_in(fr_value_box_t *vb, void const *in)
Copy a C value value to a value box.
Definition: value.h:798