From nobody Tue Nov 26 14:31:00 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 3045D1D271D for ; Thu, 17 Oct 2024 21:23:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729200210; cv=none; b=LF7CnmdQbp7AJ+xbi48OyFjhkNdyk50xHA+oTcMqLR7q8RthGnmwxZN+6cKbcVR8vBFwHY3274cVMx4MWf+9XsGHdS7pqT0Hl5/t3VOTqYFu9+4XA+sYmZkGwwtVd+dxfNysQCzd2J+csQkKYLpYKYo6LVB66WO094V89du0GRA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729200210; c=relaxed/simple; bh=7Mg5CBIRfXFvKhEZdwUsMj7JaZlVfw+xEIhicjSQnpo=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=WontlH/rcLOd2TdslAAxw9WqUzlnPMqERToT+AwKVh/66ugv9NIs1lwPmmb4vQ3UJ5mTrMdnyJZMuQERPRa8v7hxnE4dJ/Zv/DDFaAM2C5ATJpuO0bGNSHtFZtp7/sQdJDk2hyGES6jsQoE9WzvhHjd0gH0MaHPif1WgL4wp41E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id C3A5EC4CED0; Thu, 17 Oct 2024 21:23:29 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1t1XyM-00000003wE8-1sqb; Thu, 17 Oct 2024 17:23:54 -0400 Message-ID: <20241017212354.312058429@goodmis.org> User-Agent: quilt/0.68 Date: Thu, 17 Oct 2024 17:23:31 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Tomas Glozar , John Kacur Subject: [for-next][PATCH 3/6] rtla/utils: Add idle state disabling via libcpupower References: <20241017212328.560060762@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Link: https://lore.kernel.org/20241017140914.3200454-4-tglozar@redhat.com Signed-off-by: Tomas Glozar Signed-off-by: Steven Rostedt (Google) --- tools/tracing/rtla/src/utils.c | 150 +++++++++++++++++++++++++++++++++ tools/tracing/rtla/src/utils.h | 13 +++ 2 files changed, 163 insertions(+) diff --git a/tools/tracing/rtla/src/utils.c b/tools/tracing/rtla/src/utils.c index 6fae234aaf36..4995d35cf3ec 100644 --- a/tools/tracing/rtla/src/utils.c +++ b/tools/tracing/rtla/src/utils.c @@ -4,6 +4,9 @@ */ =20 #define _GNU_SOURCE +#ifdef HAVE_LIBCPUPOWER_SUPPORT +#include +#endif /* HAVE_LIBCPUPOWER_SUPPORT */ #include #include #include @@ -515,6 +518,153 @@ int set_cpu_dma_latency(int32_t latency) return fd; } =20 +#ifdef HAVE_LIBCPUPOWER_SUPPORT +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 subsequent= ly + * 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 =3D cpuidle_state_count(cpu); + + if (nr_states =3D=3D 0) + return 0; + + if (saved_cpu_idle_disable_state =3D=3D NULL) { + nr_cpus =3D sysconf(_SC_NPROCESSORS_CONF); + saved_cpu_idle_disable_state =3D calloc(nr_cpus, sizeof(unsigned int *)); + if (!saved_cpu_idle_disable_state) + return -1; + } + + saved_cpu_idle_disable_state[cpu] =3D calloc(nr_states, sizeof(unsigned i= nt)); + if (!saved_cpu_idle_disable_state[cpu]) + return -1; + saved_cpu_idle_disable_state_alloc_ctr++; + + for (state =3D 0; state < nr_states; state++) { + disabled =3D cpuidle_is_state_disabled(cpu, state); + if (disabled < 0) + return disabled; + saved_cpu_idle_disable_state[cpu][state] =3D 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 =3D cpuidle_state_count(cpu); + + if (nr_states =3D=3D 0) + return 0; + + if (!saved_cpu_idle_disable_state) + return -1; + + for (state =3D 0; state < nr_states; state++) { + if (!saved_cpu_idle_disable_state[cpu]) + return -1; + disabled =3D saved_cpu_idle_disable_state[cpu][state]; + result =3D cpuidle_state_disable(cpu, state, disabled); + if (result < 0) + return result; + } + + free(saved_cpu_idle_disable_state[cpu]); + saved_cpu_idle_disable_state[cpu] =3D NULL; + saved_cpu_idle_disable_state_alloc_ctr--; + if (saved_cpu_idle_disable_state_alloc_ctr =3D=3D 0) { + free(saved_cpu_idle_disable_state); + saved_cpu_idle_disable_state =3D NULL; + } + + return nr_states; +} + +/* + * free_cpu_idle_disable_states - free saved idle state disable for all cp= us + * + * 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; + int nr_cpus; + + if (!saved_cpu_idle_disable_state) + return; + + nr_cpus =3D sysconf(_SC_NPROCESSORS_CONF); + + for (cpu =3D 0; cpu < nr_cpus; cpu++) { + free(saved_cpu_idle_disable_state[cpu]); + saved_cpu_idle_disable_state[cpu] =3D NULL; + } + + free(saved_cpu_idle_disable_state); + saved_cpu_idle_disable_state =3D NULL; +} + +/* + * set_deepest_cpu_idle_state - limit idle state of cpu + * + * Disables all idle states deeper than the one given in + * deepest_state (assuming states with higher number are deeper). + * + * 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_deepest_cpu_idle_state(unsigned int cpu, unsigned int deepest_stat= e) +{ + unsigned int nr_states; + unsigned int state; + int result; + + nr_states =3D cpuidle_state_count(cpu); + + for (state =3D deepest_state + 1; state < nr_states; state++) { + result =3D cpuidle_state_disable(cpu, state, 1); + if (result < 0) + return result; + } + + return nr_states; +} +#endif /* HAVE_LIBCPUPOWER_SUPPORT */ + #define _STR(x) #x #define STR(x) _STR(x) =20 diff --git a/tools/tracing/rtla/src/utils.h b/tools/tracing/rtla/src/utils.h index 99c9cf81bcd0..101d4799a009 100644 --- a/tools/tracing/rtla/src/utils.h +++ b/tools/tracing/rtla/src/utils.h @@ -66,6 +66,19 @@ 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); +#ifdef HAVE_LIBCPUPOWER_SUPPORT +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_deepest_cpu_idle_state(unsigned int cpu, unsigned int state); +static inline int have_libcpupower_support(void) { return 1; } +#else +static inline int save_cpu_idle_disable_state(unsigned int cpu) { return -= 1; } +static inline int restore_cpu_idle_disable_state(unsigned int cpu) { retur= n -1; } +static inline void free_cpu_idle_disable_states(void) { } +static inline int set_deepest_cpu_idle_state(unsigned int cpu, unsigned in= t state) { return -1; } +static inline int have_libcpupower_support(void) { return 0; } +#endif /* HAVE_LIBCPUPOWER_SUPPORT */ int auto_house_keeping(cpu_set_t *monitored_cpus); =20 #define ns_to_usf(x) (((double)x/1000)) --=20 2.45.2