Util-Linux Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] lscpu: Add shared cached info for s390 lscpu -C
@ 2020-06-05 16:15 Sumanth Korikkar
  2020-06-08  7:55 ` Karel Zak
  0 siblings, 1 reply; 2+ messages in thread
From: Sumanth Korikkar @ 2020-06-05 16:15 UTC (permalink / raw)
  To: kzak, util-linux; +Cc: heiko.carstens, tmricht, svens, Sumanth Korikkar

The shared cache info for s390 can be found in /proc/cpuinfo.
lscpu without any options already processes this info. Fix this
in lscpu -C and provide detailed stat.

Test for s390:
./lscpu -C
NAME ONE-SIZE ALL-SIZE WAYS TYPE        LEVEL  SETS PHY-LINE COHERENCY-SIZE
L1d      128K     256K    8 Data            1    64                     256
L1i      128K     256K    8 Instruction     1    64                     256
L2d        4M       8M    8 Data            2  2048                     256
L2i        2M       4M    8 Instruction     2  1024                     256
L3       128M            32 Unified         3 16384                     256
L4       672M            42 Unified         4 65536                     256

Signed-off-by: Sumanth Korikkar <sumanthk@linux.ibm.com>
---
 sys-utils/lscpu.c | 169 +++++++++++++++++++++++++++-------------------
 1 file changed, 101 insertions(+), 68 deletions(-)

diff --git a/sys-utils/lscpu.c b/sys-utils/lscpu.c
index d6c3f2e83..d5ba84de3 100644
--- a/sys-utils/lscpu.c
+++ b/sys-utils/lscpu.c
@@ -319,7 +319,7 @@ lookup_cache(char *line, struct lscpu_desc *desc)
 	struct cpu_cache *cache;
 	long long size;
 	char *p, type;
-	int level;
+	int level, line_size, associativity;
 
 	/* Make sure line starts with "cache<nr> :" */
 	if (strncmp(line, "cache", 5) != 0)
@@ -350,6 +350,14 @@ lookup_cache(char *line, struct lscpu_desc *desc)
 	if (!p || sscanf(p, "size=%lld", &size) != 1)
 	       return 0;
 
+	p = strstr(line, "line_size=");
+	if (!p || sscanf(p, "line_size=%u", &line_size) != 1)
+		return 0;
+
+	p = strstr(line, "associativity=");
+	if (!p || sscanf(p, "associativity=%u", &associativity) != 1)
+		return 0;
+
 	desc->necaches++;
 	desc->ecaches = xrealloc(desc->ecaches,
 				 desc->necaches * sizeof(struct cpu_cache));
@@ -363,6 +371,11 @@ lookup_cache(char *line, struct lscpu_desc *desc)
 
 	cache->level = level;
 	cache->size = size * 1024;
+	cache->ways_of_associativity = associativity;
+	cache->coherency_line_size = line_size;
+	/* Number of sets for s390. For safety, just check divide by zero */
+	cache->number_of_sets = line_size ? (cache->size / line_size): 0;
+	cache->number_of_sets = associativity ? (cache->number_of_sets / associativity) : 0;
 
 	cache->type = type == 'i' ? xstrdup("Instruction") :
 		      type == 'd' ? xstrdup("Data") :
@@ -1673,82 +1686,102 @@ print_caches_readable(struct lscpu_desc *desc, int cols[], int ncols,
 			err(EXIT_FAILURE, _("failed to allocate output column"));
 	}
 
-	for (i = desc->ncaches - 1; i >= 0; i--) {
-		struct cpu_cache *ca = &desc->caches[i];
-		struct libscols_line *line;
-		int c;
+	struct cpu_cache *ca, *cachesrc;
+	int end, j, shared_allsize;
+	for (j = 0; j < 2; j++) {
+		/* First check the caches from /sys/devices */
+		if (j == 0) {
+			cachesrc = desc->caches;
+			end = desc->ncaches - 1;
+			shared_allsize = 0;
+		}
+		else {
+			/* Check shared caches from /proc/cpuinfo s390 */
+			cachesrc = desc->ecaches;
+			end = desc->necaches - 1;
+			/* Dont use get_cache_full_size */
+			shared_allsize = 1;
+		}
 
-		line = scols_table_new_line(table, NULL);
-		if (!line)
-			err(EXIT_FAILURE, _("failed to allocate output line"));
+		for (i = end; i >= 0; i--) {
+			ca = &cachesrc[i];
+			struct libscols_line *line;
+			int c;
 
-		for (c = 0; c < ncols; c++) {
-			char *data = NULL;
-			int col = cols[c];
+			line = scols_table_new_line(table, NULL);
+			if (!line)
+				err(EXIT_FAILURE, _("failed to allocate output line"));
 
-			switch (col) {
-			case COL_CACHE_NAME:
-				if (ca->name)
-					data = xstrdup(ca->name);
-				break;
-			case COL_CACHE_ONESIZE:
-				if (!ca->size)
+			for (c = 0; c < ncols; c++) {
+				char *data = NULL;
+				int col = cols[c];
+
+				switch (col) {
+				case COL_CACHE_NAME:
+					if (ca->name)
+						data = xstrdup(ca->name);
+					break;
+				case COL_CACHE_ONESIZE:
+					if (!ca->size)
+						break;
+					if (mod->bytes)
+						xasprintf(&data, "%" PRIu64, ca->size);
+					else
+						data = size_to_human_string(SIZE_SUFFIX_1LETTER, ca->size);
+					break;
+				case COL_CACHE_ALLSIZE:
+				{
+					uint64_t sz = 0;
+					if (shared_allsize)
+						break;
+					if (get_cache_full_size(desc, ca, &sz) != 0)
+						break;
+					if (mod->bytes)
+						xasprintf(&data, "%" PRIu64, sz);
+					else
+						data = size_to_human_string(SIZE_SUFFIX_1LETTER, sz);
+					break;
+				}
+				case COL_CACHE_WAYS:
+					if (ca->ways_of_associativity)
+						xasprintf(&data, "%u", ca->ways_of_associativity);
 					break;
-				if (mod->bytes)
-					xasprintf(&data, "%" PRIu64, ca->size);
-				else
-					data = size_to_human_string(SIZE_SUFFIX_1LETTER, ca->size);
-				break;
-			case COL_CACHE_ALLSIZE:
-			{
-				uint64_t sz = 0;
 
-				if (get_cache_full_size(desc, ca, &sz) != 0)
+				case COL_CACHE_TYPE:
+					if (ca->type)
+						data = xstrdup(ca->type);
 					break;
-				if (mod->bytes)
-					xasprintf(&data, "%" PRIu64, sz);
-				else
-					data = size_to_human_string(SIZE_SUFFIX_1LETTER, sz);
-				break;
-			}
-			case COL_CACHE_WAYS:
-				if (ca->ways_of_associativity)
-					xasprintf(&data, "%u", ca->ways_of_associativity);
-				break;
+				case COL_CACHE_LEVEL:
+					if (ca->level)
+						xasprintf(&data, "%d", ca->level);
+					break;
+				case COL_CACHE_ALLOCPOL:
+					if (ca->allocation_policy)
+						data = xstrdup(ca->allocation_policy);
+					break;
+				case COL_CACHE_WRITEPOL:
+					if (ca->write_policy)
+						data = xstrdup(ca->write_policy);
+					break;
+				case COL_CACHE_PHYLINE:
+					if (ca->physical_line_partition)
+						xasprintf(&data, "%u", ca->physical_line_partition);
+					break;
+				case COL_CACHE_SETS:
+					if (ca->number_of_sets)
+						xasprintf(&data, "%u", ca->number_of_sets);
+					break;
+				case COL_CACHE_COHERENCYSIZE:
+					if (ca->coherency_line_size)
+						xasprintf(&data, "%u", ca->coherency_line_size);
+					break;
+				}
 
-			case COL_CACHE_TYPE:
-				if (ca->type)
-					data = xstrdup(ca->type);
-				break;
-			case COL_CACHE_LEVEL:
-				if (ca->level)
-					xasprintf(&data, "%d", ca->level);
-				break;
-			case COL_CACHE_ALLOCPOL:
-				if (ca->allocation_policy)
-					data = xstrdup(ca->allocation_policy);
-				break;
-			case COL_CACHE_WRITEPOL:
-				if (ca->write_policy)
-					data = xstrdup(ca->write_policy);
-				break;
-			case COL_CACHE_PHYLINE:
-				if (ca->physical_line_partition)
-					xasprintf(&data, "%u", ca->physical_line_partition);
-				break;
-			case COL_CACHE_SETS:
-				if (ca->number_of_sets)
-					xasprintf(&data, "%u", ca->number_of_sets);
-				break;
-			case COL_CACHE_COHERENCYSIZE:
-				if (ca->coherency_line_size)
-					xasprintf(&data, "%u", ca->coherency_line_size);
-				break;
+				if (data && scols_line_refer_data(line, c, data))
+					err(EXIT_FAILURE, _("failed to add output data"));
 			}
-
-			if (data && scols_line_refer_data(line, c, data))
-				err(EXIT_FAILURE, _("failed to add output data"));
 		}
+
 	}
 
 	scols_print_table(table);
-- 
2.23.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] lscpu: Add shared cached info for s390 lscpu -C
  2020-06-05 16:15 [PATCH] lscpu: Add shared cached info for s390 lscpu -C Sumanth Korikkar
@ 2020-06-08  7:55 ` Karel Zak
  0 siblings, 0 replies; 2+ messages in thread
From: Karel Zak @ 2020-06-08  7:55 UTC (permalink / raw)
  To: Sumanth Korikkar; +Cc: util-linux, heiko.carstens, tmricht, svens

On Fri, Jun 05, 2020 at 06:15:10PM +0200, Sumanth Korikkar wrote:
>  sys-utils/lscpu.c | 169 +++++++++++++++++++++++++++-------------------
>  1 file changed, 101 insertions(+), 68 deletions(-)

Applied, thanks.

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-05 16:15 [PATCH] lscpu: Add shared cached info for s390 lscpu -C Sumanth Korikkar
2020-06-08  7:55 ` Karel Zak

Util-Linux Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/util-linux/0 util-linux/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 util-linux util-linux/ https://lore.kernel.org/util-linux \
		util-linux@vger.kernel.org
	public-inbox-index util-linux

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.util-linux


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git