plugin_sched_switch.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
  3. *
  4. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation;
  8. * version 2.1 of the License (not later!)
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this program; if not, see <http://www.gnu.org/licenses>
  17. *
  18. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "event-parse.h"
  24. static void write_state(struct trace_seq *s, int val)
  25. {
  26. const char states[] = "SDTtZXxW";
  27. int found = 0;
  28. int i;
  29. for (i = 0; i < (sizeof(states) - 1); i++) {
  30. if (!(val & (1 << i)))
  31. continue;
  32. if (found)
  33. trace_seq_putc(s, '|');
  34. found = 1;
  35. trace_seq_putc(s, states[i]);
  36. }
  37. if (!found)
  38. trace_seq_putc(s, 'R');
  39. }
  40. static void write_and_save_comm(struct format_field *field,
  41. struct tep_record *record,
  42. struct trace_seq *s, int pid)
  43. {
  44. const char *comm;
  45. int len;
  46. comm = (char *)(record->data + field->offset);
  47. len = s->len;
  48. trace_seq_printf(s, "%.*s",
  49. field->size, comm);
  50. /* make sure the comm has a \0 at the end. */
  51. trace_seq_terminate(s);
  52. comm = &s->buffer[len];
  53. /* Help out the comm to ids. This will handle dups */
  54. tep_register_comm(field->event->pevent, comm, pid);
  55. }
  56. static int sched_wakeup_handler(struct trace_seq *s,
  57. struct tep_record *record,
  58. struct event_format *event, void *context)
  59. {
  60. struct format_field *field;
  61. unsigned long long val;
  62. if (tep_get_field_val(s, event, "pid", record, &val, 1))
  63. return trace_seq_putc(s, '!');
  64. field = tep_find_any_field(event, "comm");
  65. if (field) {
  66. write_and_save_comm(field, record, s, val);
  67. trace_seq_putc(s, ':');
  68. }
  69. trace_seq_printf(s, "%lld", val);
  70. if (tep_get_field_val(s, event, "prio", record, &val, 0) == 0)
  71. trace_seq_printf(s, " [%lld]", val);
  72. if (tep_get_field_val(s, event, "success", record, &val, 1) == 0)
  73. trace_seq_printf(s, " success=%lld", val);
  74. if (tep_get_field_val(s, event, "target_cpu", record, &val, 0) == 0)
  75. trace_seq_printf(s, " CPU:%03llu", val);
  76. return 0;
  77. }
  78. static int sched_switch_handler(struct trace_seq *s,
  79. struct tep_record *record,
  80. struct event_format *event, void *context)
  81. {
  82. struct format_field *field;
  83. unsigned long long val;
  84. if (tep_get_field_val(s, event, "prev_pid", record, &val, 1))
  85. return trace_seq_putc(s, '!');
  86. field = tep_find_any_field(event, "prev_comm");
  87. if (field) {
  88. write_and_save_comm(field, record, s, val);
  89. trace_seq_putc(s, ':');
  90. }
  91. trace_seq_printf(s, "%lld ", val);
  92. if (tep_get_field_val(s, event, "prev_prio", record, &val, 0) == 0)
  93. trace_seq_printf(s, "[%d] ", (int) val);
  94. if (tep_get_field_val(s, event, "prev_state", record, &val, 0) == 0)
  95. write_state(s, val);
  96. trace_seq_puts(s, " ==> ");
  97. if (tep_get_field_val(s, event, "next_pid", record, &val, 1))
  98. return trace_seq_putc(s, '!');
  99. field = tep_find_any_field(event, "next_comm");
  100. if (field) {
  101. write_and_save_comm(field, record, s, val);
  102. trace_seq_putc(s, ':');
  103. }
  104. trace_seq_printf(s, "%lld", val);
  105. if (tep_get_field_val(s, event, "next_prio", record, &val, 0) == 0)
  106. trace_seq_printf(s, " [%d]", (int) val);
  107. return 0;
  108. }
  109. int TEP_PLUGIN_LOADER(struct tep_handle *pevent)
  110. {
  111. tep_register_event_handler(pevent, -1, "sched", "sched_switch",
  112. sched_switch_handler, NULL);
  113. tep_register_event_handler(pevent, -1, "sched", "sched_wakeup",
  114. sched_wakeup_handler, NULL);
  115. tep_register_event_handler(pevent, -1, "sched", "sched_wakeup_new",
  116. sched_wakeup_handler, NULL);
  117. return 0;
  118. }
  119. void TEP_PLUGIN_UNLOADER(struct tep_handle *pevent)
  120. {
  121. tep_unregister_event_handler(pevent, -1, "sched", "sched_switch",
  122. sched_switch_handler, NULL);
  123. tep_unregister_event_handler(pevent, -1, "sched", "sched_wakeup",
  124. sched_wakeup_handler, NULL);
  125. tep_unregister_event_handler(pevent, -1, "sched", "sched_wakeup_new",
  126. sched_wakeup_handler, NULL);
  127. }