From patchwork Thu Jan 11 09:16:06 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 124167 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp413995qgn; Thu, 11 Jan 2018 01:16:17 -0800 (PST) X-Google-Smtp-Source: ACJfBovLKc7jFG4F4n9Z+47k6hsrYivMWUfTtYj+QcliAGSft+/Kgklc+pVDpYYM6WLnaCReeG4x X-Received: by 10.159.218.151 with SMTP id w23mr13417919plp.100.1515662177668; Thu, 11 Jan 2018 01:16:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515662177; cv=none; d=google.com; s=arc-20160816; b=qYcxDZCNUEWU2+uNVA0P2G9TDXgO30j+qkmp4c4vqjkydjMY+yej9EEhu02ZbRsAav by2RMny50wR88/LwVudAnYbfo1dqriXnXFQpsuPidPdLuG0q//7I1miufyuR89qD3RMh DhPFxZlU1L1Zo8wG9SQugco5H55V3XQe9OkNq2qVUPoYrS5gITEdogb02zp+CWr2JdZD PXleUwXEFoOGM3kr4oTTfKFM0xD48NcK145Ez4HmYZmGPxsXTSQkvJDFIHxzKMlwQXJg Ox3tTJDqPkbXo+vyk2pLE4cMyzq+t51TEhrYQE6s3MHF8pfW2Cvyjp2xsxYRUiHgLC4v /s+g== 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=Y+G7gLEY2DOTU0PlYpfu4MEpsgJe9yKLA4J2egLtmSc=; b=NjvAO5Go1148aGPcsWGOIl2MnyvZzPYtodN1qhh/S6ll16RbdRz4En5NaWD7Ze+tEi 5J3NXWNO9AkpvpSQIjLydwi0xLpK8VVKCE8GtGo6DIoflkyvviurOsmi1nV8lEPW5PHh BvBptVsoI4NoryNujnHkC8MRX1qvz80vGZfLcWzpy/my+yoX3Nyh5gT9FZPFfh8KIqfC IDL/x888sb4Cn+1DYAjqjPnO4F9qp49eIzX5dgsYBAqA5tuG6xLqtKukucyMMULtxja+ 5X0IsTUb2ouocs0P1CVcrnF3+B0TBkA2ETsM28lhu/jpDcz07zn7zJAe/IKPwNjfRdSt TLiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=g2yuJCxq; 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 x4si13163383pfx.81.2018.01.11.01.16.17; Thu, 11 Jan 2018 01:16:17 -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=g2yuJCxq; 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 S1753879AbeAKJQO (ORCPT + 28 others); Thu, 11 Jan 2018 04:16:14 -0500 Received: from mail-pf0-f196.google.com ([209.85.192.196]:43742 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752917AbeAKJQL (ORCPT ); Thu, 11 Jan 2018 04:16:11 -0500 Received: by mail-pf0-f196.google.com with SMTP id e3so1214131pfi.10 for ; Thu, 11 Jan 2018 01:16:10 -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=Y+G7gLEY2DOTU0PlYpfu4MEpsgJe9yKLA4J2egLtmSc=; b=g2yuJCxqvOZ4xbyv71NlZAk5hle9DlpGhN1hVOWMvLKjgPNX2RrWi/woFji6swqEuu o6kor3Ofb3zA/nex8ZK+3KW+v74XlhnfyJcQuUWB7Os+E+QPHV3cZUhzV2vT9dN9uC8a oK6wtdqKHv01ARctpL2+C8KITjybKqPxiKo2w= 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=Y+G7gLEY2DOTU0PlYpfu4MEpsgJe9yKLA4J2egLtmSc=; b=l9FPP8wPRCc2nYVT+M3rTHYxlL1D1zmqtfwvBj2ioCtou01lFl2AlblNIQvDLj3Rx5 ZqvlYKlWHZV6Yew678uEM4+jqriF4ctcZOmGUBV/d6Wpo+DQ71AzuXMTY+vm8j2IIvkJ nEsODx5UodiNqZQgwjnTLKN2qAkaC3OUoqi+lmmen5rN1SM85EHtM7iroKOW0elISIdW /UO3SHOgnlSkDyXHZEIu2X42wRDiNgyd238nx6Ixg7Di5Cq7DoUZqw+tJjR20a1xYwlo XlYTL9Mi+v62G1QYWCV7pfkZnxxqcxHa6Jmzh04FrGCbzflWh+D9ysSw6TKhUJHHLx9t Q+HQ== X-Gm-Message-State: AKGB3mIUVvKYpKzVEQXd6qhcZlPYQAlb+iyARGSnr9YB0CPGn6RrIpGT 5vusg2xfXxqafYoP58LHiAmZkA== X-Received: by 10.101.81.5 with SMTP id f5mr14570682pgq.22.1515662170384; Thu, 11 Jan 2018 01:16:10 -0800 (PST) Received: from localhost ([122.166.139.51]) by smtp.gmail.com with ESMTPSA id n128sm26628349pga.57.2018.01.11.01.16.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Jan 2018 01:16:09 -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 V2] thermal: Add cooling device's statistics in sysfs Date: Thu, 11 Jan 2018 14:46:06 +0530 Message-Id: <88a57e29b2d35f65aa94cfb18c61e2280fd47ea0.1515662107.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 --- 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 | 145 ++++++++++++++++++++++++++++++++++++++ include/linux/thermal.h | 1 + 5 files changed, 155 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..4bb3bc999d3c 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,43 @@ 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; + + 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 */