1. #include <inttypes.h>
  2. #include <kstat.h>
  3. #include <stdio.h>
  4. #include <sys/sysinfo.h>
  5. #include <unistd.h>
  6. int main(int argc, char **argv)
  7. {
  8. //RCW: Open the chain, kstats are accessible via a linked list
  9. // kc is a fully initialized kstat structure.
  10. kstat_ctl_t *kc = kstat_open();
  11. if (kc == NULL)
  12. {
  13. fprintf(stderr, "\terror opening kstat\n");
  14. return -1;
  15. }
  16. // get number of statistic updates so far (=secs since boot)
  17. kstat_t *sysinfo_ksp = kstat_lookup(kc, "unix", 0, "sysinfo");
  18. if (sysinfo_ksp == NULL)
  19. {
  20. fprintf(stderr, "\terror lookingup sysinfo\n");
  21. return -1;
  22. }
  23. sysinfo_t sysinfo;
  24. if (kstat_read(kc, sysinfo_ksp, &sysinfo) == -1)
  25. {
  26. fprintf(stderr, "\terror reading kstat for sysinfo\n");
  27. return -1;
  28. }
  29. kstat_t *vminfo_ksp = kstat_lookup(kc, "unix", -1, "vminfo");
  30. if (vminfo_ksp == NULL)
  31. {
  32. fprintf(stderr, "\terror lookingup vminfo\n");
  33. return -1;
  34. }
  35. vminfo_t vminfo;
  36. if (kstat_read(kc, vminfo_ksp, &vminfo) == -1)
  37. {
  38. fprintf(stderr, "\terror reading kstat for vminfo\n");
  39. return -1;
  40. }
  41. printf("swap_free with updates:\t%" PRIu64 "\n", (vminfo.swap_free * PAGESIZE) / sysinfo.updates);
  42. // get lbolt and divide by hz to get secs since boot
  43. kstat_t *system_misc_ksp = kstat_lookup(kc, "unix", 0, "system_misc");
  44. if (system_misc_ksp == NULL)
  45. {
  46. fprintf(stderr, "\terror lookingup system_misc_ksp\n");
  47. return -1;
  48. }
  49. if (kstat_read(kc, system_misc_ksp, NULL) == -1)
  50. {
  51. fprintf(stderr, "\terror reading kstat for system_misc\n");
  52. return -1;
  53. }
  54. kstat_named_t *lbolt_knp = kstat_data_lookup(system_misc_ksp, "lbolt");
  55. if (lbolt_knp == NULL)
  56. {
  57. fprintf(stderr, "\terror lookingup lbolt\n");
  58. return -1;
  59. }
  60. int hz = sysconf(_SC_CLK_TCK);
  61. uint32_t ltime = lbolt_knp->value.l / hz;
  62. printf("swap_free with ltime:\t%" PRIu64 "\n", (vminfo.swap_free * PAGESIZE) / ltime);
  63. kstat_close(kc);
  64. return 0;
  65. }

swap_free on solaris

Comments powered by Disqus