vserver 1.9.3
[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_to_cpumask(node_dev->sysdev.id);
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         unsigned long inactive;
42         unsigned long active;
43         unsigned long free;
44
45         si_meminfo_node(&i, nid);
46         __get_zone_counts(&active, &inactive, &free, NODE_DATA(nid));
47
48         n = sprintf(buf, "\n"
49                        "Node %d MemTotal:     %8lu kB\n"
50                        "Node %d MemFree:      %8lu kB\n"
51                        "Node %d MemUsed:      %8lu kB\n"
52                        "Node %d Active:       %8lu kB\n"
53                        "Node %d Inactive:     %8lu kB\n"
54                        "Node %d HighTotal:    %8lu kB\n"
55                        "Node %d HighFree:     %8lu kB\n"
56                        "Node %d LowTotal:     %8lu kB\n"
57                        "Node %d LowFree:      %8lu kB\n",
58                        nid, K(i.totalram),
59                        nid, K(i.freeram),
60                        nid, K(i.totalram - i.freeram),
61                        nid, K(active),
62                        nid, K(inactive),
63                        nid, K(i.totalhigh),
64                        nid, K(i.freehigh),
65                        nid, K(i.totalram - i.totalhigh),
66                        nid, K(i.freeram - i.freehigh));
67         n += hugetlb_report_node_meminfo(nid, buf + n);
68         return n;
69 }
70
71 #undef K
72 static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
73
74 static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
75 {
76         unsigned long numa_hit, numa_miss, interleave_hit, numa_foreign;
77         unsigned long local_node, other_node;
78         int i, cpu;
79         pg_data_t *pg = NODE_DATA(dev->id);
80         numa_hit = 0;
81         numa_miss = 0;
82         interleave_hit = 0;
83         numa_foreign = 0;
84         local_node = 0;
85         other_node = 0;
86         for (i = 0; i < MAX_NR_ZONES; i++) {
87                 struct zone *z = &pg->node_zones[i];
88                 for (cpu = 0; cpu < NR_CPUS; cpu++) {
89                         struct per_cpu_pageset *ps = &z->pageset[cpu];
90                         numa_hit += ps->numa_hit;
91                         numa_miss += ps->numa_miss;
92                         numa_foreign += ps->numa_foreign;
93                         interleave_hit += ps->interleave_hit;
94                         local_node += ps->local_node;
95                         other_node += ps->other_node;
96                 }
97         }
98         return sprintf(buf,
99                        "numa_hit %lu\n"
100                        "numa_miss %lu\n"
101                        "numa_foreign %lu\n"
102                        "interleave_hit %lu\n"
103                        "local_node %lu\n"
104                        "other_node %lu\n",
105                        numa_hit,
106                        numa_miss,
107                        numa_foreign,
108                        interleave_hit,
109                        local_node,
110                        other_node);
111 }
112 static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
113
114 /*
115  * register_node - Setup a driverfs device for a node.
116  * @num - Node number to use when creating the device.
117  *
118  * Initialize and register the node device.
119  */
120 int __init register_node(struct node *node, int num, struct node *parent)
121 {
122         int error;
123
124         node->sysdev.id = num;
125         node->sysdev.cls = &node_class;
126         error = sysdev_register(&node->sysdev);
127
128         if (!error){
129                 sysdev_create_file(&node->sysdev, &attr_cpumap);
130                 sysdev_create_file(&node->sysdev, &attr_meminfo);
131                 sysdev_create_file(&node->sysdev, &attr_numastat);
132         }
133         return error;
134 }
135
136
137 int __init register_node_type(void)
138 {
139         return sysdev_class_register(&node_class);
140 }
141 postcore_initcall(register_node_type);