perfmon_default_smpl.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 2002-2003 Hewlett-Packard Co
  3. * Stephane Eranian <eranian@hpl.hp.com>
  4. *
  5. * This file implements the default sampling buffer format
  6. * for the Linux/ia64 perfmon-2 subsystem.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/types.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <asm/delay.h>
  13. #include <linux/smp.h>
  14. #include <asm/perfmon.h>
  15. #include <asm/perfmon_default_smpl.h>
  16. MODULE_AUTHOR("Stephane Eranian <eranian@hpl.hp.com>");
  17. MODULE_DESCRIPTION("perfmon default sampling format");
  18. MODULE_LICENSE("GPL");
  19. #define DEFAULT_DEBUG 1
  20. #ifdef DEFAULT_DEBUG
  21. #define DPRINT(a) \
  22. do { \
  23. if (unlikely(pfm_sysctl.debug >0)) { printk("%s.%d: CPU%d ", __func__, __LINE__, smp_processor_id()); printk a; } \
  24. } while (0)
  25. #define DPRINT_ovfl(a) \
  26. do { \
  27. if (unlikely(pfm_sysctl.debug > 0 && pfm_sysctl.debug_ovfl >0)) { printk("%s.%d: CPU%d ", __func__, __LINE__, smp_processor_id()); printk a; } \
  28. } while (0)
  29. #else
  30. #define DPRINT(a)
  31. #define DPRINT_ovfl(a)
  32. #endif
  33. static int
  34. default_validate(struct task_struct *task, unsigned int flags, int cpu, void *data)
  35. {
  36. pfm_default_smpl_arg_t *arg = (pfm_default_smpl_arg_t*)data;
  37. int ret = 0;
  38. if (data == NULL) {
  39. DPRINT(("[%d] no argument passed\n", task_pid_nr(task)));
  40. return -EINVAL;
  41. }
  42. DPRINT(("[%d] validate flags=0x%x CPU%d\n", task_pid_nr(task), flags, cpu));
  43. /*
  44. * must hold at least the buffer header + one minimally sized entry
  45. */
  46. if (arg->buf_size < PFM_DEFAULT_SMPL_MIN_BUF_SIZE) return -EINVAL;
  47. DPRINT(("buf_size=%lu\n", arg->buf_size));
  48. return ret;
  49. }
  50. static int
  51. default_get_size(struct task_struct *task, unsigned int flags, int cpu, void *data, unsigned long *size)
  52. {
  53. pfm_default_smpl_arg_t *arg = (pfm_default_smpl_arg_t *)data;
  54. /*
  55. * size has been validated in default_validate
  56. */
  57. *size = arg->buf_size;
  58. return 0;
  59. }
  60. static int
  61. default_init(struct task_struct *task, void *buf, unsigned int flags, int cpu, void *data)
  62. {
  63. pfm_default_smpl_hdr_t *hdr;
  64. pfm_default_smpl_arg_t *arg = (pfm_default_smpl_arg_t *)data;
  65. hdr = (pfm_default_smpl_hdr_t *)buf;
  66. hdr->hdr_version = PFM_DEFAULT_SMPL_VERSION;
  67. hdr->hdr_buf_size = arg->buf_size;
  68. hdr->hdr_cur_offs = sizeof(*hdr);
  69. hdr->hdr_overflows = 0UL;
  70. hdr->hdr_count = 0UL;
  71. DPRINT(("[%d] buffer=%p buf_size=%lu hdr_size=%lu hdr_version=%u cur_offs=%lu\n",
  72. task_pid_nr(task),
  73. buf,
  74. hdr->hdr_buf_size,
  75. sizeof(*hdr),
  76. hdr->hdr_version,
  77. hdr->hdr_cur_offs));
  78. return 0;
  79. }
  80. static int
  81. default_handler(struct task_struct *task, void *buf, pfm_ovfl_arg_t *arg, struct pt_regs *regs, unsigned long stamp)
  82. {
  83. pfm_default_smpl_hdr_t *hdr;
  84. pfm_default_smpl_entry_t *ent;
  85. void *cur, *last;
  86. unsigned long *e, entry_size;
  87. unsigned int npmds, i;
  88. unsigned char ovfl_pmd;
  89. unsigned char ovfl_notify;
  90. if (unlikely(buf == NULL || arg == NULL|| regs == NULL || task == NULL)) {
  91. DPRINT(("[%d] invalid arguments buf=%p arg=%p\n", task->pid, buf, arg));
  92. return -EINVAL;
  93. }
  94. hdr = (pfm_default_smpl_hdr_t *)buf;
  95. cur = buf+hdr->hdr_cur_offs;
  96. last = buf+hdr->hdr_buf_size;
  97. ovfl_pmd = arg->ovfl_pmd;
  98. ovfl_notify = arg->ovfl_notify;
  99. /*
  100. * precheck for sanity
  101. */
  102. if ((last - cur) < PFM_DEFAULT_MAX_ENTRY_SIZE) goto full;
  103. npmds = hweight64(arg->smpl_pmds[0]);
  104. ent = (pfm_default_smpl_entry_t *)cur;
  105. prefetch(arg->smpl_pmds_values);
  106. entry_size = sizeof(*ent) + (npmds << 3);
  107. /* position for first pmd */
  108. e = (unsigned long *)(ent+1);
  109. hdr->hdr_count++;
  110. DPRINT_ovfl(("[%d] count=%lu cur=%p last=%p free_bytes=%lu ovfl_pmd=%d ovfl_notify=%d npmds=%u\n",
  111. task->pid,
  112. hdr->hdr_count,
  113. cur, last,
  114. last-cur,
  115. ovfl_pmd,
  116. ovfl_notify, npmds));
  117. /*
  118. * current = task running at the time of the overflow.
  119. *
  120. * per-task mode:
  121. * - this is usually the task being monitored.
  122. * Under certain conditions, it might be a different task
  123. *
  124. * system-wide:
  125. * - this is not necessarily the task controlling the session
  126. */
  127. ent->pid = current->pid;
  128. ent->ovfl_pmd = ovfl_pmd;
  129. ent->last_reset_val = arg->pmd_last_reset; //pmd[0].reg_last_reset_val;
  130. /*
  131. * where did the fault happen (includes slot number)
  132. */
  133. ent->ip = regs->cr_iip | ((regs->cr_ipsr >> 41) & 0x3);
  134. ent->tstamp = stamp;
  135. ent->cpu = smp_processor_id();
  136. ent->set = arg->active_set;
  137. ent->tgid = current->tgid;
  138. /*
  139. * selectively store PMDs in increasing index number
  140. */
  141. if (npmds) {
  142. unsigned long *val = arg->smpl_pmds_values;
  143. for(i=0; i < npmds; i++) {
  144. *e++ = *val++;
  145. }
  146. }
  147. /*
  148. * update position for next entry
  149. */
  150. hdr->hdr_cur_offs += entry_size;
  151. cur += entry_size;
  152. /*
  153. * post check to avoid losing the last sample
  154. */
  155. if ((last - cur) < PFM_DEFAULT_MAX_ENTRY_SIZE) goto full;
  156. /*
  157. * keep same ovfl_pmds, ovfl_notify
  158. */
  159. arg->ovfl_ctrl.bits.notify_user = 0;
  160. arg->ovfl_ctrl.bits.block_task = 0;
  161. arg->ovfl_ctrl.bits.mask_monitoring = 0;
  162. arg->ovfl_ctrl.bits.reset_ovfl_pmds = 1; /* reset before returning from interrupt handler */
  163. return 0;
  164. full:
  165. DPRINT_ovfl(("sampling buffer full free=%lu, count=%lu, ovfl_notify=%d\n", last-cur, hdr->hdr_count, ovfl_notify));
  166. /*
  167. * increment number of buffer overflow.
  168. * important to detect duplicate set of samples.
  169. */
  170. hdr->hdr_overflows++;
  171. /*
  172. * if no notification requested, then we saturate the buffer
  173. */
  174. if (ovfl_notify == 0) {
  175. arg->ovfl_ctrl.bits.notify_user = 0;
  176. arg->ovfl_ctrl.bits.block_task = 0;
  177. arg->ovfl_ctrl.bits.mask_monitoring = 1;
  178. arg->ovfl_ctrl.bits.reset_ovfl_pmds = 0;
  179. } else {
  180. arg->ovfl_ctrl.bits.notify_user = 1;
  181. arg->ovfl_ctrl.bits.block_task = 1; /* ignored for non-blocking context */
  182. arg->ovfl_ctrl.bits.mask_monitoring = 1;
  183. arg->ovfl_ctrl.bits.reset_ovfl_pmds = 0; /* no reset now */
  184. }
  185. return -1; /* we are full, sorry */
  186. }
  187. static int
  188. default_restart(struct task_struct *task, pfm_ovfl_ctrl_t *ctrl, void *buf, struct pt_regs *regs)
  189. {
  190. pfm_default_smpl_hdr_t *hdr;
  191. hdr = (pfm_default_smpl_hdr_t *)buf;
  192. hdr->hdr_count = 0UL;
  193. hdr->hdr_cur_offs = sizeof(*hdr);
  194. ctrl->bits.mask_monitoring = 0;
  195. ctrl->bits.reset_ovfl_pmds = 1; /* uses long-reset values */
  196. return 0;
  197. }
  198. static int
  199. default_exit(struct task_struct *task, void *buf, struct pt_regs *regs)
  200. {
  201. DPRINT(("[%d] exit(%p)\n", task_pid_nr(task), buf));
  202. return 0;
  203. }
  204. static pfm_buffer_fmt_t default_fmt={
  205. .fmt_name = "default_format",
  206. .fmt_uuid = PFM_DEFAULT_SMPL_UUID,
  207. .fmt_arg_size = sizeof(pfm_default_smpl_arg_t),
  208. .fmt_validate = default_validate,
  209. .fmt_getsize = default_get_size,
  210. .fmt_init = default_init,
  211. .fmt_handler = default_handler,
  212. .fmt_restart = default_restart,
  213. .fmt_restart_active = default_restart,
  214. .fmt_exit = default_exit,
  215. };
  216. static int __init
  217. pfm_default_smpl_init_module(void)
  218. {
  219. int ret;
  220. ret = pfm_register_buffer_fmt(&default_fmt);
  221. if (ret == 0) {
  222. printk("perfmon_default_smpl: %s v%u.%u registered\n",
  223. default_fmt.fmt_name,
  224. PFM_DEFAULT_SMPL_VERSION_MAJ,
  225. PFM_DEFAULT_SMPL_VERSION_MIN);
  226. } else {
  227. printk("perfmon_default_smpl: %s cannot register ret=%d\n",
  228. default_fmt.fmt_name,
  229. ret);
  230. }
  231. return ret;
  232. }
  233. static void __exit
  234. pfm_default_smpl_cleanup_module(void)
  235. {
  236. int ret;
  237. ret = pfm_unregister_buffer_fmt(default_fmt.fmt_uuid);
  238. printk("perfmon_default_smpl: unregister %s=%d\n", default_fmt.fmt_name, ret);
  239. }
  240. module_init(pfm_default_smpl_init_module);
  241. module_exit(pfm_default_smpl_cleanup_module);