VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / base / node.c
1 /*
2  * drivers/base/node.c - basic Node class support
3  */
4
5 #include <linux/sysdev.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/mm.h>
9 #include <linux/node.h>
10 #include <linux/hugetlb.h>
11 #include <linux/cpumask.h>
12 #include <linux/topology.h>
13
14 static struct sysdev_class node_class = {
15         set_kset_name("node"),
16 };
17
18
19 static ssize_t node_read_cpumap(struct sys_device * dev, char * buf)
20 {
21         struct node *node_dev = to_node(dev);
22         cpumask_t mask = node_dev->cpumap;
23         int len;
24
25         /* 2004/06/03: buf currently PAGE_SIZE, need > 1 char per 4 bits. */
26         BUILD_BUG_ON(NR_CPUS/4 > PAGE_SIZE/2);
27
28         len = cpumask_scnprintf(buf, PAGE_SIZE-1, mask);
29         len += sprintf(buf + len, "\n");
30         return len;
31 }
32
33 static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumap, NULL);
34
35 #define K(x) ((x) << (PAGE_SHIFT - 10))
36 static ssize_t node_read_meminfo(struct sys_device * dev, char * buf)
37 {
38         int n;
39         int nid = dev->id;
40         struct sysinfo i;
41         si_meminfo_node(&i, nid);
42         n = sprintf(buf, "\n"
43                        "Node %d MemTotal:     %8lu kB\n"
44                        "Node %d MemFree:      %8lu kB\n"
45                        "Node %d MemUsed:      %8lu kB\n"
46                        "Node %d HighTotal:    %8lu kB\n"
47                        "Node %d HighFree:     %8lu kB\n"
48                        "Node %d LowTotal:     %8lu kB\n"
49                        "Node %d LowFree:      %8lu kB\n",
50                        nid, K(i.totalram),
51                        nid, K(i.freeram),
52                        nid, K(i.totalram - i.freeram),
53                        nid, K(i.totalhigh),
54                        nid, K(i.freehigh),
55                        nid, K(i.totalram - i.totalhigh),
56                        nid, K(i.freeram - i.freehigh));
57         n += hugetlb_report_node_meminfo(nid, buf + n);
58         return n;
59 }
60
61 #undef K
62 static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
63
64 static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
65 {
66         unsigned long numa_hit, numa_miss, interleave_hit, numa_foreign;
67         unsigned long local_node, other_node;
68         int i, cpu;
69         pg_data_t *pg = NODE_DATA(dev->id);
70         numa_hit = 0;
71         numa_miss = 0;
72         interleave_hit = 0;
73         numa_foreign = 0;
74         local_node = 0;
75         other_node = 0;
76         for (i = 0; i < MAX_NR_ZONES; i++) {
77                 struct zone *z = &pg->node_zones[i];
78                 for (cpu = 0; cpu < NR_CPUS; cpu++) {
79                         struct per_cpu_pageset *ps = &z->pageset[cpu];
80                         numa_hit += ps->numa_hit;
81                         numa_miss += ps->numa_miss;
82                         numa_foreign += ps->numa_foreign;
83                         interleave_hit += ps->interleave_hit;
84                         local_node += ps->local_node;
85                         other_node += ps->other_node;
86                 }
87         }
88         return sprintf(buf,
89                        "numa_hit %lu\n"
90                        "numa_miss %lu\n"
91                        "numa_foreign %lu\n"
92                        "interleave_hit %lu\n"
93                        "local_node %lu\n"
94                        "other_node %lu\n",
95                        numa_hit,
96                        numa_miss,
97                        numa_foreign,
98                        interleave_hit,
99                        local_node,
100                        other_node);
101 }
102 static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
103
104 /*
105  * register_node - Setup a driverfs device for a node.
106  * @num - Node number to use when creating the device.
107  *
108  * Initialize and register the node device.
109  */
110 int __init register_node(struct node *node, int num, struct node *parent)
111 {
112         int error;
113
114         node->cpumap = node_to_cpumask(num);
115         node->sysdev.id = num;
116         node->sysdev.cls = &node_class;
117         error = sysdev_register(&node->sysdev);
118
119         if (!error){
120                 sysdev_create_file(&node->sysdev, &attr_cpumap);
121                 sysdev_create_file(&node->sysdev, &attr_meminfo);
122                 sysdev_create_file(&node->sysdev, &attr_numastat);
123         }
124         return error;
125 }
126
127
128 int __init register_node_type(void)
129 {
130         return sysdev_class_register(&node_class);
131 }
132 postcore_initcall(register_node_type);