监控客户端开发时候用到的代码,github上一个哥么的,发现有处bug我给贡献了,呵呵。 直接贴代码了,除了mem_total之外基本都正确。total的部分,先用sysctl -a | grep realmem解决

free.c
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
/*
 * free.c - Display FreeBSD memory information
 * Wed Nov 26 19:34:54 IST 2008 vinod <vinod@segfault.in>
 * License: http://opensource.org/licenses/BSD-2-Clause
 */

#include <sys/types.h>
#include <sys/sysctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

const char *version = "$Id: free,v 0.1.3 2008/11/26 19:34:54 IST vinod $";

int
get_sysctl(char *name)
{
        int mib[4], value, i;
        size_t len, miblen = 1;

        for(i = 0; name[i] != '\0'; i++)
                if(name[i] == '.')
                        miblen++;
        len = miblen;
        sysctlnametomib(name, mib, &len);
        len = sizeof(value);
        sysctl(mib, miblen, &value, &len, NULL, 0);

        return value;
}

void
usage(void)
{
        fprintf(stderr, "usage: free [-b|-k|-m|-g] [-t] [-v]\n" \
        "  -b,-k,-m,-g show output in bytes, KB, MB, or GB\n" \
        "  -t display logical summary for RAM\n" \
        "  -v display version information and exit\n");
}

int
main(int argc, char *argv[])
{
        int c, vflag = 0, tflag = 0;
        int factor = 1;
        long int physmem, realmem;
        long int vmactive, vminactive, vmfree, vmcache, vmpage, vmwire;
        long int memfree, memused;
        long int pagesize;

        opterr = 0;

        while ((c = getopt(argc, argv, "bghkmtv")) != -1) {
                switch (c) {
                        case 'b':
                                factor = 1;
                                break;
                        case 'g':
                                factor = 1024*1024*1024;

                        case 'h':
                                usage();
                                exit(EXIT_SUCCESS);
                        case 'k':
                                factor = 1024;
                                break;
                        case 'm':
                                factor = 1024*1024;
                                break;
                        case 't':
                                tflag = 1;
                                break;
                        case 'v':
                                vflag = 1;
                                break;
                        case '?':
                        default:
                                fprintf(stderr, "%s: invalid option -- %c\n", argv[0], optopt);
                                usage();
                                exit(EXIT_FAILURE);
                }
        }

        argc -= optind;
        argv += optind;

        if(vflag) {
                fprintf(stderr, "%s\nbuilt %s %s\n", version,
                                __DATE__, __TIME__);
                exit(EXIT_SUCCESS);
        }

        physmem    = labs(get_sysctl("hw.physmem"));
        realmem    = labs(get_sysctl("hw.realmem"));
        pagesize   = labs(get_sysctl("hw.pagesize"));

        vmpage     = labs(get_sysctl("vm.stats.vm.v_page_count") * pagesize);
        vmwire     = labs(get_sysctl("vm.stats.vm.v_wire_count") * pagesize);
        vmactive   = labs(get_sysctl("vm.stats.vm.v_active_count") * pagesize);
        vminactive = labs(get_sysctl("vm.stats.vm.v_inactive_count") * pagesize);
        vmcache    = labs(get_sysctl("vm.stats.vm.v_cache_count") * pagesize);
        vmfree     = labs(get_sysctl("vm.stats.vm.v_free_count") * pagesize);

        printf("         %15s %15s %15s %15s %15s %15s\n", "total", "active", "free", "inactive", "wire", "cached");
        printf("Memory:  %15ld %15ld %15ld %15ld %15ld %15ld\n",
                        realmem/factor,
                        vmactive/factor,
                        vmfree/factor,
                        vminactive/factor,
                        vmwire/factor,
                        vmcache/factor);

        /*
         * logical summary
         */
        if(tflag) {
                memfree = vminactive + vmfree + vmcache;
                memused        = realmem - memfree;

                printf("Summary: %15ld %15ld %15ld\n",
                                realmem/factor,
                                memused/factor,
                                memfree/factor);
        }

        return (EXIT_SUCCESS);
}

项目位置

https://github.com/evoup/free

发表评论