event_buffer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * @file event_buffer.c
  3. *
  4. * @remark Copyright 2002 OProfile authors
  5. * @remark Read the file COPYING
  6. *
  7. * @author John Levon <levon@movementarian.org>
  8. *
  9. * This is the global event buffer that the user-space
  10. * daemon reads from. The event buffer is an untyped array
  11. * of unsigned longs. Entries are prefixed by the
  12. * escape value ESCAPE_CODE followed by an identifying code.
  13. */
  14. #include <linux/vmalloc.h>
  15. #include <linux/oprofile.h>
  16. #include <linux/sched.h>
  17. #include <linux/capability.h>
  18. #include <linux/dcookies.h>
  19. #include <linux/fs.h>
  20. #include <asm/uaccess.h>
  21. #include "oprof.h"
  22. #include "event_buffer.h"
  23. #include "oprofile_stats.h"
  24. DEFINE_MUTEX(buffer_mutex);
  25. static unsigned long buffer_opened;
  26. static DECLARE_WAIT_QUEUE_HEAD(buffer_wait);
  27. static unsigned long *event_buffer;
  28. static unsigned long buffer_size;
  29. static unsigned long buffer_watershed;
  30. static size_t buffer_pos;
  31. /* atomic_t because wait_event checks it outside of buffer_mutex */
  32. static atomic_t buffer_ready = ATOMIC_INIT(0);
  33. /* Add an entry to the event buffer. When we
  34. * get near to the end we wake up the process
  35. * sleeping on the read() of the file.
  36. */
  37. void add_event_entry(unsigned long value)
  38. {
  39. if (buffer_pos == buffer_size) {
  40. atomic_inc(&oprofile_stats.event_lost_overflow);
  41. return;
  42. }
  43. event_buffer[buffer_pos] = value;
  44. if (++buffer_pos == buffer_size - buffer_watershed) {
  45. atomic_set(&buffer_ready, 1);
  46. wake_up(&buffer_wait);
  47. }
  48. }
  49. /* Wake up the waiting process if any. This happens
  50. * on "echo 0 >/dev/oprofile/enable" so the daemon
  51. * processes the data remaining in the event buffer.
  52. */
  53. void wake_up_buffer_waiter(void)
  54. {
  55. mutex_lock(&buffer_mutex);
  56. atomic_set(&buffer_ready, 1);
  57. wake_up(&buffer_wait);
  58. mutex_unlock(&buffer_mutex);
  59. }
  60. int alloc_event_buffer(void)
  61. {
  62. int err = -ENOMEM;
  63. unsigned long flags;
  64. spin_lock_irqsave(&oprofilefs_lock, flags);
  65. buffer_size = oprofile_buffer_size;
  66. buffer_watershed = oprofile_buffer_watershed;
  67. spin_unlock_irqrestore(&oprofilefs_lock, flags);
  68. if (buffer_watershed >= buffer_size)
  69. return -EINVAL;
  70. event_buffer = vmalloc(sizeof(unsigned long) * buffer_size);
  71. if (!event_buffer)
  72. goto out;
  73. err = 0;
  74. out:
  75. return err;
  76. }
  77. void free_event_buffer(void)
  78. {
  79. vfree(event_buffer);
  80. event_buffer = NULL;
  81. }
  82. static int event_buffer_open(struct inode *inode, struct file *file)
  83. {
  84. int err = -EPERM;
  85. if (!capable(CAP_SYS_ADMIN))
  86. return -EPERM;
  87. if (test_and_set_bit_lock(0, &buffer_opened))
  88. return -EBUSY;
  89. /* Register as a user of dcookies
  90. * to ensure they persist for the lifetime of
  91. * the open event file
  92. */
  93. err = -EINVAL;
  94. file->private_data = dcookie_register();
  95. if (!file->private_data)
  96. goto out;
  97. if ((err = oprofile_setup()))
  98. goto fail;
  99. /* NB: the actual start happens from userspace
  100. * echo 1 >/dev/oprofile/enable
  101. */
  102. return 0;
  103. fail:
  104. dcookie_unregister(file->private_data);
  105. out:
  106. __clear_bit_unlock(0, &buffer_opened);
  107. return err;
  108. }
  109. static int event_buffer_release(struct inode *inode, struct file *file)
  110. {
  111. oprofile_stop();
  112. oprofile_shutdown();
  113. dcookie_unregister(file->private_data);
  114. buffer_pos = 0;
  115. atomic_set(&buffer_ready, 0);
  116. __clear_bit_unlock(0, &buffer_opened);
  117. return 0;
  118. }
  119. static ssize_t event_buffer_read(struct file *file, char __user *buf,
  120. size_t count, loff_t *offset)
  121. {
  122. int retval = -EINVAL;
  123. size_t const max = buffer_size * sizeof(unsigned long);
  124. /* handling partial reads is more trouble than it's worth */
  125. if (count != max || *offset)
  126. return -EINVAL;
  127. wait_event_interruptible(buffer_wait, atomic_read(&buffer_ready));
  128. if (signal_pending(current))
  129. return -EINTR;
  130. /* can't currently happen */
  131. if (!atomic_read(&buffer_ready))
  132. return -EAGAIN;
  133. mutex_lock(&buffer_mutex);
  134. atomic_set(&buffer_ready, 0);
  135. retval = -EFAULT;
  136. count = buffer_pos * sizeof(unsigned long);
  137. if (copy_to_user(buf, event_buffer, count))
  138. goto out;
  139. retval = count;
  140. buffer_pos = 0;
  141. out:
  142. mutex_unlock(&buffer_mutex);
  143. return retval;
  144. }
  145. const struct file_operations event_buffer_fops = {
  146. .open = event_buffer_open,
  147. .release = event_buffer_release,
  148. .read = event_buffer_read,
  149. };