vserver 1.9.3
[linux-2.6.git] / include / linux / vserver / cvirt.h
1 /* _VX_CVIRT_H defined below */
2
3 #if     defined(__KERNEL__) && defined(_VX_INFO_DEF_)
4
5 #include <linux/utsname.h>
6 #include <linux/rwsem.h>
7 #include <linux/jiffies.h>
8 #include <linux/time.h>
9 #include <linux/sched.h>
10 #include <linux/kernel_stat.h>
11 #include <asm/atomic.h>
12
13 /* context sub struct */
14
15 struct _vx_cvirt {
16         int max_threads;                /* maximum allowed threads */
17         atomic_t nr_threads;            /* number of current threads */
18         atomic_t nr_running;            /* number of running threads */
19
20         atomic_t nr_onhold;             /* processes on hold */
21         uint32_t onhold_last;           /* jiffies when put on hold */
22
23         struct timespec bias_idle;
24         struct timespec bias_uptime;    /* context creation point */
25
26         struct new_utsname utsname;
27
28         spinlock_t load_lock;           /* lock for the load averages */
29         uint32_t load_last;             /* last time load was cacled */
30         uint32_t load[3];               /* load averages 1,5,15 */
31
32         struct cpu_usage_stat cpustat[NR_CPUS];
33 };
34
35 struct sock_acc {
36         atomic_t count;
37         atomic_t total;
38 };
39
40 struct _vx_cacct {
41         unsigned long total_forks;
42
43         struct sock_acc sock[5][3];
44 };
45
46
47 static inline long vx_sock_count(struct _vx_cacct *cacct, int type, int pos)
48 {
49         return atomic_read(&cacct->sock[type][pos].count);
50 }
51
52
53 static inline long vx_sock_total(struct _vx_cacct *cacct, int type, int pos)
54 {
55         return atomic_read(&cacct->sock[type][pos].total);
56 }
57
58
59 extern uint64_t vx_idle_jiffies(void);
60
61 static inline void vx_info_init_cvirt(struct _vx_cvirt *cvirt)
62 {
63         uint64_t idle_jiffies = vx_idle_jiffies();
64
65         do_posix_clock_monotonic_gettime(&cvirt->bias_uptime);
66         jiffies_to_timespec(idle_jiffies, &cvirt->bias_idle);
67         atomic_set(&cvirt->nr_threads, 0);
68         atomic_set(&cvirt->nr_running, 0);
69         atomic_set(&cvirt->nr_onhold, 0);
70
71         down_read(&uts_sem);
72         cvirt->utsname = system_utsname;
73         up_read(&uts_sem);
74
75         spin_lock_init(&cvirt->load_lock);
76         cvirt->load_last = jiffies;
77         cvirt->load[0] = 0;
78         cvirt->load[1] = 0;
79         cvirt->load[2] = 0;
80 }
81
82 static inline void vx_info_exit_cvirt(struct _vx_cvirt *cvirt)
83 {
84 #ifdef  CONFIG_VSERVER_DEBUG
85         int value;
86
87         if ((value = atomic_read(&cvirt->nr_threads)))
88                 printk("!!! cvirt: %p[nr_threads] = %d on exit.\n",
89                         cvirt, value);
90         if ((value = atomic_read(&cvirt->nr_running)))
91                 printk("!!! cvirt: %p[nr_running] = %d on exit.\n",
92                         cvirt, value);
93 #endif
94         return;
95 }
96
97 static inline void vx_info_init_cacct(struct _vx_cacct *cacct)
98 {
99         int i,j;
100
101         for (i=0; i<5; i++) {
102                 for (j=0; j<3; j++) {
103                         atomic_set(&cacct->sock[i][j].count, 0);
104                         atomic_set(&cacct->sock[i][j].total, 0);
105                 }
106         }
107 }
108
109 static inline void vx_info_exit_cacct(struct _vx_cacct *cacct)
110 {
111         return;
112 }
113
114 #define LOAD_INT(x) ((x) >> FSHIFT)
115 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
116
117
118 static inline int vx_info_proc_cvirt(struct _vx_cvirt *cvirt, char *buffer)
119 {
120         int length = 0;
121         int a, b, c;
122
123         length += sprintf(buffer + length,
124                 "BiasUptime:\t%lu.%02lu\n",
125                         (unsigned long)cvirt->bias_uptime.tv_sec,
126                         (cvirt->bias_uptime.tv_nsec / (NSEC_PER_SEC / 100)));
127         length += sprintf(buffer + length,
128                 "SysName:\t%.*s\n"
129                 "NodeName:\t%.*s\n"
130                 "Release:\t%.*s\n"
131                 "Version:\t%.*s\n"
132                 "Machine:\t%.*s\n"
133                 "DomainName:\t%.*s\n"
134                 ,__NEW_UTS_LEN, cvirt->utsname.sysname
135                 ,__NEW_UTS_LEN, cvirt->utsname.nodename
136                 ,__NEW_UTS_LEN, cvirt->utsname.release
137                 ,__NEW_UTS_LEN, cvirt->utsname.version
138                 ,__NEW_UTS_LEN, cvirt->utsname.machine
139                 ,__NEW_UTS_LEN, cvirt->utsname.domainname
140                 );
141
142         a = cvirt->load[0] + (FIXED_1/200);
143         b = cvirt->load[1] + (FIXED_1/200);
144         c = cvirt->load[2] + (FIXED_1/200);
145         length += sprintf(buffer + length,
146                 "nr_threads:\t%d\n"
147                 "nr_running:\t%d\n"
148                 "nr_onhold:\t%d\n"
149                 "loadavg:\t%d.%02d %d.%02d %d.%02d\n"
150                 ,atomic_read(&cvirt->nr_threads)
151                 ,atomic_read(&cvirt->nr_running)
152                 ,atomic_read(&cvirt->nr_onhold)
153                 ,LOAD_INT(a), LOAD_FRAC(a)
154                 ,LOAD_INT(b), LOAD_FRAC(b)
155                 ,LOAD_INT(c), LOAD_FRAC(c)
156                 );
157         return length;
158 }
159
160 static inline int vx_info_proc_cacct(struct _vx_cacct *cacct, char *buffer)
161 {
162         int i,j, length = 0;
163         static char *type[] = { "UNSPEC", "UNIX", "INET", "INET6", "OTHER" };
164
165         for (i=0; i<5; i++) {
166                 length += sprintf(buffer + length,
167                         "%s:", type[i]);
168                 for (j=0; j<3; j++) {
169                         length += sprintf(buffer + length,
170                                 "\t%12lu/%-12lu"
171                                 ,vx_sock_count(cacct, i, j)
172                                 ,vx_sock_total(cacct, i, j)
173                                 );
174                 }
175                 buffer[length++] = '\n';
176         }
177         length += sprintf(buffer + length,
178                 "forks:\t%lu\n", cacct->total_forks);
179         return length;
180 }
181
182 #else   /* _VX_INFO_DEF_ */
183 #ifndef _VX_CVIRT_H
184 #define _VX_CVIRT_H
185
186 #include "switch.h"
187
188 /*  cvirt vserver commands */
189
190
191 #ifdef  __KERNEL__
192
193 struct timespec;
194
195 void vx_vsi_uptime(struct timespec *, struct timespec *);
196
197 struct vx_info;
198
199 void vx_update_load(struct vx_info *);
200
201
202 #endif  /* __KERNEL__ */
203
204 #endif  /* _VX_CVIRT_H */
205 #endif