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