* find the node that a cpu belongs to
@ 2009-03-17 1:09 Kornilios Kourtis
2009-03-17 10:45 ` Andi Kleen
0 siblings, 1 reply; 6+ messages in thread
From: Kornilios Kourtis @ 2009-03-17 1:09 UTC (permalink / raw)
To: linux-numa
Hi,
I was wondering if you would be interested in adding a libnuma function
for determing the node that a cpu belongs to.
Something like this maybe ?
I could write a more complete patch if needed
(libnuma.h,version.ldscript,numa.3 ?)
--- libnuma.c.2 2009-03-16 23:32:52.000000000 +0000
+++ libnuma.c 2009-03-17 00:28:21.000000000 +0000
@@ -1784,3 +1784,34 @@ err:
numa_bitmask_free(mask);
return NULL;
}
+
+int numa_node_from_cpu_v2(int cpu)
+{
+ struct bitmask *bmp;
+ int ncpus, nnodes, node, ret;
+
+ ncpus = numa_num_possible_cpus();
+ if (cpu > ncpus){
+ errno = ERANGE;
+ return -1;
+ }
+
+ bmp = numa_bitmask_alloc(ncpus);
+ nnodes = numa_num_configured_nodes();
+ for (node = 0; node < nnodes; node++){
+ numa_node_to_cpus_v2(node, bmp);
+ if (numa_bitmask_isbitset(bmp, cpu)){
+ ret = node;
+ goto end;
+ }
+ }
+
+ /* I don't think this is supposed to happen */
+ ret = -1;
+ errno = EINVAL;
+end:
+ numa_bitmask_free(bmp);
+ return ret;
+}
+__asm__(".symver numa_node_from_cpu_v2,numa_node_from_cpu@@libnuma_1.2");
--
Kornilios Kourtis
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: find the node that a cpu belongs to 2009-03-17 1:09 find the node that a cpu belongs to Kornilios Kourtis @ 2009-03-17 10:45 ` Andi Kleen 2009-03-17 11:32 ` Kornilios Kourtis 0 siblings, 1 reply; 6+ messages in thread From: Andi Kleen @ 2009-03-17 10:45 UTC (permalink / raw) To: Kornilios Kourtis; +Cc: linux-numa On Tue, Mar 17, 2009 at 03:09:10AM +0200, Kornilios Kourtis wrote: > Hi, > > I was wondering if you would be interested in adding a libnuma function > for determing the node that a cpu belongs to. Concrete use case please. Why would you want to do that in a program? -Andi -- ak@linux.intel.com -- Speaking for myself only. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: find the node that a cpu belongs to 2009-03-17 10:45 ` Andi Kleen @ 2009-03-17 11:32 ` Kornilios Kourtis 2009-03-17 11:59 ` Andi Kleen 2009-03-19 14:16 ` Cliff Wickman 0 siblings, 2 replies; 6+ messages in thread From: Kornilios Kourtis @ 2009-03-17 11:32 UTC (permalink / raw) To: Andi Kleen; +Cc: linux-numa On Tue, Mar 17, 2009 at 11:45:22AM +0100, Andi Kleen wrote: > On Tue, Mar 17, 2009 at 03:09:10AM +0200, Kornilios Kourtis wrote: > > Hi, > > > > I was wondering if you would be interested in adding a libnuma function > > for determing the node that a cpu belongs to. > > Concrete use case please. Why would you want to do that in a program? My specific case is quite simple: I have a program that creates a number of threads that are bound to run in specific cpus. I want to pre-allocate memory in the node of the cpu that the thread will run before the threads are created using numa_alloc_onnode(). Obviously, there isn't a hard requirement to include such a function in libnuma, since it could be implemented using existing functions, or make the allocations after the threads are spawned using numa_alloc_local(). On the other hand I'm guessing that it could be useful to other cases as well. cheers, -- Kornilios Kourtis ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: find the node that a cpu belongs to 2009-03-17 11:32 ` Kornilios Kourtis @ 2009-03-17 11:59 ` Andi Kleen 2009-03-18 15:19 ` Kornilios Kourtis 2009-03-19 14:16 ` Cliff Wickman 1 sibling, 1 reply; 6+ messages in thread From: Andi Kleen @ 2009-03-17 11:59 UTC (permalink / raw) To: Kornilios Kourtis; +Cc: Andi Kleen, linux-numa On Tue, Mar 17, 2009 at 01:32:42PM +0200, Kornilios Kourtis wrote: > On Tue, Mar 17, 2009 at 11:45:22AM +0100, Andi Kleen wrote: > > On Tue, Mar 17, 2009 at 03:09:10AM +0200, Kornilios Kourtis wrote: > > > Hi, > > > > > > I was wondering if you would be interested in adding a libnuma function > > > for determing the node that a cpu belongs to. > > > > Concrete use case please. Why would you want to do that in a program? > > My specific case is quite simple: I have a program that creates a number > of threads that are bound to run in specific cpus. I want to pre-allocate > memory in the node of the cpu that the thread will run before the threads > are created using numa_alloc_onnode(). Normally the number of threads is dependent on the data layout, isn't it? It seems like a backwards way of doing things, but ok. BTW another known topology reporting hole is there is currently no way to report number of CPUs for a given node. Normally it's just max/cpus, but that can change with CPU hotplug. On the other hand CPU/node hotplug is currently not really supported anyways. -Andi -- ak@linux.intel.com -- Speaking for myself only. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: find the node that a cpu belongs to 2009-03-17 11:59 ` Andi Kleen @ 2009-03-18 15:19 ` Kornilios Kourtis 0 siblings, 0 replies; 6+ messages in thread From: Kornilios Kourtis @ 2009-03-18 15:19 UTC (permalink / raw) To: linux-numa; +Cc: Andi Kleen On Tue, Mar 17, 2009 at 12:59:07PM +0100, Andi Kleen wrote: > Normally the number of threads is dependent on the data layout, isn't it? > It seems like a backwards way of doing things, but ok. I'm including a better patch for this. > BTW another known topology reporting hole is there is currently no way > to report number of CPUs for a given node. Normally it's just max/cpus, > but that can change with CPU hotplug. On the other hand CPU/node hotplug > is currently not really supported anyways. I took a quick look at this and I guess the most straightforward way would be to intersect the set of numa_node_to_cpus() with the set of online cpus (/sys/devices/system/cpu/online). I'm not exactly sure about the format of this file and a quick grep at kernel sources didn't reveal much. I might take another look if I find some idle time. cheers, Add a numa_node_of_cpu() function for retrieving the local node of a cpu. diff -uprN numactl-2.0.3-rc2.orig/libnuma.c numactl-2.0.3-rc2/libnuma.c --- numactl-2.0.3-rc2.orig/libnuma.c 2009-03-17 14:37:14.000000000 +0200 +++ numactl-2.0.3-rc2/libnuma.c 2009-03-18 16:44:29.000000000 +0200 @@ -1310,6 +1310,33 @@ __asm__(".symver numa_node_to_cpus_v2,nu make_internal_alias(numa_node_to_cpus_v1); make_internal_alias(numa_node_to_cpus_v2); +/* report the node of the specified cpu */ +int numa_node_of_cpu(int cpu) +{ + struct bitmask *bmp; + int ncpus, nnodes, node, ret; + + ncpus = numa_num_possible_cpus(); + if (cpu > ncpus){ + errno = EINVAL; + return -1; + } + bmp = numa_bitmask_alloc(ncpus); + nnodes = numa_num_configured_nodes(); + for (node = 0; node < nnodes; node++){ + numa_node_to_cpus_v2_int(node, bmp); + if (numa_bitmask_isbitset(bmp, cpu)){ + ret = node; + goto end; + } + } + ret = -1; + errno = EINVAL; +end: + numa_bitmask_free(bmp); + return ret; +} + int numa_run_on_node_mask_v1(const nodemask_t *mask) diff -uprN numactl-2.0.3-rc2.orig/numa.3 numactl-2.0.3-rc2/numa.3 --- numactl-2.0.3-rc2.orig/numa.3 2009-03-17 14:37:14.000000000 +0200 +++ numactl-2.0.3-rc2/numa.3 2009-03-18 16:54:29.000000000 +0200 @@ -115,6 +115,8 @@ numa \- NUMA policy library .BI "int numa_sched_setaffinity(pid_t " pid ", struct bitmask *" mask ); .br .BI "int numa_node_to_cpus(int " node ", unsigned long *" buffer ", int " bufferlen ); +.br +.BI "int numa_node_of_cpu(int " cpu "); .sp .BI "struct bitmask *numa_allocate_cpumask();" .sp @@ -220,6 +222,7 @@ Most functions in this library are only their memory. The exceptions to this are: .IR numa_node_to_cpus (), +.IR numa_node_of_cpu (), .IR numa_bind (), .IR numa_run_on_node (), .IR numa_run_on_node_mask () @@ -730,6 +733,13 @@ will be set to .I ERANGE and \-1 returned. On success 0 is returned. +.BR numa_node_of_cpu () +returns the node that a cpu belongs to. If the user supplies an invalid cpu +.I errno +will be set to +.I EINVAL +and \-1 will be returned. + .BR numa_allocate_cpumask () returns a bitmask of a size equal to the kernel's cpu mask (kernel type cpumask_t). In other words, large enough to represent diff -uprN numactl-2.0.3-rc2.orig/numa.h numactl-2.0.3-rc2/numa.h --- numactl-2.0.3-rc2.orig/numa.h 2009-03-17 14:37:14.000000000 +0200 +++ numactl-2.0.3-rc2/numa.h 2009-03-18 14:44:07.000000000 +0200 @@ -273,6 +273,9 @@ static inline void numa_free_cpumask(str /* Convert node to CPU mask. -1/errno on failure, otherwise 0. */ int numa_node_to_cpus(int, struct bitmask *); +/* report the node of the specified cpu. -1/errno on invalid cpu. */ +int numa_node_of_cpu(int cpu); + /* Report distance of node1 from node2. 0 on error.*/ int numa_distance(int node1, int node2); diff -uprN numactl-2.0.3-rc2.orig/versions.ldscript numactl-2.0.3-rc2/versions.ldscript --- numactl-2.0.3-rc2.orig/versions.ldscript 2009-03-17 14:37:14.000000000 +0200 +++ numactl-2.0.3-rc2/versions.ldscript 2009-03-18 14:33:02.000000000 +0200 @@ -118,6 +118,7 @@ libnuma_1.2 { numa_node_size64; numa_node_size; numa_node_to_cpus; + numa_node_of_cpu; numa_num_configured_cpus; numa_num_configured_nodes; numa_num_possible_nodes; -- Kornilios Kourtis ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: find the node that a cpu belongs to 2009-03-17 11:32 ` Kornilios Kourtis 2009-03-17 11:59 ` Andi Kleen @ 2009-03-19 14:16 ` Cliff Wickman 1 sibling, 0 replies; 6+ messages in thread From: Cliff Wickman @ 2009-03-19 14:16 UTC (permalink / raw) To: Kornilios Kourtis; +Cc: Andi Kleen, linux-numa Hi Kornilios, I applied and tested your patch (adding numa_node_of_cpu()). It looks good. Thanks. It is in the numactl-2.0.3-rc2.tar.gz tarball. (ftp://oss.sgi.com/www/projects/libnuma/download/) -Cliff -- Cliff Wickman Silicon Graphics, Inc. cpw@sgi.com (651) 683-3824 ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2009-03-19 14:16 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2009-03-17 1:09 find the node that a cpu belongs to Kornilios Kourtis 2009-03-17 10:45 ` Andi Kleen 2009-03-17 11:32 ` Kornilios Kourtis 2009-03-17 11:59 ` Andi Kleen 2009-03-18 15:19 ` Kornilios Kourtis 2009-03-19 14:16 ` Cliff Wickman
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).