Discussion:
[Linuxptp-devel] [PATCH RFC 1/2] util: add function for rate limiting.
Miroslav Lichvar
2015-09-07 16:25:25 UTC
Permalink
---
util.c | 14 ++++++++++++++
util.h | 18 ++++++++++++++++++
2 files changed, 32 insertions(+)

diff --git a/util.c b/util.c
index 6bbbe91..e10cedc 100644
--- a/util.c
+++ b/util.c
@@ -512,3 +512,17 @@ void parray_extend(void ***a, ...)
va_end(ap);
(*a)[len - 1] = NULL;
}
+
+int rate_limited(int interval, time_t *last)
+{
+ struct timespec ts;
+
+ if (clock_gettime(CLOCK_MONOTONIC, &ts))
+ return 1;
+ if (*last + interval > ts.tv_sec)
+ return 1;
+
+ *last = ts.tv_sec;
+
+ return 0;
+}
diff --git a/util.h b/util.h
index cc521b6..8ce2470 100644
--- a/util.h
+++ b/util.h
@@ -354,4 +354,22 @@ void parray_append(void ***a, void *p);
*/
void parray_extend(void ***a, ...);

+/**
+ * Check if enough time has passed to implement a simple rate limiting.
+ *
+ * @param interval Minimum interval between two calls returning 0 (in seconds).
+ * @param last Time of the last call that returned 0, input/output.
+ * @return 1 when rate limited, 0 otherwise.
+ */
+int rate_limited(int interval, time_t *last);
+
+/* Convenient wrapper to limit the rate of some code */
+#define LIMIT_RATE(i, x...) \
+ do { \
+ static time_t last = -i; \
+ if (!rate_limited(i, &last)) { \
+ x; \
+ } \
+ } while (0);
+
#endif
--
2.1.0


------------------------------------------------------------------------------
Miroslav Lichvar
2015-09-07 16:25:26 UTC
Permalink
Upgrade the message level to info so the user can see it, but print it
at most once per 5 minutes to not spam the syslog.
---
port.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/port.c b/port.c
index 9c804cf..527cd6b 100644
--- a/port.c
+++ b/port.c
@@ -1688,8 +1688,8 @@ static void process_delay_resp(struct port *p, struct ptp_message *m)
}
if (rsp->hdr.logMessageInterval < -10 ||
rsp->hdr.logMessageInterval > 22) {
- pr_debug("port %hu: ignore bogus delay request interval 2^%d",
- portnum(p), rsp->hdr.logMessageInterval);
+ LIMIT_RATE(300, pr_info("port %hu: ignore bogus delay request interval 2^%d",
+ portnum(p), rsp->hdr.logMessageInterval));
return;
}
p->logMinDelayReqInterval = rsp->hdr.logMessageInterval;
--
2.1.0


------------------------------------------------------------------------------
Richard Cochran
2015-09-10 08:55:10 UTC
Permalink
Post by Miroslav Lichvar
Upgrade the message level to info so the user can see it, but print it
at most once per 5 minutes to not spam the syslog.
---
port.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/port.c b/port.c
index 9c804cf..527cd6b 100644
--- a/port.c
+++ b/port.c
@@ -1688,8 +1688,8 @@ static void process_delay_resp(struct port *p, struct ptp_message *m)
}
if (rsp->hdr.logMessageInterval < -10 ||
rsp->hdr.logMessageInterval > 22) {
- pr_debug("port %hu: ignore bogus delay request interval 2^%d",
- portnum(p), rsp->hdr.logMessageInterval);
+ LIMIT_RATE(300, pr_info("port %hu: ignore bogus delay request interval 2^%d",
+ portnum(p), rsp->hdr.logMessageInterval));
I like this feature, but it would be so much nicer to have:

pl_debug(300, "port %hu: ignore bogus delay request interval 2^%d",
portnum(p), rsp->hdr.logMessageInterval);

I mean having one new pl_ macro for each pr_ macro.

What do you think?

Thanks,
Richard
Miroslav Lichvar
2015-09-10 09:23:10 UTC
Permalink
Post by Richard Cochran
pl_debug(300, "port %hu: ignore bogus delay request interval 2^%d",
portnum(p), rsp->hdr.logMessageInterval);
I mean having one new pl_ macro for each pr_ macro.
What do you think?
I think that's a good idea. I'll send a new version.
--
Miroslav Lichvar
Loading...