summaryrefslogtreecommitdiff
path: root/testsuite/tests/rts/pause-resume/pause_resume.c
blob: 213adf726cfd6d3d5f80be85c9b7f95bdc5be537 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <assert.h>
#include <stdio.h>
#include <unistd.h>

#include "Rts.h"
#include "RtsAPI.h"

#include "pause_resume.h"

void expectNoChange(const char * msg, volatile unsigned int * count);
void expectChange(const char * msg, volatile unsigned int * count);

// Test rts_pause/rts_resume by observing a count that we expect to be
// incremented by concurrent Haskell thread(s). We expect rts_pause to stop
// those threads and hence stop incrementing the count.
void pauseAndResume
    ( bool assertNotPaused // [in] True to enable assertions before rts_pause and after rts_resume.
                           // Often disabled when calling this concurrently.
    , volatile unsigned int * count  // [in] Haskell threads should be forever incrementing this.
    )
{
    // Assert the RTS is resumed.
    if (assertNotPaused)
    {
        expectChange("RTS should be running", count);
        if(rts_isPaused()) {
            errorBelch("Expected the RTS to be resumed.");
            exit(1);
        }
    }

    // Pause and assert.
    PauseToken * token = rts_pause();
    Capability * cap = pauseTokenCapability(token);
    if(cap == NULL) {
        errorBelch("rts_pause() returned NULL.");
        exit(1);
    }

    if(!rts_isPaused()) {
        errorBelch("Expected the RTS to be paused.");
        exit(1);
    }

    expectNoChange("RTS should be paused", count);

    // Resume.
    rts_resume(token);

    // Assert the RTS is resumed.
    if (assertNotPaused)
    {
        expectChange("RTS should be resumed", count);
        if(rts_isPaused()) {
            errorBelch("Expected the RTS to be resumed.");
            exit(1);
        }
    }
}

void helloWorld()
{
    printf("Hello World!");
}

// Pause tht RTS and call all RtsAPI.h functions.
void pauseAndUseRtsAPIAndResume
    ( HaskellObj haskellFn          // [in] A Haskell function (StablePtr (a -> a))
    , HaskellObj haskellFnArgument  // [in] An argument to apply to haskellFn (a)
    , HaskellObj obj1  // [in] arbitrary haskell value to evaluate of arbitrary type.
    , HaskellObj obj2  // [in] arbitrary haskell value to evaluate of arbitrary type.
    , HsStablePtr stablePtrIO  // [in] arbitrary haskell IO action to execute (StablePtr (IO t))
    )
{
    // Pause the RTS.
    printf("Pause the RTS...");
    PauseToken * token = rts_pause();
    Capability * cap = pauseTokenCapability(token);
    printf("Paused\n");

    // Note the original capability. We assert that cap is not changed by
    // functions that take &cap.
    Capability *const cap0 = cap;

    // Call RtsAPI.h functions
    printf("getRTSStats...\n");
    RTSStats s;
    getRTSStats (&s);
    printf("getRTSStatsEnabled...\n");
    getRTSStatsEnabled();
    printf("getAllocations...\n");
    getAllocations();
    printf("rts_getSchedStatus...\n");
    rts_getSchedStatus(cap);
    printf("rts_getChar, rts_mkChar...\n");
    rts_getChar     (rts_mkChar       ( cap, 0 ));
    printf("rts_getInt, rts_mkInt...\n");
    rts_getInt      (rts_mkInt        ( cap, 0 ));
    printf("rts_getInt8, rts_mkInt8...\n");
    rts_getInt8     (rts_mkInt8       ( cap, 0 ));
    printf("rts_getInt16, rts_mkInt16...\n");
    rts_getInt16    (rts_mkInt16      ( cap, 0 ));
    printf("rts_getInt32, rts_mkInt32...\n");
    rts_getInt32    (rts_mkInt32      ( cap, 0 ));
    printf("rts_getInt64, rts_mkInt64...\n");
    rts_getInt64    (rts_mkInt64      ( cap, 0 ));
    printf("rts_getWord, rts_mkWord...\n");
    rts_getWord     (rts_mkWord       ( cap, 0 ));
    printf("rts_getWord8, rts_mkWord8...\n");
    rts_getWord8    (rts_mkWord8      ( cap, 0 ));
    printf("rts_getWord16, rts_mkWord16...\n");
    rts_getWord16   (rts_mkWord16     ( cap, 0 ));
    printf("rts_getWord32, rts_mkWord32...\n");
    rts_getWord32   (rts_mkWord32     ( cap, 0 ));
    printf("rts_getWord64, rts_mkWord64...\n");
    rts_getWord64   (rts_mkWord64     ( cap, 0 ));
    printf("rts_getPtr, rts_mkPtr...\n");
    int x = 0;
    rts_getPtr      (rts_mkPtr        ( cap, &x));
    printf("rts_getFunPtr, rts_mkFunPtr...\n");
    rts_getFunPtr   (rts_mkFunPtr     ( cap, &helloWorld ));
    printf("rts_getFloat, rts_mkFloat...\n");
    rts_getFloat    (rts_mkFloat      ( cap, 0.0 ));
    printf("rts_getDouble, rts_mkDouble...\n");
    rts_getDouble   (rts_mkDouble     ( cap, 0.0 ));
    printf("rts_getStablePtr, rts_mkStablePtr...\n");
    rts_getStablePtr (rts_mkStablePtr ( cap, &x ));
    printf("rts_getBool, rts_mkBool...\n");
    rts_getBool     (rts_mkBool       ( cap, 0 ));
    printf("rts_mkString...\n");
    rts_mkString     ( cap, "Hello ghc-debug!" );
    printf("rts_apply...\n");
    rts_apply        ( cap, (HaskellObj)deRefStablePtr(haskellFn), haskellFnArgument );

    printf("rts_eval...\n");
    HaskellObj ret;
    rts_eval(&cap, obj1, &ret);
    assert(cap == cap0);

    printf("rts_eval_...\n");
    rts_eval_ (&cap, obj2, 50, &ret);
    assert(cap == cap0);

    printf("rts_evalIO...\n");
    HaskellObj io = (HaskellObj)deRefStablePtr(stablePtrIO);
    rts_evalIO (&cap, io, &ret);
    assert(cap == cap0);

    printf("rts_evalStableIOMain...\n");
    HsStablePtr retStablePtr;
    rts_evalStableIOMain (&cap, stablePtrIO, &retStablePtr);
    assert(cap == cap0);

    printf("rts_evalStableIO...\n");
    rts_evalStableIO (&cap, stablePtrIO, &retStablePtr);
    assert(cap == cap0);

    printf("rts_evalLazyIO...\n");
    rts_evalLazyIO (&cap, io, &ret);
    assert(cap == cap0);

    printf("rts_evalLazyIO_...\n");
    rts_evalLazyIO_ (&cap,  io, 50, &ret);
    assert(cap == cap0);

    printf("rts_setInCallCapability...\n");
    rts_setInCallCapability (0, 1);
    printf("rts_pinThreadToNumaNode...\n");
    rts_pinThreadToNumaNode (0);

    // Resume the RTS.
    printf("Resume the RTS...");
    rts_resume(token);
    assert(cap == cap0);
    printf("Resumed\n");
}

void* pauseAndResumeViaThread_helper(void * count)
{
    pauseAndResume(false, (volatile unsigned int *)count);
    return NULL;
}

// Call pauseAndResume via a new thread and return the thread ID.
void pauseAndResumeViaThread
    ( volatile unsigned int * count  // [in] Haskell threads should be forever incrementing this.
    )
{
    OSThreadId threadId;
    createOSThread(&threadId, "Pause and resume thread", &pauseAndResumeViaThread_helper, (void *)count);
}

const int TIMEOUT = 1000000; // 1 second

// Wait for &count to change (else exit(1) after TIMEOUT).
void expectChange(const char * msg, volatile unsigned int * count)
{
    unsigned int count_0 = *count;
    int microSecondsLeft = TIMEOUT;
    unsigned int sleepTime = 10000;
    while (true)
    {
        usleep(sleepTime);
        microSecondsLeft -= sleepTime;

        if (count_0 != *count)
        {
            // Change detected.
            return;
        }

        if (microSecondsLeft < 0)
        {
            printf("Expected: %s\n", msg);
            exit(1);
        }
    }
}

// Ensure &count does NOT change (for TIMEOUT else exit(1)).
void expectNoChange(const char * msg, volatile unsigned int * count)
{
    unsigned int count_0 = *count;
    int microSecondsLeft = TIMEOUT;
    unsigned int sleepTime = 10000;
    while (true)
    {
        usleep(sleepTime);
        microSecondsLeft -= sleepTime;

        if (count_0 != *count)
        {
            // Change detected.
            printf("Expected: %s\n", msg);
            exit(1);
        }

        if (microSecondsLeft < 0)
        {
            return;
        }
    }
}