1 | #include <stdio.h>
|
---|
2 | #include <Application.h>
|
---|
3 | #include <Looper.h>
|
---|
4 | #include <MessageRunner.h>
|
---|
5 | #include <debugger.h>
|
---|
6 | #include <OS.h>
|
---|
7 | #include <syscalls.h>
|
---|
8 |
|
---|
9 | enum {
|
---|
10 | fileCnt = 50000
|
---|
11 | };
|
---|
12 |
|
---|
13 | typedef bigtime_t Time;
|
---|
14 |
|
---|
15 | enum {
|
---|
16 | second = 1000000
|
---|
17 | };
|
---|
18 |
|
---|
19 | inline bigtime_t Now()
|
---|
20 | {
|
---|
21 | return system_time();
|
---|
22 | }
|
---|
23 |
|
---|
24 |
|
---|
25 | enum {
|
---|
26 | resetMsg = 1,
|
---|
27 | timerMsg,
|
---|
28 | };
|
---|
29 |
|
---|
30 | enum {
|
---|
31 | watchdogTimeout = second,
|
---|
32 | };
|
---|
33 |
|
---|
34 | class WatchdogLooper: public BLooper
|
---|
35 | {
|
---|
36 | private:
|
---|
37 | thread_id fMainThread;
|
---|
38 | BMessageRunner *fTimer;
|
---|
39 |
|
---|
40 | public:
|
---|
41 | WatchdogLooper(thread_id mainThread): BLooper("watchdog"), fMainThread(mainThread)
|
---|
42 | {
|
---|
43 | BMessage tMsg(timerMsg);
|
---|
44 | fTimer = new BMessageRunner(BMessenger(this), tMsg, watchdogTimeout, 1);
|
---|
45 | }
|
---|
46 |
|
---|
47 | ~WatchdogLooper()
|
---|
48 | {
|
---|
49 | delete fTimer;
|
---|
50 | }
|
---|
51 |
|
---|
52 | void MessageReceived(BMessage *msg)
|
---|
53 | {
|
---|
54 | switch (msg->what) {
|
---|
55 | case resetMsg: {
|
---|
56 | delete fTimer;
|
---|
57 | BMessage tMsg(timerMsg);
|
---|
58 | fTimer = new BMessageRunner(BMessenger(this), tMsg, watchdogTimeout, 1);
|
---|
59 | break;
|
---|
60 | }
|
---|
61 | case timerMsg: {
|
---|
62 | char str[256];
|
---|
63 | sprintf(str, "FileTest: timeout, thread: %d", fMainThread);
|
---|
64 | fprintf(stderr, "%s\n", str);
|
---|
65 | _kern_kernel_debugger(str);
|
---|
66 | break;
|
---|
67 | }
|
---|
68 | default:
|
---|
69 | BLooper::MessageReceived(msg);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | };
|
---|
74 |
|
---|
75 |
|
---|
76 | class TestApplication: public BApplication
|
---|
77 | {
|
---|
78 | private:
|
---|
79 | WatchdogLooper *fWatchdog;
|
---|
80 | public:
|
---|
81 | TestApplication(): BApplication("application/x-vnd.test.app") {
|
---|
82 | fWatchdog = new WatchdogLooper(find_thread(NULL));
|
---|
83 | fWatchdog->Run();
|
---|
84 | }
|
---|
85 |
|
---|
86 | ~TestApplication()
|
---|
87 | {
|
---|
88 | delete fWatchdog;
|
---|
89 | }
|
---|
90 |
|
---|
91 | void ArgvReceived(int32 argCnt, char **args)
|
---|
92 | {
|
---|
93 | BMessenger watchdog(fWatchdog);
|
---|
94 |
|
---|
95 | FILE *f;
|
---|
96 | char name[256];
|
---|
97 | Time t, t2, times[fileCnt];
|
---|
98 | if (argCnt != 2) {
|
---|
99 | PostMessage(B_QUIT_REQUESTED);
|
---|
100 | return;
|
---|
101 | }
|
---|
102 |
|
---|
103 | t = Now();
|
---|
104 | for (int i = 0; i < fileCnt; i++) {
|
---|
105 | sprintf(name, "%s/%d", args[1], i);
|
---|
106 | f = fopen(name, "w");
|
---|
107 | fprintf(f, "File %d/n", i);
|
---|
108 | fclose(f); f = NULL;
|
---|
109 | watchdog.SendMessage(resetMsg);
|
---|
110 | t2 = Now();
|
---|
111 | times[i] = t2 - t;
|
---|
112 | t = t2;
|
---|
113 | }
|
---|
114 | for (int i = 0; i < fileCnt; i++) {
|
---|
115 | printf("%.16E\n", double(times[i]) / double(second));
|
---|
116 | }
|
---|
117 |
|
---|
118 | PostMessage(B_QUIT_REQUESTED);
|
---|
119 | }
|
---|
120 |
|
---|
121 | };
|
---|
122 |
|
---|
123 | int main(int argCnt, char **args)
|
---|
124 | {
|
---|
125 | TestApplication app;
|
---|
126 | app.Run();
|
---|
127 | return 0;
|
---|
128 | }
|
---|