summaryrefslogtreecommitdiffstats
path: root/mbbsd/osdep.c
blob: f1b652429cf1d43b41f9e72bdd9eea0c778ce40d (plain) (blame)
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
/* $Id: osdep.c,v 1.5 2002/07/21 09:26:02 in2 Exp $ */
#include "bbs.h"

#if __FreeBSD__

#include <kvm.h>

int
cpuload(char *str)
{
    double          l[3] = {-1, -1, -1};
    if (getloadavg(l, 3) != 3)
    l[0] = -1;

    if (str) {
    if (l[0] != -1)
        sprintf(str, " %.2f %.2f %.2f", l[0], l[1], l[2]);
    else
        strcpy(str, " (unknown) ");
    }
    return (int)l[0];
}

double
swapused(long *total, long *used)
{
    double          percent = -1;
    kvm_t          *kd;
    struct kvm_swap swapinfo;
    int             pagesize;

    kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
    if (kd) {
    if (kvm_getswapinfo(kd, &swapinfo, 1, 0) == 0) {
        pagesize = getpagesize();
        *total = swapinfo.ksw_total * pagesize;
        *used = swapinfo.ksw_used * pagesize;
        if (*total != 0)
        percent = (double)*used / (double)*total;
    }
    kvm_close(kd);
    }
    return percent;
}

#else
int
cpuload(char *str)
{
    double          l[3] = {-1, -1, -1};
    FILE           *fp;

    if ((fp = fopen("/proc/loadavg", "r"))) {
    if (fscanf(fp, "%lf %lf %lf", &l[0], &l[1], &l[2]) != 3)
        l[0] = -1;
    fclose(fp);
    }
    if (str) {
    if (l[0] != -1)
        sprintf(str, " %.2f %.2f %.2f", l[0], l[1], l[2]);
    else
        strcpy(str, " (unknown) ");
    }
    return (int)l[0];
}

double
swapused(long *total, long *used)
{
    double          percent = -1;
    char            buf[101];
    FILE           *fp;

    if ((fp = fopen("/proc/meminfo", "r"))) {
    while (fgets(buf, 100, fp) && buf[0] != 'S');
    if (sscanf(buf + 6, "%ld %ld", total, used) == 2)
        if (*total != 0)
        percent = (double)*used / (double)*total;
    fclose(fp);
    }
    return percent;
}

#endif