1 /**
2 * collectd - src/utils_cache.c
3 * Copyright (C) 2007,2008 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Author:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_avltree.h"
26 #include "utils_cache.h"
27 #include "utils_threshold.h"
29 #include <assert.h>
30 #include <pthread.h>
32 typedef struct cache_entry_s
33 {
34 char name[6 * DATA_MAX_NAME_LEN];
35 int values_num;
36 gauge_t *values_gauge;
37 value_t *values_raw;
38 /* Time contained in the package
39 * (for calculating rates) */
40 time_t last_time;
41 /* Time according to the local clock
42 * (for purging old entries) */
43 time_t last_update;
44 /* Interval in which the data is collected
45 * (for purding old entries) */
46 int interval;
47 int state;
49 /*
50 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
51 * ! 0 ! 1 ! 2 ! 3 ! 4 ! 5 ! 6 ! 7 ! 8 ! ...
52 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
53 * ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ds0 ! ds1 ! ds2 ! ...
54 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+----
55 * ! t = 0 ! t = 1 ! t = 2 ! ...
56 * +-----------------+-----------------+-----------------+----
57 */
58 gauge_t *history;
59 size_t history_index; /* points to the next position to write to. */
60 size_t history_length;
61 } cache_entry_t;
63 static c_avl_tree_t *cache_tree = NULL;
64 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
66 static int cache_compare (const cache_entry_t *a, const cache_entry_t *b)
67 {
68 assert ((a != NULL) && (b != NULL));
69 return (strcmp (a->name, b->name));
70 } /* int cache_compare */
72 static cache_entry_t *cache_alloc (int values_num)
73 {
74 cache_entry_t *ce;
76 ce = (cache_entry_t *) malloc (sizeof (cache_entry_t));
77 if (ce == NULL)
78 {
79 ERROR ("utils_cache: cache_alloc: malloc failed.");
80 return (NULL);
81 }
82 memset (ce, '\0', sizeof (cache_entry_t));
83 ce->values_num = values_num;
85 ce->values_gauge = calloc (values_num, sizeof (*ce->values_gauge));
86 ce->values_raw = calloc (values_num, sizeof (*ce->values_raw));
87 if ((ce->values_gauge == NULL) || (ce->values_raw == NULL))
88 {
89 sfree (ce->values_gauge);
90 sfree (ce->values_raw);
91 sfree (ce);
92 ERROR ("utils_cache: cache_alloc: calloc failed.");
93 return (NULL);
94 }
96 ce->history = NULL;
97 ce->history_length = 0;
99 return (ce);
100 } /* cache_entry_t *cache_alloc */
102 static void cache_free (cache_entry_t *ce)
103 {
104 if (ce == NULL)
105 return;
107 sfree (ce->values_gauge);
108 sfree (ce->values_raw);
109 sfree (ce->history);
110 sfree (ce);
111 } /* void cache_free */
113 static int uc_send_notification (const char *name)
114 {
115 cache_entry_t *ce = NULL;
116 int status;
118 char *name_copy;
119 char *host;
120 char *plugin;
121 char *plugin_instance;
122 char *type;
123 char *type_instance;
125 notification_t n;
127 name_copy = strdup (name);
128 if (name_copy == NULL)
129 {
130 ERROR ("uc_send_notification: strdup failed.");
131 return (-1);
132 }
134 status = parse_identifier (name_copy, &host,
135 &plugin, &plugin_instance,
136 &type, &type_instance);
137 if (status != 0)
138 {
139 ERROR ("uc_send_notification: Cannot parse name `%s'", name);
140 return (-1);
141 }
143 /* Copy the associative members */
144 notification_init (&n, NOTIF_FAILURE, /* host = */ NULL,
145 host, plugin, plugin_instance, type, type_instance);
147 sfree (name_copy);
148 name_copy = host = plugin = plugin_instance = type = type_instance = NULL;
150 pthread_mutex_lock (&cache_lock);
152 /*
153 * Set the time _after_ getting the lock because we don't know how long
154 * acquiring the lock takes and we will use this time later to decide
155 * whether or not the state is OKAY.
156 */
157 n.time = time (NULL);
159 status = c_avl_get (cache_tree, name, (void *) &ce);
160 if (status != 0)
161 {
162 pthread_mutex_unlock (&cache_lock);
163 sfree (name_copy);
164 return (-1);
165 }
167 /* Check if the entry has been updated in the meantime */
168 if ((n.time - ce->last_update) < (2 * ce->interval))
169 {
170 ce->state = STATE_OKAY;
171 pthread_mutex_unlock (&cache_lock);
172 sfree (name_copy);
173 return (-1);
174 }
176 ssnprintf (n.message, sizeof (n.message),
177 "%s has not been updated for %i seconds.", name,
178 (int) (n.time - ce->last_update));
180 pthread_mutex_unlock (&cache_lock);
182 plugin_dispatch_notification (&n);
184 return (0);
185 } /* int uc_send_notification */
187 static void uc_check_range (const data_set_t *ds, cache_entry_t *ce)
188 {
189 int i;
191 for (i = 0; i < ds->ds_num; i++)
192 {
193 if (isnan (ce->values_gauge[i]))
194 continue;
195 else if (ce->values_gauge[i] < ds->ds[i].min)
196 ce->values_gauge[i] = NAN;
197 else if (ce->values_gauge[i] > ds->ds[i].max)
198 ce->values_gauge[i] = NAN;
199 }
200 } /* void uc_check_range */
202 static int uc_insert (const data_set_t *ds, const value_list_t *vl,
203 const char *key)
204 {
205 int i;
206 char *key_copy;
207 cache_entry_t *ce;
209 /* `cache_lock' has been locked by `uc_update' */
211 key_copy = strdup (key);
212 if (key_copy == NULL)
213 {
214 ERROR ("uc_insert: strdup failed.");
215 return (-1);
216 }
218 ce = cache_alloc (ds->ds_num);
219 if (ce == NULL)
220 {
221 sfree (key_copy);
222 ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
223 return (-1);
224 }
226 sstrncpy (ce->name, key, sizeof (ce->name));
228 for (i = 0; i < ds->ds_num; i++)
229 {
230 switch (ds->ds[i].type)
231 {
232 case DS_TYPE_COUNTER:
233 ce->values_gauge[i] = NAN;
234 ce->values_raw[i].counter = vl->values[i].counter;
235 break;
237 case DS_TYPE_GAUGE:
238 ce->values_gauge[i] = vl->values[i].gauge;
239 ce->values_raw[i].gauge = vl->values[i].gauge;
240 break;
242 case DS_TYPE_DERIVE:
243 ce->values_gauge[i] = NAN;
244 ce->values_raw[i].derive = vl->values[i].derive;
245 break;
247 case DS_TYPE_ABSOLUTE:
248 ce->values_gauge[i] = NAN;
249 if (vl->interval > 0)
250 ce->values_gauge[i] = ((double) vl->values[i].absolute)
251 / ((double) vl->interval);
252 ce->values_raw[i].absolute = vl->values[i].absolute;
253 break;
255 default:
256 /* This shouldn't happen. */
257 ERROR ("uc_insert: Don't know how to handle data source type %i.",
258 ds->ds[i].type);
259 return (-1);
260 } /* switch (ds->ds[i].type) */
261 } /* for (i) */
263 /* Prune invalid gauge data */
264 uc_check_range (ds, ce);
266 ce->last_time = vl->time;
267 ce->last_update = time (NULL);
268 ce->interval = vl->interval;
269 ce->state = STATE_OKAY;
271 if (c_avl_insert (cache_tree, key_copy, ce) != 0)
272 {
273 sfree (key_copy);
274 ERROR ("uc_insert: c_avl_insert failed.");
275 return (-1);
276 }
278 DEBUG ("uc_insert: Added %s to the cache.", key);
279 return (0);
280 } /* int uc_insert */
282 int uc_init (void)
283 {
284 if (cache_tree == NULL)
285 cache_tree = c_avl_create ((int (*) (const void *, const void *))
286 cache_compare);
288 return (0);
289 } /* int uc_init */
291 int uc_check_timeout (void)
292 {
293 time_t now;
294 cache_entry_t *ce;
296 char **keys = NULL;
297 int keys_len = 0;
299 char *key;
300 c_avl_iterator_t *iter;
301 int i;
303 pthread_mutex_lock (&cache_lock);
305 now = time (NULL);
307 /* Build a list of entries to be flushed */
308 iter = c_avl_get_iterator (cache_tree);
309 while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
310 {
311 /* If entry has not been updated, add to `keys' array */
312 if ((now - ce->last_update) >= (2 * ce->interval))
313 {
314 char **tmp;
316 tmp = (char **) realloc ((void *) keys,
317 (keys_len + 1) * sizeof (char *));
318 if (tmp == NULL)
319 {
320 ERROR ("uc_check_timeout: realloc failed.");
321 c_avl_iterator_destroy (iter);
322 sfree (keys);
323 pthread_mutex_unlock (&cache_lock);
324 return (-1);
325 }
327 keys = tmp;
328 keys[keys_len] = strdup (key);
329 if (keys[keys_len] == NULL)
330 {
331 ERROR ("uc_check_timeout: strdup failed.");
332 continue;
333 }
334 keys_len++;
335 }
336 } /* while (c_avl_iterator_next) */
338 ce = NULL;
340 for (i = 0; i < keys_len; i++)
341 {
342 int status;
344 status = ut_check_interesting (keys[i]);
346 if (status < 0)
347 {
348 ERROR ("uc_check_timeout: ut_check_interesting failed.");
349 sfree (keys[i]);
350 continue;
351 }
352 else if (status == 0) /* ``service'' is uninteresting */
353 {
354 DEBUG ("uc_check_timeout: %s is missing but ``uninteresting''",
355 keys[i]);
356 status = c_avl_remove (cache_tree, keys[i],
357 (void *) &key, (void *) &ce);
358 if (status != 0)
359 {
360 ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
361 }
362 sfree (keys[i]);
363 sfree (key);
364 cache_free (ce);
365 continue;
366 }
368 /* If we get here, the value is ``interesting''. Query the record from the
369 * cache and update the state field. */
370 if (c_avl_get (cache_tree, keys[i], (void *) &ce) != 0)
371 {
372 ERROR ("uc_check_timeout: cannot get data for %s from cache", keys[i]);
373 /* Do not free `keys[i]' so a notification is sent further down. */
374 continue;
375 }
376 assert (ce != NULL);
378 if (status == 2) /* persist */
379 {
380 DEBUG ("uc_check_timeout: %s is missing, sending notification.",
381 keys[i]);
382 ce->state = STATE_MISSING;
383 /* Do not free `keys[i]' so a notification is sent further down. */
384 }
385 else if (status == 1) /* do not persist */
386 {
387 if (ce->state == STATE_MISSING)
388 {
389 DEBUG ("uc_check_timeout: %s is missing but "
390 "notification has already been sent.",
391 keys[i]);
392 /* Set `keys[i]' to NULL to no notification is sent. */
393 sfree (keys[i]);
394 }
395 else /* (ce->state != STATE_MISSING) */
396 {
397 DEBUG ("uc_check_timeout: %s is missing, sending one notification.",
398 keys[i]);
399 ce->state = STATE_MISSING;
400 /* Do not free `keys[i]' so a notification is sent further down. */
401 }
402 }
403 else
404 {
405 WARNING ("uc_check_timeout: ut_check_interesting (%s) returned "
406 "invalid status %i.",
407 keys[i], status);
408 sfree (keys[i]);
409 }
411 /* Make really sure the next iteration doesn't work with this pointer.
412 * There have been too many bugs in the past.. :/ -- octo */
413 ce = NULL;
414 } /* for (keys[i]) */
416 c_avl_iterator_destroy (iter);
418 pthread_mutex_unlock (&cache_lock);
420 for (i = 0; i < keys_len; i++)
421 {
422 if (keys[i] == NULL)
423 continue;
425 uc_send_notification (keys[i]);
426 sfree (keys[i]);
427 }
429 sfree (keys);
431 return (0);
432 } /* int uc_check_timeout */
434 int uc_update (const data_set_t *ds, const value_list_t *vl)
435 {
436 char name[6 * DATA_MAX_NAME_LEN];
437 cache_entry_t *ce = NULL;
438 int send_okay_notification = 0;
439 time_t update_delay = 0;
440 notification_t n;
441 int status;
442 int i;
444 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
445 {
446 ERROR ("uc_update: FORMAT_VL failed.");
447 return (-1);
448 }
450 pthread_mutex_lock (&cache_lock);
452 status = c_avl_get (cache_tree, name, (void *) &ce);
453 if (status != 0) /* entry does not yet exist */
454 {
455 status = uc_insert (ds, vl, name);
456 pthread_mutex_unlock (&cache_lock);
457 return (status);
458 }
460 assert (ce != NULL);
461 assert (ce->values_num == ds->ds_num);
463 if (ce->last_time >= vl->time)
464 {
465 pthread_mutex_unlock (&cache_lock);
466 NOTICE ("uc_update: Value too old: name = %s; value time = %u; "
467 "last cache update = %u;",
468 name, (unsigned int) vl->time, (unsigned int) ce->last_time);
469 return (-1);
470 }
472 /* Send a notification (after the lock has been released) if we switch the
473 * state from something else to `okay'. */
474 if (ce->state == STATE_MISSING)
475 {
476 send_okay_notification = 1;
477 ce->state = STATE_OKAY;
478 update_delay = time (NULL) - ce->last_update;
479 }
481 for (i = 0; i < ds->ds_num; i++)
482 {
483 switch (ds->ds[i].type)
484 {
485 case DS_TYPE_COUNTER:
486 {
487 counter_t diff;
489 /* check if the counter has wrapped around */
490 if (vl->values[i].counter < ce->values_raw[i].counter)
491 {
492 if (ce->values_raw[i].counter <= 4294967295U)
493 diff = (4294967295U - ce->values_raw[i].counter)
494 + vl->values[i].counter;
495 else
496 diff = (18446744073709551615ULL - ce->values_raw[i].counter)
497 + vl->values[i].counter;
498 }
499 else /* counter has NOT wrapped around */
500 {
501 diff = vl->values[i].counter - ce->values_raw[i].counter;
502 }
504 ce->values_gauge[i] = ((double) diff)
505 / ((double) (vl->time - ce->last_time));
506 ce->values_raw[i].counter = vl->values[i].counter;
507 }
508 break;
510 case DS_TYPE_GAUGE:
511 ce->values_raw[i].gauge = vl->values[i].gauge;
512 ce->values_gauge[i] = vl->values[i].gauge;
513 break;
515 case DS_TYPE_DERIVE:
516 {
517 derive_t diff;
519 diff = vl->values[i].derive - ce->values_raw[i].derive;
521 ce->values_gauge[i] = ((double) diff)
522 / ((double) (vl->time - ce->last_time));
523 ce->values_raw[i].derive = vl->values[i].derive;
524 }
525 break;
527 case DS_TYPE_ABSOLUTE:
528 ce->values_gauge[i] = ((double) vl->values[i].absolute)
529 / ((double) (vl->time - ce->last_time));
530 ce->values_raw[i].absolute = vl->values[i].absolute;
531 break;
533 default:
534 /* This shouldn't happen. */
535 pthread_mutex_unlock (&cache_lock);
536 ERROR ("uc_update: Don't know how to handle data source type %i.",
537 ds->ds[i].type);
538 return (-1);
539 } /* switch (ds->ds[i].type) */
541 DEBUG ("uc_update: %s: ds[%i] = %lf", name, i, ce->values_gauge[i]);
542 } /* for (i) */
544 /* Update the history if it exists. */
545 if (ce->history != NULL)
546 {
547 assert (ce->history_index < ce->history_length);
548 for (i = 0; i < ce->values_num; i++)
549 {
550 size_t hist_idx = (ce->values_num * ce->history_index) + i;
551 ce->history[hist_idx] = ce->values_gauge[i];
552 }
554 assert (ce->history_length > 0);
555 ce->history_index = (ce->history_index + 1) % ce->history_length;
556 }
558 /* Prune invalid gauge data */
559 uc_check_range (ds, ce);
561 ce->last_time = vl->time;
562 ce->last_update = time (NULL);
563 ce->interval = vl->interval;
565 pthread_mutex_unlock (&cache_lock);
567 if (send_okay_notification == 0)
568 return (0);
570 /* Do not send okay notifications for uninteresting values, i. e. values for
571 * which no threshold is configured. */
572 status = ut_check_interesting (name);
573 if (status <= 0)
574 return (0);
576 /* Initialize the notification */
577 memset (&n, '\0', sizeof (n));
578 NOTIFICATION_INIT_VL (&n, vl, ds);
580 n.severity = NOTIF_OKAY;
581 n.time = vl->time;
583 ssnprintf (n.message, sizeof (n.message),
584 "Received a value for %s. It was missing for %u seconds.",
585 name, (unsigned int) update_delay);
587 plugin_dispatch_notification (&n);
589 return (0);
590 } /* int uc_update */
592 int uc_get_rate_by_name (const char *name, gauge_t **ret_values, size_t *ret_values_num)
593 {
594 gauge_t *ret = NULL;
595 size_t ret_num = 0;
596 cache_entry_t *ce = NULL;
597 int status = 0;
599 pthread_mutex_lock (&cache_lock);
601 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
602 {
603 assert (ce != NULL);
605 ret_num = ce->values_num;
606 ret = (gauge_t *) malloc (ret_num * sizeof (gauge_t));
607 if (ret == NULL)
608 {
609 ERROR ("utils_cache: uc_get_rate_by_name: malloc failed.");
610 status = -1;
611 }
612 else
613 {
614 memcpy (ret, ce->values_gauge, ret_num * sizeof (gauge_t));
615 }
616 }
617 else
618 {
619 DEBUG ("utils_cache: uc_get_rate_by_name: No such value: %s", name);
620 status = -1;
621 }
623 pthread_mutex_unlock (&cache_lock);
625 if (status == 0)
626 {
627 *ret_values = ret;
628 *ret_values_num = ret_num;
629 }
631 return (status);
632 } /* gauge_t *uc_get_rate_by_name */
634 gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
635 {
636 char name[6 * DATA_MAX_NAME_LEN];
637 gauge_t *ret = NULL;
638 size_t ret_num = 0;
639 int status;
641 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
642 {
643 ERROR ("utils_cache: uc_get_rate: FORMAT_VL failed.");
644 return (NULL);
645 }
647 status = uc_get_rate_by_name (name, &ret, &ret_num);
648 if (status != 0)
649 return (NULL);
651 /* This is important - the caller has no other way of knowing how many
652 * values are returned. */
653 if (ret_num != (size_t) ds->ds_num)
654 {
655 ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
656 "but uc_get_rate_by_name returned %zu.",
657 ds->type, ds->ds_num, ret_num);
658 sfree (ret);
659 return (NULL);
660 }
662 return (ret);
663 } /* gauge_t *uc_get_rate */
665 int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number)
666 {
667 c_avl_iterator_t *iter;
668 char *key;
669 cache_entry_t *value;
671 char **names = NULL;
672 time_t *times = NULL;
673 size_t number = 0;
675 int status = 0;
677 if ((ret_names == NULL) || (ret_number == NULL))
678 return (-1);
680 pthread_mutex_lock (&cache_lock);
682 iter = c_avl_get_iterator (cache_tree);
683 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
684 {
685 char **temp;
687 if (ret_times != NULL)
688 {
689 time_t *tmp_times;
691 tmp_times = (time_t *) realloc (times, sizeof (time_t) * (number + 1));
692 if (tmp_times == NULL)
693 {
694 status = -1;
695 break;
696 }
697 times = tmp_times;
698 times[number] = value->last_time;
699 }
701 temp = (char **) realloc (names, sizeof (char *) * (number + 1));
702 if (temp == NULL)
703 {
704 status = -1;
705 break;
706 }
707 names = temp;
708 names[number] = strdup (key);
709 if (names[number] == NULL)
710 {
711 status = -1;
712 break;
713 }
714 number++;
715 } /* while (c_avl_iterator_next) */
717 c_avl_iterator_destroy (iter);
718 pthread_mutex_unlock (&cache_lock);
720 if (status != 0)
721 {
722 size_t i;
724 for (i = 0; i < number; i++)
725 {
726 sfree (names[i]);
727 }
728 sfree (names);
730 return (-1);
731 }
733 *ret_names = names;
734 if (ret_times != NULL)
735 *ret_times = times;
736 *ret_number = number;
738 return (0);
739 } /* int uc_get_names */
741 int uc_get_state (const data_set_t *ds, const value_list_t *vl)
742 {
743 char name[6 * DATA_MAX_NAME_LEN];
744 cache_entry_t *ce = NULL;
745 int ret = STATE_ERROR;
747 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
748 {
749 ERROR ("uc_get_state: FORMAT_VL failed.");
750 return (STATE_ERROR);
751 }
753 pthread_mutex_lock (&cache_lock);
755 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
756 {
757 assert (ce != NULL);
758 ret = ce->state;
759 }
761 pthread_mutex_unlock (&cache_lock);
763 return (ret);
764 } /* int uc_get_state */
766 int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state)
767 {
768 char name[6 * DATA_MAX_NAME_LEN];
769 cache_entry_t *ce = NULL;
770 int ret = -1;
772 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
773 {
774 ERROR ("uc_get_state: FORMAT_VL failed.");
775 return (STATE_ERROR);
776 }
778 pthread_mutex_lock (&cache_lock);
780 if (c_avl_get (cache_tree, name, (void *) &ce) == 0)
781 {
782 assert (ce != NULL);
783 ret = ce->state;
784 ce->state = state;
785 }
787 pthread_mutex_unlock (&cache_lock);
789 return (ret);
790 } /* int uc_set_state */
792 int uc_get_history_by_name (const char *name,
793 gauge_t *ret_history, size_t num_steps, size_t num_ds)
794 {
795 cache_entry_t *ce = NULL;
796 size_t i;
797 int status = 0;
799 pthread_mutex_lock (&cache_lock);
801 status = c_avl_get (cache_tree, name, (void *) &ce);
802 if (status != 0)
803 {
804 pthread_mutex_unlock (&cache_lock);
805 return (-ENOENT);
806 }
808 if (((size_t) ce->values_num) != num_ds)
809 {
810 pthread_mutex_unlock (&cache_lock);
811 return (-EINVAL);
812 }
814 /* Check if there are enough values available. If not, increase the buffer
815 * size. */
816 if (ce->history_length < num_steps)
817 {
818 gauge_t *tmp;
819 size_t i;
821 tmp = realloc (ce->history, sizeof (*ce->history)
822 * num_steps * ce->values_num);
823 if (tmp == NULL)
824 {
825 pthread_mutex_unlock (&cache_lock);
826 return (-ENOMEM);
827 }
829 for (i = ce->history_length * ce->values_num;
830 i < (num_steps * ce->values_num);
831 i++)
832 tmp[i] = NAN;
834 ce->history = tmp;
835 ce->history_length = num_steps;
836 } /* if (ce->history_length < num_steps) */
838 /* Copy the values to the output buffer. */
839 for (i = 0; i < num_steps; i++)
840 {
841 size_t src_index;
842 size_t dst_index;
844 if (i < ce->history_index)
845 src_index = ce->history_index - (i + 1);
846 else
847 src_index = ce->history_length + ce->history_index - (i + 1);
848 src_index = src_index * num_ds;
850 dst_index = i * num_ds;
852 memcpy (ret_history + dst_index, ce->history + src_index,
853 sizeof (*ret_history) * num_ds);
854 }
856 pthread_mutex_unlock (&cache_lock);
858 return (0);
859 } /* int uc_get_history_by_name */
861 int uc_get_history (const data_set_t *ds, const value_list_t *vl,
862 gauge_t *ret_history, size_t num_steps, size_t num_ds)
863 {
864 char name[6 * DATA_MAX_NAME_LEN];
866 if (FORMAT_VL (name, sizeof (name), vl, ds) != 0)
867 {
868 ERROR ("utils_cache: uc_get_history: FORMAT_VL failed.");
869 return (-1);
870 }
872 return (uc_get_history_by_name (name, ret_history, num_steps, num_ds));
873 } /* int uc_get_history */
875 /* vim: set sw=2 ts=8 sts=2 tw=78 : */