oprofile.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * @file oprofile.h
  3. *
  4. * API for machine-specific interrupts to interface
  5. * to oprofile.
  6. *
  7. * @remark Copyright 2002 OProfile authors
  8. * @remark Read the file COPYING
  9. *
  10. * @author John Levon <levon@movementarian.org>
  11. */
  12. #ifndef OPROFILE_H
  13. #define OPROFILE_H
  14. #include <linux/types.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/init.h>
  17. #include <linux/errno.h>
  18. #include <linux/printk.h>
  19. #include <linux/atomic.h>
  20. /* Each escaped entry is prefixed by ESCAPE_CODE
  21. * then one of the following codes, then the
  22. * relevant data.
  23. * These #defines live in this file so that arch-specific
  24. * buffer sync'ing code can access them.
  25. */
  26. #define ESCAPE_CODE ~0UL
  27. #define CTX_SWITCH_CODE 1
  28. #define CPU_SWITCH_CODE 2
  29. #define COOKIE_SWITCH_CODE 3
  30. #define KERNEL_ENTER_SWITCH_CODE 4
  31. #define KERNEL_EXIT_SWITCH_CODE 5
  32. #define MODULE_LOADED_CODE 6
  33. #define CTX_TGID_CODE 7
  34. #define TRACE_BEGIN_CODE 8
  35. #define TRACE_END_CODE 9
  36. #define XEN_ENTER_SWITCH_CODE 10
  37. #define SPU_PROFILING_CODE 11
  38. #define SPU_CTX_SWITCH_CODE 12
  39. #define IBS_FETCH_CODE 13
  40. #define IBS_OP_CODE 14
  41. struct super_block;
  42. struct dentry;
  43. struct file_operations;
  44. struct pt_regs;
  45. /* Operations structure to be filled in */
  46. struct oprofile_operations {
  47. /* create any necessary configuration files in the oprofile fs.
  48. * Optional. */
  49. int (*create_files)(struct dentry * root);
  50. /* Do any necessary interrupt setup. Optional. */
  51. int (*setup)(void);
  52. /* Do any necessary interrupt shutdown. Optional. */
  53. void (*shutdown)(void);
  54. /* Start delivering interrupts. */
  55. int (*start)(void);
  56. /* Stop delivering interrupts. */
  57. void (*stop)(void);
  58. /* Arch-specific buffer sync functions.
  59. * Return value = 0: Success
  60. * Return value = -1: Failure
  61. * Return value = 1: Run generic sync function
  62. */
  63. int (*sync_start)(void);
  64. int (*sync_stop)(void);
  65. /* Initiate a stack backtrace. Optional. */
  66. void (*backtrace)(struct pt_regs * const regs, unsigned int depth);
  67. /* Multiplex between different events. Optional. */
  68. int (*switch_events)(void);
  69. /* CPU identification string. */
  70. char * cpu_type;
  71. };
  72. /**
  73. * One-time initialisation. *ops must be set to a filled-in
  74. * operations structure. This is called even in timer interrupt
  75. * mode so an arch can set a backtrace callback.
  76. *
  77. * If an error occurs, the fields should be left untouched.
  78. */
  79. int oprofile_arch_init(struct oprofile_operations * ops);
  80. /**
  81. * One-time exit/cleanup for the arch.
  82. */
  83. void oprofile_arch_exit(void);
  84. /**
  85. * Add a sample. This may be called from any context.
  86. */
  87. void oprofile_add_sample(struct pt_regs * const regs, unsigned long event);
  88. /**
  89. * Add an extended sample. Use this when the PC is not from the regs, and
  90. * we cannot determine if we're in kernel mode from the regs.
  91. *
  92. * This function does perform a backtrace.
  93. *
  94. */
  95. void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
  96. unsigned long event, int is_kernel);
  97. /**
  98. * Add an hardware sample.
  99. */
  100. void oprofile_add_ext_hw_sample(unsigned long pc, struct pt_regs * const regs,
  101. unsigned long event, int is_kernel,
  102. struct task_struct *task);
  103. /* Use this instead when the PC value is not from the regs. Doesn't
  104. * backtrace. */
  105. void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event);
  106. /* add a backtrace entry, to be called from the ->backtrace callback */
  107. void oprofile_add_trace(unsigned long eip);
  108. /**
  109. * Create a file of the given name as a child of the given root, with
  110. * the specified file operations.
  111. */
  112. int oprofilefs_create_file(struct super_block * sb, struct dentry * root,
  113. char const * name, const struct file_operations * fops);
  114. int oprofilefs_create_file_perm(struct super_block * sb, struct dentry * root,
  115. char const * name, const struct file_operations * fops, int perm);
  116. /** Create a file for read/write access to an unsigned long. */
  117. int oprofilefs_create_ulong(struct super_block * sb, struct dentry * root,
  118. char const * name, ulong * val);
  119. /** Create a file for read-only access to an unsigned long. */
  120. int oprofilefs_create_ro_ulong(struct super_block * sb, struct dentry * root,
  121. char const * name, ulong * val);
  122. /** Create a file for read-only access to an atomic_t. */
  123. int oprofilefs_create_ro_atomic(struct super_block * sb, struct dentry * root,
  124. char const * name, atomic_t * val);
  125. /** create a directory */
  126. struct dentry *oprofilefs_mkdir(struct dentry *parent, char const *name);
  127. /**
  128. * Write the given asciz string to the given user buffer @buf, updating *offset
  129. * appropriately. Returns bytes written or -EFAULT.
  130. */
  131. ssize_t oprofilefs_str_to_user(char const * str, char __user * buf, size_t count, loff_t * offset);
  132. /**
  133. * Convert an unsigned long value into ASCII and copy it to the user buffer @buf,
  134. * updating *offset appropriately. Returns bytes written or -EFAULT.
  135. */
  136. ssize_t oprofilefs_ulong_to_user(unsigned long val, char __user * buf, size_t count, loff_t * offset);
  137. /**
  138. * Read an ASCII string for a number from a userspace buffer and fill *val on success.
  139. * Returns 0 on success, < 0 on error.
  140. */
  141. int oprofilefs_ulong_from_user(unsigned long * val, char const __user * buf, size_t count);
  142. /** lock for read/write safety */
  143. extern raw_spinlock_t oprofilefs_lock;
  144. /**
  145. * Add the contents of a circular buffer to the event buffer.
  146. */
  147. void oprofile_put_buff(unsigned long *buf, unsigned int start,
  148. unsigned int stop, unsigned int max);
  149. unsigned long oprofile_get_cpu_buffer_size(void);
  150. void oprofile_cpu_buffer_inc_smpl_lost(void);
  151. /* cpu buffer functions */
  152. struct op_sample;
  153. struct op_entry {
  154. struct ring_buffer_event *event;
  155. struct op_sample *sample;
  156. unsigned long size;
  157. unsigned long *data;
  158. };
  159. void oprofile_write_reserve(struct op_entry *entry,
  160. struct pt_regs * const regs,
  161. unsigned long pc, int code, int size);
  162. int oprofile_add_data(struct op_entry *entry, unsigned long val);
  163. int oprofile_add_data64(struct op_entry *entry, u64 val);
  164. int oprofile_write_commit(struct op_entry *entry);
  165. #ifdef CONFIG_HW_PERF_EVENTS
  166. int __init oprofile_perf_init(struct oprofile_operations *ops);
  167. void oprofile_perf_exit(void);
  168. char *op_name_from_perf_id(void);
  169. #else
  170. static inline int __init oprofile_perf_init(struct oprofile_operations *ops)
  171. {
  172. pr_info("oprofile: hardware counters not available\n");
  173. return -ENODEV;
  174. }
  175. static inline void oprofile_perf_exit(void) { }
  176. #endif /* CONFIG_HW_PERF_EVENTS */
  177. #endif /* OPROFILE_H */