Message ID | 85b1efc3ddd698b3ac81aa72a6dc987ee17da3e2.1706180726.git.maciej.wieczor-retman@intel.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-38489-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2553:b0:103:945f:af90 with SMTP id p19csp1565720dyi; Thu, 25 Jan 2024 03:18:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IF4zdzzn0DIC6/0HkgABjsuQ5HCiKjojcPLY5v3igoo91lgYWdSbGDWKtzbavZ8A91hMqYS X-Received: by 2002:a17:902:b942:b0:1d7:1342:64cd with SMTP id h2-20020a170902b94200b001d7134264cdmr618591pls.10.1706181484907; Thu, 25 Jan 2024 03:18:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706181484; cv=pass; d=google.com; s=arc-20160816; b=uyQdWuq+HSea79EXUWK0YG4RbTpwWGF3S65MyHC6DOOaW0P9qkMgJ1DAEf6exbCxEb 5iHzMEqIor+TM/t0mjeSfilR4/7sHJs0xQN6jLHiow46go79HvZB7IiAnyWAddrL5d3W dUE+6jORQ+f5XzTj+lndoSLwxzI34T2WHw5yPyO6P9PVqiBI/qI7kfGSfaoYwaLkL5C2 s4+DA+5pnnY94kGfv6gjDdjZFq9n1z40gs+ukw3asfuQRsYDoQhYZBtqmJq33/0v1t7F lGOwBj65g1iScwzevDMfryqgOAQk1IkcZPOM9KLxq+MeaMMN74KKPVYlcRBliyH2EI5E Glzg== 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=HYMlGCgFO5vns8wtsQGKaR83HXE+jUT24KH8r+G2HnY=; fh=fnwFv1nS+x8hI9pZ0wUR4YbmriHn0jk42nwaadNtlEo=; b=pZAGpGic9gC0vltdyA7DCo/YiC0fEQViloyh+XFStCpRezYVrL8RPaqUHvNaEkAgYS 7PAsWc8k97Qyv0zq5KseLuF8mW66AagRK9g8eqHj1q002Kpdk0blgLI0jAQors+7AV0t owWX8eG7eJjlkuD0LJvPyUhYnt3O1jGa6Tf8o8S/H5fwMFWc5UXkouMgO2lhfH77PHxU +K5MnqUubUHmJy6u7T6YwbUFAAX7h40sJJgHCEQd86eAY3lpt4fIBgmvij7UD8dpEjLt lh5rJ/QVMju2YEtfqIbeAFcwK84MRHxZ2DMVk4BKnF67U0tlvzFNi8dsD7KgITFWHfgK YO9w== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=RkoI9W8M; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-38489-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38489-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id f13-20020a170902ce8d00b001ce5ba27888si223333plg.211.2024.01.25.03.18.04 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 03:18:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-38489-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=RkoI9W8M; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-38489-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38489-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 271552979A0 for <ouuuleilei@gmail.com>; Thu, 25 Jan 2024 11:16:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2912E51038; Thu, 25 Jan 2024 11:11:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="RkoI9W8M" Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.88]) (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 F01DF51C20; Thu, 25 Jan 2024 11:11:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.55.52.88 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706181075; cv=none; b=ftXSwv2feydq3QG2go/H/0QdeDi/bhxTgMjxRRm26unpin85tr8zmxlKmRaMFuOZ/848pn1kL4XLGYPAiRwi2FvK918Tpo/ICzcaDHHaCpIDDg3m+1RZApKjY+27y/4gd8tgpnma06UJs3RyzeM7eujWTwlEx9LZkLbU98Wq0Ew= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706181075; c=relaxed/simple; bh=33NHf8hKMsj/l+cEJaT9H1sPZXBn7CyH0lKsCXmRwnY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XBiiepnmm8KvuzurBWcvCORJyf63cyDSGj/YXT9o5dAgGxsCqLLAdkRshTuAeyJnsOX2kqWxxAZFX+CVWdNGt52nnDsH45GsWniAGQadAl8sg3T9p2PVffFiooJOgNMa8lzjEgMB1+lvvvH0oa4eKs5JhkNktuVJeFxClnKEceA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=RkoI9W8M; arc=none smtp.client-ip=192.55.52.88 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706181074; x=1737717074; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=33NHf8hKMsj/l+cEJaT9H1sPZXBn7CyH0lKsCXmRwnY=; b=RkoI9W8Mz63QrjvG0brad1lZBbN6GfOeMDx/c0mMn40K0aeDT6FPfhKX BNkMAuK86Rp76B6PSrKSQ+ABUrblXjHHsOQGUZM0FtXKtO6CGXt4QJH5Q yCNflk1ywWurGCDcQUdxWZXUxZobJhQOmBlKuF9ohSx3mqVFrbkH0C4MO 7lPg8bO0X88L0pwfHr3AkSXVFrCyf9E+DuHf9ItRkStgYRzbLPxlQbgI6 bLF+Be1HV3wvEOt+Ds04jRuFs6vPuQBhA6IWh9BTMxCj4gI1vJm4NbzA+ HzN14ptn+VOj9tnPPf+Xy5kN6eoGpKPUJOumnWPjhtJawvvUDUb267wym Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="433286699" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="433286699" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jan 2024 03:11:13 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="736286620" X-IronPort-AV: E=Sophos;i="6.05,216,1701158400"; d="scan'208";a="736286620" Received: from apejovix-mobl1.ger.corp.intel.com (HELO wieczorr-mobl1.intel.com) ([10.213.0.239]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jan 2024 03:11:10 -0800 From: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com> To: reinette.chatre@intel.com, fenghua.yu@intel.com, shuah@kernel.org Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, ilpo.jarvinen@linux.intel.com Subject: [PATCH v3 2/5] selftests/resctrl: Add helpers for the non-contiguous test Date: Thu, 25 Jan 2024 12:10:55 +0100 Message-ID: <85b1efc3ddd698b3ac81aa72a6dc987ee17da3e2.1706180726.git.maciej.wieczor-retman@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <cover.1706180726.git.maciej.wieczor-retman@intel.com> References: <cover.1706180726.git.maciej.wieczor-retman@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789060956817691602 X-GMAIL-MSGID: 1789060956817691602 |
Series |
selftests/resctrl: Add non-contiguous CBMs in Intel CAT selftest
|
|
Commit Message
Maciej Wieczor-Retman
Jan. 25, 2024, 11:10 a.m. UTC
The CAT non-contiguous selftests have to read the file responsible for
reporting support of non-contiguous CBMs in kernel (resctrl). Then the
test compares if that information matches what is reported by CPUID
output.
Add a generic helper function to read an unsigned number from a file in
/sys/fs/resctrl/info/<RESOURCE>/<FILE>.
Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com>
---
Changelog v3:
- Rewrite patch message.
- Add documentation and rewrote the function. (Reinette)
Changelog v2:
- Add this patch.
tools/testing/selftests/resctrl/resctrl.h | 1 +
tools/testing/selftests/resctrl/resctrlfs.c | 39 +++++++++++++++++++++
2 files changed, 40 insertions(+)
Comments
On Thu, 25 Jan 2024, Maciej Wieczor-Retman wrote: > The CAT non-contiguous selftests have to read the file responsible for > reporting support of non-contiguous CBMs in kernel (resctrl). Then the > test compares if that information matches what is reported by CPUID > output. > > Add a generic helper function to read an unsigned number from a file in > /sys/fs/resctrl/info/<RESOURCE>/<FILE>. > > Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com> > --- > Changelog v3: > - Rewrite patch message. > - Add documentation and rewrote the function. (Reinette) > > Changelog v2: > - Add this patch. > > tools/testing/selftests/resctrl/resctrl.h | 1 + > tools/testing/selftests/resctrl/resctrlfs.c | 39 +++++++++++++++++++++ > 2 files changed, 40 insertions(+) > > diff --git a/tools/testing/selftests/resctrl/resctrl.h b/tools/testing/selftests/resctrl/resctrl.h > index a1462029998e..5116ea082d03 100644 > --- a/tools/testing/selftests/resctrl/resctrl.h > +++ b/tools/testing/selftests/resctrl/resctrl.h > @@ -162,6 +162,7 @@ unsigned int count_contiguous_bits(unsigned long val, unsigned int *start); > int get_full_cbm(const char *cache_type, unsigned long *mask); > int get_mask_no_shareable(const char *cache_type, unsigned long *mask); > int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size); > +int resource_info_unsigned_get(const char *resource, const char *filename, unsigned int *val); > void ctrlc_handler(int signum, siginfo_t *info, void *ptr); > int signal_handler_register(void); > void signal_handler_unregister(void); > diff --git a/tools/testing/selftests/resctrl/resctrlfs.c b/tools/testing/selftests/resctrl/resctrlfs.c > index 5750662cce57..cb5147c5f9a9 100644 > --- a/tools/testing/selftests/resctrl/resctrlfs.c > +++ b/tools/testing/selftests/resctrl/resctrlfs.c > @@ -249,6 +249,45 @@ static int get_bit_mask(const char *filename, unsigned long *mask) > return 0; > } > > +/* > + * resource_info_unsigned_get - Read an unsigned value from a file in > + * /sys/fs/resctrl/info/RESOURCE/FILENAME > + * @resource: Resource name that matches directory names in > + * /sys/fs/resctrl/info > + * @filename: Filename of a file located in a directory specified with the > + * 'resource' variable. > + * @val: Variable where the read value is saved on success. > + * > + * Return: = 0 on success, < 0 on failure. On success the read value is saved into the 'val' > + * variable. > + */ > +int resource_info_unsigned_get(const char *resource, const char *filename, > + unsigned int *val) > +{ > + char reason[128], file_path[PATH_MAX]; > + FILE *fp; > + > + snprintf(file_path, sizeof(file_path), "%s/%s/%s", INFO_PATH, resource, > + filename); > + > + fp = fopen(file_path, "r"); > + if (!fp) { > + snprintf(reason, sizeof(reason), "Error in opening %s file\n", filename); > + ksft_perror(reason); Was this the conclusion of the kstf_perror() discussion with Reinette? I expected a bit different outcome when I stopped following it... In any case, it would be nice though if ksft_perror() (or some kselftest.h function yet to be added with a different name) would accept full printf interface and just add the errno string into the end of the string so one would not need to build constructs like this at all. It will require a bit of macro trickery into kselftest.h. I don't know how it should handle the case where somebody just passes a char pointer to it, not a string literal, but I guess it would just throw an error while compiling if somebody tries to do that as the macro string literal concatenation could not build useful/compilable token. It would make these prints informative enough to become actually useful without needed to resort to preparing the string in advance which seems to be required almost every single case with the current interface. > + return -1; > + } > + > + if (fscanf(fp, "%u", val) <= 0) { > + snprintf(reason, sizeof(reason), "Could not get %s's contents\n", filename); > + ksft_perror(reason); > + fclose(fp); > + return -1; > + } > + > + fclose(fp); > + return 0; > +} > + > /* > * create_bit_mask- Create bit mask from start, len pair > * @start: LSB of the mask >
On 1/25/2024 4:14 AM, Ilpo Järvinen wrote: > On Thu, 25 Jan 2024, Maciej Wieczor-Retman wrote: >> + fp = fopen(file_path, "r"); >> + if (!fp) { >> + snprintf(reason, sizeof(reason), "Error in opening %s file\n", filename); >> + ksft_perror(reason); > > Was this the conclusion of the kstf_perror() discussion with Reinette? I > expected a bit different outcome when I stopped following it... > > In any case, it would be nice though if ksft_perror() (or some kselftest.h > function yet to be added with a different name) would accept full printf > interface and just add the errno string into the end of the string so one > would not need to build constructs like this at all. > > It will require a bit of macro trickery into kselftest.h. I don't know how > it should handle the case where somebody just passes a char pointer to it, > not a string literal, but I guess it would just throw an error while > compiling if somebody tries to do that as the macro string literal > concatenation could not build useful/compilable token. > > It would make these prints informative enough to become actually useful > without needed to resort to preparing the string in advance which seems > to be required almost every single case with the current interface. I think this can be accomplished with a new: void ksft_vprint_msg(const char *msg, va_list args) .. but ksft_perror() does conform to perror() and I expect that having one support variable number of arguments while the other does to cause confusion. To support variable number of arguments with errno I'd propose just to use ksft_print_msg() with strerror(errno), errno as the arguments (or even %m that that errno handling within ksft_print_msg() aims to support). This does indeed seem to be the custom in other tests. Reinette
Hi Maciej, On 1/25/2024 3:10 AM, Maciej Wieczor-Retman wrote: > The CAT non-contiguous selftests have to read the file responsible for > reporting support of non-contiguous CBMs in kernel (resctrl). Then the > test compares if that information matches what is reported by CPUID > output. > > Add a generic helper function to read an unsigned number from a file in > /sys/fs/resctrl/info/<RESOURCE>/<FILE>. > > Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com> > --- > Changelog v3: > - Rewrite patch message. > - Add documentation and rewrote the function. (Reinette) > > Changelog v2: > - Add this patch. > > tools/testing/selftests/resctrl/resctrl.h | 1 + > tools/testing/selftests/resctrl/resctrlfs.c | 39 +++++++++++++++++++++ > 2 files changed, 40 insertions(+) > > diff --git a/tools/testing/selftests/resctrl/resctrl.h b/tools/testing/selftests/resctrl/resctrl.h > index a1462029998e..5116ea082d03 100644 > --- a/tools/testing/selftests/resctrl/resctrl.h > +++ b/tools/testing/selftests/resctrl/resctrl.h > @@ -162,6 +162,7 @@ unsigned int count_contiguous_bits(unsigned long val, unsigned int *start); > int get_full_cbm(const char *cache_type, unsigned long *mask); > int get_mask_no_shareable(const char *cache_type, unsigned long *mask); > int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size); > +int resource_info_unsigned_get(const char *resource, const char *filename, unsigned int *val); > void ctrlc_handler(int signum, siginfo_t *info, void *ptr); > int signal_handler_register(void); > void signal_handler_unregister(void); > diff --git a/tools/testing/selftests/resctrl/resctrlfs.c b/tools/testing/selftests/resctrl/resctrlfs.c > index 5750662cce57..cb5147c5f9a9 100644 > --- a/tools/testing/selftests/resctrl/resctrlfs.c > +++ b/tools/testing/selftests/resctrl/resctrlfs.c > @@ -249,6 +249,45 @@ static int get_bit_mask(const char *filename, unsigned long *mask) > return 0; > } > > +/* By not starting with /** the following will not be interpreted as kernel-doc but the formatting does appear to follow the syntax, but not consistently so. I think it would be more readable if the kernel-doc syntax is followed consistently. > + * resource_info_unsigned_get - Read an unsigned value from a file in > + * /sys/fs/resctrl/info/RESOURCE/FILENAME "Read an unsigned value from /sys/fs/resctrl/info/RESOURCE/FILENAME"? > + * @resource: Resource name that matches directory names in names? (plural) > + * /sys/fs/resctrl/info > + * @filename: Filename of a file located in a directory specified with the > + * 'resource' variable. I think this can be shortened to "File in /sys/fs/resctrl/info/@resource" > + * @val: Variable where the read value is saved on success. "Contains read value on success." (no need to refer to it as a variable/parameter, it is implied by syntax). > + * > + * Return: = 0 on success, < 0 on failure. On success the read value is saved into the 'val' > + * variable. "saved into the 'val' variable" -> "saved into @val" (since syntax indicates it is the parameter there is no need to elaborate). Also please let lines in comments be of consistent length. > + */ > +int resource_info_unsigned_get(const char *resource, const char *filename, > + unsigned int *val) > +{ > + char reason[128], file_path[PATH_MAX]; > + FILE *fp; > + > + snprintf(file_path, sizeof(file_path), "%s/%s/%s", INFO_PATH, resource, > + filename); > + > + fp = fopen(file_path, "r"); > + if (!fp) { > + snprintf(reason, sizeof(reason), "Error in opening %s file\n", filename); (apart from other discussions). "file" in message seems redundant. It can just be "Error opening %s". It may also be useful to print file_path instead of filename to be specific of what the code tried to open. > + ksft_perror(reason); > + return -1; > + } > + > + if (fscanf(fp, "%u", val) <= 0) { > + snprintf(reason, sizeof(reason), "Could not get %s's contents\n", filename); > + ksft_perror(reason); filename -> file_path ? > + fclose(fp); > + return -1; > + } > + > + fclose(fp); > + return 0; > +} > + Reinette
Hello! On 2024-01-26 at 13:08:19 -0800, Reinette Chatre wrote: >Hi Maciej, > >On 1/25/2024 3:10 AM, Maciej Wieczor-Retman wrote: >> The CAT non-contiguous selftests have to read the file responsible for >> reporting support of non-contiguous CBMs in kernel (resctrl). Then the >> test compares if that information matches what is reported by CPUID >> output. >> >> Add a generic helper function to read an unsigned number from a file in >> /sys/fs/resctrl/info/<RESOURCE>/<FILE>. >> >> Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com> >> --- >> Changelog v3: >> - Rewrite patch message. >> - Add documentation and rewrote the function. (Reinette) >> >> Changelog v2: >> - Add this patch. >> >> tools/testing/selftests/resctrl/resctrl.h | 1 + >> tools/testing/selftests/resctrl/resctrlfs.c | 39 +++++++++++++++++++++ >> 2 files changed, 40 insertions(+) >> >> diff --git a/tools/testing/selftests/resctrl/resctrl.h b/tools/testing/selftests/resctrl/resctrl.h >> index a1462029998e..5116ea082d03 100644 >> --- a/tools/testing/selftests/resctrl/resctrl.h >> +++ b/tools/testing/selftests/resctrl/resctrl.h >> @@ -162,6 +162,7 @@ unsigned int count_contiguous_bits(unsigned long val, unsigned int *start); >> int get_full_cbm(const char *cache_type, unsigned long *mask); >> int get_mask_no_shareable(const char *cache_type, unsigned long *mask); >> int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size); >> +int resource_info_unsigned_get(const char *resource, const char *filename, unsigned int *val); >> void ctrlc_handler(int signum, siginfo_t *info, void *ptr); >> int signal_handler_register(void); >> void signal_handler_unregister(void); >> diff --git a/tools/testing/selftests/resctrl/resctrlfs.c b/tools/testing/selftests/resctrl/resctrlfs.c >> index 5750662cce57..cb5147c5f9a9 100644 >> --- a/tools/testing/selftests/resctrl/resctrlfs.c >> +++ b/tools/testing/selftests/resctrl/resctrlfs.c >> @@ -249,6 +249,45 @@ static int get_bit_mask(const char *filename, unsigned long *mask) >> return 0; >> } >> >> +/* > >By not starting with /** the following will not be interpreted as kernel-doc >but the formatting does appear to follow the syntax, but not consistently so. >I think it would be more readable if the kernel-doc syntax is followed consistently. Sure, I'll change it. > >> + * resource_info_unsigned_get - Read an unsigned value from a file in >> + * /sys/fs/resctrl/info/RESOURCE/FILENAME > >"Read an unsigned value from /sys/fs/resctrl/info/RESOURCE/FILENAME"? Okay > >> + * @resource: Resource name that matches directory names in > >names? (plural) Right, it doesn't make much sense, I'll move it to singular. > >> + * /sys/fs/resctrl/info >> + * @filename: Filename of a file located in a directory specified with the >> + * 'resource' variable. > >I think this can be shortened to "File in /sys/fs/resctrl/info/@resource" Sure, thanks > >> + * @val: Variable where the read value is saved on success. > >"Contains read value on success." > >(no need to refer to it as a variable/parameter, it is implied by syntax). Right, I'll change it. > >> + * >> + * Return: = 0 on success, < 0 on failure. On success the read value is saved into the 'val' >> + * variable. > >"saved into the 'val' variable" -> "saved into @val" (since syntax indicates it is the parameter >there is no need to elaborate). Sure, thanks >Also please let lines in comments be of consistent length. Okay, I'll keep it to 80 characters. > >> + */ > > >> +int resource_info_unsigned_get(const char *resource, const char *filename, >> + unsigned int *val) >> +{ >> + char reason[128], file_path[PATH_MAX]; >> + FILE *fp; >> + >> + snprintf(file_path, sizeof(file_path), "%s/%s/%s", INFO_PATH, resource, >> + filename); >> + >> + fp = fopen(file_path, "r"); >> + if (!fp) { >> + snprintf(reason, sizeof(reason), "Error in opening %s file\n", filename); > >(apart from other discussions). "file" in message seems redundant. It can just be "Error >opening %s". It may also be useful to print file_path instead of filename to be specific >of what the code tried to open. Okay, I'll change it to file_path. > >> + ksft_perror(reason); >> + return -1; >> + } >> + >> + if (fscanf(fp, "%u", val) <= 0) { >> + snprintf(reason, sizeof(reason), "Could not get %s's contents\n", filename); >> + ksft_perror(reason); > >filename -> file_path ? Same as above. > >> + fclose(fp); >> + return -1; >> + } >> + >> + fclose(fp); >> + return 0; >> +} >> + > > >Reinette
Hi, On 2024-01-26 at 10:58:04 -0800, Reinette Chatre wrote: > > >On 1/25/2024 4:14 AM, Ilpo Järvinen wrote: >> On Thu, 25 Jan 2024, Maciej Wieczor-Retman wrote: > > >>> + fp = fopen(file_path, "r"); >>> + if (!fp) { >>> + snprintf(reason, sizeof(reason), "Error in opening %s file\n", filename); >>> + ksft_perror(reason); >> >> Was this the conclusion of the kstf_perror() discussion with Reinette? I >> expected a bit different outcome when I stopped following it... >> >> In any case, it would be nice though if ksft_perror() (or some kselftest.h >> function yet to be added with a different name) would accept full printf >> interface and just add the errno string into the end of the string so one >> would not need to build constructs like this at all. >> >> It will require a bit of macro trickery into kselftest.h. I don't know how >> it should handle the case where somebody just passes a char pointer to it, >> not a string literal, but I guess it would just throw an error while >> compiling if somebody tries to do that as the macro string literal >> concatenation could not build useful/compilable token. >> >> It would make these prints informative enough to become actually useful >> without needed to resort to preparing the string in advance which seems >> to be required almost every single case with the current interface. > >I think this can be accomplished with a new: > void ksft_vprint_msg(const char *msg, va_list args) > >... but ksft_perror() does conform to perror() and I expect that having one >support variable number of arguments while the other does to cause confusion. > >To support variable number of arguments with errno I'd propose just to use >ksft_print_msg() with strerror(errno), errno as the arguments (or even %m >that that errno handling within ksft_print_msg() aims to support). This does >indeed seem to be the custom in other tests. Does something like this look okay? fp = fopen(file_path, "r"); if (!fp) { ksft_print_msg("Error in opening %s\n: %m\n", file_path); return -1; } The '%m' seems to work fine but doesn't print errno's number code. Do you want me to add errno after '%m' so it is the same as ksft_perror()? I looked through some other tests where '%m' is used, and only few ones add errno with '%d'. >Reinette
On Wed, 31 Jan 2024, Maciej Wieczor-Retman wrote: > On 2024-01-26 at 10:58:04 -0800, Reinette Chatre wrote: > >On 1/25/2024 4:14 AM, Ilpo Järvinen wrote: > >> On Thu, 25 Jan 2024, Maciej Wieczor-Retman wrote: > > > >>> + fp = fopen(file_path, "r"); > >>> + if (!fp) { > >>> + snprintf(reason, sizeof(reason), "Error in opening %s file\n", filename); > >>> + ksft_perror(reason); > >> > >> Was this the conclusion of the kstf_perror() discussion with Reinette? I > >> expected a bit different outcome when I stopped following it... > >> > >> In any case, it would be nice though if ksft_perror() (or some kselftest.h > >> function yet to be added with a different name) would accept full printf > >> interface and just add the errno string into the end of the string so one > >> would not need to build constructs like this at all. > >> > >> It will require a bit of macro trickery into kselftest.h. I don't know how > >> it should handle the case where somebody just passes a char pointer to it, > >> not a string literal, but I guess it would just throw an error while > >> compiling if somebody tries to do that as the macro string literal > >> concatenation could not build useful/compilable token. > >> > >> It would make these prints informative enough to become actually useful > >> without needed to resort to preparing the string in advance which seems > >> to be required almost every single case with the current interface. > > > >I think this can be accomplished with a new: > > void ksft_vprint_msg(const char *msg, va_list args) > > > >... but ksft_perror() does conform to perror() and I expect that having one > >support variable number of arguments while the other does to cause confusion. > > > >To support variable number of arguments with errno I'd propose just to use > >ksft_print_msg() with strerror(errno), errno as the arguments (or even %m > >that that errno handling within ksft_print_msg() aims to support). This does > >indeed seem to be the custom in other tests. > > Does something like this look okay? > > fp = fopen(file_path, "r"); > if (!fp) { > ksft_print_msg("Error in opening %s\n: %m\n", file_path); > return -1; > } > > The '%m' seems to work fine but doesn't print errno's number code. Do you want > me to add errno after '%m' so it is the same as ksft_perror()? I looked through > some other tests where '%m' is used, and only few ones add errno with '%d'. I think %m is enough.
diff --git a/tools/testing/selftests/resctrl/resctrl.h b/tools/testing/selftests/resctrl/resctrl.h index a1462029998e..5116ea082d03 100644 --- a/tools/testing/selftests/resctrl/resctrl.h +++ b/tools/testing/selftests/resctrl/resctrl.h @@ -162,6 +162,7 @@ unsigned int count_contiguous_bits(unsigned long val, unsigned int *start); int get_full_cbm(const char *cache_type, unsigned long *mask); int get_mask_no_shareable(const char *cache_type, unsigned long *mask); int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size); +int resource_info_unsigned_get(const char *resource, const char *filename, unsigned int *val); void ctrlc_handler(int signum, siginfo_t *info, void *ptr); int signal_handler_register(void); void signal_handler_unregister(void); diff --git a/tools/testing/selftests/resctrl/resctrlfs.c b/tools/testing/selftests/resctrl/resctrlfs.c index 5750662cce57..cb5147c5f9a9 100644 --- a/tools/testing/selftests/resctrl/resctrlfs.c +++ b/tools/testing/selftests/resctrl/resctrlfs.c @@ -249,6 +249,45 @@ static int get_bit_mask(const char *filename, unsigned long *mask) return 0; } +/* + * resource_info_unsigned_get - Read an unsigned value from a file in + * /sys/fs/resctrl/info/RESOURCE/FILENAME + * @resource: Resource name that matches directory names in + * /sys/fs/resctrl/info + * @filename: Filename of a file located in a directory specified with the + * 'resource' variable. + * @val: Variable where the read value is saved on success. + * + * Return: = 0 on success, < 0 on failure. On success the read value is saved into the 'val' + * variable. + */ +int resource_info_unsigned_get(const char *resource, const char *filename, + unsigned int *val) +{ + char reason[128], file_path[PATH_MAX]; + FILE *fp; + + snprintf(file_path, sizeof(file_path), "%s/%s/%s", INFO_PATH, resource, + filename); + + fp = fopen(file_path, "r"); + if (!fp) { + snprintf(reason, sizeof(reason), "Error in opening %s file\n", filename); + ksft_perror(reason); + return -1; + } + + if (fscanf(fp, "%u", val) <= 0) { + snprintf(reason, sizeof(reason), "Could not get %s's contents\n", filename); + ksft_perror(reason); + fclose(fp); + return -1; + } + + fclose(fp); + return 0; +} + /* * create_bit_mask- Create bit mask from start, len pair * @start: LSB of the mask