vm.c revision 164718
10SN/A/*-
24487SN/A * Copyright (c) 1998 Andrzej Bialecki
30SN/A * All rights reserved.
40SN/A *
50SN/A * Redistribution and use in source and binary forms, with or without
60SN/A * modification, are permitted provided that the following conditions
72362SN/A * are met:
80SN/A * 1. Redistributions of source code must retain the above copyright
92362SN/A *    notice, this list of conditions and the following disclaimer.
100SN/A * 2. Redistributions in binary form must reproduce the above copyright
110SN/A *    notice, this list of conditions and the following disclaimer in the
120SN/A *    documentation and/or other materials provided with the distribution.
130SN/A *
140SN/A * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
150SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
160SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
170SN/A * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
180SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
190SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
200SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212362SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222362SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232362SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
240SN/A * SUCH DAMAGE.
250SN/A *
260SN/A * $FreeBSD: head/release/picobsd/tinyware/vm/vm.c 164718 2006-11-28 12:46:02Z ru $
270SN/A */
280SN/A
290SN/A#include <stdio.h>
300SN/A#include <sys/types.h>
310SN/A#include <sys/sysctl.h>
320SN/A#include <sys/vmmeter.h>
330SN/A#include <vm/vm_param.h>
340SN/A
350SN/A#define pgtok(a) ((a) * (u_int) pagesize >> 10)
360SN/A
370SN/Aint
380SN/Avm_i()
390SN/A{
400SN/A#define CNT	49
410SN/A	int cnt[CNT];
420SN/A	char names[CNT*16];
430SN/A	char *a, *namep[CNT*16];
440SN/A	int i,len;
450SN/A	long long inttotal=0;
460SN/A	long uptime=1;
470SN/A
480SN/A	len=sizeof(cnt);
490SN/A	i = sysctlbyname("hw.intrcnt", &cnt, &len, NULL, 0);
500SN/A	if (i != 0)
510SN/A		return i ;
520SN/A	len=sizeof(names);
530SN/A	i = sysctlbyname("hw.intrnames", &names, &len, NULL, 0);
540SN/A	if (i != 0)
550SN/A		return i ;
560SN/A
570SN/A	for( i=0, a = names ; i < CNT && a < names+sizeof(names) ; ) {
580SN/A	    namep[i++] = a++;
590SN/A	    while (a < names+sizeof(names) && *a)
600SN/A		a++ ;
610SN/A	    a++ ; /* skip \0 */
620SN/A	}
630SN/A	printf("interrupt                   total       rate\n");
640SN/A        inttotal = 0;
650SN/A	for (i=0; i< CNT ; i++)
660SN/A	    if (cnt[i] >0) {
670SN/A		printf("%-12s %20lu %10lu\n", namep[i], cnt[i], cnt[i]/uptime);
680SN/A                inttotal += cnt[i];
690SN/A	    }
700SN/A        printf("Total        %20llu %10llu\n", inttotal,
710SN/A                        inttotal / (u_int64_t) uptime);
720SN/A	return 0;
730SN/A}
740SN/Aint
750SN/Amain(int argc, char *argv[])
760SN/A{
770SN/A	int mib[2],i=0,len;
780SN/A	int pagesize, pagesize_len;
790SN/A	struct vmtotal v;
800SN/A
810SN/A	if (argc > 1 && !strcmp(argv[1], "-i")) {
820SN/A	    if (vm_i())
834487SN/A		fprintf(stderr, "vm -i stats not available via sysctl\n");
840SN/A		return 0 ;
850SN/A	}
860SN/A	pagesize_len = sizeof(int);
870SN/A	sysctlbyname("vm.stats.vm.v_page_size",&pagesize,&pagesize_len,NULL,0);
880SN/A
890SN/A	len=sizeof(struct vmtotal);
900SN/A	mib[0]=CTL_VM;
910SN/A	mib[1]=VM_METER;
920SN/A	for(;;) {
930SN/A		sysctl(mib,2,&v,&len,NULL,0);
940SN/A		if(i==0) {
950SN/A			printf("  procs    kB virt mem       real mem     shared vm   shared real    free\n");
960SN/A			printf(" r w l s    tot     act    tot    act    tot    act    tot    act\n");
970SN/A		}
980SN/A		printf("%2hd%2hd%2hd%2hd",v.t_rq-1,v.t_dw+v.t_pw,v.t_sl,v.t_sw);
990SN/A		printf("%7d %7d %7d%7d",
1000SN/A			pgtok(v.t_vm),pgtok(v.t_avm),
1010SN/A			pgtok(v.t_rm),pgtok(v.t_arm));
1020SN/A		printf("%7d%7d%7d%7d%7d\n",
1030SN/A			pgtok(v.t_vmshr),pgtok(v.t_avmshr),
1040SN/A			pgtok(v.t_rmshr),pgtok(v.t_armshr),
1050SN/A			pgtok(v.t_free));
1060SN/A		sleep(5);
1070SN/A		i++;
1080SN/A		if(i>22) i=0;
1090SN/A	}
1100SN/A	exit(0);
1110SN/A
1124487SN/A}
1130SN/A