updated
[util-vserver.git] / util-vserver / kernel / 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         uint64_t bias_jiffies;          /* 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         cvirt->bias_jiffies = get_jiffies_64();
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                 "BiasJiffies:\t%lld\n", (long long int)cvirt->bias_jiffies);
125         length += sprintf(buffer + length,
126                 "SysName:\t%.*s\n"
127                 "NodeName:\t%.*s\n"
128                 "Release:\t%.*s\n"
129                 "Version:\t%.*s\n"
130                 "Machine:\t%.*s\n"
131                 "DomainName:\t%.*s\n"
132                 ,__NEW_UTS_LEN, cvirt->utsname.sysname
133                 ,__NEW_UTS_LEN, cvirt->utsname.nodename
134                 ,__NEW_UTS_LEN, cvirt->utsname.release
135                 ,__NEW_UTS_LEN, cvirt->utsname.version
136                 ,__NEW_UTS_LEN, cvirt->utsname.machine
137                 ,__NEW_UTS_LEN, cvirt->utsname.domainname
138                 );
139
140         a = cvirt->load[0] + (FIXED_1/200);
141         b = cvirt->load[1] + (FIXED_1/200);
142         c = cvirt->load[2] + (FIXED_1/200);
143         length += sprintf(buffer + length,
144                 "nr_threads:\t%d\n"
145                 "nr_running:\t%d\n"
146                 "nr_onhold:\t%d\n"
147                 "loadavg:\t%d.%02d %d.%02d %d.%02d\n"
148                 ,atomic_read(&cvirt->nr_threads)
149                 ,atomic_read(&cvirt->nr_running)
150                 ,atomic_read(&cvirt->nr_onhold)
151                 ,LOAD_INT(a), LOAD_FRAC(a)
152                 ,LOAD_INT(b), LOAD_FRAC(b)
153                 ,LOAD_INT(c), LOAD_FRAC(c)
154                 );
155         return length;
156 }
157
158 static inline int vx_info_proc_cacct(struct _vx_cacct *cacct, char *buffer)
159 {
160         int i,j, length = 0;
161         static char *type[] = { "UNSPEC", "UNIX", "INET", "INET6", "OTHER" };
162
163         for (i=0; i<5; i++) {
164                 length += sprintf(buffer + length,
165                         "%s:", type[i]);
166                 for (j=0; j<3; j++) {
167                         length += sprintf(buffer + length,
168                                 "\t%12lu/%-12lu"
169                                 ,vx_sock_count(cacct, i, j)
170                                 ,vx_sock_total(cacct, i, j)
171                                 );
172                 }
173                 buffer[length++] = '\n';
174         }
175         length += sprintf(buffer + length,
176                 "forks:\t%lu\n", cacct->total_forks);
177         return length;
178 }
179
180 #else   /* _VX_INFO_DEF_ */
181 #ifndef _VX_CVIRT_H
182 #define _VX_CVIRT_H
183
184 #include "switch.h"
185
186 /*  cvirt vserver commands */
187
188
189 #ifdef  __KERNEL__
190
191 struct timespec;
192
193 void vx_vsi_uptime(struct timespec *, struct timespec *);
194
195 struct vx_info;
196
197 void vx_update_load(struct vx_info *);
198
199
200 #endif  /* __KERNEL__ */
201
202 #endif  /* _VX_CVIRT_H */
203 #endif