/Users/deen/code/yugabyte-db/src/yb/util/spinlock_profiling-test.cc
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | #include <glog/logging.h> |
33 | | #include <gtest/gtest.h> |
34 | | |
35 | | #include "yb/gutil/spinlock.h" |
36 | | #include "yb/util/spinlock_profiling.h" |
37 | | #include "yb/util/test_macros.h" |
38 | | #include "yb/util/test_util.h" |
39 | | #include "yb/util/trace.h" |
40 | | |
41 | | // Can't include gutil/synchronization_profiling.h directly as it'll |
42 | | // declare a weak symbol directly in this unit test, which the runtime |
43 | | // linker will prefer over equivalent strong symbols for some reason. By |
44 | | // declaring the symbol without providing an empty definition, the strong |
45 | | // symbols are chosen when provided via shared libraries. |
46 | | // |
47 | | // Further reading: |
48 | | // - http://stackoverflow.com/questions/20658809/dynamic-loading-and-weak-symbol-resolution |
49 | | // - http://notmysock.org/blog/php/weak-symbols-arent.html |
50 | | namespace gutil { |
51 | | extern void SubmitSpinLockProfileData(const void *, int64); |
52 | | } // namespace gutil |
53 | | |
54 | | namespace yb { |
55 | | |
56 | | class SpinLockProfilingTest : public YBTest {}; |
57 | | |
58 | 1 | TEST_F(SpinLockProfilingTest, TestSpinlockProfiling) { |
59 | 1 | scoped_refptr<Trace> t(new Trace); |
60 | 1 | base::SpinLock lock; |
61 | 1 | { |
62 | 1 | ADOPT_TRACE(t.get()); |
63 | 1 | gutil::SubmitSpinLockProfileData(&lock, 4000000); |
64 | 1 | } |
65 | 1 | string result = t->DumpToString(true); |
66 | 1 | LOG(INFO) << "trace: " << result; |
67 | | // We can't assert more specifically because the CyclesPerSecond |
68 | | // on different machines might be different. |
69 | 1 | ASSERT_STR_CONTAINS(result, "Waited "); |
70 | 1 | ASSERT_STR_CONTAINS(result, "on lock "); |
71 | | |
72 | 1 | ASSERT_GT(GetSpinLockContentionMicros(), 0); |
73 | 1 | } |
74 | | |
75 | 1 | TEST_F(SpinLockProfilingTest, TestStackCollection) { |
76 | 1 | StartSynchronizationProfiling(); |
77 | 1 | base::SpinLock lock; |
78 | 1 | gutil::SubmitSpinLockProfileData(&lock, 12345); |
79 | 1 | StopSynchronizationProfiling(); |
80 | 1 | std::stringstream str; |
81 | 1 | int64_t dropped = 0; |
82 | 1 | FlushSynchronizationProfile(&str, &dropped); |
83 | 1 | string s = str.str(); |
84 | 1 | ASSERT_STR_CONTAINS(s, "12345\t1 @ "); |
85 | 1 | ASSERT_EQ(0, dropped); |
86 | 1 | } |
87 | | |
88 | | } // namespace yb |