1 /*------------------------------------------------------------------------------
2  *
3  * Copyright (c) 2011-2021, EURid vzw. All rights reserved.
4  * The YADIFA TM software product is provided under the BSD 3-clause license:
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  *        * Redistributions of source code must retain the above copyright
11  *          notice, this list of conditions and the following disclaimer.
12  *        * Redistributions in binary form must reproduce the above copyright
13  *          notice, this list of conditions and the following disclaimer in the
14  *          documentation and/or other materials provided with the distribution.
15  *        * Neither the name of EURid nor the names of its contributors may be
16  *          used to endorse or promote products derived from this software
17  *          without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  *------------------------------------------------------------------------------
32  *
33  */
34 
35 #include "dnscore/dnscore-config.h"
36 #include <unistd.h>
37 
38 #include "dnscore/logger.h"
39 #include "dnscore/pace.h"
40 
41 extern logger_handle *g_system_logger;
42 #define MODULE_MSG_HANDLE g_system_logger
43 
44 #define PACE_DUMP 0
45 
46 #define PACE_MODE_SMOOTH 0
47 #define PACE_MODE_HARD   1
48 
49 #define PACE_MODE PACE_MODE_HARD
50 
51 void
pace_init(pace_s * pace,u64 min_us,u64 max_us,const char * name)52 pace_init(pace_s *pace, u64 min_us, u64 max_us, const char *name)
53 {
54     if(min_us > max_us)
55     {
56         u64 tmp = max_us;
57         max_us = min_us;
58         min_us = tmp;
59     }
60 
61     pace->min_us = min_us;
62     pace->max_us = max_us;
63 #if PACE_MODE == PACE_MODE_SMOOTH
64     pace->current_us = (max_us + min_us) >> 1;
65 #else
66     pace->current_us = min_us;
67 #endif
68     pace->counter = 0;
69 
70     pace->name = name;
71 }
72 
73 /**
74  * Will pause for a while
75  */
76 
77 void
pace_wait(pace_s * pace)78 pace_wait(pace_s *pace)
79 {
80     u64 start = timeus();
81 
82     if((pace->counter > 0) && ((pace->counter & 3) != 0))
83     {
84 #if defined(PACE_DUMP) && (PACE_DUMP > 0)
85         u64 current = pace->current_us;
86 #endif
87         pace->current_us <<= 1;
88 
89         if(pace->current_us == 0)
90         {
91             pace->current_us = 1;
92         }
93 
94         if(pace->current_us > pace->max_us)
95         {
96             pace->current_us = pace->max_us;
97         }
98 
99 #if defined(PACE_DUMP) && (PACE_DUMP > 0)
100         if(current != pace->current_us)
101         {
102             log_debug("pace: '%s' waiting for %lluµs (#%llu)", pace->name, pace->current_us, pace->counter);
103         }
104 #endif
105     }
106     else
107     {
108         pace->wait_start = start;
109 
110 #if defined(PACE_DUMP) && (PACE_DUMP > 0)
111         log_debug("pace: '%s' waiting for %lluµs (#%llu)", pace->name, pace->current_us, pace->counter);
112 #endif
113     }
114 
115     pace->counter++;
116 
117     u64 elapsed = 0;
118     u64 current = pace->current_us;
119     do
120     {
121          if(elapsed > current)
122          {
123              log_err("pace_wait: impossible! elapsed = %llu > %llu", elapsed, current);
124              break;
125          }
126 
127         usleep(current - elapsed);
128         u64 now = timeus();
129 
130         if(now < start)
131         {
132             log_err("pace_wait: now=%llu < start=%llu (%llu)", now, start, start-now);
133             break;
134         }
135         elapsed = now - start;
136     }
137     while(elapsed < current);
138 }
139 
140 /**
141  * Will update the pace taking the fact that now we have work to do
142  */
143 
144 void
pace_work(pace_s * pace)145 pace_work(pace_s *pace)
146 {
147     pace->wait_end = timeus();
148 
149 #if defined(PACE_DUMP) && (PACE_DUMP > 0)
150     log_debug("pace: '%s' working after %lluµs (%lluµs #%llu)",
151             pace->name, pace->wait_end - pace->wait_start, pace->current_us, pace->counter);
152 #endif
153 
154     pace->counter = 0;
155 #if PACE_MODE == PACE_MODE_SMOOTH
156     pace->current_us >>= 1;
157     if(pace->current_us < pace->min_us)
158     {
159         pace->current_us = pace->min_us;
160     }
161 #else
162     pace->current_us = pace->min_us;
163 #endif
164 }
165