1 /*
2 Copyright 2021 Northern.tech AS
3
4 This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 3.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18
19 To the extent this program is licensed as part of the Enterprise
20 versions of CFEngine, the applicable Commercial Open Source License
21 (COSL) may apply to this file if you as a licensee so wish it. See
22 included file COSL.txt.
23 */
24
25 #include <cf3.defs.h>
26
27
28 /*
29 * Cumulative statistics support and conversion to instant values
30 */
31
32 typedef struct PrevValue_ PrevValue;
33
34 struct PrevValue_
35 {
36 char *name;
37 char *subname;
38 union
39 {
40 unsigned u32;
41 unsigned long long u64;
42 } value;
43 time_t timestamp;
44 PrevValue *next;
45 };
46
47 /* Globals */
48
49 static PrevValue *values;
50
51 /* Implementation */
52
AppendNewValue(const char * name,const char * subname,time_t timestamp)53 static PrevValue *AppendNewValue(const char *name, const char *subname, time_t timestamp)
54 {
55 PrevValue *v = xmalloc(sizeof(PrevValue));
56
57 v->name = xstrdup(name);
58 v->subname = xstrdup(subname);
59 v->timestamp = timestamp;
60 v->next = values;
61 values = v;
62 return v;
63 }
64
GetInstantUint32Value(const char * name,const char * subname,unsigned value,time_t timestamp)65 unsigned GetInstantUint32Value(const char *name, const char *subname, unsigned value, time_t timestamp)
66 {
67 PrevValue *v;
68
69 for (v = values; v; v = v->next)
70 {
71 if (!strcmp(v->name, name) && !strcmp(v->subname, subname))
72 {
73 unsigned diff;
74 unsigned difft;
75
76 /* Check for wraparound */
77 if (value < v->value.u32)
78 {
79 diff = INT_MAX - v->value.u32 + value;
80 }
81 else
82 {
83 diff = value - v->value.u32;
84 }
85 difft = timestamp - v->timestamp;
86
87 v->value.u32 = value;
88 v->timestamp = timestamp;
89
90 if (difft != 0)
91 {
92 return diff / difft;
93 }
94 else
95 {
96 return (unsigned) -1;
97 }
98 }
99 }
100
101 AppendNewValue(name, subname, timestamp)->value.u32 = value;
102 return (unsigned) -1;
103 }
104
GetInstantUint64Value(const char * name,const char * subname,unsigned long long value,time_t timestamp)105 unsigned long long GetInstantUint64Value(const char *name, const char *subname, unsigned long long value,
106 time_t timestamp)
107 {
108 PrevValue *v;
109
110 for (v = values; v; v = v->next)
111 {
112 if (!strcmp(v->name, name) && !strcmp(v->subname, subname))
113 {
114 unsigned long long diff = value - v->value.u64;
115 unsigned difft = timestamp - v->timestamp;
116
117 v->value.u64 = value;
118 v->timestamp = timestamp;
119
120 if (difft != 0)
121 {
122 return diff / difft;
123 }
124 else
125 {
126 return (unsigned long long) -1;
127 }
128 }
129 }
130
131 AppendNewValue(name, subname, timestamp)->value.u64 = value;
132 return (unsigned long long) -1;
133 }
134