1 
2 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "ProfileBuffer.h"
8 #include "ThreadInfo.h"
9 
10 #include "mozilla/PowerOfTwo.h"
11 #include "mozilla/ProfileBufferChunkManagerWithLocalLimit.h"
12 #include "mozilla/ProfileChunkedBuffer.h"
13 
14 #include "gtest/gtest.h"
15 
16 // Make sure we can record one entry and read it
TEST(ThreadProfile,InsertOneEntry)17 TEST(ThreadProfile, InsertOneEntry)
18 {
19   mozilla::ProfileBufferChunkManagerWithLocalLimit chunkManager(
20       2 * (1 + uint32_t(sizeof(ProfileBufferEntry))) * 4,
21       2 * (1 + uint32_t(sizeof(ProfileBufferEntry))));
22   mozilla::ProfileChunkedBuffer profileChunkedBuffer(
23       mozilla::ProfileChunkedBuffer::ThreadSafety::WithMutex, chunkManager);
24   auto pb = mozilla::MakeUnique<ProfileBuffer>(profileChunkedBuffer);
25   pb->AddEntry(ProfileBufferEntry::Time(123.1));
26   ProfileBufferEntry entry = pb->GetEntry(pb->BufferRangeStart());
27   ASSERT_TRUE(entry.IsTime());
28   ASSERT_EQ(123.1, entry.GetDouble());
29 }
30 
31 // See if we can insert some entries
TEST(ThreadProfile,InsertEntriesNoWrap)32 TEST(ThreadProfile, InsertEntriesNoWrap)
33 {
34   mozilla::ProfileBufferChunkManagerWithLocalLimit chunkManager(
35       100 * (1 + uint32_t(sizeof(ProfileBufferEntry))),
36       100 * (1 + uint32_t(sizeof(ProfileBufferEntry))) / 4);
37   mozilla::ProfileChunkedBuffer profileChunkedBuffer(
38       mozilla::ProfileChunkedBuffer::ThreadSafety::WithMutex, chunkManager);
39   auto pb = mozilla::MakeUnique<ProfileBuffer>(profileChunkedBuffer);
40   const int test_size = 50;
41   for (int i = 0; i < test_size; i++) {
42     pb->AddEntry(ProfileBufferEntry::Time(i));
43   }
44   int times = 0;
45   uint64_t readPos = pb->BufferRangeStart();
46   while (readPos != pb->BufferRangeEnd()) {
47     ProfileBufferEntry entry = pb->GetEntry(readPos);
48     readPos++;
49     if (entry.GetKind() == ProfileBufferEntry::Kind::INVALID) {
50       continue;
51     }
52     ASSERT_TRUE(entry.IsTime());
53     ASSERT_EQ(times, entry.GetDouble());
54     times++;
55   }
56   ASSERT_EQ(test_size, times);
57 }
58