openzfs_sysctl.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. htop - generic/openzfs_sysctl.c
  3. (C) 2014 Hisham H. Muhammad
  4. Released under the GNU GPLv2+, see the COPYING file
  5. in the source distribution for its full text.
  6. */
  7. #include "generic/openzfs_sysctl.h"
  8. #include <stdlib.h>
  9. #include <sys/types.h> // IWYU pragma: keep
  10. #include <sys/sysctl.h> // needs <sys/types.h> for u_int with gcc
  11. #include "zfs/ZfsArcStats.h"
  12. static int MIB_kstat_zfs_misc_arcstats_size[5];
  13. static int MIB_kstat_zfs_misc_arcstats_c_min[5];
  14. static int MIB_kstat_zfs_misc_arcstats_c_max[5];
  15. static int MIB_kstat_zfs_misc_arcstats_mfu_size[5];
  16. static int MIB_kstat_zfs_misc_arcstats_mru_size[5];
  17. static int MIB_kstat_zfs_misc_arcstats_anon_size[5];
  18. static int MIB_kstat_zfs_misc_arcstats_hdr_size[5];
  19. static int MIB_kstat_zfs_misc_arcstats_other_size[5];
  20. static int MIB_kstat_zfs_misc_arcstats_compressed_size[5];
  21. static int MIB_kstat_zfs_misc_arcstats_uncompressed_size[5];
  22. void openzfs_sysctl_init(ZfsArcStats* stats) {
  23. size_t len;
  24. unsigned long long int arcSize;
  25. len = sizeof(arcSize);
  26. if (sysctlbyname("kstat.zfs.misc.arcstats.size", &arcSize, &len, NULL, 0) == 0 && arcSize != 0) {
  27. stats->enabled = 1;
  28. len = 5;
  29. sysctlnametomib("kstat.zfs.misc.arcstats.size", MIB_kstat_zfs_misc_arcstats_size, &len);
  30. sysctlnametomib("kstat.zfs.misc.arcstats.c_min", MIB_kstat_zfs_misc_arcstats_c_min, &len);
  31. sysctlnametomib("kstat.zfs.misc.arcstats.c_max", MIB_kstat_zfs_misc_arcstats_c_max, &len);
  32. sysctlnametomib("kstat.zfs.misc.arcstats.mfu_size", MIB_kstat_zfs_misc_arcstats_mfu_size, &len);
  33. sysctlnametomib("kstat.zfs.misc.arcstats.mru_size", MIB_kstat_zfs_misc_arcstats_mru_size, &len);
  34. sysctlnametomib("kstat.zfs.misc.arcstats.anon_size", MIB_kstat_zfs_misc_arcstats_anon_size, &len);
  35. sysctlnametomib("kstat.zfs.misc.arcstats.hdr_size", MIB_kstat_zfs_misc_arcstats_hdr_size, &len);
  36. sysctlnametomib("kstat.zfs.misc.arcstats.other_size", MIB_kstat_zfs_misc_arcstats_other_size, &len);
  37. if (sysctlnametomib("kstat.zfs.misc.arcstats.compressed_size", MIB_kstat_zfs_misc_arcstats_compressed_size, &len) == 0) {
  38. stats->isCompressed = 1;
  39. sysctlnametomib("kstat.zfs.misc.arcstats.uncompressed_size", MIB_kstat_zfs_misc_arcstats_uncompressed_size, &len);
  40. } else {
  41. stats->isCompressed = 0;
  42. }
  43. } else {
  44. stats->enabled = 0;
  45. }
  46. }
  47. void openzfs_sysctl_updateArcStats(ZfsArcStats* stats) {
  48. size_t len;
  49. if (stats->enabled) {
  50. len = sizeof(stats->size);
  51. sysctl(MIB_kstat_zfs_misc_arcstats_size, 5, &(stats->size), &len, NULL, 0);
  52. stats->size /= 1024;
  53. len = sizeof(stats->min);
  54. sysctl(MIB_kstat_zfs_misc_arcstats_c_min, 5, &(stats->min), &len, NULL, 0);
  55. stats->min /= 1024;
  56. len = sizeof(stats->max);
  57. sysctl(MIB_kstat_zfs_misc_arcstats_c_max, 5, &(stats->max), &len, NULL, 0);
  58. stats->max /= 1024;
  59. len = sizeof(stats->MFU);
  60. sysctl(MIB_kstat_zfs_misc_arcstats_mfu_size, 5, &(stats->MFU), &len, NULL, 0);
  61. stats->MFU /= 1024;
  62. len = sizeof(stats->MRU);
  63. sysctl(MIB_kstat_zfs_misc_arcstats_mru_size, 5, &(stats->MRU), &len, NULL, 0);
  64. stats->MRU /= 1024;
  65. len = sizeof(stats->anon);
  66. sysctl(MIB_kstat_zfs_misc_arcstats_anon_size, 5, &(stats->anon), &len, NULL, 0);
  67. stats->anon /= 1024;
  68. len = sizeof(stats->header);
  69. sysctl(MIB_kstat_zfs_misc_arcstats_hdr_size, 5, &(stats->header), &len, NULL, 0);
  70. stats->header /= 1024;
  71. len = sizeof(stats->other);
  72. sysctl(MIB_kstat_zfs_misc_arcstats_other_size, 5, &(stats->other), &len, NULL, 0);
  73. stats->other /= 1024;
  74. if (stats->isCompressed) {
  75. len = sizeof(stats->compressed);
  76. sysctl(MIB_kstat_zfs_misc_arcstats_compressed_size, 5, &(stats->compressed), &len, NULL, 0);
  77. stats->compressed /= 1024;
  78. len = sizeof(stats->uncompressed);
  79. sysctl(MIB_kstat_zfs_misc_arcstats_uncompressed_size, 5, &(stats->uncompressed), &len, NULL, 0);
  80. stats->uncompressed /= 1024;
  81. }
  82. }
  83. }