~ubuntu-branches/ubuntu/utopic/mtbl/utopic-proposed

« back to all changes in this revision

Viewing changes to libmy/rate.c

  • Committer: Package Import Robot
  • Author(s): Robert S. Edmonds
  • Date: 2014-01-21 16:30:22 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20140121163022-g1077ma2csn1gne8
Tags: 0.4-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2008, 2009, 2012, 2013 by Farsight Security, Inc.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *    http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
#include <stdint.h>
 
18
#include <math.h>
 
19
 
 
20
#include "my_alloc.h"
 
21
#include "my_time.h"
 
22
 
 
23
#include "rate.h"
 
24
 
 
25
struct rate {
 
26
        struct timespec next_tick, period, start;
 
27
        uint64_t        count;
 
28
        unsigned        adj_rate, rate, freq;
 
29
};
 
30
 
 
31
static inline int64_t
 
32
ts_nanos(struct timespec *ts)
 
33
{
 
34
        return (ts->tv_sec * 1000000000 + ts->tv_nsec);
 
35
}
 
36
 
 
37
static inline struct timespec
 
38
calc_next_tick(const struct timespec *t, const struct timespec *m)
 
39
{
 
40
        struct timespec res;
 
41
 
 
42
        res = *t;
 
43
        if (m->tv_sec > 0) {
 
44
                res.tv_sec -= (res.tv_sec % m->tv_sec);
 
45
                res.tv_sec += m->tv_sec;
 
46
        }
 
47
        if (m->tv_nsec > 0) {
 
48
                res.tv_nsec -= (res.tv_nsec % m->tv_nsec);
 
49
                res.tv_nsec += m->tv_nsec;
 
50
        } else {
 
51
                res.tv_nsec = 0;
 
52
        }
 
53
 
 
54
        while (res.tv_nsec >= 1000000000) {
 
55
                res.tv_sec += 1;
 
56
                res.tv_nsec -= 1000000000;
 
57
        }
 
58
 
 
59
        return (res);
 
60
}
 
61
 
 
62
static inline void
 
63
adjust_rate(struct rate *r, struct timespec *now)
 
64
{
 
65
        struct timespec elapsed;
 
66
        double ratio;
 
67
        unsigned actual_rate;
 
68
 
 
69
        /* amount of time elapsed since first sleep */
 
70
        elapsed = *now;
 
71
        my_timespec_sub(&r->start, &elapsed);
 
72
 
 
73
        /* the average event rate that has been maintained over the
 
74
         * lifespan of this rate-limiter.
 
75
         */
 
76
        actual_rate = r->count / (ts_nanos(&elapsed) / (1000000000 + 0.0));
 
77
 
 
78
        /* simple ratio of nominal event rate and average event rate */
 
79
        ratio = r->rate / (actual_rate + 0.0);
 
80
 
 
81
        /* clamp this ratio to a small interval */
 
82
        if (ratio < 0.99)
 
83
                ratio = 0.99;
 
84
        if (ratio > 1.01)
 
85
                ratio = 1.01;
 
86
 
 
87
        /* calculate a new, adjusted rate based on this ratio */
 
88
        r->adj_rate *= ratio;
 
89
 
 
90
        /* calculate a new tick period based on the adjusted rate */
 
91
        const double period = 1.0 / (r->adj_rate + 0.0);
 
92
        my_timespec_from_double(period, &r->period);
 
93
}
 
94
 
 
95
struct rate *
 
96
rate_init(unsigned rate, unsigned freq)
 
97
{
 
98
        struct rate *r;
 
99
 
 
100
        r = calloc(1, sizeof(*r));
 
101
        if (r == NULL)
 
102
                return (NULL);
 
103
        r->adj_rate = rate;
 
104
        r->rate = rate;
 
105
        r->freq = freq;
 
106
 
 
107
        /* calculate the tick period */
 
108
        const double period = 1.0 / (r->rate + 0.0);
 
109
        my_timespec_from_double(period, &r->period);
 
110
 
 
111
        return (r);
 
112
}
 
113
 
 
114
void
 
115
rate_destroy(struct rate **r)
 
116
{
 
117
        if (*r != NULL) {
 
118
                free(*r);
 
119
                *r = NULL;
 
120
        }
 
121
}
 
122
 
 
123
void
 
124
rate_sleep(struct rate *r)
 
125
{
 
126
        struct timespec now, til;
 
127
 
 
128
        /* what clock to use depends on whether clock_nanosleep() is available */
 
129
#if HAVE_CLOCK_NANOSLEEP
 
130
        static const clockid_t rate_clock = CLOCK_MONOTONIC;
 
131
#else
 
132
        static const clockid_t rate_clock = CLOCK_REALTIME;
 
133
#endif
 
134
 
 
135
        if (r == NULL)
 
136
                return;
 
137
 
 
138
        /* update the event counter */
 
139
        r->count += 1;
 
140
 
 
141
        /* fetch the current time */
 
142
        clock_gettime(rate_clock, &now);
 
143
 
 
144
        /* special case: if this is the first call to rate_sleep(),
 
145
         * calculate when the next tick will be. this is a little bit more
 
146
         * accurate than calculating it in rate_init().
 
147
         */
 
148
        if (r->count == 1) {
 
149
                r->start = now;
 
150
                r->next_tick = calc_next_tick(&now, &r->period);
 
151
        }
 
152
 
 
153
        /* adjust the rate and period every 'freq' events.
 
154
         * skip the first window of 'freq' events.
 
155
         * disabled if 'freq' is 0.
 
156
         */
 
157
        if (r->freq != 0 && (r->count % r->freq) == 0 && r->count > r->freq)
 
158
                adjust_rate(r, &now);
 
159
 
 
160
        /* 'til', amount of time remaining until the next tick */
 
161
        til = r->next_tick;
 
162
        my_timespec_sub(&now, &til);
 
163
 
 
164
        /* if 'til' is in the past, don't bother sleeping */
 
165
        if (ts_nanos(&til) > 0) {
 
166
                /* do the sleep */
 
167
#if HAVE_CLOCK_NANOSLEEP
 
168
                clock_nanosleep(rate_clock, TIMER_ABSTIME, &r->next_tick, NULL);
 
169
#else
 
170
                struct timespec rel;
 
171
                rel = r->next_tick;
 
172
                my_timespec_sub(&now, &rel);
 
173
                my_nanosleep(&rel);
 
174
#endif
 
175
 
 
176
                /* re-fetch the current time */
 
177
                clock_gettime(rate_clock, &now);
 
178
        }
 
179
 
 
180
        /* calculate the next tick */
 
181
        r->next_tick = calc_next_tick(&now, &r->period);
 
182
}