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