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