]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/base/node.c
[PATCH] zoned vm counters: conversion of nr_bounce to per zone counter
[mirror_ubuntu-hirsute-kernel.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 #include <linux/nodemask.h>
14 #include <linux/cpu.h>
15
16 static struct sysdev_class node_class = {
17 set_kset_name("node"),
18 };
19
20
21 static ssize_t node_read_cpumap(struct sys_device * dev, char * buf)
22 {
23 struct node *node_dev = to_node(dev);
24 cpumask_t mask = node_to_cpumask(node_dev->sysdev.id);
25 int len;
26
27 /* 2004/06/03: buf currently PAGE_SIZE, need > 1 char per 4 bits. */
28 BUILD_BUG_ON(MAX_NUMNODES/4 > PAGE_SIZE/2);
29
30 len = cpumask_scnprintf(buf, PAGE_SIZE-1, mask);
31 len += sprintf(buf + len, "\n");
32 return len;
33 }
34
35 static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumap, NULL);
36
37 #define K(x) ((x) << (PAGE_SHIFT - 10))
38 static ssize_t node_read_meminfo(struct sys_device * dev, char * buf)
39 {
40 int n;
41 int nid = dev->id;
42 struct sysinfo i;
43 unsigned long inactive;
44 unsigned long active;
45 unsigned long free;
46
47 si_meminfo_node(&i, nid);
48 __get_zone_counts(&active, &inactive, &free, NODE_DATA(nid));
49
50
51 n = sprintf(buf, "\n"
52 "Node %d MemTotal: %8lu kB\n"
53 "Node %d MemFree: %8lu kB\n"
54 "Node %d MemUsed: %8lu kB\n"
55 "Node %d Active: %8lu kB\n"
56 "Node %d Inactive: %8lu kB\n"
57 "Node %d HighTotal: %8lu kB\n"
58 "Node %d HighFree: %8lu kB\n"
59 "Node %d LowTotal: %8lu kB\n"
60 "Node %d LowFree: %8lu kB\n"
61 "Node %d Dirty: %8lu kB\n"
62 "Node %d Writeback: %8lu kB\n"
63 "Node %d FilePages: %8lu kB\n"
64 "Node %d Mapped: %8lu kB\n"
65 "Node %d AnonPages: %8lu kB\n"
66 "Node %d PageTables: %8lu kB\n"
67 "Node %d NFS Unstable: %8lu kB\n"
68 "Node %d Bounce: %8lu kB\n"
69 "Node %d Slab: %8lu kB\n",
70 nid, K(i.totalram),
71 nid, K(i.freeram),
72 nid, K(i.totalram - i.freeram),
73 nid, K(active),
74 nid, K(inactive),
75 nid, K(i.totalhigh),
76 nid, K(i.freehigh),
77 nid, K(i.totalram - i.totalhigh),
78 nid, K(i.freeram - i.freehigh),
79 nid, K(node_page_state(nid, NR_FILE_DIRTY)),
80 nid, K(node_page_state(nid, NR_WRITEBACK)),
81 nid, K(node_page_state(nid, NR_FILE_PAGES)),
82 nid, K(node_page_state(nid, NR_FILE_MAPPED)),
83 nid, K(node_page_state(nid, NR_ANON_PAGES)),
84 nid, K(node_page_state(nid, NR_PAGETABLE)),
85 nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
86 nid, K(node_page_state(nid, NR_BOUNCE)),
87 nid, K(node_page_state(nid, NR_SLAB)));
88 n += hugetlb_report_node_meminfo(nid, buf + n);
89 return n;
90 }
91
92 #undef K
93 static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
94
95 static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
96 {
97 unsigned long numa_hit, numa_miss, interleave_hit, numa_foreign;
98 unsigned long local_node, other_node;
99 int i, cpu;
100 pg_data_t *pg = NODE_DATA(dev->id);
101 numa_hit = 0;
102 numa_miss = 0;
103 interleave_hit = 0;
104 numa_foreign = 0;
105 local_node = 0;
106 other_node = 0;
107 for (i = 0; i < MAX_NR_ZONES; i++) {
108 struct zone *z = &pg->node_zones[i];
109 for_each_online_cpu(cpu) {
110 struct per_cpu_pageset *ps = zone_pcp(z,cpu);
111 numa_hit += ps->numa_hit;
112 numa_miss += ps->numa_miss;
113 numa_foreign += ps->numa_foreign;
114 interleave_hit += ps->interleave_hit;
115 local_node += ps->local_node;
116 other_node += ps->other_node;
117 }
118 }
119 return sprintf(buf,
120 "numa_hit %lu\n"
121 "numa_miss %lu\n"
122 "numa_foreign %lu\n"
123 "interleave_hit %lu\n"
124 "local_node %lu\n"
125 "other_node %lu\n",
126 numa_hit,
127 numa_miss,
128 numa_foreign,
129 interleave_hit,
130 local_node,
131 other_node);
132 }
133 static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
134
135 static ssize_t node_read_distance(struct sys_device * dev, char * buf)
136 {
137 int nid = dev->id;
138 int len = 0;
139 int i;
140
141 /* buf currently PAGE_SIZE, need ~4 chars per node */
142 BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
143
144 for_each_online_node(i)
145 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
146
147 len += sprintf(buf + len, "\n");
148 return len;
149 }
150 static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
151
152
153 /*
154 * register_node - Setup a driverfs device for a node.
155 * @num - Node number to use when creating the device.
156 *
157 * Initialize and register the node device.
158 */
159 int register_node(struct node *node, int num, struct node *parent)
160 {
161 int error;
162
163 node->sysdev.id = num;
164 node->sysdev.cls = &node_class;
165 error = sysdev_register(&node->sysdev);
166
167 if (!error){
168 sysdev_create_file(&node->sysdev, &attr_cpumap);
169 sysdev_create_file(&node->sysdev, &attr_meminfo);
170 sysdev_create_file(&node->sysdev, &attr_numastat);
171 sysdev_create_file(&node->sysdev, &attr_distance);
172 }
173 return error;
174 }
175
176 /**
177 * unregister_node - unregister a node device
178 * @node: node going away
179 *
180 * Unregisters a node device @node. All the devices on the node must be
181 * unregistered before calling this function.
182 */
183 void unregister_node(struct node *node)
184 {
185 sysdev_remove_file(&node->sysdev, &attr_cpumap);
186 sysdev_remove_file(&node->sysdev, &attr_meminfo);
187 sysdev_remove_file(&node->sysdev, &attr_numastat);
188 sysdev_remove_file(&node->sysdev, &attr_distance);
189
190 sysdev_unregister(&node->sysdev);
191 }
192
193 struct node node_devices[MAX_NUMNODES];
194
195 /*
196 * register cpu under node
197 */
198 int register_cpu_under_node(unsigned int cpu, unsigned int nid)
199 {
200 if (node_online(nid)) {
201 struct sys_device *obj = get_cpu_sysdev(cpu);
202 if (!obj)
203 return 0;
204 return sysfs_create_link(&node_devices[nid].sysdev.kobj,
205 &obj->kobj,
206 kobject_name(&obj->kobj));
207 }
208
209 return 0;
210 }
211
212 int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
213 {
214 if (node_online(nid)) {
215 struct sys_device *obj = get_cpu_sysdev(cpu);
216 if (obj)
217 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
218 kobject_name(&obj->kobj));
219 }
220 return 0;
221 }
222
223 int register_one_node(int nid)
224 {
225 int error = 0;
226 int cpu;
227
228 if (node_online(nid)) {
229 int p_node = parent_node(nid);
230 struct node *parent = NULL;
231
232 if (p_node != nid)
233 parent = &node_devices[p_node];
234
235 error = register_node(&node_devices[nid], nid, parent);
236
237 /* link cpu under this node */
238 for_each_present_cpu(cpu) {
239 if (cpu_to_node(cpu) == nid)
240 register_cpu_under_node(cpu, nid);
241 }
242 }
243
244 return error;
245
246 }
247
248 void unregister_one_node(int nid)
249 {
250 unregister_node(&node_devices[nid]);
251 }
252
253 static int __init register_node_type(void)
254 {
255 return sysdev_class_register(&node_class);
256 }
257 postcore_initcall(register_node_type);