1# Copyright 2015-2017 ARM Limited 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# 15 16"""Utils module has generic utils that will be used across 17objects 18""" 19import collections 20import warnings 21from trappy.utils import listify 22 23 24def normalize_list(val, lst): 25 """Normalize a unitary list""" 26 27 if len(lst) != 1: 28 raise RuntimeError("Cannot Normalize a non-unitary list") 29 30 return lst * val 31 32 33def decolonize(val): 34 """Remove the colon at the end of the word 35 This will be used by the unique word of 36 template class to sanitize attr accesses 37 """ 38 39 return val.strip(":") 40 41 42def get_trace_event_data(trace, execnames=None, pids=None): 43 """Create a list of objects that can be consumed by EventPlot to plot 44 task residency like kernelshark 45 46 """ 47 48 if execnames: 49 execnames = listify(execnames) 50 51 if pids: 52 pids = listify(pids) 53 54 data = collections.defaultdict(list) 55 pmap = {} 56 57 data_frame = trace.sched_switch.data_frame 58 start_idx = data_frame.index.values[0] 59 end_idx = data_frame.index.values[-1] 60 61 procs = set() 62 63 for index, row in data_frame.iterrows(): 64 prev_pid = row["prev_pid"] 65 next_pid = row["next_pid"] 66 next_comm = row["next_comm"] 67 68 if prev_pid in pmap: 69 name = pmap[prev_pid] 70 data[name][-1][1] = index 71 del pmap[prev_pid] 72 73 name = "{}-{}".format(next_comm, next_pid) 74 75 if next_pid in pmap: 76 # Corrupted trace probably due to dropped events. We 77 # don't know when the pid in pmap finished. We just 78 # ignore it and don't plot it 79 warn_str = "Corrupted trace (dropped events) for PID {} at time {}". \ 80 format(next_pid, index) 81 warnings.warn(warn_str) 82 del pmap[next_pid] 83 del data[name][-1] 84 85 if next_pid != 0 and not next_comm.startswith("migration"): 86 87 if execnames and next_comm not in execnames: 88 continue 89 90 if pids and next_pid not in pids: 91 continue 92 93 data[name].append([index, end_idx, row["__cpu"]]) 94 pmap[next_pid] = name 95 procs.add(name) 96 97 return data, procs, [start_idx, end_idx] 98