File: message.c

package info (click to toggle)
evms 2.5.2-1.sarge2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 14,248 kB
  • ctags: 15,488
  • sloc: ansic: 201,340; perl: 12,421; sh: 4,262; makefile: 1,516; yacc: 316; sed: 16
file content (414 lines) | stat: -rw-r--r-- 10,811 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 *
 *   (C) Copyright IBM Corp. 2001, 2003
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program;  if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Module: message.c
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/time.h>

#include "fullengine.h"
#include "memman.h"
#include "message.h"
#include "engine.h"
#include "handlemgr.h"

/*
 * Global data
 */
ui_callbacks_t * ui_callbacks = NULL;

/*
 * message_buffer has some added fudge space in the buffer to allow for the
 * header we prepend and cover any possible overrun from expanding the format
 * string.
 */
char message_buffer[MAX_USER_MESSAGE_LEN + 1024];

int engine_user_message(int    * answer,
			char * * choice_text,
			char   * message_fmt,
			...) {
	int rc = 0;

	LOG_PROC_ENTRY();

	if (ui_callbacks != NULL) {
		if (ui_callbacks->user_message != NULL) {
			va_list args;

			if (engine_mode & ENGINE_DAEMON) {
				strcpy(message_buffer, "Daemon: ");
			} else {
				strcpy(message_buffer, "Engine: ");
			}

			va_start(args, message_fmt);
			vsprintf(message_buffer + strlen(message_buffer), message_fmt, args);
			va_end(args);

			/*
			 * Put the message (and later, any answer) into the log.  Use the
			 * CRITICAL level so that the message will always go to the log,
			 * even though the message itself may not be CRITICAL.
			 */
			LOG_CRITICAL("Message is: %s\n", message_buffer);
			rc = ui_callbacks->user_message(message_buffer, answer, choice_text);

			if (rc == 0) {
				if ((answer != NULL) && (choice_text != NULL)) {
					LOG_CRITICAL("Answer is: \"%s\"\n", choice_text[*answer]);
				}
			}
		}
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


int plugin_user_message(plugin_record_t * plugin,
			int             * answer,
			char          * * choice_text,
			char            * message_fmt,
			...) {
	int rc = 0;

	LOG_PROC_ENTRY();

	if (ui_callbacks != NULL) {
		if (ui_callbacks->user_message != NULL) {
			va_list args;

			strcpy(message_buffer, plugin->short_name);
			strcat(message_buffer, ": ");

			va_start(args, message_fmt);
			vsprintf(message_buffer + strlen(message_buffer), message_fmt, args);
			va_end(args);

			/*
			 * Put the message (and later, any answer) into the log.  Use the
			 * CRITICAL level so that the message will always go to the log,
			 * even though the message itself may not be CRITICAL.
			 */
			LOG_CRITICAL("Message is: %s\n", message_buffer);
			rc = ui_callbacks->user_message(message_buffer, answer, choice_text);

			if (rc == 0) {
				if ((answer != NULL) && (choice_text != NULL)) {
					LOG_CRITICAL("Answer is: \"%s\"\n", choice_text[*answer]);
				}
			}
		}
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


int plugin_user_communication(void                * object_instance,
			      char                * message_text,
			      option_desc_array_t * options){
	int rc = 0;

	LOG_PROC_ENTRY();

	if (ui_callbacks != NULL) {
		if (ui_callbacks->user_communication != NULL) {

			task_context_t * task = engine_alloc(sizeof(task_context_t));

			if (task != NULL) {
				task_handle_t task_handle;

				/*
				 * Fill in the appropriate task fields.  The rest are
				 * left 0 (NULL).
				 */

				task->plugin = NULL;
				task->object = object_instance;
				task->action = EVMS_Task_Message;

				task->option_descriptors = options;

				/* Make a handle for the task. */
				rc = create_handle(task,
						   TASK,
						   &task_handle);

				if (rc == 0) {

					/* Call the UI callback. */
					rc = ui_callbacks->user_communication(message_text, task_handle);

				} else {
					LOG_WARNING("create_handle() returned error %d.\n", rc);
				}

				engine_free(task);

			} else {
				LOG_CRITICAL("Memory allocation of task_context_t failed.\n");
				rc = ENOMEM;
			}
		}
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}


#define NUM_TIMERS	1024
typedef struct timer_data_s {
	u_int64_t	timers[NUM_TIMERS];
	u_int64_t	counts[NUM_TIMERS];
	u_int64_t	rates[NUM_TIMERS];
	int		oldest;
	int		newest;
} timer_data_t;

static void calculate_time_estimate(progress_t * progress) {

	timer_data_t * timer_data;
	int newest;
	int oldest;
	u_int64_t block_time;
	u_int64_t block_count;
	u_int64_t ms_per_chunk;
	struct timeval time;
	struct timezone tz;

	LOG_PROC_ENTRY();

	if (progress->timer_data == NULL) {
		LOG_DEBUG("progress has no plug-in private data.  Can't calculate a time estimate without timer data.\n");
                LOG_PROC_EXIT_VOID();
		return;
	}

	timer_data = progress->timer_data;
	newest = timer_data->newest;
	oldest = timer_data->oldest;

	if (newest < NUM_TIMERS - 1) {
		newest++;
	} else {
		newest = 0;
	}
	timer_data->newest = newest;

	if (newest == oldest) {
		if (oldest < NUM_TIMERS - 1) {
			oldest++;
		} else {
			oldest = 0;
		}
	}

	gettimeofday(&time, &tz);
	timer_data->timers[newest] = (u_int64_t) time.tv_sec * (u_int64_t) 1000000 + (u_int64_t) time.tv_usec;
	timer_data->counts[newest] = progress->count;

	/*
	 * Don't use more than the last thirty seconds of data
	 * for making an estimate.
	 */
	while (timer_data->timers[newest] - timer_data->timers[oldest] > 30000000) {
		int next_oldest = oldest;

		if (next_oldest < NUM_TIMERS - 1) {
			next_oldest++;
		} else {
			next_oldest = 0;
		}
		if (next_oldest == newest) {
			break;
		} else {
			oldest = next_oldest;
		}
	}
	timer_data->oldest = oldest;

	block_time  = timer_data->timers[newest] - timer_data->timers[oldest];
	block_count = timer_data->counts[newest] - timer_data->counts[oldest];
	if (block_count != 0) {
		ms_per_chunk = block_time / block_count;
	} else {
		ms_per_chunk = UINT64_MAX;
	}
	timer_data->rates[newest] = ms_per_chunk;

	/*
	 * Wait until we have at least five seconds of data before making an
	 * estimate of the remaining time.
	 */
	if (timer_data->timers[newest] - timer_data->timers[oldest] > 5000000) {

		/*
		 * Can't give an estimate if the count hasn't incremented at all
		 * across the array of timers.
		 */
		if (block_count != 0) {
			u_int64_t rem_chunks = progress->total_count - progress->count;
			u_int64_t average_ms_per_chunk = 0;
			u_int64_t rem_time_ms;
			uint rem_secs;
			int i = 0;
			int j = oldest;

			while (j != newest) {
				if (timer_data->rates[j] != -1) {
					average_ms_per_chunk += timer_data->rates[j];
					i++;
				}
				if (j < NUM_TIMERS - 1) {
					j++;
				} else {
					j = 0;
				}
			}

			average_ms_per_chunk /= i;

			/*
			 * The calculation:
			 *   rem_time_ms = rem_chunks * ms_per_chunk;
			 * can cause rem_time_ms to drift wildly between
			 * progress reports.  ms_per_chunk fluctuates.  It also
			 * loses precision from the division since it is an
			 * integer.  For large values of rem_chunks, the
			 * fluctuations and loss of precision get amplified,
			 * resulting in drifting time estimates.
			 *
			 * So, use the time and count for the whole block when
			 * calculating rem_time_ms.  It helps preserve some
			 * precision which keeps the times more consistent
			 * between the progress reports.
			 */
			rem_time_ms = (rem_chunks / block_count) * block_time +
				      (rem_chunks % block_count) * average_ms_per_chunk;

			/*
			 * Add a half second before rounding to seconds.
			 * Without it, the calculation will round down to zero
			 * as soon as rem_time_ms is under 1000000.  That leaves
			 * the time estimate at zero for the last second.
			 * Depending on how the UI handles it, the time estimate
			 * either reads 00:00 or it disappears for the last
			 * second before progress is complete.  Either one looks
			 * funny.  The extra half second makes the time estimate
			 * appear to hit zero when progress is complete.
			 */
			rem_secs = (rem_time_ms + 500000) / 1000000;

			/*
			 * This check helps dampen the bouncing of the estimate
			 * due to fractional noise from the calculations above.
			 */
			if (abs(rem_secs - progress->remaining_seconds) > 3) {
				progress->remaining_seconds += (int) (rem_secs - progress->remaining_seconds) / 2;
			} else {
				if (rem_secs < progress->remaining_seconds) {
					progress->remaining_seconds = rem_secs;
				}
			}
		}
	}

	LOG_PROC_EXIT_VOID();
	return;
}


int plugin_progress(progress_t * progress) {

	int rc;

	LOG_PROC_ENTRY();

	LOG_DEBUG("    id:                %d\n", progress->id);
	LOG_DEBUG("    title:             %s\n", progress->title);
	LOG_DEBUG("    description:       %s\n", progress->description);
	LOG_DEBUG("    type:              %s\n", (progress->type == DISPLAY_PERCENT) ? "DISPLAY_PERCENT" :
						 (progress->type == DISPLAY_COUNT)   ? "DISPLAY_COUNT" :
						 (progress->type == INDETERMINATE)   ? "INDETERMINATE" :
			       							       "(unknown)");
	LOG_DEBUG("    count:             %"PRIu64"\n", progress->count);
	LOG_DEBUG("    total_count:       %"PRIu64"\n", progress->total_count);

	if (ui_callbacks == NULL) {
		LOG_DEBUG("There are no UI callbacks.\n");
		LOG_PROC_EXIT_INT(ENOSYS);
		return ENOSYS;
	}

	if (ui_callbacks->progress == NULL) {
		LOG_DEBUG("The UI did not provide a progress callback.\n");
		LOG_PROC_EXIT_INT(ENOSYS);
		return ENOSYS;
	}

	if (progress->timer_data != NULL) {
		calculate_time_estimate(progress);
	}

	LOG_DEBUG("    remaining_seconds: %u\n", progress->remaining_seconds);
	
	/*
	 * Allocate an initilaize the timer data on the first progress call
	 * if the progress is not indeterminate and the caller did not forbid
	 * time estimates.
	 */
	if ((progress->id == 0) &&
	    (progress->type != INDETERMINATE) &&
	    !(progress->flags & PROGRESS_NO_TIME_ESTIMATE)) {

		progress->timer_data = engine_alloc(sizeof(timer_data_t));
		if (progress->timer_data != NULL) {
			struct timeval  time;
			struct timezone tz;

			/* Fill in the first entry */
			gettimeofday(&time, &tz);
			progress->timer_data->timers[0] = (u_int64_t) time.tv_sec * (u_int64_t) 1000000 + (u_int64_t) time.tv_usec;
			progress->timer_data->oldest = 0;
		}
	}

	rc = ui_callbacks->progress(progress);

	if (progress->count >= progress->total_count) {
		/* engine_free() handles NULL pointers. */
		engine_free(progress->timer_data);
		progress->timer_data = NULL;
	}

	LOG_PROC_EXIT_INT(rc);
	return rc;
}