oprofile_files.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * @file oprofile_files.c
  3. *
  4. * @remark Copyright 2002 OProfile authors
  5. * @remark Read the file COPYING
  6. *
  7. * @author John Levon <levon@movementarian.org>
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/oprofile.h>
  11. #include <linux/jiffies.h>
  12. #include "event_buffer.h"
  13. #include "oprofile_stats.h"
  14. #include "oprof.h"
  15. #define BUFFER_SIZE_DEFAULT 131072
  16. #define CPU_BUFFER_SIZE_DEFAULT 8192
  17. #define BUFFER_WATERSHED_DEFAULT 32768 /* FIXME: tune */
  18. #define TIME_SLICE_DEFAULT 1
  19. unsigned long oprofile_buffer_size;
  20. unsigned long oprofile_cpu_buffer_size;
  21. unsigned long oprofile_buffer_watershed;
  22. unsigned long oprofile_time_slice;
  23. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  24. static ssize_t timeout_read(struct file *file, char __user *buf,
  25. size_t count, loff_t *offset)
  26. {
  27. return oprofilefs_ulong_to_user(jiffies_to_msecs(oprofile_time_slice),
  28. buf, count, offset);
  29. }
  30. static ssize_t timeout_write(struct file *file, char const __user *buf,
  31. size_t count, loff_t *offset)
  32. {
  33. unsigned long val;
  34. int retval;
  35. if (*offset)
  36. return -EINVAL;
  37. retval = oprofilefs_ulong_from_user(&val, buf, count);
  38. if (retval <= 0)
  39. return retval;
  40. retval = oprofile_set_timeout(val);
  41. if (retval)
  42. return retval;
  43. return count;
  44. }
  45. static const struct file_operations timeout_fops = {
  46. .read = timeout_read,
  47. .write = timeout_write,
  48. .llseek = default_llseek,
  49. };
  50. #endif
  51. static ssize_t depth_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  52. {
  53. return oprofilefs_ulong_to_user(oprofile_backtrace_depth, buf, count,
  54. offset);
  55. }
  56. static ssize_t depth_write(struct file *file, char const __user *buf, size_t count, loff_t *offset)
  57. {
  58. unsigned long val;
  59. int retval;
  60. if (*offset)
  61. return -EINVAL;
  62. if (!oprofile_ops.backtrace)
  63. return -EINVAL;
  64. retval = oprofilefs_ulong_from_user(&val, buf, count);
  65. if (retval <= 0)
  66. return retval;
  67. retval = oprofile_set_ulong(&oprofile_backtrace_depth, val);
  68. if (retval)
  69. return retval;
  70. return count;
  71. }
  72. static const struct file_operations depth_fops = {
  73. .read = depth_read,
  74. .write = depth_write,
  75. .llseek = default_llseek,
  76. };
  77. static ssize_t pointer_size_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  78. {
  79. return oprofilefs_ulong_to_user(sizeof(void *), buf, count, offset);
  80. }
  81. static const struct file_operations pointer_size_fops = {
  82. .read = pointer_size_read,
  83. .llseek = default_llseek,
  84. };
  85. static ssize_t cpu_type_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  86. {
  87. return oprofilefs_str_to_user(oprofile_ops.cpu_type, buf, count, offset);
  88. }
  89. static const struct file_operations cpu_type_fops = {
  90. .read = cpu_type_read,
  91. .llseek = default_llseek,
  92. };
  93. static ssize_t enable_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  94. {
  95. return oprofilefs_ulong_to_user(oprofile_started, buf, count, offset);
  96. }
  97. static ssize_t enable_write(struct file *file, char const __user *buf, size_t count, loff_t *offset)
  98. {
  99. unsigned long val;
  100. int retval;
  101. if (*offset)
  102. return -EINVAL;
  103. retval = oprofilefs_ulong_from_user(&val, buf, count);
  104. if (retval <= 0)
  105. return retval;
  106. retval = 0;
  107. if (val)
  108. retval = oprofile_start();
  109. else
  110. oprofile_stop();
  111. if (retval)
  112. return retval;
  113. return count;
  114. }
  115. static const struct file_operations enable_fops = {
  116. .read = enable_read,
  117. .write = enable_write,
  118. .llseek = default_llseek,
  119. };
  120. static ssize_t dump_write(struct file *file, char const __user *buf, size_t count, loff_t *offset)
  121. {
  122. wake_up_buffer_waiter();
  123. return count;
  124. }
  125. static const struct file_operations dump_fops = {
  126. .write = dump_write,
  127. .llseek = noop_llseek,
  128. };
  129. void oprofile_create_files(struct dentry *root)
  130. {
  131. /* reinitialize default values */
  132. oprofile_buffer_size = BUFFER_SIZE_DEFAULT;
  133. oprofile_cpu_buffer_size = CPU_BUFFER_SIZE_DEFAULT;
  134. oprofile_buffer_watershed = BUFFER_WATERSHED_DEFAULT;
  135. oprofile_time_slice = msecs_to_jiffies(TIME_SLICE_DEFAULT);
  136. oprofilefs_create_file(root, "enable", &enable_fops);
  137. oprofilefs_create_file_perm(root, "dump", &dump_fops, 0666);
  138. oprofilefs_create_file(root, "buffer", &event_buffer_fops);
  139. oprofilefs_create_ulong(root, "buffer_size", &oprofile_buffer_size);
  140. oprofilefs_create_ulong(root, "buffer_watershed", &oprofile_buffer_watershed);
  141. oprofilefs_create_ulong(root, "cpu_buffer_size", &oprofile_cpu_buffer_size);
  142. oprofilefs_create_file(root, "cpu_type", &cpu_type_fops);
  143. oprofilefs_create_file(root, "backtrace_depth", &depth_fops);
  144. oprofilefs_create_file(root, "pointer_size", &pointer_size_fops);
  145. #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
  146. oprofilefs_create_file(root, "time_slice", &timeout_fops);
  147. #endif
  148. oprofile_create_stats_files(root);
  149. if (oprofile_ops.create_files)
  150. oprofile_ops.create_files(root);
  151. }