hwsampler_files.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * arch/s390/oprofile/hwsampler_files.c
  3. *
  4. * Copyright IBM Corp. 2010
  5. * Author: Mahesh Salgaonkar (mahesh@linux.vnet.ibm.com)
  6. */
  7. #include <linux/oprofile.h>
  8. #include <linux/errno.h>
  9. #include <linux/fs.h>
  10. #include "../../../drivers/oprofile/oprof.h"
  11. #include "hwsampler.h"
  12. #define DEFAULT_INTERVAL 4096
  13. #define DEFAULT_SDBT_BLOCKS 1
  14. #define DEFAULT_SDB_BLOCKS 511
  15. static unsigned long oprofile_hw_interval = DEFAULT_INTERVAL;
  16. static unsigned long oprofile_min_interval;
  17. static unsigned long oprofile_max_interval;
  18. static unsigned long oprofile_sdbt_blocks = DEFAULT_SDBT_BLOCKS;
  19. static unsigned long oprofile_sdb_blocks = DEFAULT_SDB_BLOCKS;
  20. static int hwsampler_file;
  21. static int hwsampler_running; /* start_mutex must be held to change */
  22. static struct oprofile_operations timer_ops;
  23. static int oprofile_hwsampler_start(void)
  24. {
  25. int retval;
  26. hwsampler_running = hwsampler_file;
  27. if (!hwsampler_running)
  28. return timer_ops.start();
  29. retval = hwsampler_allocate(oprofile_sdbt_blocks, oprofile_sdb_blocks);
  30. if (retval)
  31. return retval;
  32. retval = hwsampler_start_all(oprofile_hw_interval);
  33. if (retval)
  34. hwsampler_deallocate();
  35. return retval;
  36. }
  37. static void oprofile_hwsampler_stop(void)
  38. {
  39. if (!hwsampler_running) {
  40. timer_ops.stop();
  41. return;
  42. }
  43. hwsampler_stop_all();
  44. hwsampler_deallocate();
  45. return;
  46. }
  47. static ssize_t hwsampler_read(struct file *file, char __user *buf,
  48. size_t count, loff_t *offset)
  49. {
  50. return oprofilefs_ulong_to_user(hwsampler_file, buf, count, offset);
  51. }
  52. static ssize_t hwsampler_write(struct file *file, char const __user *buf,
  53. size_t count, loff_t *offset)
  54. {
  55. unsigned long val;
  56. int retval;
  57. if (*offset)
  58. return -EINVAL;
  59. retval = oprofilefs_ulong_from_user(&val, buf, count);
  60. if (retval)
  61. return retval;
  62. if (oprofile_started)
  63. /*
  64. * save to do without locking as we set
  65. * hwsampler_running in start() when start_mutex is
  66. * held
  67. */
  68. return -EBUSY;
  69. hwsampler_file = val;
  70. return count;
  71. }
  72. static const struct file_operations hwsampler_fops = {
  73. .read = hwsampler_read,
  74. .write = hwsampler_write,
  75. };
  76. static int oprofile_create_hwsampling_files(struct super_block *sb,
  77. struct dentry *root)
  78. {
  79. struct dentry *hw_dir;
  80. /* reinitialize default values */
  81. hwsampler_file = 1;
  82. hw_dir = oprofilefs_mkdir(sb, root, "hwsampling");
  83. if (!hw_dir)
  84. return -EINVAL;
  85. oprofilefs_create_file(sb, hw_dir, "hwsampler", &hwsampler_fops);
  86. oprofilefs_create_ulong(sb, hw_dir, "hw_interval",
  87. &oprofile_hw_interval);
  88. oprofilefs_create_ro_ulong(sb, hw_dir, "hw_min_interval",
  89. &oprofile_min_interval);
  90. oprofilefs_create_ro_ulong(sb, hw_dir, "hw_max_interval",
  91. &oprofile_max_interval);
  92. oprofilefs_create_ulong(sb, hw_dir, "hw_sdbt_blocks",
  93. &oprofile_sdbt_blocks);
  94. return 0;
  95. }
  96. int oprofile_hwsampler_init(struct oprofile_operations* ops)
  97. {
  98. if (hwsampler_setup())
  99. return -ENODEV;
  100. /*
  101. * create hwsampler files only if hwsampler_setup() succeeds.
  102. */
  103. oprofile_min_interval = hwsampler_query_min_interval();
  104. if (oprofile_min_interval < 0) {
  105. oprofile_min_interval = 0;
  106. return -ENODEV;
  107. }
  108. oprofile_max_interval = hwsampler_query_max_interval();
  109. if (oprofile_max_interval < 0) {
  110. oprofile_max_interval = 0;
  111. return -ENODEV;
  112. }
  113. if (oprofile_timer_init(ops))
  114. return -ENODEV;
  115. printk(KERN_INFO "oprofile: using hardware sampling\n");
  116. memcpy(&timer_ops, ops, sizeof(timer_ops));
  117. ops->start = oprofile_hwsampler_start;
  118. ops->stop = oprofile_hwsampler_stop;
  119. ops->create_files = oprofile_create_hwsampling_files;
  120. return 0;
  121. }
  122. void oprofile_hwsampler_exit(void)
  123. {
  124. oprofile_timer_exit();
  125. hwsampler_shutdown();
  126. }