Received: by 2002:ab2:6c55:0:b0:1fd:c486:4f03 with SMTP id v21csp500123lqp; Wed, 12 Jun 2024 07:56:30 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWyecg4OZeBaPjkAuOTqrk/0UEHeb/6q5XM/IdAPoSuNn9Jj/E+rpNyIBDt7KvipLHxk/mZXKqrY9SDCf4w51QJlDoNW+mS4NGSLuUFCQ== X-Google-Smtp-Source: AGHT+IFHhwQc9BcZuXEOoJMc9YK5Gjz5sCwcJEiqY8y6iNlOshTqcqiQ69U7/5/IvcWWto8qNeAO X-Received: by 2002:a05:6214:3109:b0:6b0:8752:1f84 with SMTP id 6a1803df08f44-6b191a652bemr19959356d6.27.1718204189765; Wed, 12 Jun 2024 07:56:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718204189; cv=pass; d=google.com; s=arc-20160816; b=sVbOJBism0GWbrnMgH1n+zzCR9jRQDtt4E+V2S+isaSAGqvc7jtNeI8Pj4vj/Lg5Gn rHWKVZgjygCJi0KEYumtHp34BF5ksFuo7X9t8aAg9Y6vmVibzIzvU+HB/9y0q9dRxWLX 4+tBfVVLJsI5l/I3O3I0vOtkd7goeIH6EzJJ6TKW7pPnIz2b8gWXHn5XrSF9B9K2xlNJ eotRNdsNCSDCtN80xwxMS9aPIcNqZLtpHx7U7vUiCnctpxBBJCDzOo0tGaWr4fZnb3Cl OM3bcYAatBVkJmPKED1FFOVSGeHcvvmF0JzXaC4bhcZBsJ0BFoVEDZ9StlWRdhRwlmQq 4ArQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=6tFx/qXVo6O1Pq7NugdqAhdDFg17/I3UZeqAqLRYlrw=; fh=K8mLBiK/3C/kmCEXsUOFCCNvyk4rolfkOaiOXXuEDi8=; b=G5JyajMdgOSCwQGQTlhLFuU1dao9t7BSdkbksDMnrhbVLBjASmjrG6Be+unQhWNsKK ZRxxQSg8jhXepQ7ONsiGAJYvjkNBh1B7n9VIFWU8McDnTKNz+fKacTjw+B1Zrr+wG4Yi btRmnB1xllE5/DxoroFFBi9Yn8BMw3zU/ImU+NgBqxAmlbLr97lyQjoPConWjt6Tbl9R cSRDy6GX6qF5QKWIxyOpwNXcpuTL+PGnA+8CBdjnbrUAMOGjq3syMalJsQtbf1wvd5/+ NAjEetEfDTH2Vz6NcmvZNyx+bmoctZkGC7mXYYjaJaU5Ju4Cwk39iUFo3lJ1wvsRxNlI qeeA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Kk1bJ282; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-211716-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-211716-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 6a1803df08f44-6b04f968f12si155571866d6.198.2024.06.12.07.56.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 07:56:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-211716-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Kk1bJ282; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-211716-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-211716-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 6A87D1C219B3 for ; Wed, 12 Jun 2024 14:56:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 57B7817FAB2; Wed, 12 Jun 2024 14:56:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Kk1bJ282" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C529017F4F4 for ; Wed, 12 Jun 2024 14:56:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718204162; cv=none; b=V/mL19gAPInInB2o+flyt2poa70NkRlaqHk6eJUFPlsDpUjXevklWjYCS6qW9CspOrafdr09FqtD8b66FThZP1EJQvcA7EuSMPV07S7uQc8a+Aov46Hpm2PnPy6gN6Q4MmPcJKtaRklNyKW4vbjSfX02g1ZlPEqsBHXcbCaz+gA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718204162; c=relaxed/simple; bh=cJUXunAqIxilQZmNC5/bPQJRP2yY+mwrA9Z610+q4z0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KkWzcoSZjVl1bkdXom+Oh9w8Uq4SaG4TNvkT13YZmpwUX0yKt3jRUDpk99zcpnim9bno8WsDu6oi+Gym2TkaKpBp2PH2L9gKowxdjI3fQQ0QLGJ14dJ6bF4kjJS6Ch8z70w5FhIcUx0o9dCYKz4acqolt0uMREElIA6VeUNg7g8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Kk1bJ282; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718204159; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6tFx/qXVo6O1Pq7NugdqAhdDFg17/I3UZeqAqLRYlrw=; b=Kk1bJ2826QUb5/WaQQTBEVOd9uQSf7p0x0Ht2QiKjhyr54TUaPU3440wxVDE/MJZkjt06/ bFoJ9s0bWWa0/osz9LtmEoH+CzPsQO3xbqu4+H56Gx7J5ktQQWtT5f75REvjm8Q8kT/tLW mPCkliynJvxVHxMkC1vfQZ4IdOeNtbw= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-376-4mOH9h5IPVaP7ddRIxOVcQ-1; Wed, 12 Jun 2024 10:55:57 -0400 X-MC-Unique: 4mOH9h5IPVaP7ddRIxOVcQ-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8C2B71955E7F; Wed, 12 Jun 2024 14:55:56 +0000 (UTC) Received: from fedora.brq.redhat.com (unknown [10.43.17.68]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 65C591954AC0; Wed, 12 Jun 2024 14:55:54 +0000 (UTC) From: tglozar@redhat.com To: bristot@kernel.org, rostedt@goodmis.org Cc: linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org, jkacur@redhat.com, jwyatt@redhat.com, Tomas Glozar Subject: [PATCH 2/5] rtla/utils: Add idle state disabling via libcpupower Date: Wed, 12 Jun 2024 16:54:36 +0200 Message-ID: <20240612145439.206990-3-tglozar@redhat.com> In-Reply-To: <20240612145439.206990-1-tglozar@redhat.com> References: <20240612145439.206990-1-tglozar@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 From: Tomas Glozar Add functions to utils.c to disable idle states through functions of libcpupower. This will serve as the basis for disabling idle states per cpu when running timerlat. Signed-off-by: Tomas Glozar --- tools/tracing/rtla/src/utils.c | 133 +++++++++++++++++++++++++++++++++ tools/tracing/rtla/src/utils.h | 4 + 2 files changed, 137 insertions(+) diff --git a/tools/tracing/rtla/src/utils.c b/tools/tracing/rtla/src/utils.c index 9ac71a66840c..638c50f0f88e 100644 --- a/tools/tracing/rtla/src/utils.c +++ b/tools/tracing/rtla/src/utils.c @@ -4,6 +4,7 @@ */ #define _GNU_SOURCE +#include #include #include #include @@ -519,6 +520,138 @@ int set_cpu_dma_latency(int32_t latency) return fd; } +static unsigned int **saved_cpu_idle_disable_state; +static size_t saved_cpu_idle_disable_state_alloc_ctr; + +/* + * save_cpu_idle_state_disable - save disable for all idle states of a cpu + * + * Saves the current disable of all idle states of a cpu, to be subsequently + * restored via restore_cpu_idle_disable_state. + * + * Return: idle state count on success, negative on error + */ +int save_cpu_idle_disable_state(unsigned int cpu) +{ + unsigned int nr_states; + unsigned int state; + int disabled; + int nr_cpus; + + nr_states = cpuidle_state_count(cpu); + + if (nr_states == 0) + return 0; + + if (saved_cpu_idle_disable_state == NULL) { + nr_cpus = sysconf(_SC_NPROCESSORS_CONF); + saved_cpu_idle_disable_state = calloc(nr_cpus, sizeof(unsigned int *)); + } + + saved_cpu_idle_disable_state[cpu] = calloc(nr_states, sizeof(unsigned int)); + saved_cpu_idle_disable_state_alloc_ctr++; + + for (state = 0; state < nr_states; state++) { + disabled = cpuidle_is_state_disabled(cpu, state); + if (disabled < 0) + return disabled; + saved_cpu_idle_disable_state[cpu][state] = disabled; + } + + return nr_states; +} + +/* + * restore_cpu_idle_disable_state - restore disable for all idle states of a cpu + * + * Restores the current disable state of all idle states of a cpu that was + * previously saved by save_cpu_idle_disable_state. + * + * Return: idle state count on success, negative on error + */ +int restore_cpu_idle_disable_state(unsigned int cpu) +{ + unsigned int nr_states; + unsigned int state; + int disabled; + int result; + + nr_states = cpuidle_state_count(cpu); + + if (nr_states == 0) + return 0; + + for (state = 0; state < nr_states; state++) { + disabled = saved_cpu_idle_disable_state[cpu][state]; + result = cpuidle_state_disable(cpu, state, disabled); + if (result < 0) + return result; + } + + free(saved_cpu_idle_disable_state[cpu]); + saved_cpu_idle_disable_state[cpu] = NULL; + saved_cpu_idle_disable_state_alloc_ctr--; + if (saved_cpu_idle_disable_state_alloc_ctr == 0) { + free(saved_cpu_idle_disable_state); + saved_cpu_idle_disable_state = NULL; + } + + return nr_states; +} + +/* + * free_cpu_idle_disable_states - free saved idle state disable for all cpus + * + * Frees the memory used for storing cpu idle state disable for all cpus + * and states. + * + * Normally, the memory is freed automatically in + * restore_cpu_idle_disable_state; this is mostly for cleaning up after an + * error. + */ +void free_cpu_idle_disable_states(void) +{ + int cpu; + + if (!saved_cpu_idle_disable_state) + return; + + for (cpu = 0; cpu < sysconf(_SC_NPROCESSORS_CONF); cpu++) { + if (!saved_cpu_idle_disable_state[cpu]) + continue; + free(saved_cpu_idle_disable_state[cpu]); + saved_cpu_idle_disable_state[cpu] = NULL; + } + + free(saved_cpu_idle_disable_state); + saved_cpu_idle_disable_state = NULL; +} + +/* + * set_cpu_idle_disable_state - set disable for all idle states of a cpu + * + * This is used to reduce the exit from idle latency. Unlike + * set_cpu_dma_latency, it can disable idle states per cpu. + * + * Return: idle state count on success, negative on error + */ +int set_cpu_idle_disable_state(unsigned int cpu, unsigned int disable) +{ + unsigned int nr_states; + unsigned int state; + int result; + + nr_states = cpuidle_state_count(cpu); + + for (state = 0; state < nr_states; state++) { + result = cpuidle_state_disable(cpu, state, disable); + if (result < 0) + return result; + } + + return nr_states; +} + #define _STR(x) #x #define STR(x) _STR(x) diff --git a/tools/tracing/rtla/src/utils.h b/tools/tracing/rtla/src/utils.h index d44513e6c66a..b310ed5cd867 100644 --- a/tools/tracing/rtla/src/utils.h +++ b/tools/tracing/rtla/src/utils.h @@ -64,6 +64,10 @@ int set_comm_sched_attr(const char *comm_prefix, struct sched_attr *attr); int set_comm_cgroup(const char *comm_prefix, const char *cgroup); int set_pid_cgroup(pid_t pid, const char *cgroup); int set_cpu_dma_latency(int32_t latency); +int save_cpu_idle_disable_state(unsigned int cpu); +int restore_cpu_idle_disable_state(unsigned int cpu); +void free_cpu_idle_disable_states(void); +int set_cpu_idle_disable_state(unsigned int cpu, unsigned int disable); int auto_house_keeping(cpu_set_t *monitored_cpus); #define ns_to_usf(x) (((double)x/1000)) -- 2.43.0