[RFC/RFT 01/42] backports: average: backport efficiency improvements

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Johannes Berg <johannes.berg@xxxxxxxxx>

This is what we get for not copying it directly :-)
For now, backport af556884359 manually.

Signed-off-by: Johannes Berg <johannes.berg@xxxxxxxxx>
---
 backport/compat/average.c        | 20 ++++++++++++--------
 backport/include/linux/average.h |  2 +-
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/backport/compat/average.c b/backport/compat/average.c
index 37cbfd6..b7a51a1 100644
--- a/backport/compat/average.c
+++ b/backport/compat/average.c
@@ -11,23 +11,27 @@
 #include <linux/average.h>
 #include <linux/module.h>
 #include <linux/bug.h>
+#include <linux/log2.h>
 
 /**
  * ewma_init() - Initialize EWMA parameters
  * @avg: Average structure
  * @factor: Factor to use for the scaled up internal value. The maximum value
- *	of averages can be ULONG_MAX/(factor*weight).
+ *	of averages can be ULONG_MAX/(factor*weight). For performance reasons
+ *	factor has to be a power of 2.
  * @weight: Exponential weight, or decay rate. This defines how fast the
- *	influence of older values decreases. Has to be bigger than 1.
+ *	influence of older values decreases. For performance reasons weight has
+ *	to be a power of 2.
  *
  * Initialize the EWMA parameters for a given struct ewma @avg.
  */
 void ewma_init(struct ewma *avg, unsigned long factor, unsigned long weight)
 {
-	WARN_ON(weight <= 1 || factor == 0);
+	WARN_ON(!is_power_of_2(weight) || !is_power_of_2(factor));
+
+	avg->weight = ilog2(weight);
+	avg->factor = ilog2(factor);
 	avg->internal = 0;
-	avg->weight = weight;
-	avg->factor = factor;
 }
 EXPORT_SYMBOL_GPL(ewma_init);
 
@@ -41,9 +45,9 @@ EXPORT_SYMBOL_GPL(ewma_init);
 struct ewma *ewma_add(struct ewma *avg, unsigned long val)
 {
 	avg->internal = avg->internal  ?
-		(((avg->internal * (avg->weight - 1)) +
-			(val * avg->factor)) / avg->weight) :
-		(val * avg->factor);
+		(((avg->internal << avg->weight) - avg->internal) +
+			(val << avg->factor)) >> avg->weight :
+		(val << avg->factor);
 	return avg;
 }
 EXPORT_SYMBOL_GPL(ewma_add);
diff --git a/backport/include/linux/average.h b/backport/include/linux/average.h
index 16eff0d..2d6210e 100644
--- a/backport/include/linux/average.h
+++ b/backport/include/linux/average.h
@@ -29,6 +29,6 @@ extern struct ewma *ewma_add(struct ewma *avg, unsigned long val);
  */
 static inline unsigned long ewma_read(const struct ewma *avg)
 {
-	return DIV_ROUND_CLOSEST(avg->internal, avg->factor);
+	return avg->internal >> avg->factor;
 }
 #endif /* (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,37)) */
-- 
1.8.0

--
To unsubscribe from this list: send the line "unsubscribe backports" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux