| 1 | /* |
| 2 | * Copyright (C) 2011-2012 Julien Desfossez |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License Version 2 as |
| 6 | * published by the Free Software Foundation; |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #include <babeltrace/babeltrace.h> |
| 19 | |
| 20 | #include "lttngtoptypes.h" |
| 21 | #include "common.h" |
| 22 | #include "cputop.h" |
| 23 | |
| 24 | void update_cputop_data(unsigned long timestamp, int64_t cpu, int prev_pid, |
| 25 | int next_pid, char *prev_comm, char *next_comm) |
| 26 | { |
| 27 | struct cputime *tmpcpu; |
| 28 | unsigned long elapsed; |
| 29 | |
| 30 | tmpcpu = get_cpu(cpu); |
| 31 | |
| 32 | if (tmpcpu->current_task && tmpcpu->current_task->pid == prev_pid) { |
| 33 | elapsed = timestamp - tmpcpu->task_start; |
| 34 | tmpcpu->current_task->totalcpunsec += elapsed; |
| 35 | tmpcpu->current_task->threadstotalcpunsec += elapsed; |
| 36 | if (tmpcpu->current_task->threadparent && |
| 37 | tmpcpu->current_task->pid != tmpcpu->current_task->tid) |
| 38 | tmpcpu->current_task->threadparent->threadstotalcpunsec += elapsed; |
| 39 | } |
| 40 | |
| 41 | if (next_pid != 0) |
| 42 | tmpcpu->current_task = get_proc(<tngtop, next_pid, next_comm, timestamp); |
| 43 | else |
| 44 | tmpcpu->current_task = NULL; |
| 45 | |
| 46 | tmpcpu->task_start = timestamp; |
| 47 | } |
| 48 | |
| 49 | enum bt_cb_ret handle_sched_switch(struct bt_ctf_event *call_data, |
| 50 | void *private_data) |
| 51 | { |
| 52 | const struct bt_definition *scope; |
| 53 | unsigned long timestamp; |
| 54 | uint64_t cpu_id; |
| 55 | char *prev_comm, *next_comm; |
| 56 | int prev_tid, next_tid; |
| 57 | |
| 58 | timestamp = bt_ctf_get_timestamp(call_data); |
| 59 | if (timestamp == -1ULL) |
| 60 | goto error; |
| 61 | |
| 62 | scope = bt_ctf_get_top_level_scope(call_data, |
| 63 | BT_EVENT_FIELDS); |
| 64 | prev_comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data, |
| 65 | scope, "_prev_comm")); |
| 66 | if (bt_ctf_field_get_error()) { |
| 67 | fprintf(stderr, "Missing prev_comm context info\n"); |
| 68 | goto error; |
| 69 | } |
| 70 | |
| 71 | next_comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data, |
| 72 | scope, "_next_comm")); |
| 73 | if (bt_ctf_field_get_error()) { |
| 74 | fprintf(stderr, "Missing next_comm context info\n"); |
| 75 | goto error; |
| 76 | } |
| 77 | |
| 78 | prev_tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, |
| 79 | scope, "_prev_tid")); |
| 80 | if (bt_ctf_field_get_error()) { |
| 81 | fprintf(stderr, "Missing prev_tid context info\n"); |
| 82 | goto error; |
| 83 | } |
| 84 | |
| 85 | next_tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, |
| 86 | scope, "_next_tid")); |
| 87 | if (bt_ctf_field_get_error()) { |
| 88 | fprintf(stderr, "Missing next_tid context info\n"); |
| 89 | goto error; |
| 90 | } |
| 91 | |
| 92 | cpu_id = get_cpu_id(call_data); |
| 93 | |
| 94 | update_cputop_data(timestamp, cpu_id, prev_tid, next_tid, |
| 95 | prev_comm, next_comm); |
| 96 | |
| 97 | return BT_CB_OK; |
| 98 | |
| 99 | error: |
| 100 | return BT_CB_ERROR_STOP; |
| 101 | } |
| 102 | |
| 103 | enum bt_cb_ret handle_sched_process_free(struct bt_ctf_event *call_data, |
| 104 | void *private_data) |
| 105 | { |
| 106 | const struct bt_definition *scope; |
| 107 | unsigned long timestamp; |
| 108 | char *comm; |
| 109 | int tid; |
| 110 | |
| 111 | timestamp = bt_ctf_get_timestamp(call_data); |
| 112 | if (timestamp == -1ULL) |
| 113 | goto error; |
| 114 | |
| 115 | scope = bt_ctf_get_top_level_scope(call_data, |
| 116 | BT_EVENT_FIELDS); |
| 117 | comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data, |
| 118 | scope, "_comm")); |
| 119 | if (bt_ctf_field_get_error()) { |
| 120 | fprintf(stderr, "Missing procname context info\n"); |
| 121 | goto error; |
| 122 | } |
| 123 | |
| 124 | tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, |
| 125 | scope, "_tid")); |
| 126 | if (bt_ctf_field_get_error()) { |
| 127 | fprintf(stderr, "Missing tid field\n"); |
| 128 | goto error; |
| 129 | } |
| 130 | |
| 131 | death_proc(<tngtop, tid, comm, timestamp); |
| 132 | |
| 133 | return BT_CB_OK; |
| 134 | |
| 135 | error: |
| 136 | return BT_CB_ERROR_STOP; |
| 137 | |
| 138 | } |
| 139 | |