cpu_buffer.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @file cpu_buffer.h
  3. *
  4. * @remark Copyright 2002 OProfile authors
  5. * @remark Read the file COPYING
  6. *
  7. * @author John Levon <levon@movementarian.org>
  8. */
  9. #ifndef OPROFILE_CPU_BUFFER_H
  10. #define OPROFILE_CPU_BUFFER_H
  11. #include <linux/types.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/cache.h>
  15. #include <linux/sched.h>
  16. struct task_struct;
  17. int alloc_cpu_buffers(void);
  18. void free_cpu_buffers(void);
  19. void start_cpu_work(void);
  20. void end_cpu_work(void);
  21. /* CPU buffer is composed of such entries (which are
  22. * also used for context switch notes)
  23. */
  24. struct op_sample {
  25. unsigned long eip;
  26. unsigned long event;
  27. };
  28. struct oprofile_cpu_buffer {
  29. volatile unsigned long head_pos;
  30. volatile unsigned long tail_pos;
  31. unsigned long buffer_size;
  32. struct task_struct *last_task;
  33. int last_is_kernel;
  34. int tracing;
  35. struct op_sample *buffer;
  36. unsigned long sample_received;
  37. unsigned long sample_lost_overflow;
  38. unsigned long backtrace_aborted;
  39. unsigned long sample_invalid_eip;
  40. int cpu;
  41. struct delayed_work work;
  42. };
  43. DECLARE_PER_CPU(struct oprofile_cpu_buffer, cpu_buffer);
  44. /*
  45. * Resets the cpu buffer to a sane state.
  46. *
  47. * reset these to invalid values; the next sample collected will
  48. * populate the buffer with proper values to initialize the buffer
  49. */
  50. static inline void cpu_buffer_reset(int cpu)
  51. {
  52. struct oprofile_cpu_buffer *cpu_buf = &per_cpu(cpu_buffer, cpu);
  53. cpu_buf->last_is_kernel = -1;
  54. cpu_buf->last_task = NULL;
  55. }
  56. static inline
  57. struct op_sample *cpu_buffer_write_entry(struct oprofile_cpu_buffer *cpu_buf)
  58. {
  59. return &cpu_buf->buffer[cpu_buf->head_pos];
  60. }
  61. static inline
  62. void cpu_buffer_write_commit(struct oprofile_cpu_buffer *b)
  63. {
  64. unsigned long new_head = b->head_pos + 1;
  65. /*
  66. * Ensure anything written to the slot before we increment is
  67. * visible
  68. */
  69. wmb();
  70. if (new_head < b->buffer_size)
  71. b->head_pos = new_head;
  72. else
  73. b->head_pos = 0;
  74. }
  75. static inline
  76. struct op_sample *cpu_buffer_read_entry(struct oprofile_cpu_buffer *cpu_buf)
  77. {
  78. return &cpu_buf->buffer[cpu_buf->tail_pos];
  79. }
  80. /* "acquire" as many cpu buffer slots as we can */
  81. static inline
  82. unsigned long cpu_buffer_entries(struct oprofile_cpu_buffer *b)
  83. {
  84. unsigned long head = b->head_pos;
  85. unsigned long tail = b->tail_pos;
  86. if (head >= tail)
  87. return head - tail;
  88. return head + (b->buffer_size - tail);
  89. }
  90. /* transient events for the CPU buffer -> event buffer */
  91. #define CPU_IS_KERNEL 1
  92. #define CPU_TRACE_BEGIN 2
  93. #define IBS_FETCH_BEGIN 3
  94. #define IBS_OP_BEGIN 4
  95. #endif /* OPROFILE_CPU_BUFFER_H */