summaryrefslogtreecommitdiffstats
path: root/src/usr/trace/runtime/rt_daemon.C
blob: 0e8300f953f3e09ce0535b9391f219d2b6c3c43d (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/trace/runtime/rt_daemon.C $                           */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2017                             */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#include "../daemon/daemon.H"
#include "../daemonif.H"
#include "../service.H"
#include "../buffer.H"
#include "../bufferpage.H"
#include "../entry.H"
#include "../compdesc.H"
#include "../debug.H"

#include <errl/hberrltypes.H>
#include <string.h>
#include <util/align.H>


namespace TRACEDAEMON
{
    using namespace TRACE;

    Daemon::Daemon() : iv_service(nullptr), iv_totalPruned(0)
    {
        iv_first = iv_last = BufferPage::allocate(true);
    }

    Daemon::~Daemon()
    {
        assert(0, "No need to destruct the trace Daemon");
    }

    void Daemon::signal_trace_daemon(void)
    {
        // Put here to avoid build issue instead of in class constructor
        // iv_service won't be used until these functions are called
        iv_service = Service::getGlobalInstance();

        // Collect trace entries from client.
        collectTracePages();

        // Reduce buffer space in daemon-side buffer.
        pruneTraceEntries();

        // combine trace buffer pages to remove pruned entries
        coalescePages();
    }


    void Daemon::collectTracePages()
    {
        // Collect buffer pages from front-end.
        BufferPage* srcPages[BUFFER_COUNT];
        for (size_t i = 0; i < BUFFER_COUNT; i++)
        {
            if (iv_service->iv_buffers[i] == nullptr)
            {
                iv_curPages[i] = srcPages[i] = nullptr;
            }
            else
            {
                iv_curPages[i] = srcPages[i] =
                    iv_service->iv_buffers[i]->claimPages();
            }
            iv_curOffset[i] = 0;
        }

        uint32_t entry_number = 0;
        // Process buffer pages.
        do
        {
            size_t whichBuffer = BUFFER_COUNT;
            Entry* whichEntry = nullptr;
            uint64_t minTimeStamp = UINT64_MAX;

            // Find the entry with the earliest timestamp.
            for (size_t i = 0; i < BUFFER_COUNT; i++)
            {
                if (nullptr == iv_curPages[i]) continue;

                Entry* entry =
                    reinterpret_cast<Entry*>(
                        &((&(iv_curPages[i]->data[0]))[iv_curOffset[i]])
                    );

                trace_bin_entry_t* binEntry =
                    reinterpret_cast<trace_bin_entry_t*>(
                        &(entry->data[0])
                    );


                uint64_t curTimeStamp =
                    TWO_UINT32_TO_UINT64(binEntry->stamp.tbh,
                                         binEntry->stamp.tbl);

                if (curTimeStamp < minTimeStamp )
                {
                    whichBuffer = i;
                    whichEntry = entry;
                    minTimeStamp = curTimeStamp;
                }
            }

            // Did not find another entry, our work is done.
            if (whichBuffer == BUFFER_COUNT)
            {
                break;
            }

            if ((whichEntry == nullptr) || (whichEntry->comp == nullptr))
            {
                break;
            }

            // Increment pointers to next buffer entry.
            iv_curOffset[whichBuffer] += whichEntry->size + sizeof(Entry);
            if (iv_curOffset[whichBuffer] >= iv_curPages[whichBuffer]->usedSize)
            {
                iv_curPages[whichBuffer] = iv_curPages[whichBuffer]->next;
                iv_curOffset[whichBuffer] = 0;
            }

            // Allocate a new back-end entry.
            Entry* mainBuffEntry = nullptr;
            while (nullptr ==
                (mainBuffEntry =
                    iv_last->claimEntry(whichEntry->size + sizeof(Entry))))
            {
                BufferPage* n = BufferPage::allocate(true);

                // Remember iv_last = Last newest combined-buffer page.
                // Set n to the newest page, and push current iv_last back
                n->next = iv_last;
                iv_last->prev = n;
                iv_last = n;
                iv_last->prev = nullptr;
            }

            // Move entry from front-end buffer to back-end.
            replaceEntry(whichEntry, mainBuffEntry);

            entry_number++;
        } while(1);

        // Release pages.
        for (size_t i = 0; i < BUFFER_COUNT; i++)
        {
            while(srcPages[i])
            {
                BufferPage* tmp = srcPages[i]->next;
                BufferPage::deallocate(srcPages[i]);
                srcPages[i] = tmp;
            }
        }
    }


    void Daemon::replaceEntry(Entry* from, Entry* to)
    {
        do
        {
            // Copy entry content to new entry.
            memcpy(to,
                   from,
                   from->size + sizeof(Entry));

            // Update next object's pointer.
            if (to->next)
            {
                to->next->prev = to;
            }
            else
            {
                if (to->comp != nullptr)
                {
                    to->comp->iv_last = to;
                }
            }

            // Update prev object's pointer.
            //     Buffer ensures that an entry's "next" is written before
            //     the "next->prev", so we can be certain that if to->prev
            //     then to->prev->next is finalized.
            if (to->prev)
            {
                to->prev->next = to;
            }
            else // If there is no previous, this is the first (most recent)
                 // for the component, so update the component object.
            {
                if (to->comp != nullptr)
                {
                    Buffer* b =
                        iv_service->iv_buffers[to->comp->iv_bufferType];

                    // Need to toggle the consumer lock on this one, so use
                    // the consumerOp to move the compoment->first from
                    // 'from' to 'to'.
                    //
                    // If it fails (first != from anymore) then retry this
                    // whole sequence.
                    if (!b->consumerOp(&to->comp->iv_first, from,
                                       &to->comp->iv_first, to))
                    {
                        continue;
                    }
                }
            }

            // Successfully updated everything, break from loop.
            break;

        } while (1);
    }


    void Daemon::pruneTraceEntries(bool i_all)
    {
        ComponentList::List::iterator component;

        size_t pruned = 0;

        // Iterate through the components...
        bool more = iv_service->iv_compList->first(component);
        while(more)
        {
            Entry* entry = component->iv_last;
            Entry* orig_entry = entry;

            // Invalidate entries until the component is small enough.
            while((entry) &&
                    ((component->iv_curSize > component->iv_maxSize) ||
                     i_all)
                 )
            {
                if (!reinterpret_cast<BufferPage*>(
                        ALIGN_PAGE_DOWN(
                            reinterpret_cast<uint64_t>(entry)))->commonPage)
                {
                    break;
                }

                entry->comp = nullptr; // Invalidate entry.

                __sync_sub_and_fetch(&component->iv_curSize, entry->size);
                pruned += entry->size;

                entry = entry->prev;
            }

            if (entry != orig_entry)
            {

                // Break chain of linked list.
                if (entry != nullptr)
                {
                    entry->next = nullptr;
                }

                // Update component pointers.
                Buffer* b =
                    iv_service->iv_buffers[component->iv_bufferType];

                // consumerOp pseudo-code:
                //    if (entry == nullptr) component->first = nullptr;
                //    component->last = entry;
                b->consumerOp(&entry, nullptr,
                              &component->iv_first, nullptr,
                              &component->iv_last, entry);
            }

            // Get next component.
            more = iv_service->iv_compList->next(component);
        }

        // Record size of pruned entries in a global.
        if (pruned)
        {
            iv_totalPruned += pruned;
        }
    }

    void Daemon::coalescePages()
    {
        // Skip if there hasn't been enough entries pruned to make this
        // worth our while.
        if (iv_totalPruned < PAGESIZE) { return; }

        iv_totalPruned = 0;

        // Allocate a new back-end page for the coalesced entries.
        BufferPage* outputPage = BufferPage::allocate(true);
        BufferPage* originalOutputPage = outputPage;

        // Get the first page from the original back-end buffer.
        BufferPage* currentPage = iv_first;

        // Iterate through the back-end pages.
        while(currentPage != nullptr)
        {
            // Look at all the entries on the back-end pages.
            size_t offset = 0;
            while (offset < currentPage->usedSize)
            {
                Entry* entry =
                    reinterpret_cast<Entry*>(&currentPage->data[offset]);

                if (nullptr != entry->comp) // Ensure entry is valid.
                {
                    Entry* newEntry = nullptr;

                    // Allocate space on new back-end pages.
                    while (nullptr == (newEntry =
                        outputPage->claimEntry(entry->size + sizeof(Entry))))
                    {
                        BufferPage* newPage = BufferPage::allocate(true);

                        newPage->next = outputPage;
                        outputPage->prev = newPage;
                        outputPage = newPage;
                    }

                    // Move entry to new back-end page.
                    replaceEntry(entry, newEntry);
                }

                offset += entry->size + sizeof(Entry);
            }

            currentPage = currentPage->prev;
        }

        BufferPage* oldPage = iv_first;

        // Update back-end buffer pointers to point to new back-end pages.
        iv_last = outputPage;
        iv_first = originalOutputPage;

        // Delete the old back-end pages.
        while(oldPage)
        {
            BufferPage* temp = oldPage->prev;
            BufferPage::deallocate(oldPage);
            oldPage = temp;
        }

    }

}
OpenPOWER on IntegriCloud