blob: 393ebbe12c3e6353e0bf837c9afcfd4ebaf8d92a [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2// Copyright 2009 Google Inc. All Rights Reserved.
3// Author: fikes@google.com (Andrew Fikes)
4//
5// Use of this source code is governed by a BSD-style license that can
6// be found in the LICENSE file.
7
8
9#include "config_for_unittests.h"
10#include <stdio.h> // for puts()
11#include "stack_trace_table.h"
12#include "base/logging.h"
13#include "base/spinlock.h"
14#include "static_vars.h"
15
16#undef ARRAYSIZE // may be defined on, eg, windows
17#define ARRAYSIZE(a) ( sizeof(a) / sizeof(*(a)) )
18
19static void CheckTracesAndReset(tcmalloc::StackTraceTable* table,
20 const uintptr_t* expected, int len) {
21 void** entries = table->ReadStackTracesAndClear();
22 for (int i = 0; i < len; ++i) {
23 CHECK_EQ(reinterpret_cast<uintptr_t>(entries[i]), expected[i]);
24 }
25 delete[] entries;
26}
27
28static void AddTrace(tcmalloc::StackTraceTable* table,
29 const tcmalloc::StackTrace& t) {
30 // Normally we'd need this lock, but since the test is single-threaded
31 // we don't. I comment it out on windows because the DLL-decl thing
32 // is really annoying in this case.
33#ifndef _MSC_VER
34 SpinLockHolder h(tcmalloc::Static::pageheap_lock());
35#endif
36 table->AddTrace(t);
37}
38
39int main(int argc, char **argv) {
40 tcmalloc::StackTraceTable table;
41
42 // Empty table
43 CHECK_EQ(table.depth_total(), 0);
44 CHECK_EQ(table.bucket_total(), 0);
45 static const uintptr_t k1[] = {0};
46 CheckTracesAndReset(&table, k1, ARRAYSIZE(k1));
47
48 tcmalloc::StackTrace t1;
49 t1.size = static_cast<uintptr_t>(1024);
50 t1.depth = static_cast<uintptr_t>(2);
51 t1.stack[0] = reinterpret_cast<void*>(1);
52 t1.stack[1] = reinterpret_cast<void*>(2);
53
54
55 tcmalloc::StackTrace t2;
56 t2.size = static_cast<uintptr_t>(512);
57 t2.depth = static_cast<uintptr_t>(2);
58 t2.stack[0] = reinterpret_cast<void*>(2);
59 t2.stack[1] = reinterpret_cast<void*>(1);
60
61 // Table w/ just t1
62 AddTrace(&table, t1);
63 CHECK_EQ(table.depth_total(), 2);
64 CHECK_EQ(table.bucket_total(), 1);
65 static const uintptr_t k2[] = {1, 1024, 2, 1, 2, 0};
66 CheckTracesAndReset(&table, k2, ARRAYSIZE(k2));
67
68 // Table w/ t1, t2
69 AddTrace(&table, t1);
70 AddTrace(&table, t2);
71 CHECK_EQ(table.depth_total(), 4);
72 CHECK_EQ(table.bucket_total(), 2);
Brian Silverman20350ac2021-11-17 18:19:55 -080073 static const uintptr_t k3[] = {1, 512, 2, 2, 1, 1, 1024, 2, 1, 2, 0};
Austin Schuh745610d2015-09-06 18:19:50 -070074 CheckTracesAndReset(&table, k3, ARRAYSIZE(k3));
75
Brian Silverman20350ac2021-11-17 18:19:55 -080076 // Table w/ t1, t3
Austin Schuh745610d2015-09-06 18:19:50 -070077 // Same stack as t1, but w/ different size
78 tcmalloc::StackTrace t3;
79 t3.size = static_cast<uintptr_t>(2);
80 t3.depth = static_cast<uintptr_t>(2);
81 t3.stack[0] = reinterpret_cast<void*>(1);
82 t3.stack[1] = reinterpret_cast<void*>(2);
83
Austin Schuh745610d2015-09-06 18:19:50 -070084 AddTrace(&table, t1);
85 AddTrace(&table, t3);
Brian Silverman20350ac2021-11-17 18:19:55 -080086 CHECK_EQ(table.depth_total(), 4);
87 CHECK_EQ(table.bucket_total(), 2);
88 static const uintptr_t k5[] = {1, 2, 2, 1, 2, 1, 1024, 2, 1, 2, 0};
Austin Schuh745610d2015-09-06 18:19:50 -070089 CheckTracesAndReset(&table, k5, ARRAYSIZE(k5));
90
91 puts("PASS");
92 return 0;
93}