plugin_sched_switch.c 4.4 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. #include "trace-seq.h"
  25. static void write_state(struct trace_seq *s, int val)
  26. {
  27. const char states[] = "SDTtZXxW";
  28. int found = 0;
  29. int i;
  30. for (i = 0; i < (sizeof(states) - 1); i++) {
  31. if (!(val & (1 << i)))
  32. continue;
  33. if (found)
  34. trace_seq_putc(s, '|');
  35. found = 1;
  36. trace_seq_putc(s, states[i]);
  37. }
  38. if (!found)
  39. trace_seq_putc(s, 'R');
  40. }
  41. static void write_and_save_comm(struct tep_format_field *field,
  42. struct tep_record *record,
  43. struct trace_seq *s, int pid)
  44. {
  45. const char *comm;
  46. int len;
  47. comm = (char *)(record->data + field->offset);
  48. len = s->len;
  49. trace_seq_printf(s, "%.*s",
  50. field->size, comm);
  51. /* make sure the comm has a \0 at the end. */
  52. trace_seq_terminate(s);
  53. comm = &s->buffer[len];
  54. /* Help out the comm to ids. This will handle dups */
  55. tep_register_comm(field->event->pevent, comm, pid);
  56. }
  57. static int sched_wakeup_handler(struct trace_seq *s,
  58. struct tep_record *record,
  59. struct tep_event_format *event, void *context)
  60. {
  61. struct tep_format_field *field;
  62. unsigned long long val;
  63. if (tep_get_field_val(s, event, "pid", record, &val, 1))
  64. return trace_seq_putc(s, '!');
  65. field = tep_find_any_field(event, "comm");
  66. if (field) {
  67. write_and_save_comm(field, record, s, val);
  68. trace_seq_putc(s, ':');
  69. }
  70. trace_seq_printf(s, "%lld", val);
  71. if (tep_get_field_val(s, event, "prio", record, &val, 0) == 0)
  72. trace_seq_printf(s, " [%lld]", val);
  73. if (tep_get_field_val(s, event, "success", record, &val, 1) == 0)
  74. trace_seq_printf(s, " success=%lld", val);
  75. if (tep_get_field_val(s, event, "target_cpu", record, &val, 0) == 0)
  76. trace_seq_printf(s, " CPU:%03llu", val);
  77. return 0;
  78. }
  79. static int sched_switch_handler(struct trace_seq *s,
  80. struct tep_record *record,
  81. struct tep_event_format *event, void *context)
  82. {
  83. struct tep_format_field *field;
  84. unsigned long long val;
  85. if (tep_get_field_val(s, event, "prev_pid", record, &val, 1))
  86. return trace_seq_putc(s, '!');
  87. field = tep_find_any_field(event, "prev_comm");
  88. if (field) {
  89. write_and_save_comm(field, record, s, val);
  90. trace_seq_putc(s, ':');
  91. }
  92. trace_seq_printf(s, "%lld ", val);
  93. if (tep_get_field_val(s, event, "prev_prio", record, &val, 0) == 0)
  94. trace_seq_printf(s, "[%d] ", (int) val);
  95. if (tep_get_field_val(s, event, "prev_state", record, &val, 0) == 0)
  96. write_state(s, val);
  97. trace_seq_puts(s, " ==> ");
  98. if (tep_get_field_val(s, event, "next_pid", record, &val, 1))
  99. return trace_seq_putc(s, '!');
  100. field = tep_find_any_field(event, "next_comm");
  101. if (field) {
  102. write_and_save_comm(field, record, s, val);
  103. trace_seq_putc(s, ':');
  104. }
  105. trace_seq_printf(s, "%lld", val);
  106. if (tep_get_field_val(s, event, "next_prio", record, &val, 0) == 0)
  107. trace_seq_printf(s, " [%d]", (int) val);
  108. return 0;
  109. }
  110. int TEP_PLUGIN_LOADER(struct tep_handle *pevent)
  111. {
  112. tep_register_event_handler(pevent, -1, "sched", "sched_switch",
  113. sched_switch_handler, NULL);
  114. tep_register_event_handler(pevent, -1, "sched", "sched_wakeup",
  115. sched_wakeup_handler, NULL);
  116. tep_register_event_handler(pevent, -1, "sched", "sched_wakeup_new",
  117. sched_wakeup_handler, NULL);
  118. return 0;
  119. }
  120. void TEP_PLUGIN_UNLOADER(struct tep_handle *pevent)
  121. {
  122. tep_unregister_event_handler(pevent, -1, "sched", "sched_switch",
  123. sched_switch_handler, NULL);
  124. tep_unregister_event_handler(pevent, -1, "sched", "sched_wakeup",
  125. sched_wakeup_handler, NULL);
  126. tep_unregister_event_handler(pevent, -1, "sched", "sched_wakeup_new",
  127. sched_wakeup_handler, NULL);
  128. }