summaryrefslogtreecommitdiffstats
path: root/tools/cygprofile/cygprofile_unittest.cc
blob: 2022c03b462452f0ab2240c987ac742426f84eb6 (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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "tools/cygprofile/cygprofile.h"

#include <vector>

#include <sys/time.h>

#include "base/bind.h"
#include "base/callback.h"
#include "base/logging.h"
#include "base/synchronization/waitable_event.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace cygprofile {
namespace {

void FlushEntries(std::vector<LogEntry>* destination,
                  std::vector<LogEntry>* entries) {
  CHECK_EQ(0U, destination->size());
  // Move the provided |entries| vector to the provided |destination| so that
  // the unit test that triggered the flush can check it.
  destination->swap(*entries);
}

// Flush callback that should not be invoked.
void CheckFlushDoesNotHappen(std::vector<LogEntry>* entries) {
  NOTREACHED();
}

uint64_t GetUsecSecTimeFromTimeSpec(struct timespec timespec) {
  return timespec.tv_sec * 1000 * 1000 + timespec.tv_nsec / 1000;
}

TEST(CygprofileTest, ThreadLogBasic) {
  ThreadLog thread_log(base::Bind(&CheckFlushDoesNotHappen));

  thread_log.AddEntry(reinterpret_cast<void*>(0x2));
  thread_log.AddEntry(reinterpret_cast<void*>(0x1));

  std::vector<LogEntry> entries;
  thread_log.TakeEntries(&entries);

  ASSERT_EQ(2U, entries.size());
  // The entries should appear in their insertion order.
  const LogEntry& first_entry = entries[0];
  ASSERT_EQ(reinterpret_cast<uintptr_t>(first_entry.address), 2);
  ASSERT_EQ(getpid(), first_entry.pid);
  ASSERT_LT(0, first_entry.tid);

  const LogEntry& second_entry = entries[1];
  ASSERT_EQ(1, reinterpret_cast<uintptr_t>(second_entry.address));
  ASSERT_EQ(first_entry.pid, second_entry.pid);
  ASSERT_EQ(first_entry.tid, second_entry.tid);

  ASSERT_GE(GetUsecSecTimeFromTimeSpec(second_entry.time),
            GetUsecSecTimeFromTimeSpec(first_entry.time));
}

TEST(CygprofileTest, ManagerBasic) {
  base::WaitableEvent wait_event(true, false);
  base::WaitableEvent notify_event(true, false);

  ThreadLogsManager manager(
      base::Bind(&base::WaitableEvent::Wait, base::Unretained(&wait_event)),
      base::Bind(&base::WaitableEvent::Signal,
                 base::Unretained(&notify_event)));

  std::vector<LogEntry> entries;
  scoped_ptr<ThreadLog> thread_log(
      new ThreadLog(base::Bind(&FlushEntries, base::Unretained(&entries))));

  thread_log->AddEntry(reinterpret_cast<void*>(0x2));
  thread_log->AddEntry(reinterpret_cast<void*>(0x3));

  // This should make the manager spawn its internal flush thread which will
  // wait for a notification before it starts doing some work.
  manager.AddLog(thread_log.Pass());

  EXPECT_EQ(0U, entries.size());
  // This will wake up the internal thread.
  wait_event.Signal();
  // Now it's our turn to wait until it performed the flush.
  notify_event.Wait();

  // The flush should have moved the data to the local vector of entries.
  EXPECT_EQ(2U, entries.size());
  ASSERT_EQ(2, reinterpret_cast<uintptr_t>(entries[0].address));
  ASSERT_EQ(3, reinterpret_cast<uintptr_t>(entries[1].address));
}

}  // namespace
}  // namespace cygprofile

// Custom runner implementation since base's one requires JNI on Android.
int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}