mailarchive of the pengutronix oss-tools mailing list
 help / color / mirror / Atom feed
* [OSS-Tools] [PATCH v2] barebox-state: have the --set option to avoid writes if possible
@ 2020-11-13 19:08 Ahmad Fatoum
  2021-03-16 14:24 ` Roland Hieber
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2020-11-13 19:08 UTC (permalink / raw)
  To: oss-tools

barebox-state --set always dirties the state when successful. Users
seeking to conserve write cycles thus have to --get the variable in
question first to check whether to write it. Make life of such users
easier by having barebox-state support this out-of-the-box.

This allows users to fire and forget execute barebox-state.

This arguably should have been the behavior from the beginning,
the state implementation shared by barebox and dt-utils already
marks the state dirty if buckets appear corrupted on probe, so
there is no extra benefit in always executing the write.

The comparison to determine whether the state should be dirtied
does an extra allocation in interest of clarity.
This overhead is deemed negligible compared to I/O and it makes
the code easier to follow.

Suggested-by: Jan Lübbe <jlu@pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2:
  - incorporate Jan's (off-list) suggestion to just change --set
    behavior. state implementation already dirties state if a
    bucket is corrupt, so there is really no valid use case for
    not conserving writes by default.
---
 src/barebox-state.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/barebox-state.c b/src/barebox-state.c
index cd56ce7192c3..7b5c0dae00dd 100644
--- a/src/barebox-state.c
+++ b/src/barebox-state.c
@@ -283,6 +283,7 @@ static int state_set_var(struct state *state, const char *var, const char *val)
 {
 	struct state_variable *sv;
 	struct variable_str_type *vtype;
+	char *oldval;
 	int ret;
 
 	sv = state_find_var(state, var);
@@ -296,6 +297,14 @@ static int state_set_var(struct state *state, const char *var, const char *val)
 	if (!vtype->set)
 		return -EPERM;
 
+	oldval = vtype->get(sv);
+	if (!IS_ERR(oldval)) {
+		bool equal = strcmp(oldval, val) == 0;
+		free(oldval);
+		if (equal)
+			return 0;
+	}
+
 	ret = vtype->set(sv, val);
 	if (ret)
 		return ret;
-- 
2.28.0


_______________________________________________
OSS-Tools mailing list
OSS-Tools@pengutronix.de

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [OSS-Tools] [PATCH v2] barebox-state: have the --set option to avoid writes if possible
  2020-11-13 19:08 [OSS-Tools] [PATCH v2] barebox-state: have the --set option to avoid writes if possible Ahmad Fatoum
@ 2021-03-16 14:24 ` Roland Hieber
  0 siblings, 0 replies; 2+ messages in thread
From: Roland Hieber @ 2021-03-16 14:24 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: oss-tools

On Fri, Nov 13, 2020 at 08:08:56PM +0100, Ahmad Fatoum wrote:
> barebox-state --set always dirties the state when successful. Users
> seeking to conserve write cycles thus have to --get the variable in
> question first to check whether to write it. Make life of such users
> easier by having barebox-state support this out-of-the-box.
> 
> This allows users to fire and forget execute barebox-state.
> 
> This arguably should have been the behavior from the beginning,
> the state implementation shared by barebox and dt-utils already
> marks the state dirty if buckets appear corrupted on probe, so
> there is no extra benefit in always executing the write.
> 
> The comparison to determine whether the state should be dirtied
> does an extra allocation in interest of clarity.
> This overhead is deemed negligible compared to I/O and it makes
> the code easier to follow.
> 
> Suggested-by: Jan Lübbe <jlu@pengutronix.de>
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
> v1 -> v2:
>   - incorporate Jan's (off-list) suggestion to just change --set
>     behavior. state implementation already dirties state if a
>     bucket is corrupt, so there is really no valid use case for
>     not conserving writes by default.
> ---
>  src/barebox-state.c | 9 +++++++++
>  1 file changed, 9 insertions(+)

Thanks, applied.

 - Roland

> 
> diff --git a/src/barebox-state.c b/src/barebox-state.c
> index cd56ce7192c3..7b5c0dae00dd 100644
> --- a/src/barebox-state.c
> +++ b/src/barebox-state.c
> @@ -283,6 +283,7 @@ static int state_set_var(struct state *state, const char *var, const char *val)
>  {
>  	struct state_variable *sv;
>  	struct variable_str_type *vtype;
> +	char *oldval;
>  	int ret;
>  
>  	sv = state_find_var(state, var);
> @@ -296,6 +297,14 @@ static int state_set_var(struct state *state, const char *var, const char *val)
>  	if (!vtype->set)
>  		return -EPERM;
>  
> +	oldval = vtype->get(sv);
> +	if (!IS_ERR(oldval)) {
> +		bool equal = strcmp(oldval, val) == 0;
> +		free(oldval);
> +		if (equal)
> +			return 0;
> +	}
> +
>  	ret = vtype->set(sv, val);
>  	if (ret)
>  		return ret;
> -- 
> 2.28.0
> 
> 
> _______________________________________________
> OSS-Tools mailing list
> OSS-Tools@pengutronix.de

-- 
Roland Hieber, Pengutronix e.K.          | r.hieber@pengutronix.de     |
Steuerwalder Str. 21                     | https://www.pengutronix.de/ |
31137 Hildesheim, Germany                | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686         | Fax:   +49-5121-206917-5555 |

_______________________________________________
OSS-Tools mailing list
OSS-Tools@pengutronix.de

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-03-16 14:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-13 19:08 [OSS-Tools] [PATCH v2] barebox-state: have the --set option to avoid writes if possible Ahmad Fatoum
2021-03-16 14:24 ` Roland Hieber

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox