Ticket #1656: get_system_info_test.cpp

File get_system_info_test.cpp, 2.2 KB (added by kaoutsis, 16 years ago)

the test app

Line 
1// Purpose: test if get_system_info suceeds.
2
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7
8#include <OS.h>
9
10//#define VERBOSE_MODE
11#ifdef VERBOSE_MODE
12# define VERBOSE(x) printf x
13# define get_system_info_etc get_system_info_verbose
14#else
15# define VERBOSE(x) ;
16# define get_system_info_etc get_system_info_laconic
17#endif
18
19
20void
21get_system_info_laconic(system_info& systemInfo)
22{
23 get_system_info(&systemInfo);
24}
25
26
27void
28get_system_info_verbose(system_info& systemInfo)
29{
30 get_system_info(&systemInfo);
31 printf("------------ system info ---------------\n");
32 printf("available memory: \t%ld MB\n", (systemInfo.max_pages * 4096) / (1024 * 1024));
33 printf("used memory: \t\t%ld MB\n", (systemInfo.used_pages * 4096) / (1024 * 1024));
34 printf("free memory: \t\t%ld MB\n",
35 ((systemInfo.max_pages - systemInfo.used_pages) * 4096) / (1024 * 1024));
36 printf("-----------------------------------------\n");
37}
38
39
40int
41main()
42{
43 system_info info;
44 system_info tempInfo;
45 get_system_info_etc(info);
46
47 size_t chunkSize = 64 * 1024 * 1024;
48 VERBOSE(("allocating %d MB\n", chunkSize / (1024 * 1024)));
49 char* base = (char*)malloc(chunkSize);
50 if (base) {
51 VERBOSE(("before writing anything...\n"));
52 get_system_info_etc(info);
53 for (uint32 index = 0; index < chunkSize; index++)
54 base[index] = 'a';
55 VERBOSE(("after writing...\n"));
56 get_system_info_etc(info);
57 tempInfo.used_pages = info.used_pages;
58 free(base);
59 } else {
60 fprintf(stderr, "malloc FAILED\n");
61 exit(1);
62 }
63
64 VERBOSE(("memory is freed\n"));
65
66 get_system_info_etc(info);
67
68 if (tempInfo.used_pages == info.used_pages) {
69 // Still the get_system_info shows the allocated pages.
70
71 printf("get_system_info: test FAILED\n");
72
73 // Find how long the delay is.
74 int numOfAdditionalGetSysInfoCalls = 0;
75 bigtime_t start = system_time();
76 bigtime_t stop = 0;
77 while (tempInfo.used_pages == info.used_pages) {
78 stop = 0;
79 get_system_info(&info);
80 stop = system_time();
81 numOfAdditionalGetSysInfoCalls++;
82 snooze(50);
83 }
84
85 // one last time
86 get_system_info_verbose(info);
87
88 printf("number of numOfAdditionalGetSysInfoCalls %d\n", numOfAdditionalGetSysInfoCalls);
89 printf("delay is %ld µs\n", stop - start);
90 } else
91 printf("get_system_info: test PASSED\n");
92
93 return 0;
94}