libroot/posix/stdio: Remove unused portions.
[haiku.git] / src / bin / vmstat.cpp
blob9d6085b32f34299dff5024712a63690004e30aaf
1 /*
2 * Copyright 2008, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <getopt.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
12 #include <system_info.h>
15 static struct option const kLongOptions[] = {
16 {"periodic", no_argument, 0, 'p'},
17 {"rate", required_argument, 0, 'r'},
18 {"help", no_argument, 0, 'h'},
19 {NULL}
22 extern const char *__progname;
23 static const char *kProgramName = __progname;
26 void
27 usage(int status)
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);
34 exit(status);
38 int
39 main(int argc, char** argv)
41 bool periodically = false;
42 bigtime_t rate = 1000000LL;
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;
59 periodically = true;
60 break;
61 case 'h':
62 usage(0);
63 break;
64 default:
65 usage(1);
66 break;
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;
77 printf("max memory:\t\t%Lu\n", info.max_pages * B_PAGE_SIZE);
78 printf("free memory:\t\t%Lu\n", info.free_memory);
79 printf("needed memory:\t\t%Lu\n", info.needed_memory);
80 printf("block cache memory:\t%Lu\n", info.block_cache_pages * B_PAGE_SIZE);
81 printf("max swap space:\t\t%Lu\n", info.max_swap_pages * B_PAGE_SIZE);
82 printf("free swap space:\t%Lu\n", info.free_swap_pages * B_PAGE_SIZE);
83 printf("page faults:\t\t%lu\n", info.page_faults);
85 if (periodically) {
86 puts("\npage faults used memory used swap block cache");
87 system_info lastInfo = info;
89 while (true) {
90 snooze(rate);
92 get_system_info(&info);
94 int32 pageFaults = info.page_faults - lastInfo.page_faults;
95 int64 usedMemory
96 = (info.max_pages * B_PAGE_SIZE - info.free_memory)
97 - (lastInfo.max_pages * B_PAGE_SIZE - lastInfo.free_memory);
98 int64 usedSwap
99 = ((info.max_swap_pages - info.free_swap_pages)
100 - (lastInfo.max_swap_pages - lastInfo.free_swap_pages))
101 * B_PAGE_SIZE;
102 int64 blockCache
103 = (info.block_cache_pages - lastInfo.block_cache_pages)
104 * B_PAGE_SIZE;
105 printf("%11" B_PRId32 " %11" B_PRId64 " %11" B_PRId64 " %11"
106 B_PRId64 "\n", pageFaults, usedMemory, usedSwap, blockCache);
108 lastInfo = info;
112 return 0;