1/*
2 * Copyright 2008, Axel D��rfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include <getopt.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#include <system_info.h>
13
14
15static struct option const kLongOptions[] = {
16	{"periodic", no_argument, 0, 'p'},
17	{"rate", required_argument, 0, 'r'},
18	{"help", no_argument, 0, 'h'},
19	{NULL}
20};
21
22extern const char *__progname;
23static const char *kProgramName = __progname;
24
25
26void
27usage(int status)
28{
29	fprintf(stderr, "usage: %s [-p] [-r <time>]\n"
30		" -p,--periodic\tDumps changes periodically every second.\n"
31		" -r,--rate\tDumps changes periodically every <time> milli seconds.\n",
32		kProgramName);
33
34	exit(status);
35}
36
37
38int
39main(int argc, char** argv)
40{
41	bool periodically = false;
42	bigtime_t rate = 1000000LL;
43
44	int c;
45	while ((c = getopt_long(argc, argv, "pr:h", kLongOptions, NULL)) != -1) {
46		switch (c) {
47			case 0:
48				break;
49			case 'p':
50				periodically = true;
51				break;
52			case 'r':
53				rate = atoi(optarg) * 1000LL;
54				if (rate <= 0) {
55					fprintf(stderr, "%s: Invalid rate: %s\n",
56						kProgramName, optarg);
57					return 1;
58				}
59				periodically = true;
60				break;
61			case 'h':
62				usage(0);
63				break;
64			default:
65				usage(1);
66				break;
67		}
68	}
69	system_info info;
70	status_t status = get_system_info(&info);
71	if (status != B_OK) {
72		fprintf(stderr, "%s: cannot get system info: %s\n", kProgramName,
73			strerror(status));
74		return 1;
75	}
76
77	printf("max memory:\t\t%" B_PRIu64 "\n", info.max_pages * B_PAGE_SIZE);
78	printf("free memory:\t\t%" B_PRIu64 "\n", info.free_memory);
79	printf("needed memory:\t\t%" B_PRIu64 "\n", info.needed_memory);
80	printf("block cache memory:\t%" B_PRIu64 "\n",
81		info.block_cache_pages * B_PAGE_SIZE);
82	printf("max swap space:\t\t%" B_PRIu64 "\n",
83		info.max_swap_pages * B_PAGE_SIZE);
84	printf("free swap space:\t%" B_PRIu64 "\n",
85		info.free_swap_pages * B_PAGE_SIZE);
86	printf("page faults:\t\t%" B_PRIu32 "\n", info.page_faults);
87
88	if (periodically) {
89		puts("\npage faults  used memory    used swap  block cache");
90		system_info lastInfo = info;
91
92		while (true) {
93			snooze(rate);
94
95			get_system_info(&info);
96
97			int32 pageFaults = info.page_faults - lastInfo.page_faults;
98			int64 usedMemory
99				= (info.max_pages * B_PAGE_SIZE - info.free_memory)
100					- (lastInfo.max_pages * B_PAGE_SIZE - lastInfo.free_memory);
101			int64 usedSwap
102				= ((info.max_swap_pages - info.free_swap_pages)
103						- (lastInfo.max_swap_pages - lastInfo.free_swap_pages))
104					* B_PAGE_SIZE;
105			int64 blockCache
106				= (info.block_cache_pages - lastInfo.block_cache_pages)
107					* B_PAGE_SIZE;
108			printf("%11" B_PRId32 "  %11" B_PRId64 "  %11" B_PRId64 "  %11"
109				B_PRId64 "\n", pageFaults, usedMemory, usedSwap, blockCache);
110
111			lastInfo = info;
112		}
113	}
114
115	return 0;
116}
117