summaryrefslogtreecommitdiffstats
path: root/kcontrol/info/memory_fbsd.cpp
blob: f307563f3f1a1c8de2ae51c5c084b20c87a261d9 (plain)
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

#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/vmmeter.h>

#include <vm/vm_param.h>

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

void KMemoryWidget::update()
{
    char buf[80], *used_str, *total_str;
    /* Stuff for sysctl */
    unsigned long memory;
    size_t len;
    /* Stuff for swap display */
    int used, total, _free;
    FILE *pipe;

    len=sizeof(memory);
    sysctlbyname("hw.physmem", &memory, &len, NULL, 0);
  
    // Numerical values

    // total physical memory (without swap space)
    Memory_Info[TOTAL_MEM] = MEMORY(memory);

    // added by Brad Hughes bhughes@trolltech.com
    struct vmtotal vmem;
    
    #warning "FIXME: Memory_Info[CACHED_MEM]"
    Memory_Info[CACHED_MEM] = NO_MEMORY_INFO;

    // The sysctls don't work in a nice manner under FreeBSD v2.2.x
    // so we assume that if sysctlbyname doesn't return what we
    // prefer, assume it's the old data types.   FreeBSD prior
    // to 4.0-R isn't supported by the rest of KDE, so what is
    // this code doing here.

    len = sizeof(vmem);
    if (sysctlbyname("vm.vmmeter", &vmem, &len, NULL, 0) == 0) 
	Memory_Info[SHARED_MEM]   = MEMORY(vmem.t_armshr) * PAGE_SIZE;
    else 
        Memory_Info[SHARED_MEM]   = NO_MEMORY_INFO;

    int buffers;
    len = sizeof (buffers);
    if ((sysctlbyname("vfs.bufspace", &buffers, &len, NULL, 0) == -1) || !len)
	Memory_Info[BUFFER_MEM]   = NO_MEMORY_INFO;
    else
	Memory_Info[BUFFER_MEM]   = MEMORY(buffers);

    // total free physical memory (without swap space)
    int free;
    len = sizeof (buffers);
    if ((sysctlbyname("vm.stats.vm.v_free_count", &free, &len, NULL, 0) == -1) || !len)
	Memory_Info[FREE_MEM]     = NO_MEMORY_INFO;
    else
	Memory_Info[FREE_MEM]     = MEMORY(free) * getpagesize();

    // Q&D hack for swap display. Borrowed from xsysinfo-1.4
    if ((pipe = popen("/usr/sbin/pstat -ks", "r")) == NULL) {
	used = total = 1;
	return;
    }

    fgets(buf, sizeof(buf), pipe);
    fgets(buf, sizeof(buf), pipe);
    fgets(buf, sizeof(buf), pipe);
    fgets(buf, sizeof(buf), pipe);
    pclose(pipe);

    strtok(buf, " ");
    total_str = strtok(NULL, " ");
    used_str = strtok(NULL, " ");
    used = atoi(used_str);
    total = atoi(total_str);

    _free=total-used;

    // total size of all swap-partitions
    Memory_Info[SWAP_MEM] = MEMORY(total) * 1024;

    // free memory in swap-partitions
    Memory_Info[FREESWAP_MEM] = MEMORY(_free) * 1024;
}