From fdfe840e480c56dc1119c31bb4fcc211b1b9b46f Mon Sep 17 00:00:00 2001 From: One Thousand Gnomes Date: Tue, 17 Dec 2013 15:07:31 +0000 Subject: [PATCH 1/5] cpupower: Fix sscanf robustness in cpufreq-set The cpufreq-set tool has a missing length check. This is basically just correctness but still should get fixed. One of a set of sscanf problems reported by Jackie Chang Signed-off-by: Alan Cox [rjw: Subject] Signed-off-by: Rafael J. Wysocki --- tools/power/cpupower/utils/cpufreq-set.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/power/cpupower/utils/cpufreq-set.c b/tools/power/cpupower/utils/cpufreq-set.c index dd1539eb8c63..a416de80c55e 100644 --- a/tools/power/cpupower/utils/cpufreq-set.c +++ b/tools/power/cpupower/utils/cpufreq-set.c @@ -257,7 +257,7 @@ int cmd_freq_set(int argc, char **argv) print_unknown_arg(); return -EINVAL; } - if ((sscanf(optarg, "%s", gov)) != 1) { + if ((sscanf(optarg, "%19s", gov)) != 1) { print_unknown_arg(); return -EINVAL; } From f677b30b487ca3763c3de3f1b4d8c976c2961cd1 Mon Sep 17 00:00:00 2001 From: Lv Zheng Date: Wed, 15 Jan 2014 12:04:09 +0800 Subject: [PATCH 2/5] ACPICA: acpidump: Cleanup tools/power/acpi makefiles. This patch cleans up old tools/power/acpi Makefile for further porting, make it compiled in a similar way as the other tools. No functional changes. The CFLAGS is modified as follows: 1. Previous cc flags: -Wall -Wstrict-prototypes -Wdeclaration-after-statement -Os -s \ -D_LINUX -DDEFINE_ALTERNATE_TYPES -I../../../include 2. Current cc flags: DEBUG=false: -D_LINUX -DDEFINE_ALTERNATE_TYPES -I../../../include -Wall \ -Wstrict-prototypes -Wdeclaration-after-statement -Os \ -fomit-frame-pointer Normal: -D_LINUX -DDEFINE_ALTERNATE_TYPES -I../../../include -Wall \ -Wstrict-prototypes -Wdeclaration-after-statement -O1 -g -DDEBUG There is only one difference: -fomit-frame-pointer. Signed-off-by: Lv Zheng Signed-off-by: Rafael J. Wysocki --- tools/power/acpi/Makefile | 149 ++++++++++++++++-- tools/power/acpi/{ => man}/acpidump.8 | 0 .../acpi/{ => tools/acpidump}/acpidump.c | 0 3 files changed, 137 insertions(+), 12 deletions(-) rename tools/power/acpi/{ => man}/acpidump.8 (100%) rename tools/power/acpi/{ => tools/acpidump}/acpidump.c (100%) diff --git a/tools/power/acpi/Makefile b/tools/power/acpi/Makefile index bafeb8d662a3..d9186a2fdf06 100644 --- a/tools/power/acpi/Makefile +++ b/tools/power/acpi/Makefile @@ -1,18 +1,143 @@ -PROG= acpidump -SRCS= acpidump.c +# tools/power/acpi/Makefile - ACPI tool Makefile +# +# Copyright (c) 2013, Intel Corporation +# Author: Lv Zheng +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; version 2 +# of the License. + +OUTPUT=./ +ifeq ("$(origin O)", "command line") + OUTPUT := $(O)/ +endif + +ifneq ($(OUTPUT),) +# check that the output directory actually exists +OUTDIR := $(shell cd $(OUTPUT) && /bin/pwd) +$(if $(OUTDIR),, $(error output directory "$(OUTPUT)" does not exist)) +endif + +# --- CONFIGURATION BEGIN --- + +# Set the following to `true' to make a unstripped, unoptimized +# binary. Leave this set to `false' for production use. +DEBUG ?= true + +# make the build silent. Set this to something else to make it noisy again. +V ?= false + +# Prefix to the directories we're installing to +DESTDIR ?= + +# --- CONFIGURATION END --- + +# Directory definitions. These are default and most probably +# do not need to be changed. Please note that DESTDIR is +# added in front of any of them + +bindir ?= /usr/bin +sbindir ?= /usr/sbin +mandir ?= /usr/man + +# Toolchain: what tools do we use, and what options do they need: + +INSTALL = /usr/bin/install -c +INSTALL_PROGRAM = ${INSTALL} +INSTALL_DATA = ${INSTALL} -m 644 +INSTALL_SCRIPT = ${INSTALL_PROGRAM} + +# If you are running a cross compiler, you may want to set this +# to something more interesting, like "arm-linux-". If you want +# to compile vs uClibc, that can be done here as well. +CROSS = #/usr/i386-linux-uclibc/usr/bin/i386-uclibc- +CC = $(CROSS)gcc +LD = $(CROSS)gcc +STRIP = $(CROSS)strip +HOSTCC = gcc + +# check if compiler option is supported +cc-supports = ${shell if $(CC) ${1} -S -o /dev/null -x c /dev/null > /dev/null 2>&1; then echo "$(1)"; fi;} + +# use '-Os' optimization if available, else use -O2 +OPTIMIZATION := $(call cc-supports,-Os,-O2) + +WARNINGS := -Wall +WARNINGS += $(call cc-supports,-Wstrict-prototypes) +WARNINGS += $(call cc-supports,-Wdeclaration-after-statement) + KERNEL_INCLUDE := ../../../include -CFLAGS += -Wall -Wstrict-prototypes -Wdeclaration-after-statement -Os -s -D_LINUX -DDEFINE_ALTERNATE_TYPES -I$(KERNEL_INCLUDE) +CFLAGS += -D_LINUX -DDEFINE_ALTERNATE_TYPES -I$(KERNEL_INCLUDE) +CFLAGS += $(WARNINGS) -all: acpidump -$(PROG) : $(SRCS) - $(CC) $(CFLAGS) $(SRCS) -o $(PROG) +ifeq ($(strip $(V)),false) + QUIET=@ + ECHO=@echo +else + QUIET= + ECHO=@\# +endif +export QUIET ECHO -CLEANFILES= $(PROG) +# if DEBUG is enabled, then we do not strip or optimize +ifeq ($(strip $(DEBUG)),true) + CFLAGS += -O1 -g -DDEBUG + STRIPCMD = /bin/true -Since_we_are_debugging +else + CFLAGS += $(OPTIMIZATION) -fomit-frame-pointer + STRIPCMD = $(STRIP) -s --remove-section=.note --remove-section=.comment +endif -clean : - rm -f $(CLEANFILES) $(patsubst %.c,%.o, $(SRCS)) *~ +# if DEBUG is enabled, then we do not strip or optimize +ifeq ($(strip $(DEBUG)),true) + CFLAGS += -O1 -g -DDEBUG + STRIPCMD = /bin/true -Since_we_are_debugging +else + CFLAGS += $(OPTIMIZATION) -fomit-frame-pointer + STRIPCMD = $(STRIP) -s --remove-section=.note --remove-section=.comment +endif -install : - install acpidump /usr/sbin/acpidump - install acpidump.8 /usr/share/man/man8 +# --- ACPIDUMP BEGIN --- +vpath %.c \ + tools/acpidump + +DUMP_OBJS = \ + acpidump.o + +DUMP_OBJS := $(addprefix $(OUTPUT)tools/acpidump/,$(DUMP_OBJS)) + +$(OUTPUT)acpidump: $(DUMP_OBJS) + $(ECHO) " LD " $@ + $(QUIET) $(LD) $(CFLAGS) $(LDFLAGS) $(DUMP_OBJS) -L$(OUTPUT) -o $@ + $(QUIET) $(STRIPCMD) $@ + +$(OUTPUT)tools/acpidump/%.o: %.c + $(ECHO) " CC " $@ + $(QUIET) $(CC) -c $(CFLAGS) -o $@ $< + +# --- ACPIDUMP END --- + +all: $(OUTPUT)acpidump + echo $(OUTPUT) + +clean: + -find $(OUTPUT) \( -not -type d \) -and \( -name '*~' -o -name '*.[oas]' \) -type f -print \ + | xargs rm -f + -rm -f $(OUTPUT)acpidump + +install-tools: + $(INSTALL) -d $(DESTDIR)${bindir} + $(INSTALL_PROGRAM) $(OUTPUT)acpidump $(DESTDIR)${sbindir} + +install-man: + $(INSTALL_DATA) -D man/acpidump.8 $(DESTDIR)${mandir}/man8/acpidump.8 + +install: all install-tools install-man + +uninstall: + - rm -f $(DESTDIR)${sbindir}/acpidump + - rm -f $(DESTDIR)${mandir}/man8/acpidump.8 + +.PHONY: all utils install-tools install-man install uninstall clean diff --git a/tools/power/acpi/acpidump.8 b/tools/power/acpi/man/acpidump.8 similarity index 100% rename from tools/power/acpi/acpidump.8 rename to tools/power/acpi/man/acpidump.8 diff --git a/tools/power/acpi/acpidump.c b/tools/power/acpi/tools/acpidump/acpidump.c similarity index 100% rename from tools/power/acpi/acpidump.c rename to tools/power/acpi/tools/acpidump/acpidump.c From a0c4acc09e381112e7a4cea3aad9ba001907665e Mon Sep 17 00:00:00 2001 From: Lv Zheng Date: Wed, 15 Jan 2014 12:04:17 +0800 Subject: [PATCH 3/5] ACPICA: acpidump: Enable tools Makefile to include acpi tools. This patch enables ACPI tool build in the tools/Makefile, so that the ACPI tools can be built/cleaned/installed along with other tools. Signed-off-by: Lv Zheng Signed-off-by: Rafael J. Wysocki --- tools/Makefile | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tools/Makefile b/tools/Makefile index a9b02008443c..ecdc2f84953b 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -3,6 +3,7 @@ include scripts/Makefile.include help: @echo 'Possible targets:' @echo '' + @echo ' acpi - ACPI tools' @echo ' cgroup - cgroup tools' @echo ' cpupower - a tool for all things x86 CPU power' @echo ' firewire - the userspace part of nosy, an IEEE-1394 traffic sniffer' @@ -33,6 +34,9 @@ help: @echo ' the respective build directory.' @echo ' clean: a summary clean target to clean _all_ folders' +acpi: FORCE + $(call descend,power/$@) + cpupower: FORCE $(call descend,power/$@) @@ -54,6 +58,9 @@ turbostat x86_energy_perf_policy: FORCE tmon: FORCE $(call descend,thermal/$@) +acpi_install: + $(call descend,power/$(@:_install=),install) + cpupower_install: $(call descend,power/$(@:_install=),install) @@ -69,11 +76,14 @@ turbostat_install x86_energy_perf_policy_install: tmon_install: $(call descend,thermal/$(@:_install=),install) -install: cgroup_install cpupower_install firewire_install lguest_install \ +install: acpi_install cgroup_install cpupower_install firewire_install lguest_install \ perf_install selftests_install turbostat_install usb_install \ virtio_install vm_install net_install x86_energy_perf_policy_install \ tmon +acpi_clean: + $(call descend,power/acpi,clean) + cpupower_clean: $(call descend,power/cpupower,clean) @@ -95,8 +105,8 @@ turbostat_clean x86_energy_perf_policy_clean: tmon_clean: $(call descend,thermal/tmon,clean) -clean: cgroup_clean cpupower_clean firewire_clean lguest_clean perf_clean \ - selftests_clean turbostat_clean usb_clean virtio_clean \ +clean: acpi_clean cgroup_clean cpupower_clean firewire_clean lguest_clean \ + perf_clean selftests_clean turbostat_clean usb_clean virtio_clean \ vm_clean net_clean x86_energy_perf_policy_clean tmon_clean .PHONY: FORCE From 2754c447b7a72599f309ab97aa38a4707f054b6f Mon Sep 17 00:00:00 2001 From: Lv Zheng Date: Wed, 15 Jan 2014 12:04:24 +0800 Subject: [PATCH 4/5] ACPICA: acpidump: Update MAINTAINERS file to include tools folder for ACPI/ACPICA. This patch updates MAINTAINERS file, adding tools/power/acpi for ACPI and ACPICA. Signed-off-by: Lv Zheng Signed-off-by: Rafael J. Wysocki --- MAINTAINERS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 31a046213274..b8cae76a9aec 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -252,6 +252,7 @@ F: Documentation/ABI/testing/sysfs-bus-acpi F: drivers/pci/*acpi* F: drivers/pci/*/*acpi* F: drivers/pci/*/*/*acpi* +F: tools/power/acpi ACPI COMPONENT ARCHITECTURE (ACPICA) M: Robert Moore @@ -266,6 +267,7 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm S: Supported F: drivers/acpi/acpica/ F: include/acpi/ +F: tools/power/acpi/ ACPI FAN DRIVER M: Zhang Rui From ee8b09cd60bfe45d856e7c3bef8742835686bf4e Mon Sep 17 00:00:00 2001 From: Todd E Brandt Date: Thu, 16 Jan 2014 16:18:22 -0800 Subject: [PATCH 5/5] PM / tools: new tool for suspend/resume performance optimization This tool is designed to assist kernel and OS developers in optimizing their linux stack's suspend/resume time. Using a kernel image built with a few extra options enabled, the tool will execute a suspend and will capture dmesg and ftrace data until resume is complete. This data is transformed into a device timeline and a callgraph to give a quick and detailed view of which devices and callbacks are taking the most time in suspend/resume. The output is a single html file which can be viewed in firefox or chrome. References: https://01.org/suspendresume Signed-off-by: Todd Brandt Signed-off-by: Rafael J. Wysocki --- scripts/analyze_suspend.py | 1446 ++++++++++++++++++++++++++++++++++++ 1 file changed, 1446 insertions(+) create mode 100755 scripts/analyze_suspend.py diff --git a/scripts/analyze_suspend.py b/scripts/analyze_suspend.py new file mode 100755 index 000000000000..4f2cc12dc7c7 --- /dev/null +++ b/scripts/analyze_suspend.py @@ -0,0 +1,1446 @@ +#!/usr/bin/python +# +# Tool for analyzing suspend/resume timing +# Copyright (c) 2013, Intel Corporation. +# +# This program is free software; you can redistribute it and/or modify it +# under the terms and conditions of the GNU General Public License, +# version 2, as published by the Free Software Foundation. +# +# This program is distributed in the hope it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. +# +# Authors: +# Todd Brandt +# +# Description: +# This tool is designed to assist kernel and OS developers in optimizing +# their linux stack's suspend/resume time. Using a kernel image built +# with a few extra options enabled, the tool will execute a suspend and +# will capture dmesg and ftrace data until resume is complete. This data +# is transformed into a device timeline and a callgraph to give a quick +# and detailed view of which devices and callbacks are taking the most +# time in suspend/resume. The output is a single html file which can be +# viewed in firefox or chrome. +# +# The following kernel build options are required: +# CONFIG_PM_DEBUG=y +# CONFIG_PM_SLEEP_DEBUG=y +# CONFIG_FTRACE=y +# CONFIG_FUNCTION_TRACER=y +# CONFIG_FUNCTION_GRAPH_TRACER=y +# +# The following additional kernel parameters are required: +# (e.g. in file /etc/default/grub) +# GRUB_CMDLINE_LINUX_DEFAULT="... initcall_debug log_buf_len=16M ..." +# + +import sys +import time +import os +import string +import re +import array +import platform +import datetime +import struct + +# -- classes -- + +class SystemValues: + testdir = "." + tpath = "/sys/kernel/debug/tracing/" + mempath = "/dev/mem" + powerfile = "/sys/power/state" + suspendmode = "mem" + prefix = "test" + teststamp = "" + dmesgfile = "" + ftracefile = "" + htmlfile = "" + rtcwake = False + def setOutputFile(self): + if((self.htmlfile == "") and (self.dmesgfile != "")): + m = re.match(r"(?P.*)_dmesg\.txt$", self.dmesgfile) + if(m): + self.htmlfile = m.group("name")+".html" + if((self.htmlfile == "") and (self.ftracefile != "")): + m = re.match(r"(?P.*)_ftrace\.txt$", self.ftracefile) + if(m): + self.htmlfile = m.group("name")+".html" + if(self.htmlfile == ""): + self.htmlfile = "output.html" + def initTestOutput(self): + hostname = platform.node() + if(hostname != ""): + self.prefix = hostname + v = os.popen("cat /proc/version").read().strip() + kver = string.split(v)[2] + self.testdir = os.popen("date \"+suspend-%m%d%y-%H%M%S\"").read().strip() + self.teststamp = "# "+self.testdir+" "+self.prefix+" "+self.suspendmode+" "+kver + self.dmesgfile = self.testdir+"/"+self.prefix+"_"+self.suspendmode+"_dmesg.txt" + self.ftracefile = self.testdir+"/"+self.prefix+"_"+self.suspendmode+"_ftrace.txt" + self.htmlfile = self.testdir+"/"+self.prefix+"_"+self.suspendmode+".html" + os.mkdir(self.testdir) + +class Data: + altdevname = dict() + usedmesg = False + useftrace = False + notestrun = False + verbose = False + phases = [] + dmesg = {} # root data structure + start = 0.0 + end = 0.0 + stamp = {'time': "", 'host': "", 'mode': ""} + id = 0 + tSuspended = 0.0 + fwValid = False + fwSuspend = 0 + fwResume = 0 + def initialize(self): + self.dmesg = { # dmesg log data + 'suspend_general': {'list': dict(), 'start': -1.0, 'end': -1.0, + 'row': 0, 'color': "#CCFFCC", 'order': 0}, + 'suspend_early': {'list': dict(), 'start': -1.0, 'end': -1.0, + 'row': 0, 'color': "green", 'order': 1}, + 'suspend_noirq': {'list': dict(), 'start': -1.0, 'end': -1.0, + 'row': 0, 'color': "#00FFFF", 'order': 2}, + 'suspend_cpu': {'list': dict(), 'start': -1.0, 'end': -1.0, + 'row': 0, 'color': "blue", 'order': 3}, + 'resume_cpu': {'list': dict(), 'start': -1.0, 'end': -1.0, + 'row': 0, 'color': "red", 'order': 4}, + 'resume_noirq': {'list': dict(), 'start': -1.0, 'end': -1.0, + 'row': 0, 'color': "orange", 'order': 5}, + 'resume_early': {'list': dict(), 'start': -1.0, 'end': -1.0, + 'row': 0, 'color': "yellow", 'order': 6}, + 'resume_general': {'list': dict(), 'start': -1.0, 'end': -1.0, + 'row': 0, 'color': "#FFFFCC", 'order': 7} + } + self.phases = self.sortedPhases() + def normalizeTime(self): + tSus = tRes = self.tSuspended + if self.fwValid: + tSus -= -self.fwSuspend / 1000000000.0 + tRes -= self.fwResume / 1000000000.0 + self.tSuspended = 0.0 + self.start -= tSus + self.end -= tRes + for phase in self.phases: + zero = tRes + if "suspend" in phase: + zero = tSus + p = self.dmesg[phase] + p['start'] -= zero + p['end'] -= zero + list = p['list'] + for name in list: + d = list[name] + d['start'] -= zero + d['end'] -= zero + if('ftrace' in d): + cg = d['ftrace'] + cg.start -= zero + cg.end -= zero + for line in cg.list: + line.time -= zero + if self.fwValid: + fws = -self.fwSuspend / 1000000000.0 + fwr = self.fwResume / 1000000000.0 + list = dict() + self.id += 1 + devid = "dc%d" % self.id + list["firmware-suspend"] = \ + {'start': fws, 'end': 0, 'pid': 0, 'par': "", + 'length': -fws, 'row': 0, 'id': devid }; + self.id += 1 + devid = "dc%d" % self.id + list["firmware-resume"] = \ + {'start': 0, 'end': fwr, 'pid': 0, 'par': "", + 'length': fwr, 'row': 0, 'id': devid }; + self.dmesg['BIOS'] = \ + {'list': list, 'start': fws, 'end': fwr, + 'row': 0, 'color': "purple", 'order': 4} + self.dmesg['resume_cpu']['order'] += 1 + self.dmesg['resume_noirq']['order'] += 1 + self.dmesg['resume_early']['order'] += 1 + self.dmesg['resume_general']['order'] += 1 + self.phases = self.sortedPhases() + def vprint(self, msg): + if(self.verbose): + print(msg) + def dmesgSortVal(self, phase): + return self.dmesg[phase]['order'] + def sortedPhases(self): + return sorted(self.dmesg, key=self.dmesgSortVal) + def sortedDevices(self, phase): + list = self.dmesg[phase]['list'] + slist = [] + tmp = dict() + for devname in list: + dev = list[devname] + tmp[dev['start']] = devname + for t in sorted(tmp): + slist.append(tmp[t]) + return slist + def fixupInitcalls(self, phase, end): + # if any calls never returned, clip them at system resume end + phaselist = self.dmesg[phase]['list'] + for devname in phaselist: + dev = phaselist[devname] + if(dev['end'] < 0): + dev['end'] = end + self.vprint("%s (%s): callback didn't return" % (devname, phase)) + def fixupInitcallsThatDidntReturn(self): + # if any calls never returned, clip them at system resume end + for phase in self.phases: + self.fixupInitcalls(phase, self.dmesg['resume_general']['end']) + if(phase == "resume_general"): + break + def newAction(self, phase, name, pid, parent, start, end): + self.id += 1 + devid = "dc%d" % self.id + list = self.dmesg[phase]['list'] + length = -1.0 + if(start >= 0 and end >= 0): + length = end - start + list[name] = {'start': start, 'end': end, 'pid': pid, 'par': parent, + 'length': length, 'row': 0, 'id': devid } + def deviceIDs(self, devlist, phase): + idlist = [] + for p in self.phases: + if(p[0] != phase[0]): + continue + list = data.dmesg[p]['list'] + for devname in list: + if devname in devlist: + idlist.append(list[devname]['id']) + return idlist + def deviceParentID(self, devname, phase): + pdev = "" + pdevid = "" + for p in self.phases: + if(p[0] != phase[0]): + continue + list = data.dmesg[p]['list'] + if devname in list: + pdev = list[devname]['par'] + for p in self.phases: + if(p[0] != phase[0]): + continue + list = data.dmesg[p]['list'] + if pdev in list: + return list[pdev]['id'] + return pdev + def deviceChildrenIDs(self, devname, phase): + devlist = [] + for p in self.phases: + if(p[0] != phase[0]): + continue + list = data.dmesg[p]['list'] + for child in list: + if(list[child]['par'] == devname): + devlist.append(child) + return self.deviceIDs(devlist, phase) + +class FTraceLine: + time = 0.0 + length = 0.0 + fcall = False + freturn = False + fevent = False + depth = 0 + name = "" + def __init__(self, t, m, d): + self.time = float(t) + # check to see if this is a trace event + em = re.match(r"^ *\/\* *(?P.*) \*\/ *$", m) + if(em): + self.name = em.group("msg") + self.fevent = True + return + # convert the duration to seconds + if(d): + self.length = float(d)/1000000 + # the indentation determines the depth + match = re.match(r"^(?P *)(?P.*)$", m) + if(not match): + return + self.depth = self.getDepth(match.group('d')) + m = match.group('o') + # function return + if(m[0] == '}'): + self.freturn = True + if(len(m) > 1): + # includes comment with function name + match = re.match(r"^} *\/\* *(?P.*) *\*\/$", m) + if(match): + self.name = match.group('n') + # function call + else: + self.fcall = True + # function call with children + if(m[-1] == '{'): + match = re.match(r"^(?P.*) *\(.*", m) + if(match): + self.name = match.group('n') + # function call with no children (leaf) + elif(m[-1] == ';'): + self.freturn = True + match = re.match(r"^(?P.*) *\(.*", m) + if(match): + self.name = match.group('n') + # something else (possibly a trace marker) + else: + self.name = m + def getDepth(self, str): + return len(str)/2 + +class FTraceCallGraph: + start = -1.0 + end = -1.0 + list = [] + invalid = False + depth = 0 + def __init__(self): + self.start = -1.0 + self.end = -1.0 + self.list = [] + self.depth = 0 + def setDepth(self, line): + if(line.fcall and not line.freturn): + line.depth = self.depth + self.depth += 1 + elif(line.freturn and not line.fcall): + self.depth -= 1 + line.depth = self.depth + else: + line.depth = self.depth + def addLine(self, line, match): + if(not self.invalid): + self.setDepth(line) + if(line.depth == 0 and line.freturn): + self.end = line.time + self.list.append(line) + return True + if(self.invalid): + return False + if(len(self.list) >= 1000000 or self.depth < 0): + first = self.list[0] + self.list = [] + self.list.append(first) + self.invalid = True + id = "task %s cpu %s" % (match.group("pid"), match.group("cpu")) + window = "(%f - %f)" % (self.start, line.time) + data.vprint("Too much data for "+id+" "+window+", ignoring this callback") + return False + self.list.append(line) + if(self.start < 0): + self.start = line.time + return False + def sanityCheck(self): + stack = dict() + cnt = 0 + for l in self.list: + if(l.fcall and not l.freturn): + stack[l.depth] = l + cnt += 1 + elif(l.freturn and not l.fcall): + if(not stack[l.depth]): + return False + stack[l.depth].length = l.length + stack[l.depth] = 0 + l.length = 0 + cnt -= 1 + if(cnt == 0): + return True + return False + def debugPrint(self, filename): + if(filename == "stdout"): + print("[%f - %f]") % (self.start, self.end) + for l in self.list: + if(l.freturn and l.fcall): + print("%f (%02d): %s(); (%.3f us)" % (l.time, l.depth, l.name, l.length*1000000)) + elif(l.freturn): + print("%f (%02d): %s} (%.3f us)" % (l.time, l.depth, l.name, l.length*1000000)) + else: + print("%f (%02d): %s() { (%.3f us)" % (l.time, l.depth, l.name, l.length*1000000)) + print(" ") + else: + fp = open(filename, 'w') + print(filename) + for l in self.list: + if(l.freturn and l.fcall): + fp.write("%f (%02d): %s(); (%.3f us)\n" % (l.time, l.depth, l.name, l.length*1000000)) + elif(l.freturn): + fp.write("%f (%02d): %s} (%.3f us)\n" % (l.time, l.depth, l.name, l.length*1000000)) + else: + fp.write("%f (%02d): %s() { (%.3f us)\n" % (l.time, l.depth, l.name, l.length*1000000)) + fp.close() + +class Timeline: + html = {} + scaleH = 0.0 # height of the timescale row as a percent of the timeline height + rowH = 0.0 # height of each row in percent of the timeline height + row_height_pixels = 30 + maxrows = 0 + height = 0 + def __init__(self): + self.html = { + 'timeline': "", + 'legend': "", + 'scale': "" + } + def setRows(self, rows): + self.maxrows = int(rows) + self.scaleH = 100.0/float(self.maxrows) + self.height = self.maxrows*self.row_height_pixels + r = float(self.maxrows - 1) + if(r < 1.0): + r = 1.0 + self.rowH = (100.0 - self.scaleH)/r + +# -- global objects -- + +sysvals = SystemValues() +data = Data() + +# -- functions -- + +# Function: initFtrace +# Description: +# Configure ftrace to capture a function trace during suspend/resume +def initFtrace(): + global sysvals + + print("INITIALIZING FTRACE...") + # turn trace off + os.system("echo 0 > "+sysvals.tpath+"tracing_on") + # set the trace clock to global + os.system("echo global > "+sysvals.tpath+"trace_clock") + # set trace buffer to a huge value + os.system("echo nop > "+sysvals.tpath+"current_tracer") + os.system("echo 100000 > "+sysvals.tpath+"buffer_size_kb") + # clear the trace buffer + os.system("echo \"\" > "+sysvals.tpath+"trace") + # set trace type + os.system("echo function_graph > "+sysvals.tpath+"current_tracer") + os.system("echo \"\" > "+sysvals.tpath+"set_ftrace_filter") + # set trace format options + os.system("echo funcgraph-abstime > "+sysvals.tpath+"trace_options") + os.system("echo funcgraph-proc > "+sysvals.tpath+"trace_options") + # focus only on device suspend and resume + os.system("cat "+sysvals.tpath+"available_filter_functions | grep dpm_run_callback > "+sysvals.tpath+"set_graph_function") + +# Function: verifyFtrace +# Description: +# Check that ftrace is working on the system +def verifyFtrace(): + global sysvals + files = ["available_filter_functions", "buffer_size_kb", + "current_tracer", "set_ftrace_filter", + "trace", "trace_marker"] + for f in files: + if(os.path.exists(sysvals.tpath+f) == False): + return False + return True + +def parseStamp(line): + global data, sysvals + stampfmt = r"# suspend-(?P[0-9]{2})(?P[0-9]{2})(?P[0-9]{2})-"+\ + "(?P[0-9]{2})(?P[0-9]{2})(?P[0-9]{2})"+\ + " (?P.*) (?P.*) (?P.*)$" + m = re.match(stampfmt, line) + if(m): + dt = datetime.datetime(int(m.group("y"))+2000, int(m.group("m")), + int(m.group("d")), int(m.group("H")), int(m.group("M")), + int(m.group("S"))) + data.stamp['time'] = dt.strftime("%B %d %Y, %I:%M:%S %p") + data.stamp['host'] = m.group("host") + data.stamp['mode'] = m.group("mode") + data.stamp['kernel'] = m.group("kernel") + sysvals.suspendmode = data.stamp['mode'] + +# Function: analyzeTraceLog +# Description: +# Analyse an ftrace log output file generated from this app during +# the execution phase. Create an "ftrace" structure in memory for +# subsequent formatting in the html output file +def analyzeTraceLog(): + global sysvals, data + + # the ftrace data is tied to the dmesg data + if(not data.usedmesg): + return + + # read through the ftrace and parse the data + data.vprint("Analyzing the ftrace data...") + ftrace_line_fmt = r"^ *(?P