rrdfamily.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #include "rrd.h"
  4. // ----------------------------------------------------------------------------
  5. // RRDFAMILY index
  6. int rrdfamily_compare(void *a, void *b) {
  7. if(((RRDFAMILY *)a)->hash_family < ((RRDFAMILY *)b)->hash_family) return -1;
  8. else if(((RRDFAMILY *)a)->hash_family > ((RRDFAMILY *)b)->hash_family) return 1;
  9. else return strcmp(((RRDFAMILY *)a)->family, ((RRDFAMILY *)b)->family);
  10. }
  11. #define rrdfamily_index_add(host, rc) (RRDFAMILY *)avl_insert_lock(&((host)->rrdfamily_root_index), (avl_t *)(rc))
  12. #define rrdfamily_index_del(host, rc) (RRDFAMILY *)avl_remove_lock(&((host)->rrdfamily_root_index), (avl_t *)(rc))
  13. static RRDFAMILY *rrdfamily_index_find(RRDHOST *host, const char *id, uint32_t hash) {
  14. RRDFAMILY tmp;
  15. tmp.family = id;
  16. tmp.hash_family = (hash)?hash:simple_hash(tmp.family);
  17. return (RRDFAMILY *)avl_search_lock(&(host->rrdfamily_root_index), (avl_t *) &tmp);
  18. }
  19. RRDFAMILY *rrdfamily_create(RRDHOST *host, const char *id) {
  20. RRDFAMILY *rc = rrdfamily_index_find(host, id, 0);
  21. if(!rc) {
  22. rc = callocz(1, sizeof(RRDFAMILY));
  23. rc->family = strdupz(id);
  24. rc->hash_family = simple_hash(rc->family);
  25. // initialize the variables index
  26. avl_init_lock(&rc->rrdvar_root_index, rrdvar_compare);
  27. RRDFAMILY *ret = rrdfamily_index_add(host, rc);
  28. if(ret != rc)
  29. error("RRDFAMILY: INTERNAL ERROR: Expected to INSERT RRDFAMILY '%s' into index, but inserted '%s'.", rc->family, (ret)?ret->family:"NONE");
  30. }
  31. rc->use_count++;
  32. return rc;
  33. }
  34. void rrdfamily_free(RRDHOST *host, RRDFAMILY *rc) {
  35. rc->use_count--;
  36. if(!rc->use_count) {
  37. RRDFAMILY *ret = rrdfamily_index_del(host, rc);
  38. if(ret != rc)
  39. error("RRDFAMILY: INTERNAL ERROR: Expected to DELETE RRDFAMILY '%s' from index, but deleted '%s'.", rc->family, (ret)?ret->family:"NONE");
  40. else {
  41. debug(D_RRD_CALLS, "RRDFAMILY: Cleaning up remaining family variables for host '%s', family '%s'", host->hostname, rc->family);
  42. rrdvar_free_remaining_variables(host, &rc->rrdvar_root_index);
  43. freez((void *) rc->family);
  44. freez(rc);
  45. }
  46. }
  47. }