summaryrefslogtreecommitdiffstats
path: root/src/usr/trace/buffer.C
blob: 5367d93eccfd664bf78c469ddd47d7645e6bdf61 (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/trace/buffer.C $                                      */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2012,2015                        */
/* [+] 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 "buffer.H"
#include "bufferpage.H"
#include "entry.H"
#include "compdesc.H"
#include "daemonif.H"

#include <assert.h>
#include <limits.h>
#include <string.h>
#include <util/align.H>
#include <util/lockfree/abaptr.H>

namespace TRACE
{
    Buffer::Buffer(DaemonIf* i_daemon, size_t i_maxPages) :
        iv_pagesAlloc(0), iv_pagesMax(i_maxPages),
        iv_firstPage(NULL), iv_daemon(i_daemon)
    {
        iv_counters.totals = 0;
        assert(i_maxPages > 0);
    }

    void Buffer::_producerEnter()
    {
        locklessCounter value;
        do
        {
            // Read current count.
            value = iv_counters;

            // If there is a consumer (daemon) present, must wait.
            while (value.consumerCount != 0)
            {
                futex_wait(&iv_counters.totals, value.totals);
                value = iv_counters;
            }

            // No consumers currently present, increment the producer count
            // (to include us).
            locklessCounter newValue = value;
            newValue.producerCount++;

            // Attempt to atomically update the count.
            if (__sync_bool_compare_and_swap(&iv_counters.totals,
                                             value.totals,
                                             newValue.totals)
               )
            {
                // Successful at atomic update, we're included in the count
                // so we can exit the loop.
                break;
            }

            // Failed to update count, so try again.
        } while(1);
    }

    void Buffer::_producerExit()
    {
        locklessCounter value;
        do
        {
            // Read current count.
            value = iv_counters;

            // Decrement count to remove us.
            locklessCounter newValue = value;
            newValue.producerCount--;

            // Attempt to atomically update count.
            if (!__sync_bool_compare_and_swap(&iv_counters.totals,
                                              value.totals,
                                              newValue.totals))
            {
                // Failed, try again.
                continue;
            }

            // If we're the last producer and there is a consumer waiting,
            // signal the consumer.
            if((newValue.producerCount == 0) && (newValue.consumerCount != 0))
            {
                futex_wake(&iv_counters.totals, UINT64_MAX);
            }

            // If we're here, we are successful, so exit the loop.
            break;

        } while(1);
    }

    void Buffer::_consumerEnter()
    {
        locklessCounter value;
        do
        {
            // Read current count.
            value = iv_counters;

            // Set us up as a pending consumer.
            locklessCounter newValue = value;
            newValue.consumerCount = 1;

            // Attempt to atomically update counts.
            if (!__sync_bool_compare_and_swap(&iv_counters.totals,
                                              value.totals,
                                              newValue.totals))
            {
                // Failed, try again.
                continue;
            }

            // If there were producers waiting, we need to wait for them
            // to clear out.
            if (0 != newValue.producerCount)
            {
                do
                {
                    futex_wait(&iv_counters.totals, newValue.totals);
                    newValue = iv_counters;

                } while(newValue.producerCount != 0);
            }

            // If we're here, we are successful, so exit the loop.
            break;

        } while(1);
    }

    void Buffer::_consumerExit()
    {
        locklessCounter value;
        do
        {
            // Read current count.
            value = iv_counters;

            // Remove ourself as the consumer.
            locklessCounter newValue = value;
            newValue.consumerCount = 0;

            // Atomically update the count.
            if (!__sync_bool_compare_and_swap(&iv_counters.totals,
                                              value.totals,
                                              newValue.totals))
            {
                // Failed, try again.
                continue;
            }

            // Successful.  Signal any producers that might be waiting and
            // exit the loop.
            futex_wake(&iv_counters.totals, UINT64_MAX);
            break;

        } while(1);
    }


    Entry* Buffer::claimEntry(ComponentDesc* i_comp, size_t i_size)
    {
        // Bump up the needed size to include the entry itself and
        // necessary alignment.
        // (Alignment is needed so that Entry's members can be atomically
        //  updated).
        i_size = ALIGN_8(i_size + sizeof(Entry));

        // If an entry is bigger than this amount, it can never be satisfied.
        if (i_size > (PAGESIZE - sizeof(BufferPage)))
        {
            return NULL;
        }

        // During the process of claiming an entry, the daemon must be
        // blocked out from tampering with the pages.
        _producerEnter();

        Entry* l_entry = NULL;

        // No we begin the search for an entry.
        do
        {
            Util::Lockfree::AbaPtr<BufferPage> original_first =
                Util::Lockfree::AbaPtr<BufferPage>(iv_firstPage);
            BufferPage* first = original_first.get();

            // Attempt to claim from the current page first.
            if (first)
            {
                l_entry = first->claimEntry(i_size);
                if (l_entry)
                {
                    // Found one, great!  We're done.
                    break;
                }

                // If there is a "next" page, another thread has already
                // allocated a new page, so just wait for it to show up.
                // (as in, some other thread is going to update iv_firstPage)
                if (first->next)
                {
                    continue;
                }
            }

            // Wasn't enough space, so try to allocate a new page.
            uint64_t pagesAllocated = iv_pagesAlloc;
            if (pagesAllocated >= iv_pagesMax)
            {
                // Not enough pages.  Wait until someone frees one.
                _producerExit();
                iv_daemon->signal();
                futex_wait(reinterpret_cast<uint64_t*>(
                                const_cast<BufferPage**>(&iv_firstPage)),
                           reinterpret_cast<uint64_t>(original_first.value()));
                _producerEnter();
                // A page might be allocated now, start over.
                continue;
            }

            // Atomically update page count.
            uint64_t newPagesAllocated = pagesAllocated + 1;
            if (!__sync_bool_compare_and_swap(&iv_pagesAlloc,
                                              pagesAllocated,
                                              newPagesAllocated))
            {
                // Someone beat us to updating the count, so start over.
                // (another thread is also trying to allocate a page, let them
                //  do it).
                continue;
            }

            // Successfully updated the count so allocate the new page.
            BufferPage* newPage = BufferPage::allocate();
            newPage->prev = first;
            original_first.set(newPage);

            // Now we have a page allocated, claim our entry first and then
            // hook it up to master list.
            l_entry = newPage->claimEntry(i_size);

            if (!original_first.update(&iv_firstPage))
            {
                // We got beat adding page to the master list, so release it
                // and use that page.
                BufferPage::deallocate(newPage);
                do
                {
                    pagesAllocated = iv_pagesAlloc;
                    newPagesAllocated = pagesAllocated - 1;
                }
                while (!__sync_bool_compare_and_swap(&iv_pagesAlloc,
                                                     pagesAllocated,
                                                     newPagesAllocated));

                // The entry we claimed out of the "new" page is no longer
                // valid since we've freed it.
                l_entry = NULL;
                continue;
            }

            // If there was a page already, update its next pointer to point
            // back at this new page.
            if (first)
            {
                if (!__sync_bool_compare_and_swap(&first->next,
                                                  NULL,
                                                  newPage))
                {
                    // We were the first one to update iv_firstPage, so
                    // first->next should have been NULL and nobody was
                    // suppose to touch it.
                    assert(false);
                }
            }

            // Since we allocated a page, signal any other tasks that might be
            // blocked waiting for a page to show up.
            futex_wake(reinterpret_cast<uint64_t*>(
                            const_cast<BufferPage**>(&iv_firstPage)),
                       UINT64_MAX);

            // And since we allocated a page, wake up the daemon if we
            // allocated the last page or if there are more than 4 pages
            // and we are an infinite buffer.  (4 pages is arbitrary).
            static const size_t SIGNAL_AFTER_N_PAGES_ALLOCATED = 4;
            if ((pagesAllocated == iv_pagesMax) ||
                ((pagesAllocated >= SIGNAL_AFTER_N_PAGES_ALLOCATED) &&
                 (iv_pagesMax == UNLIMITED)))
            {
                iv_daemon->signal();
            }


        } while(!l_entry);

        // Update component name and entry size.
        l_entry->comp = i_comp;
        l_entry->size = i_size - sizeof(Entry);

        // Leave critical section.
        _producerExit();

        return l_entry;
    }

    BufferPage* Buffer::claimPages()
    {
        // Enter critical daemon section.
        _consumerEnter();

        // Take page(s) from buffer.
        BufferPage* page =
            Util::Lockfree::AbaPtr<BufferPage>(iv_firstPage).get();
        iv_firstPage = NULL;
        iv_pagesAlloc = 0;

        // Rewind to beginning of buffer.
        if (page)
        {
            while(page->prev) { page = page->prev; }
        }

        // Signal producers that might be waiting for pages to free up.
        futex_wake(reinterpret_cast<uint64_t*>(
                            const_cast<BufferPage**>(&iv_firstPage)),
                       UINT64_MAX);

        // Exit critical daemon section.
        _consumerExit();

        return page;
    }

    void Buffer::commitEntry(Entry* i_entry)
    {
        // Prevent daemon from making updates while we're in here.
        _producerEnter();

        // Read the component from the entry itself (added as part of claiming).
        ComponentDesc* l_comp = i_entry->comp;

        Entry* l_savedNext = NULL;

        // Lockless loop to update component linked-list.
        do
        {
            // Update our entry's "next" pointer.
            // Note: Our next pointer could change out from under us by the
            // daemon's replaceEntry function, but the component iv_first
            // cannot change by the daemon until we _producerExit, therefore
            // we need to save the original next pointer for the atomic update
            // of l_comp->iv_first below.
            l_savedNext = i_entry->next = l_comp->iv_first;

            // If there is an entry, update its "prev" pointer to this entry.
            if (l_savedNext)
            {
                if (!__sync_bool_compare_and_swap(&l_savedNext->prev,
                                                  NULL,
                                                  i_entry))
                {
                    // Failed lockless update, try again.
                    continue;
                }
            }
            // Otherwise, we need to update the component list since this is
            // the first entry.
            else
            {
                if (!__sync_bool_compare_and_swap(&l_comp->iv_last,
                                                  NULL,
                                                  i_entry))
                {
                    // Failed lockless update, try again.
                    continue;
                }
            }

            // Successful at our updates, break out.
            break;

        } while(1);

        // We just added the newest entry, so update component list.
        //
        // It is possible that the committer of the preceding entry was
        // "slow", so we need to keep trying to update until the current
        // compoment "first" points at the preceding entry and then make
        // it point at this entry.
        //
        while (!__sync_bool_compare_and_swap(&l_comp->iv_first,
                                             l_savedNext,
                                             i_entry));


        // Atomically increment component size.
        __sync_add_and_fetch(&l_comp->iv_curSize, i_entry->size);

        // Mark entry as committed.
        i_entry->committed = 1;

        // All done, release for daemon.
        _producerExit();

    }

    size_t Buffer::getTrace(ComponentDesc* i_comp, void* o_data, size_t i_size)
    {
        char* l_data = reinterpret_cast<char*>(o_data);
        size_t l_size = 0;
        size_t l_entries = 0;

        // If either the pointer is null or the buffer is 0, we're just trying
        // to determine the size of the buffer.
        bool determineSize = ((o_data == NULL) || (i_size == 0));

        if (determineSize)
        {
            i_size = UINT64_MAX;
        }

        trace_buf_head_t* header = NULL;

        // If we're actually extracting, add the fsp-trace buffer header.
        if(!determineSize)
        {
            if (i_size < sizeof(trace_buf_head_t))
                return 0;

            header = reinterpret_cast<trace_buf_head_t*>(&l_data[l_size]);
            memset(header, '\0', sizeof(trace_buf_head_t));

            header->ver = TRACE_BUF_VERSION;
            header->hdr_len = sizeof(trace_buf_head_t);
            header->time_flg = TRACE_TIME_REAL;
            header->endian_flg = 'B'; // Big Endian.
            memcpy(&header->comp[0], &i_comp->iv_compName, TRAC_COMP_SIZE);
        }
        l_size += sizeof(trace_buf_head_t);

        // Prevent daemon from changing things while we're extracting.
        _producerEnter();

        size_t l_totalSize = l_size;
        Entry* entry = i_comp->iv_first;
        size_t l_entriesToExtract = 0;

        do
        {
            if ((!entry) || (!entry->comp))
            {
                break;
            }

            // First walk the list backwards to find everything that will fit.
            while(1)
            {
                // fsp-trace buffer entries have an extra word of size at the
                // end.  That is where the sizeof(uint32_t) comes from...

                if ((l_totalSize + entry->size + sizeof(uint32_t)) <= i_size)
                {
                    l_totalSize += entry->size + sizeof(uint32_t);
                    l_entriesToExtract++;

                    if ((entry->next) &&
                        (entry->next->comp))
                    {
                        entry = entry->next;
                        continue;
                    }
                }
                else // This entry was too big to fit, so roll back one.
                {
                    entry = entry->prev;
                }
                break;
            }

            // If we didn't find anything that fit, leave.
            if (l_totalSize == l_size)
            {
                break;
            }

            // If we're just trying to find the size, we're done.
            if(determineSize)
            {
                l_size = l_totalSize;
                break;
            }

            // Now we can actually copy all the entries...
            while(entry != NULL)
            {
                // Copy entry data.
                memcpy(&l_data[l_size], &entry->data[0],entry->size);
                l_size += entry->size;

                // Copy entry size.
                uint32_t entry_size = entry->size + sizeof(uint32_t);
                memcpy(&l_data[l_size], &entry_size, sizeof(uint32_t));
                l_size += sizeof(uint32_t);

                l_entries++;

                if (l_entries == l_entriesToExtract)
                {
                    break;
                }
                else
                {
                    entry = entry->prev;
                }
            };

        }
        while(0);

        // Unlock for daemon.
        _producerExit();

        // Update header.
        if (header)
        {
            header->size = l_size;
            header->next_free = l_size;
            header->te_count = l_entries;
        }

        return l_size;
    }

    bool Buffer::consumerOp(Entry** i_condAddr, Entry* i_condVal,
                                Entry** i_condActAddr,
                                Entry* i_condActVal,
                            Entry** i_addr, Entry* i_val)
    {
        bool rc = true;

        // Get consumer lock.
        _consumerEnter();

        // Primitive #1.
        if (NULL != i_condAddr)
        {
            if (i_condVal == *i_condAddr)
            {
                if (NULL != i_condActAddr)
                {
                    *i_condActAddr = i_condActVal;
                }
            }
            else
            {
                rc = false;
            }
        }

        // Primitive #2.
        if (NULL != i_addr)
        {
            *i_addr = i_val;
        }

        // Release consumer lock.
        _consumerExit();

        return rc;
    }

}
OpenPOWER on IntegriCloud