summaryrefslogtreecommitdiffstats
path: root/tools/profiler/tests/gtest/ThreadProfileTest.cpp
blob: 4399a5bc2e43b4e57cd1da5300ea0e245b311e15 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"

#include "ProfileEntry.h"
#include "ThreadProfile.h"

// Make sure we can initialize our ThreadProfile
TEST(ThreadProfile, Initialization) {
  PseudoStack* stack = PseudoStack::create();
  Thread::tid_t tid = 1000;
  ThreadInfo info("testThread", tid, true, stack, nullptr);
  RefPtr<ProfileBuffer> pb = new ProfileBuffer(10);
  ThreadProfile tp(&info, pb);
}

// Make sure we can record one tag and read it
TEST(ThreadProfile, InsertOneTag) {
  PseudoStack* stack = PseudoStack::create();
  Thread::tid_t tid = 1000;
  ThreadInfo info("testThread", tid, true, stack, nullptr);
  RefPtr<ProfileBuffer> pb = new ProfileBuffer(10);
  pb->addTag(ProfileEntry('t', 123.1));
  ASSERT_TRUE(pb->mEntries != nullptr);
  ASSERT_TRUE(pb->mEntries[pb->mReadPos].mTagName == 't');
  ASSERT_TRUE(pb->mEntries[pb->mReadPos].mTagDouble == 123.1);
}

// See if we can insert some tags
TEST(ThreadProfile, InsertTagsNoWrap) {
  PseudoStack* stack = PseudoStack::create();
  Thread::tid_t tid = 1000;
  ThreadInfo info("testThread", tid, true, stack, nullptr);
  RefPtr<ProfileBuffer> pb = new ProfileBuffer(100);
  int test_size = 50;
  for (int i = 0; i < test_size; i++) {
    pb->addTag(ProfileEntry('t', i));
  }
  ASSERT_TRUE(pb->mEntries != nullptr);
  int readPos = pb->mReadPos;
  while (readPos != pb->mWritePos) {
    ASSERT_TRUE(pb->mEntries[readPos].mTagName == 't');
    ASSERT_TRUE(pb->mEntries[readPos].mTagInt == readPos);
    readPos = (readPos + 1) % pb->mEntrySize;
  }
}

// See if wrapping works as it should in the basic case
TEST(ThreadProfile, InsertTagsWrap) {
  PseudoStack* stack = PseudoStack::create();
  Thread::tid_t tid = 1000;
  // we can fit only 24 tags in this buffer because of the empty slot
  int tags = 24;
  int buffer_size = tags + 1;
  ThreadInfo info("testThread", tid, true, stack, nullptr);
  RefPtr<ProfileBuffer> pb = new ProfileBuffer(buffer_size);
  int test_size = 43;
  for (int i = 0; i < test_size; i++) {
    pb->addTag(ProfileEntry('t', i));
  }
  ASSERT_TRUE(pb->mEntries != nullptr);
  int readPos = pb->mReadPos;
  int ctr = 0;
  while (readPos != pb->mWritePos) {
    ASSERT_TRUE(pb->mEntries[readPos].mTagName == 't');
    // the first few tags were discarded when we wrapped
    ASSERT_TRUE(pb->mEntries[readPos].mTagInt == ctr + (test_size - tags));
    ctr++;
    readPos = (readPos + 1) % pb->mEntrySize;
  }
}