cpu_buffer.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @file cpu_buffer.h
  3. *
  4. * @remark Copyright 2002-2009 OProfile authors
  5. * @remark Read the file COPYING
  6. *
  7. * @author John Levon <levon@movementarian.org>
  8. * @author Robert Richter <robert.richter@amd.com>
  9. */
  10. #ifndef OPROFILE_CPU_BUFFER_H
  11. #define OPROFILE_CPU_BUFFER_H
  12. #include <linux/types.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/cache.h>
  16. #include <linux/sched.h>
  17. #include <linux/ring_buffer.h>
  18. struct task_struct;
  19. int alloc_cpu_buffers(void);
  20. void free_cpu_buffers(void);
  21. void start_cpu_work(void);
  22. void end_cpu_work(void);
  23. /* CPU buffer is composed of such entries (which are
  24. * also used for context switch notes)
  25. */
  26. struct op_sample {
  27. unsigned long eip;
  28. unsigned long event;
  29. unsigned long data[0];
  30. };
  31. struct op_entry;
  32. struct oprofile_cpu_buffer {
  33. unsigned long buffer_size;
  34. struct task_struct *last_task;
  35. int last_is_kernel;
  36. int tracing;
  37. unsigned long sample_received;
  38. unsigned long sample_lost_overflow;
  39. unsigned long backtrace_aborted;
  40. unsigned long sample_invalid_eip;
  41. int cpu;
  42. struct delayed_work work;
  43. };
  44. DECLARE_PER_CPU(struct oprofile_cpu_buffer, cpu_buffer);
  45. /*
  46. * Resets the cpu buffer to a sane state.
  47. *
  48. * reset these to invalid values; the next sample collected will
  49. * populate the buffer with proper values to initialize the buffer
  50. */
  51. static inline void op_cpu_buffer_reset(int cpu)
  52. {
  53. struct oprofile_cpu_buffer *cpu_buf = &per_cpu(cpu_buffer, cpu);
  54. cpu_buf->last_is_kernel = -1;
  55. cpu_buf->last_task = NULL;
  56. }
  57. /*
  58. * op_cpu_buffer_add_data() and op_cpu_buffer_write_commit() may be
  59. * called only if op_cpu_buffer_write_reserve() did not return NULL or
  60. * entry->event != NULL, otherwise entry->size or entry->event will be
  61. * used uninitialized.
  62. */
  63. struct op_sample
  64. *op_cpu_buffer_write_reserve(struct op_entry *entry, unsigned long size);
  65. int op_cpu_buffer_write_commit(struct op_entry *entry);
  66. struct op_sample *op_cpu_buffer_read_entry(struct op_entry *entry, int cpu);
  67. unsigned long op_cpu_buffer_entries(int cpu);
  68. /* returns the remaining free size of data in the entry */
  69. static inline
  70. int op_cpu_buffer_add_data(struct op_entry *entry, unsigned long val)
  71. {
  72. if (!entry->size)
  73. return 0;
  74. *entry->data = val;
  75. entry->size--;
  76. entry->data++;
  77. return entry->size;
  78. }
  79. /* returns the size of data in the entry */
  80. static inline
  81. int op_cpu_buffer_get_size(struct op_entry *entry)
  82. {
  83. return entry->size;
  84. }
  85. /* returns 0 if empty or the size of data including the current value */
  86. static inline
  87. int op_cpu_buffer_get_data(struct op_entry *entry, unsigned long *val)
  88. {
  89. int size = entry->size;
  90. if (!size)
  91. return 0;
  92. *val = *entry->data;
  93. entry->size--;
  94. entry->data++;
  95. return size;
  96. }
  97. /* extra data flags */
  98. #define KERNEL_CTX_SWITCH (1UL << 0)
  99. #define IS_KERNEL (1UL << 1)
  100. #define TRACE_BEGIN (1UL << 2)
  101. #define USER_CTX_SWITCH (1UL << 3)
  102. #endif /* OPROFILE_CPU_BUFFER_H */