Browse Source

[monojob] Reset timeout when progress is made

Redefine the timeout parameter from "time since start of job" to "time
since progress was last made".  This does not affect any existing
behaviour, since all existing users of the timeout parameter do not
provide progress indication.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 10 years ago
parent
commit
b850a6be28
1 changed files with 18 additions and 11 deletions
  1. 18
    11
      src/core/monojob.c

+ 18
- 11
src/core/monojob.c View File

60
  */
60
  */
61
 int monojob_wait ( const char *string, unsigned long timeout ) {
61
 int monojob_wait ( const char *string, unsigned long timeout ) {
62
 	struct job_progress progress;
62
 	struct job_progress progress;
63
-	unsigned long start;
64
 	unsigned long last_keycheck;
63
 	unsigned long last_keycheck;
65
 	unsigned long last_progress;
64
 	unsigned long last_progress;
65
+	unsigned long last_display;
66
 	unsigned long now;
66
 	unsigned long now;
67
 	unsigned long elapsed;
67
 	unsigned long elapsed;
68
-	unsigned long completed;
69
-	unsigned long total;
68
+	unsigned long completed = 0;
69
+	unsigned long scaled_completed;
70
+	unsigned long scaled_total;
70
 	unsigned int percentage;
71
 	unsigned int percentage;
71
 	int shown_percentage = 0;
72
 	int shown_percentage = 0;
72
 	int ongoing_rc;
73
 	int ongoing_rc;
76
 	if ( string )
77
 	if ( string )
77
 		printf ( "%s...", string );
78
 		printf ( "%s...", string );
78
 	monojob_rc = -EINPROGRESS;
79
 	monojob_rc = -EINPROGRESS;
79
-	last_keycheck = last_progress = start = currticks();
80
+	last_keycheck = last_progress = last_display = currticks();
80
 	while ( monojob_rc == -EINPROGRESS ) {
81
 	while ( monojob_rc == -EINPROGRESS ) {
81
 
82
 
82
 		/* Allow job to progress */
83
 		/* Allow job to progress */
101
 		/* Monitor progress */
102
 		/* Monitor progress */
102
 		ongoing_rc = job_progress ( &monojob, &progress );
103
 		ongoing_rc = job_progress ( &monojob, &progress );
103
 
104
 
105
+		/* Reset timeout if progress has been made */
106
+		if ( completed != progress.completed )
107
+			last_progress = now;
108
+		completed = progress.completed;
109
+
104
 		/* Check for timeout, if applicable */
110
 		/* Check for timeout, if applicable */
105
-		elapsed = ( now - start );
111
+		elapsed = ( now - last_progress );
106
 		if ( timeout && ( elapsed >= timeout ) ) {
112
 		if ( timeout && ( elapsed >= timeout ) ) {
107
 			monojob_rc = ( ongoing_rc ? ongoing_rc : -ETIMEDOUT );
113
 			monojob_rc = ( ongoing_rc ? ongoing_rc : -ETIMEDOUT );
108
 			break;
114
 			break;
109
 		}
115
 		}
110
 
116
 
111
 		/* Display progress, if applicable */
117
 		/* Display progress, if applicable */
112
-		elapsed = ( now - last_progress );
118
+		elapsed = ( now - last_display );
113
 		if ( string && ( elapsed >= TICKS_PER_SEC ) ) {
119
 		if ( string && ( elapsed >= TICKS_PER_SEC ) ) {
114
 			if ( shown_percentage )
120
 			if ( shown_percentage )
115
 				printf ( "\b\b\b\b    \b\b\b\b" );
121
 				printf ( "\b\b\b\b    \b\b\b\b" );
116
 			/* Normalise progress figures to avoid overflow */
122
 			/* Normalise progress figures to avoid overflow */
117
-			completed = ( progress.completed / 128 );
118
-			total = ( progress.total / 128 );
119
-			if ( total ) {
120
-				percentage = ( ( 100 * completed ) / total );
123
+			scaled_completed = ( progress.completed / 128 );
124
+			scaled_total = ( progress.total / 128 );
125
+			if ( scaled_total ) {
126
+				percentage = ( ( 100 * scaled_completed ) /
127
+					       scaled_total );
121
 				printf ( "%3d%%", percentage );
128
 				printf ( "%3d%%", percentage );
122
 				shown_percentage = 1;
129
 				shown_percentage = 1;
123
 			} else {
130
 			} else {
124
 				printf ( "." );
131
 				printf ( "." );
125
 				shown_percentage = 0;
132
 				shown_percentage = 0;
126
 			}
133
 			}
127
-			last_progress = now;
134
+			last_display = now;
128
 		}
135
 		}
129
 	}
136
 	}
130
 	rc = monojob_rc;
137
 	rc = monojob_rc;

Loading…
Cancel
Save