[2/3] vmevent: Factor vmevent_match_attr() out of vmevent_match()

Message ID 1349346078-24874-2-git-send-email-anton.vorontsov@linaro.org
State New
Headers show

Commit Message

Anton Vorontsov Oct. 4, 2012, 10:21 a.m.
Soon we'll use this new function for other code; plus this makes code less
indented.

Signed-off-by: Anton Vorontsov <anton.vorontsov@linaro.org>
---
 mm/vmevent.c | 107 +++++++++++++++++++++++++++++++----------------------------
 1 file changed, 57 insertions(+), 50 deletions(-)

Comments

Pekka Enberg Oct. 12, 2012, 12:37 p.m. | #1
On Thu, Oct 4, 2012 at 1:21 PM, Anton Vorontsov
<anton.vorontsov@linaro.org> wrote:
> Soon we'll use this new function for other code; plus this makes code less
> indented.
>
> Signed-off-by: Anton Vorontsov <anton.vorontsov@linaro.org>
> ---
>  mm/vmevent.c | 107 +++++++++++++++++++++++++++++++----------------------------
>  1 file changed, 57 insertions(+), 50 deletions(-)
>
> diff --git a/mm/vmevent.c b/mm/vmevent.c
> index 39ef786..d434c11 100644
> --- a/mm/vmevent.c
> +++ b/mm/vmevent.c
> @@ -77,6 +77,59 @@ enum {
>         VMEVENT_ATTR_STATE_VALUE_WAS_GT = (1UL << 31),
>  };
>
> +static bool vmevent_match_attr(struct vmevent_attr *attr, u64 value)
> +{
> +       u32 state = attr->state;
> +       bool attr_lt = state & VMEVENT_ATTR_STATE_VALUE_LT;
> +       bool attr_gt = state & VMEVENT_ATTR_STATE_VALUE_GT;
> +       bool attr_eq = state & VMEVENT_ATTR_STATE_VALUE_EQ;
> +       bool edge = state & VMEVENT_ATTR_STATE_EDGE_TRIGGER;
> +       u32 was_lt_mask = VMEVENT_ATTR_STATE_VALUE_WAS_LT;
> +       u32 was_gt_mask = VMEVENT_ATTR_STATE_VALUE_WAS_GT;
> +       bool lt = value < attr->value;
> +       bool gt = value > attr->value;
> +       bool eq = value == attr->value;
> +       bool was_lt = state & was_lt_mask;
> +       bool was_gt = state & was_gt_mask;

[snip]

So I merged this patch but vmevent_match_attr() is still too ugly for
words. It really could use some serious cleanups.
Anton Vorontsov Oct. 12, 2012, 10:21 p.m. | #2
On Fri, Oct 12, 2012 at 03:37:43PM +0300, Pekka Enberg wrote:
[...]
> > +static bool vmevent_match_attr(struct vmevent_attr *attr, u64 value)
> > +{
> > +       u32 state = attr->state;
> > +       bool attr_lt = state & VMEVENT_ATTR_STATE_VALUE_LT;
> > +       bool attr_gt = state & VMEVENT_ATTR_STATE_VALUE_GT;
> > +       bool attr_eq = state & VMEVENT_ATTR_STATE_VALUE_EQ;
> > +       bool edge = state & VMEVENT_ATTR_STATE_EDGE_TRIGGER;
> > +       u32 was_lt_mask = VMEVENT_ATTR_STATE_VALUE_WAS_LT;
> > +       u32 was_gt_mask = VMEVENT_ATTR_STATE_VALUE_WAS_GT;
> > +       bool lt = value < attr->value;
> > +       bool gt = value > attr->value;
> > +       bool eq = value == attr->value;
> > +       bool was_lt = state & was_lt_mask;
> > +       bool was_gt = state & was_gt_mask;
> 
> [snip]
> 
> So I merged this patch but vmevent_match_attr() is still too ugly for
> words. It really could use some serious cleanups.

Thanks a lot for merging these cleanups!

Yes, the patch wasn't meant to simplify the matching logic, but just to
let us use the function in other places.

I once started converting the function into table-based approach, but the
code started growing, and I abandoned the idea for now. I might resume the
work just for the fun of it, but the code will be larger than this ad-hoc
function, althouh surely it will be more generic and understandable.

But let's solve primary problems with the vmevent first. :-)

Thanks,
Anton.

Patch hide | download patch | download mbox

diff --git a/mm/vmevent.c b/mm/vmevent.c
index 39ef786..d434c11 100644
--- a/mm/vmevent.c
+++ b/mm/vmevent.c
@@ -77,6 +77,59 @@  enum {
 	VMEVENT_ATTR_STATE_VALUE_WAS_GT	= (1UL << 31),
 };
 
+static bool vmevent_match_attr(struct vmevent_attr *attr, u64 value)
+{
+	u32 state = attr->state;
+	bool attr_lt = state & VMEVENT_ATTR_STATE_VALUE_LT;
+	bool attr_gt = state & VMEVENT_ATTR_STATE_VALUE_GT;
+	bool attr_eq = state & VMEVENT_ATTR_STATE_VALUE_EQ;
+	bool edge = state & VMEVENT_ATTR_STATE_EDGE_TRIGGER;
+	u32 was_lt_mask = VMEVENT_ATTR_STATE_VALUE_WAS_LT;
+	u32 was_gt_mask = VMEVENT_ATTR_STATE_VALUE_WAS_GT;
+	bool lt = value < attr->value;
+	bool gt = value > attr->value;
+	bool eq = value == attr->value;
+	bool was_lt = state & was_lt_mask;
+	bool was_gt = state & was_gt_mask;
+	bool was_eq = was_lt && was_gt;
+	bool ret = false;
+
+	if (!state)
+		return false;
+
+	if (!attr_lt && !attr_gt && !attr_eq)
+		return false;
+
+	if (((attr_lt && lt) || (attr_gt && gt) || (attr_eq && eq)) && !edge)
+		return true;
+
+	if (attr_eq && eq && was_eq) {
+		return false;
+	} else if (attr_lt && lt && was_lt && !was_eq) {
+		return false;
+	} else if (attr_gt && gt && was_gt && !was_eq) {
+		return false;
+	} else if (eq) {
+		state |= was_lt_mask;
+		state |= was_gt_mask;
+		if (attr_eq)
+			ret = true;
+	} else if (lt) {
+		state |= was_lt_mask;
+		state &= ~was_gt_mask;
+		if (attr_lt)
+			ret = true;
+	} else if (gt) {
+		state |= was_gt_mask;
+		state &= ~was_lt_mask;
+		if (attr_gt)
+			ret = true;
+	}
+
+	attr->state = state;
+	return ret;
+}
+
 static bool vmevent_match(struct vmevent_watch *watch)
 {
 	struct vmevent_config *config = &watch->config;
@@ -84,57 +137,11 @@  static bool vmevent_match(struct vmevent_watch *watch)
 
 	for (i = 0; i < config->counter; i++) {
 		struct vmevent_attr *attr = &config->attrs[i];
-		u32 state = attr->state;
-		bool attr_lt = state & VMEVENT_ATTR_STATE_VALUE_LT;
-		bool attr_gt = state & VMEVENT_ATTR_STATE_VALUE_GT;
-		bool attr_eq = state & VMEVENT_ATTR_STATE_VALUE_EQ;
-
-		if (!state)
-			continue;
+		u64 val;
 
-		if (attr_lt || attr_gt || attr_eq) {
-			bool edge = state & VMEVENT_ATTR_STATE_EDGE_TRIGGER;
-			u32 was_lt_mask = VMEVENT_ATTR_STATE_VALUE_WAS_LT;
-			u32 was_gt_mask = VMEVENT_ATTR_STATE_VALUE_WAS_GT;
-			u64 value = vmevent_sample_attr(watch, attr);
-			bool lt = value < attr->value;
-			bool gt = value > attr->value;
-			bool eq = value == attr->value;
-			bool was_lt = state & was_lt_mask;
-			bool was_gt = state & was_gt_mask;
-			bool was_eq = was_lt && was_gt;
-			bool ret = false;
-
-			if (((attr_lt && lt) || (attr_gt && gt) ||
-					(attr_eq && eq)) && !edge)
-				return true;
-
-			if (attr_eq && eq && was_eq) {
-				return false;
-			} else if (attr_lt && lt && was_lt && !was_eq) {
-				return false;
-			} else if (attr_gt && gt && was_gt && !was_eq) {
-				return false;
-			} else if (eq) {
-				state |= was_lt_mask;
-				state |= was_gt_mask;
-				if (attr_eq)
-					ret = true;
-			} else if (lt) {
-				state |= was_lt_mask;
-				state &= ~was_gt_mask;
-				if (attr_lt)
-					ret = true;
-			} else if (gt) {
-				state |= was_gt_mask;
-				state &= ~was_lt_mask;
-				if (attr_gt)
-					ret = true;
-			}
-
-			attr->state = state;
-			return ret;
-		}
+		val = vmevent_sample_attr(watch, attr);
+		if (vmevent_match_attr(attr, val))
+			return true;
 	}
 
 	return false;