Skip to contentSkip to conversation

How to get number of physical CPU in linux from user space?

PATCH In review Nakajima, Jun 6 participants 8 messages Oct 25, 2002 – Nov 8, 2002

lore
Share & tools
lore
#0 lore raw

Recent distributions or the AC tree has additional fields in /proc/cpu, which tell
- physical package id
- number of threads
for each CPU.

Using this info, you should be able to detect it. The problem is that they are not using the same keywords. I'm asking them to make those fields consistent.

Thanks,
Jun

-----Original Message-----
From: [email protected] [mailto:[email protected]]
Sent: Friday, October 25, 2002 11:20 AM
To: Martin J. Bligh
Cc: [email protected]
Subject: Re: How to get number of physical CPU in linux from user space?

Quoted 1 line · Martin J. Bligh

Define "physical CPU number" ;-) If you want to deteact which

I mean the number of cpu chip you can count on the mother board.

Quoted 4 lines

ones are paired up, I believe that if all but the last bit of the apicid is the same, they're siblings. You might have to dig the apicid out of the bootlog if the cpuinfo stuff doesn't tell you.

And you are right. Those apicid, after mask out the siblings, are put in phys_cpu_id[] array in kernel.

I think about look at bootlog too, but that is not a reliable way because bootlog might already been flush out after some time.

Cheers

Chris

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#1 lore raw

PATCH 1/1 How to get number of physical CPU in linux from user space?

Quoted 4 lines · Jun

Recent distributions or the AC tree has additional fields in /proc/cpu, which tell
- physical package id
- number of threads

Attached patch for 2.5 adds the same fields the 2.4-ac tree have. I consider those "standard" enough.

Is this something HT users want?

Robert Love

 proc.c |    5 +++++
 1 files changed, 5 insertions(+)
arch/i386/kernel/cpu/proc.c +5 -0
diff -urN linux-2.5.44/arch/i386/kernel/cpu/proc.c linux/arch/i386/kernel/cpu/proc.c
@@ -17,6 +17,7 @@
 	 * applications want to get the raw CPUID data, they should access
 	 * /dev/cpu/<cpu_nr>/cpuid instead.
 	 */
+	extern int phys_proc_id[NR_CPUS];
 	static char *x86_cap_flags[] = {
 		/* Intel-defined */
 	        "fpu", "vme", "de", "pse", "tsc", "msr", "pae", "mce",
@@ -74,6 +75,10 @@
 	/* Cache size */
 	if (c->x86_cache_size >= 0)
 		seq_printf(m, "cache size\t: %d KB\n", c->x86_cache_size);
+#ifdef CONFIG_SMP
+	seq_printf(m, "physical processor ID\t: %d\n", phys_proc_id[n]);
+	seq_printf(m, "number of siblings\t: %d\n", smp_num_siblings);
+#endif
 	
 	/* We use exception 16 if we have hardware math and we've either seen it or the CPU claims it is internal */
 	fpu_exception = c->hard_math && (ignore_irq13 || cpu_has_fpu);
#2 lore raw

arch/i386/kernel/cpu/proc.c line 81

Quoted 4 lines · Robert Love
arch/i386/kernel/cpu/proc.c +4 -0
+#ifdef CONFIG_SMP
+	seq_printf(m, "physical processor ID\t: %d\n", phys_proc_id[n]);
+	seq_printf(m, "number of siblings\t: %d\n", smp_num_siblings);
+#endif

Should this be wrapped in a if (cpu_has_ht(c)) { } ? Seems silly to be displaying HT information on non-HT CPUs.

Dave

Signature · 1 line
| Dave Jones.        http://www.codemonkey.org.uk
#3 lore raw
Quoted 2 lines · Dave Jones

Should this be wrapped in a if (cpu_has_ht(c)) { } ? Seems silly to be displaying HT information on non-HT CPUs.

I am neutral, but is fine with me. It is just "cpu_has_ht", btw.

Take two...

This displays the physical processor id and number of siblings of each processor in /proc/cpuinfo.

Robert Love

.proc.c.swp |binary

 proc.c      |    7 +++++++
 2 files changed, 7 insertions(+)
arch/i386/kernel/cpu/proc.c +7 -0
diff -urN linux-2.5.44/arch/i386/kernel/cpu/proc.c linux/arch/i386/kernel/cpu/proc.c
@@ -17,6 +17,7 @@
 	 * applications want to get the raw CPUID data, they should access
 	 * /dev/cpu/<cpu_nr>/cpuid instead.
 	 */
+	extern int phys_proc_id[NR_CPUS];
 	static char *x86_cap_flags[] = {
 		/* Intel-defined */
 	        "fpu", "vme", "de", "pse", "tsc", "msr", "pae", "mce",
@@ -74,6 +75,12 @@
 	/* Cache size */
 	if (c->x86_cache_size >= 0)
 		seq_printf(m, "cache size\t: %d KB\n", c->x86_cache_size);
+#ifdef CONFIG_SMP
+	if (cpu_has_ht) {
+		seq_printf(m, "physical processor ID\t: %d\n", phys_proc_id[n]);
+		seq_printf(m, "number of siblings\t: %d\n", smp_num_siblings);
+	}
+#endif
 	
 	/* We use exception 16 if we have hardware math and we've either seen it or the CPU claims it is internal */
 	fpu_exception = c->hard_math && (ignore_irq13 || cpu_has_fpu);
#4 lore raw

Cool. That will work for me.

Thanks

Chris

Quoted 43 lines · Robert Love
Quoted 2 lines · Dave Jones

Should this be wrapped in a if (cpu_has_ht(c)) { } ? Seems silly to be displaying HT information on non-HT CPUs.

I am neutral, but is fine with me. It is just "cpu_has_ht", btw.

Take two...

This displays the physical processor id and number of siblings of each processor in /proc/cpuinfo.

Robert Love

.proc.c.swp |binary

 proc.c      |    7 +++++++
 2 files changed, 7 insertions(+)

diff -urN linux-2.5.44/arch/i386/kernel/cpu/proc.c linux/arch/i386/kernel/cpu/proc.c

arch/i386/kernel/cpu/proc.c +7 -0
@@ -17,6 +17,7 @@
 	 * applications want to get the raw CPUID data, they should access
 	 * /dev/cpu/<cpu_nr>/cpuid instead.
 	 */
+	extern int phys_proc_id[NR_CPUS];
 	static char *x86_cap_flags[] = {
 		/* Intel-defined */
 	        "fpu", "vme", "de", "pse", "tsc", "msr", "pae", "mce",
@@ -74,6 +75,12 @@
 	/* Cache size */
 	if (c->x86_cache_size >= 0)
 		seq_printf(m, "cache size\t: %d KB\n", c->x86_cache_size);
+#ifdef CONFIG_SMP
+	if (cpu_has_ht) {
+		seq_printf(m, "physical processor ID\t: %d\n", phys_proc_id[n]);
+		seq_printf(m, "number of siblings\t: %d\n", smp_num_siblings);
+	}
+#endif
 
 	/* We use exception 16 if we have hardware math and we've either seen it or the CPU claims it is internal */
 	fpu_exception = c->hard_math && (ignore_irq13 || cpu_has_fpu);
#5 lore raw
In reply to Dave Jones
Quoted 7 lines

arch/i386/kernel/cpu/proc.c line 81

Quoted 4 lines
arch/i386/kernel/cpu/proc.c +4 -0
+#ifdef CONFIG_SMP
+	seq_printf(m, "physical processor ID\t: %d\n", phys_proc_id[n]);
+	seq_printf(m, "number of siblings\t: %d\n", smp_num_siblings);
+#endif

Should this be wrapped in a if (cpu_has_ht(c)) { } ? Seems silly to be displaying HT information on non-HT CPUs.

Well, without if () you can tell the difference between 'no hyperthreading' and 'too old kernel to tell me about hyperthreading so I'd suggest kill the if ().
Pavel

#6 lore raw
In reply to Robert Love

arch/i386/kernel/cpu/proc.c line 81

Quoted 4 lines · Robert Love
arch/i386/kernel/cpu/proc.c +4 -0
+#ifdef CONFIG_SMP
+	seq_printf(m, "physical processor ID\t: %d\n", phys_proc_id[n]);
+	seq_printf(m, "number of siblings\t: %d\n", smp_num_siblings);
+#endif

Congratulations you just broke glibc. There are reasons the -ac patch was changed to print something a little different.

#7 lore raw
In reply to Nakajima, Jun
Quoted 5 lines · Jun

Recent distributions or the AC tree has additional fields in /proc/cpu, which tell
- physical package id
- number of threads
for each CPU.

That is exactly what I am looking for.

Quoted 4 lines

Using this info, you should be able to detect it. The problem is that they are not using the same keywords. I'm asking them to make those fields consistent.

Cool. Any idea when will those feature come to stander linux kernel?

Thanks.

Chris