numa_node_size.c revision 354ebb48db8e66a853a58379a4808d5dcd1ceac3
1/******************************************************************************/
2/*                                                                            */
3/* Copyright (c) International Business Machines  Corp., 2007                 */
4/*                                                                            */
5/* This program is free software;  you can redistribute it and/or modify      */
6/* it under the terms of the GNU General Public License as published by       */
7/* the Free Software Foundation; either version 2 of the License, or          */
8/* (at your option) any later version.                                        */
9/*                                                                            */
10/* This program is distributed in the hope that it will be useful,            */
11/* but WITHOUT ANY WARRANTY;  without even the implied warranty of            */
12/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See                  */
13/* the GNU General Public License for more details.                           */
14/*                                                                            */
15/* You should have received a copy of the GNU General Public License          */
16/* along with this program;  if not, write to the Free Software               */
17/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    */
18/*                                                                            */
19/******************************************************************************/
20
21/******************************************************************************/
22/*                                                                            */
23/* File:        numa_node_size.c                                              */
24/*                                                                            */
25/* Description: Invokes numa_node_size() API                                  */
26/*                                                                            */
27/* Author:     Pradeep Kumar Surisetty pradeepkumars@in.ibm.com               */
28/*                                                                            */
29/* History:     Created - Nov 28 2007 - Pradeep Kumar Surisetty               */
30/*                                                 pradeepkumars@in.ibm.com   */
31/*                                                                            */
32/******************************************************************************/
33
34#include "config.h"
35#include <stdio.h>
36#include <stdlib.h>
37#if HAVE_NUMA_H
38#include <numa.h>
39#endif
40
41int numa_exit_on_error = 0;
42char *fmt_mem(unsigned long long mem, char *buf)
43{
44	if (mem == -1L)
45		sprintf(buf, "<not available>");
46	else
47		sprintf(buf, "%Lu MB", mem >> 20);
48	return buf;
49}
50
51void hardware(void)
52{
53#if HAVE_NUMA_H
54	int i;
55	int maxnode = numa_max_node();
56	printf("available: %d nodes (0-%d)\n", 1 + maxnode, maxnode);
57	for (i = 0; i <= maxnode; i++) {
58		char buf[64];
59		long fr;
60		unsigned long sz = numa_node_size(i, &fr);
61		printf("node %d cpus:", i);
62		printf("node %d size: %s\n", i, fmt_mem(sz, buf));
63		printf("node %d free: %s\n", i, fmt_mem(fr, buf));
64	}
65#endif
66}
67
68int main()
69{
70#if HAVE_NUMA_H
71	nodemask_t nodemask;
72	void hardware();
73	if (numa_available() < 0) {
74		printf("This system does not support NUMA policy\n");
75		numa_error("numa_available");
76		numa_exit_on_error = 1;
77		exit(numa_exit_on_error);
78	}
79	nodemask_zero(&nodemask);
80	nodemask_set(&nodemask, 1);
81	numa_bind(&nodemask);
82	hardware();
83	return numa_exit_on_error;
84#else
85	printf("NUMA is not available\n");
86	return 1;
87#endif
88}
89