File: stat.c

package info (click to toggle)
slmon 0.5.13-2.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 504 kB
  • ctags: 210
  • sloc: sh: 2,912; ansic: 1,800; makefile: 60
file content (366 lines) | stat: -rw-r--r-- 7,412 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366

/*
 * slmon
 *
 * Copyright (C) 2000, 2001, 2002 Krzysztof Luks <m00se@iq.pl>
 *
 * This program is distributed under the GPL license. For details see
 * COPYING text.
 *
 * Author: Krzysztof Luks <m00se@iq.pl>
 *
 * $Date: 2004/06/24 19:38:35 $
 * $Revision: 1.3 $
 *
 */

#include "stat.h"

/*
 * Calculate count'th cpu usage.
 */

int cpu_calc(int count)
{
    int value;
    double t = 0, i = 0;

    glibtop_cpu cpu;

    glibtop_get_cpu(&cpu);
    if (count == 0) {
	t = ((unsigned long) cpu.total) ? ((double) cpu.total) : 1.0;
	i = ((unsigned long) cpu.idle) ? ((double) cpu.idle) : 1.0;
    } else {
	t = ((unsigned long) cpu.xcpu_total[count - 1]) ? ((double) cpu.
							   xcpu_total[count
								      -
								      1]) :
	    1.0;
	i = ((unsigned long) cpu.
	     xcpu_idle[count - 1]) ? ((double) cpu.xcpu_idle[count -
							     1]) : 1.0;
    }
    t /= conf.freq;
    i /= conf.freq;

    value =
	(int) ((t - i - l[count].totallast) * 100) /
	((t - i - l[count].totallast) + (i - l[count].idlelast));

    if (value > 100)		/* FIXME: is this really needed? */
	value = 100;

    l[count].totallast = t - i;
    l[count].idlelast = i;

    return value;
}

/*
 * Get system uptime
 */

void get_uptime(int *days, int *hours, int *minutes, int *seconds)
{
    double total;
    glibtop_uptime up;

    glibtop_get_uptime(&up);
    total = up.uptime;

    *seconds = (int) total % 60;
    total /= 60.0;
    *minutes = (int) total % 60;
    total /= 60.0;
    *hours = (int) total % 24;
    total /= 24.0;
    *days = (int) total;
}

/*
 * Count users logged in on the system
 */

int users(void)
{
    int count = 0;
#ifdef HAVE_UTMP
    struct utmp *ut;

    setutent();
    while ((ut = getutent()) != NULL)
	if (ut->ut_type == USER_PROCESS)
	    count++;
    endutent();
#endif				/* !HAVE_UTMP */
    return count;
}

/*
 * Make cption for graph etc. Return pointer to that string
 */

char *cpu_caption(int num)
{
    char *t = calloc(20, 1);

    if (num == 0) {
	strcpy(t, "Total CPU usage");
    } else {
        sprintf(t, "CPU%d usage", num);
    }
    return t;
}

slmon_proc *slmon_get_proclist(int *len)
{
    slmon_proc *result = NULL;
    glibtop_proclist plist;
    glibtop_proc_state pstate;
    glibtop_proc_mem pmem;
    glibtop_proc_args pargs;
    glibtop_proc_time ptime;
    glibtop_uptime upt;
    glibtop_mem mem;
    struct passwd *pwd;
    unsigned *pids;
    int i, j, num = 0, tmp;
    float p_total, p_time;
    char *args1;

    /* we can't get process's controlling tty singe libgtop doesn't support
     * this :( */

    pids = glibtop_get_proclist(&plist, 0, 0);

    if(pids) {
	glibtop_get_uptime(&upt);
	glibtop_get_mem(&mem);
	num = plist.number;
	result = (slmon_proc *) malloc(num * sizeof(slmon_proc));
	for(i = 0; i < num; i++) {
	    result[i].pid = pids[i];

	    glibtop_get_proc_state(&pstate, pids[i]);
	    glibtop_get_proc_mem(&pmem, pids[i]);
	    glibtop_get_proc_time(&ptime, pids[i]);
	    p_time = (float) (upt.uptime * 100 - ptime.start_time) / conf.freq;
	    p_total = (float) (ptime.utime + ptime.stime) / 10.0;

	    result[i].pcpu = p_total * 100 / p_time;
	    result[i].pmem = pmem.rss * 1000 / mem.total;
	    result[i].state = pstate.state;

	    args1 = glibtop_get_proc_args(&pargs, pids[i], 0);
	    if (args1) {
		result[i].args = (char *) malloc(pargs.size + 1);
		memcpy(result[i].args, args1, pargs.size);
		result[i].args[pargs.size] = '\0';
		for (j = 0; j < pargs.size; j++)
		    if (result[i].args[j] == '\0')
			result[i].args[j] = ' ';
		g_free(args1);
	    } else {
		tmp = strlen(pstate.cmd) * sizeof(char);
		result[i].args = (char *) malloc(tmp+1);
                result[i].args = calloc(tmp + 1, 1);
	    }

	    result[i].vsz = (int) pmem.vsize >> 10;
	    result[i].rss = (int) pmem.rss >> 10;

	    pwd = getpwuid(pstate.uid);
	    tmp = strlen(pwd->pw_name) * sizeof(char);
	    result[i].user = (char *) malloc(tmp + 1);
            result[i].user = calloc(tmp + 1, 1);
	}
	g_free(pids);
    }
    memcpy(len, &num, sizeof(int));
    return result;
}

int slmon_cmp_pcpu_asc(const void *one, const void *two)
{
    slmon_proc *tmp1, *tmp2;

    tmp1 = (slmon_proc *) one;
    tmp2 = (slmon_proc *) two;

    return tmp1->pcpu - tmp2->pcpu;
}

int slmon_cmp_pcpu_desc(const void *one, const void *two)
{
    return slmon_cmp_pcpu_asc(two, one);
}

int slmon_cmp_pmem_asc(const void *one, const void *two)
{
    slmon_proc *tmp1, *tmp2;

    tmp1 = (slmon_proc *) one;
    tmp2 = (slmon_proc *) two;

    return tmp1->pmem - tmp2->pmem;
}

int slmon_cmp_pmem_desc(const void *one, const void *two)
{
    return slmon_cmp_pmem_asc(two, one);
}

int slmon_cmp_pid_asc(const void *one, const void *two)
{
    /* this function is pretty useless since glibtop_get_proclist()
       already returns processes sorted by pid */

    slmon_proc *tmp1, *tmp2;

    tmp1 = (slmon_proc *) one;
    tmp2 = (slmon_proc *) two;

    return tmp1->pid - tmp2->pid;
}

int slmon_cmp_pid_desc(const void *one, const void *two)
{
    /* this one isn't useless though ;) */
    return slmon_cmp_pid_asc(two, one);
}

int slmon_cmp_user_asc(const void *one, const void *two)
{
    slmon_proc *tmp1, *tmp2;

    tmp1 = (slmon_proc *) one;
    tmp2 = (slmon_proc *) two;

    return strcmp(tmp1->user , tmp2->user);
}

int slmon_cmp_user_desc(const void *one, const void *two)
{
    return slmon_cmp_user_asc(two, one);
}

void slmon_update_net_throughput(char *name, long in, long out)
{
	struct slmon_net *tmp;
        const size_t len = strlen(name);
	
	tmp = conf.iface;
	
	while(tmp != NULL) {
		if(!strncmp(name, tmp->name, len)) {
			tmp->last_in = in;
			tmp->last_out = out;
			return;
		}
		tmp = tmp->next;
	}
}

struct slmon_net *slmon_net_new(void)
{
    struct slmon_net *tmp;

    tmp = (struct slmon_net *) malloc(sizeof(struct slmon_net));
    tmp->name = NULL;
    tmp->next = tmp->prev = NULL;
    
    return tmp;
}

struct slmon_net *slmon_net_append(struct slmon_net *l, char *name)
{
    struct slmon_net *j, *tmp;
    int len;

    tmp = slmon_net_new();
    len = strlen(name);
    tmp->name = calloc(len + 1, 1);
    memcpy(tmp->name, name, len);
    tmp->last_in = tmp->last_out = 0;

    if(l != NULL) {
	j = (struct slmon_net *) slmon_net_find(l, name);
	if(j != NULL) {
	    slmon_net_free(tmp);
	    return l;
	}
	j = (struct slmon_net *) slmon_net_last(l);
	j->next = tmp;
	tmp->prev = j;
	conf.iface_count++;
	return l;
    } else
       return tmp;
}

struct slmon_net *slmon_net_remove(struct slmon_net * l, char *name)
{
    struct slmon_net *j, *tmp;

    j = l;

    if(!strcmp(j->name, name)) {
	l = j->next;
	l->prev = NULL;
	free(j);
	conf.iface_count--;
	return l;
    }

    while(j->next != NULL) {
	j = j->next;
	if(!strcmp(j->name, name)) {
	    j->prev->next = j->next;
	    j->next->prev = j->prev;
	    free(j);
	    break;
	}
    }

}

void slmon_net_free(struct slmon_net * l)
{
    struct slmon_net *j;

    j = l->next;
    free(l);

    while(j != NULL) {
	j = l->next;
	free(l);
    }
}

struct slmon_net *slmon_net_last(struct slmon_net *l)
{
    struct slmon_net *tmp = l;

    if(tmp != NULL)
	while(tmp->next != NULL)
	    tmp = tmp->next;

    return tmp;
}

struct slmon_net *slmon_net_find(struct slmon_net *l, char *name)
{
    struct slmon_net *j;

    j = l;

    while(j != NULL) {
	if(!strcmp(name, j->name))
	    break;
	j = j->next;
    }

    return j;
}