From patchwork Thu Jan 11 09:36:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 124178 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp432793qgn; Thu, 11 Jan 2018 01:36:21 -0800 (PST) X-Google-Smtp-Source: ACJfBossuiyKss7uVAUyz0lJFUfqC6lrNFsBxY4uaYfXKXaI/3dLggr7bQCJZzhjyRf9eKeAsOU3 X-Received: by 10.159.218.151 with SMTP id w23mr13468461plp.100.1515663381326; Thu, 11 Jan 2018 01:36:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515663381; cv=none; d=google.com; s=arc-20160816; b=bc3UatYSxFQgHtuLeiVrFiEA4E5PeGDg+t+dFrJW6uakqRacZP+iU9BPEtRtvaBO9C VZC5ynbm1hXv5E2/GOC0P1l/TCBSTkyq7rlB4GzS5oWjJ/LqDjPYK/reB2Dp0vDuFp9Y DaNU381O2RkLIm/1dM+Uk7i2lotI3xhQuJ7GDd6IUQhIU2sp2eOGnBjdUDExEE8ggaa/ 1SivDAqcQHCOhRTuQQLgT0iNK9yaxfloXXS0lOFaoMqBF8xMTUbaoHCDoHJWUg71HQhu F8fb4VZSUBRzi5aJoQ7V5ECoVLyWeW7AvNrMBMlA5v9iO/WRSaTaI7c8TCLyiQ6Ysr1k g3NQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=z8QOa0N5/M4yh3jN8lgV7zVne//BEu3ueW5F7gOHXKw=; b=Hj97nF4ni5KYK1Qy6du/6V6CIVnjv5tWzW7VTra80c6bYzTf+KZuioTaoPFmW3DoO2 KcdTKNmtLRqcKmDThemRqZ9EkH+PeO+aluViOXr7wPQ9dk9DHFqrmWWgTqEUkNN1asyX 1igVyCBGUQYN3Re9UwwBal9qcxTYMNkC4xE64KeX31Fot2FAsku52oeI2/2PWR9s1sPj mUyO8zbCAvZH1/5DZRdTorkqacey7twqEw7LEwNHfFSWrV2WpSjxeMkKoAUN2faxsZvq ThHWcOAgKcFntY8Z6SfXniJd9tZqzsul42ysKJbGNCYrcbUFs1UQ35KHNGkZEqTljjxJ Y8+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Xke+g8kx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t76si11886542pgc.219.2018.01.11.01.36.21; Thu, 11 Jan 2018 01:36:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Xke+g8kx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754803AbeAKJgT (ORCPT + 28 others); Thu, 11 Jan 2018 04:36:19 -0500 Received: from mail-pg0-f67.google.com ([74.125.83.67]:42019 "EHLO mail-pg0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753600AbeAKJgR (ORCPT ); Thu, 11 Jan 2018 04:36:17 -0500 Received: by mail-pg0-f67.google.com with SMTP id q67so1802937pga.9 for ; Thu, 11 Jan 2018 01:36:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=z8QOa0N5/M4yh3jN8lgV7zVne//BEu3ueW5F7gOHXKw=; b=Xke+g8kx3DBgHmEtCcA/KE8f2GcxkJOTatJtS0L9+MR5sMFvHCoGAHqFGFORsDLJvQ nMJR5dg7xJ9T0WBKeSOdFwGWaPYEFEsURwBK5uF6UG9gMJeGj78/9K/PdpYQQxh1QLX/ HprkVx/Vs/D08NM+h42EF1Ty02P0KqOGS5OMA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=z8QOa0N5/M4yh3jN8lgV7zVne//BEu3ueW5F7gOHXKw=; b=jEWpNwmpeV8AZkkn4UJbp+B+UPYTDcUYdCs60dY4JF7gyHgF1VYvg8KxnmnMRzaz1l oOk5BUkoLYYIiL/U/NB5ssrKkPyKdqbF+2oDWvDg7zNhEp+uqyhEM0JFM0g823d9yyV3 FiyrHg4m1Th1HoNHYL5dm1X7EOCuyB+WJc9PeiFZ0+5aJDeZNRK8sCUMZOKteihS7Yvp 2XodCwijiIboH1mfFv5HldXp0LcFPpeWigcRwmrAfbZIi1rms56iz6op6tJxa6K4GnM0 fymB+A1M7ZQo+eIOPViOwSHAdhOundDjuIToTU66jXJ1bxv/oyYuK8S0KbUZYG0M4Mkq orlA== X-Gm-Message-State: AKGB3mI8CCQWqmoVRno808zcXLcVEnr8jXIfa7HVtaMlqDTYzc8dnwYR sg/4iJd4R9HAc7xY15EvAzUHpQ== X-Received: by 10.99.141.193 with SMTP id z184mr11219731pgd.261.1515663376483; Thu, 11 Jan 2018 01:36:16 -0800 (PST) Received: from localhost ([122.166.139.51]) by smtp.gmail.com with ESMTPSA id f72sm5882895pff.145.2018.01.11.01.36.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Jan 2018 01:36:15 -0800 (PST) From: Viresh Kumar To: Zhang Rui , Eduardo Valentin Cc: Viresh Kumar , Vincent Guittot , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH V3] thermal: Add cooling device's statistics in sysfs Date: Thu, 11 Jan 2018 15:06:09 +0530 Message-Id: <087186fecdfd85bead58e110cf8bf7ccdfab517b.1515663078.git.viresh.kumar@linaro.org> X-Mailer: git-send-email 2.15.0.194.g9af6a3dea062 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This extends the sysfs interface for thermal cooling devices and exposes some pretty useful statistics. These statistics have proven to be quite useful specially while doing benchmarks related to the task scheduler, where we want to make sure that nothing has disrupted the test, specially the cooling device which may have put constraints on the CPUs. The information exposed here tells us to what extent the CPUs were constrained by the thermal framework. The read-only "total_trans" file shows the total number of cooling state transitions the device has gone through since the time the cooling device is registered or the time when statistics were reset last. The read-only "time_in_state_ms" file shows the time spent by the device in the respective cooling states. The write-only "reset" file is used to reset the statistics. This is how the directory structure looks like for a single cooling device: $ ls -R /sys/class/thermal/cooling_device0/ /sys/class/thermal/cooling_device0/: cur_state max_state power stats subsystem type uevent /sys/class/thermal/cooling_device0/power: autosuspend_delay_ms runtime_active_time runtime_suspended_time control runtime_status /sys/class/thermal/cooling_device0/stats: reset time_in_state_ms total_trans This is tested on ARM 32-bit Hisilicon hikey620 board running Ubuntu and ARM 64-bit Hisilicon hikey960 board running Android. Signed-off-by: Viresh Kumar --- V2->V3: - Total number of states is max_level + 1. The earlier version didn't take that into account and so the stats for the highest state were missing. V1->V2: - Move to sysfs from debugfs drivers/thermal/thermal_core.c | 3 +- drivers/thermal/thermal_core.h | 3 + drivers/thermal/thermal_helpers.c | 5 +- drivers/thermal/thermal_sysfs.c | 146 ++++++++++++++++++++++++++++++++++++++ include/linux/thermal.h | 1 + 5 files changed, 156 insertions(+), 2 deletions(-) -- 2.15.0.194.g9af6a3dea062 diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 2b1b0ba393a4..2cc4ae57484e 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -972,8 +972,8 @@ __thermal_cooling_device_register(struct device_node *np, cdev->ops = ops; cdev->updated = false; cdev->device.class = &thermal_class; - thermal_cooling_device_setup_sysfs(cdev); cdev->devdata = devdata; + thermal_cooling_device_setup_sysfs(cdev); dev_set_name(&cdev->device, "cooling_device%d", cdev->id); result = device_register(&cdev->device); if (result) { @@ -1106,6 +1106,7 @@ void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev) ida_simple_remove(&thermal_cdev_ida, cdev->id); device_unregister(&cdev->device); + thermal_cooling_device_remove_sysfs(cdev); } EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister); diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h index 27e3b1df7360..f6eb01e99816 100644 --- a/drivers/thermal/thermal_core.h +++ b/drivers/thermal/thermal_core.h @@ -73,6 +73,9 @@ int thermal_build_list_of_policies(char *buf); int thermal_zone_create_device_groups(struct thermal_zone_device *, int); void thermal_zone_destroy_device_groups(struct thermal_zone_device *); void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *); +void thermal_cooling_device_remove_sysfs(struct thermal_cooling_device *cdev); +void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev, + unsigned long new_state); /* used only at binding time */ ssize_t thermal_cooling_device_trip_point_show(struct device *, diff --git a/drivers/thermal/thermal_helpers.c b/drivers/thermal/thermal_helpers.c index 8cdf75adcce1..eb03d7e099bb 100644 --- a/drivers/thermal/thermal_helpers.c +++ b/drivers/thermal/thermal_helpers.c @@ -187,7 +187,10 @@ void thermal_cdev_update(struct thermal_cooling_device *cdev) if (instance->target > target) target = instance->target; } - cdev->ops->set_cur_state(cdev, target); + + if (!cdev->ops->set_cur_state(cdev, target)) + thermal_cooling_device_stats_update(cdev, target); + cdev->updated = true; mutex_unlock(&cdev->lock); trace_cdev_update(cdev, target); diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c index ba81c9080f6e..88bb26d5d375 100644 --- a/drivers/thermal/thermal_sysfs.c +++ b/drivers/thermal/thermal_sysfs.c @@ -666,6 +666,121 @@ void thermal_zone_destroy_device_groups(struct thermal_zone_device *tz) } /* sys I/F for cooling device */ +struct cooling_dev_stats { + spinlock_t lock; + unsigned int total_trans; + unsigned long state; + unsigned long max_states; + ktime_t last_time; + ktime_t *time_in_state; +}; + +static void update_time_in_state(struct cooling_dev_stats *stats) +{ + ktime_t now = ktime_get(), delta; + + delta = ktime_sub(now, stats->last_time); + stats->time_in_state[stats->state] = + ktime_add(stats->time_in_state[stats->state], delta); + stats->last_time = now; +} + +void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev, + unsigned long new_state) +{ + struct cooling_dev_stats *stats = cdev->stats; + + spin_lock(&stats->lock); + + if (stats->state == new_state) + goto unlock; + + update_time_in_state(stats); + stats->state = new_state; + stats->total_trans++; + +unlock: + spin_unlock(&stats->lock); +} + +static ssize_t +thermal_cooling_device_total_trans_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct thermal_cooling_device *cdev = to_cooling_device(dev); + struct cooling_dev_stats *stats = cdev->stats; + int ret; + + spin_lock(&stats->lock); + ret = sprintf(buf, "%u\n", stats->total_trans); + spin_unlock(&stats->lock); + + return ret; +} + +static ssize_t +thermal_cooling_device_time_in_state_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct thermal_cooling_device *cdev = to_cooling_device(dev); + struct cooling_dev_stats *stats = cdev->stats; + ssize_t len = 0; + int i; + + spin_lock(&stats->lock); + update_time_in_state(stats); + + for (i = 0; i < stats->max_states; i++) { + len += sprintf(buf + len, "state%u\t%llu\n", i, + ktime_to_ms(stats->time_in_state[i])); + } + spin_unlock(&stats->lock); + + return len; +} + +static ssize_t +thermal_cooling_device_reset_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct thermal_cooling_device *cdev = to_cooling_device(dev); + struct cooling_dev_stats *stats = cdev->stats; + int i; + + spin_lock(&stats->lock); + + stats->total_trans = 0; + stats->last_time = ktime_get(); + + for (i = 0; i < stats->max_states; i++) + stats->time_in_state[i] = ktime_set(0, 0); + + spin_unlock(&stats->lock); + + return count; +} + +static DEVICE_ATTR(total_trans, 0444, thermal_cooling_device_total_trans_show, + NULL); +static DEVICE_ATTR(time_in_state_ms, 0444, + thermal_cooling_device_time_in_state_show, NULL); +static DEVICE_ATTR(reset, 0200, NULL, thermal_cooling_device_reset_store); + +static struct attribute *cooling_device_stats_attrs[] = { + &dev_attr_total_trans.attr, + &dev_attr_time_in_state_ms.attr, + &dev_attr_reset.attr, + NULL +}; + +static const struct attribute_group cooling_device_stats_attr_group = { + .attrs = cooling_device_stats_attrs, + .name = "stats" +}; + static ssize_t thermal_cooling_device_type_show(struct device *dev, struct device_attribute *attr, char *buf) @@ -721,6 +836,7 @@ thermal_cooling_device_cur_state_store(struct device *dev, result = cdev->ops->set_cur_state(cdev, state); if (result) return result; + thermal_cooling_device_stats_update(cdev, state); return count; } @@ -745,14 +861,44 @@ static const struct attribute_group cooling_device_attr_group = { static const struct attribute_group *cooling_device_attr_groups[] = { &cooling_device_attr_group, + &cooling_device_stats_attr_group, NULL, }; void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *cdev) { + struct cooling_dev_stats *stats; + unsigned long states; + int ret, size; + + ret = cdev->ops->get_max_state(cdev, &states); + if (ret) + return; + + states++; /* Total number of states is highest state + 1 */ + size = sizeof(*stats); + size += sizeof(*stats->time_in_state) * states; + + stats = kzalloc(size, GFP_KERNEL); + if (!stats) + return; + + stats->time_in_state = (ktime_t *)(stats + 1); + cdev->stats = stats; + stats->last_time = ktime_get(); + stats->max_states = states; + cdev->stats = stats; + + spin_lock_init(&stats->lock); cdev->device.groups = cooling_device_attr_groups; } +void thermal_cooling_device_remove_sysfs(struct thermal_cooling_device *cdev) +{ + kfree(cdev->stats); + cdev->stats = NULL; +} + /* these helper will be used only at the time of bindig */ ssize_t thermal_cooling_device_trip_point_show(struct device *dev, diff --git a/include/linux/thermal.h b/include/linux/thermal.h index 8c5302374eaa..7834be668d80 100644 --- a/include/linux/thermal.h +++ b/include/linux/thermal.h @@ -148,6 +148,7 @@ struct thermal_cooling_device { struct device device; struct device_node *np; void *devdata; + void *stats; const struct thermal_cooling_device_ops *ops; bool updated; /* true if the cooling device does not need update */ struct mutex lock; /* protect thermal_instances list */