proc_stat.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include "proc.h"
  10. #include "procfs.h"
  11. #include <rthw.h>
  12. #include <rtdbg.h>
  13. #include <fcntl.h>
  14. #include <errno.h>
  15. #include <dfs_dentry.h>
  16. static void *seq_start(struct dfs_seq_file *seq, off_t *index)
  17. {
  18. off_t i = *index; // seq->index
  19. return NULL + (i == 0);
  20. }
  21. static void seq_stop(struct dfs_seq_file *seq, void *data)
  22. {
  23. }
  24. static void *seq_next(struct dfs_seq_file *seq, void *data, off_t *index)
  25. {
  26. /* data: The return value of the start or next*/
  27. off_t i = *index + 1; // seq->index
  28. *index = i;
  29. return NULL;
  30. }
  31. static int seq_show(struct dfs_seq_file *seq, void *data)
  32. {
  33. int i;
  34. rt_cpu_t pcpu;
  35. rt_uint64_t user_total = 0;
  36. rt_uint64_t system_total = 0;
  37. rt_uint64_t idle_total = 0;
  38. for (i = 0; i < RT_CPUS_NR; i++)
  39. {
  40. pcpu = rt_cpu_index(i);
  41. user_total = user_total + pcpu->cpu_stat.user;
  42. system_total = system_total + pcpu->cpu_stat.system;
  43. idle_total = idle_total + pcpu->cpu_stat.idle;
  44. }
  45. dfs_seq_printf(seq, "cpu %llu 0 %llu %llu 0 0 0 0 0 0\n", user_total, system_total, idle_total);
  46. for (i = 0; i < RT_CPUS_NR; i++)
  47. {
  48. pcpu = rt_cpu_index(i);
  49. dfs_seq_printf(seq, "cpu%d ",i);
  50. dfs_seq_printf(seq, "%llu ",pcpu->cpu_stat.user);//user
  51. dfs_seq_printf(seq, "0 ");//nice
  52. dfs_seq_printf(seq, "%llu ",pcpu->cpu_stat.system);//system
  53. dfs_seq_printf(seq, "%llu ",pcpu->cpu_stat.idle);//idle
  54. dfs_seq_printf(seq, "0 ");//iowait
  55. dfs_seq_printf(seq, "0 ");//irq
  56. dfs_seq_printf(seq, "0 ");//softirq
  57. dfs_seq_printf(seq, "0 0 0\n");//steal,guest,guest_nice
  58. }
  59. return 0;
  60. }
  61. static const struct dfs_seq_ops seq_ops = {
  62. .start = seq_start,
  63. .stop = seq_stop,
  64. .next = seq_next,
  65. .show = seq_show,
  66. };
  67. rt_weak const struct dfs_seq_ops *stat_get_seq_ops(void)
  68. {
  69. return &seq_ops;
  70. }
  71. static int proc_open(struct dfs_file *file)
  72. {
  73. return dfs_seq_open(file, stat_get_seq_ops());
  74. }
  75. static int proc_close(struct dfs_file *file)
  76. {
  77. return dfs_seq_release(file);
  78. }
  79. static const struct dfs_file_ops file_ops = {
  80. .open = proc_open,
  81. .read = dfs_seq_read,
  82. .lseek = dfs_seq_lseek,
  83. .close = proc_close,
  84. };
  85. int proc_stat_init(void)
  86. {
  87. struct proc_dentry *dentry = proc_create_data("stat", 0, NULL, &file_ops, NULL);
  88. proc_release(dentry);
  89. return 0;
  90. }
  91. INIT_ENV_EXPORT(proc_stat_init);