InDomTable.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. htop - InDomTable.c
  3. (C) 2023 htop dev team
  4. (C) 2022-2023 Sohaib Mohammed
  5. Released under the GNU GPLv2+, see the COPYING file
  6. in the source distribution for its full text.
  7. */
  8. #include "config.h" // IWYU pragma: keep
  9. #include "pcp/InDomTable.h"
  10. #include <assert.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "CRT.h"
  14. #include "DynamicColumn.h"
  15. #include "Hashtable.h"
  16. #include "Macros.h"
  17. #include "Platform.h"
  18. #include "Table.h"
  19. #include "Vector.h"
  20. #include "XUtils.h"
  21. #include "pcp/Instance.h"
  22. #include "pcp/Metric.h"
  23. #include "pcp/PCPDynamicColumn.h"
  24. InDomTable* InDomTable_new(Machine* host, pmInDom indom, int metricKey) {
  25. InDomTable* this = xCalloc(1, sizeof(InDomTable));
  26. Object_setClass(this, Class(InDomTable));
  27. this->metricKey = metricKey;
  28. this->id = indom;
  29. Table* super = &this->super;
  30. Table_init(super, Class(Row), host);
  31. return this;
  32. }
  33. void InDomTable_done(InDomTable* this) {
  34. Table_done(&this->super);
  35. }
  36. static void InDomTable_delete(Object* cast) {
  37. InDomTable* this = (InDomTable*) cast;
  38. InDomTable_done(this);
  39. free(this);
  40. }
  41. static Instance* InDomTable_getInstance(InDomTable* this, int id, bool* preExisting) {
  42. const Table* super = &this->super;
  43. Instance* inst = (Instance*) Hashtable_get(super->table, id);
  44. *preExisting = inst != NULL;
  45. if (inst) {
  46. assert(Vector_indexOf(super->rows, inst, Row_idEqualCompare) != -1);
  47. assert(Instance_getId(inst) == id);
  48. } else {
  49. inst = Instance_new(super->host, this);
  50. assert(inst->name == NULL);
  51. Instance_setId(inst, id);
  52. }
  53. return inst;
  54. }
  55. static void InDomTable_goThroughEntries(InDomTable* this) {
  56. Table* super = &this->super;
  57. /* for every instance ... */
  58. int instid = -1, offset = -1;
  59. while (Metric_iterate(this->metricKey, &instid, &offset)) {
  60. bool preExisting;
  61. Instance* inst = InDomTable_getInstance(this, instid, &preExisting);
  62. inst->offset = offset >= 0 ? offset : 0;
  63. Row* row = (Row*) inst;
  64. if (!preExisting)
  65. Table_add(super, row);
  66. row->updated = true;
  67. row->show = true;
  68. }
  69. }
  70. static void InDomTable_iterateEntries(Table* super) {
  71. InDomTable* this = (InDomTable*) super;
  72. InDomTable_goThroughEntries(this);
  73. }
  74. const TableClass InDomTable_class = {
  75. .super = {
  76. .extends = Class(Table),
  77. .delete = InDomTable_delete,
  78. },
  79. .prepare = Table_prepareEntries,
  80. .iterate = InDomTable_iterateEntries,
  81. .cleanup = Table_cleanupEntries,
  82. };