blob: 2021fecaabf98f3cd1de3b5c417d411ea0819794 [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2/* Copyright (c) 2006, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * ---
32 * Author: Sanjay Ghemawat
33 */
34
35#include <config.h>
36#include "base/spinlock.h"
37#include "base/synchronization_profiling.h"
38#include "base/spinlock_internal.h"
39#include "base/cycleclock.h"
40#include "base/sysinfo.h" /* for NumCPUs() */
41
42// NOTE on the Lock-state values:
43//
44// kSpinLockFree represents the unlocked state
45// kSpinLockHeld represents the locked state with no waiters
46//
47// Values greater than kSpinLockHeld represent the locked state with waiters,
48// where the value is the time the current lock holder had to
49// wait before obtaining the lock. The kSpinLockSleeper state is a special
50// "locked with waiters" state that indicates that a sleeper needs to
51// be woken, but the thread that just released the lock didn't wait.
52
53static int adaptive_spin_count = 0;
54
55const base::LinkerInitialized SpinLock::LINKER_INITIALIZED =
56 base::LINKER_INITIALIZED;
57
58namespace {
59struct SpinLock_InitHelper {
60 SpinLock_InitHelper() {
61 // On multi-cpu machines, spin for longer before yielding
62 // the processor or sleeping. Reduces idle time significantly.
63 if (NumCPUs() > 1) {
64 adaptive_spin_count = 1000;
65 }
66 }
67};
68
69// Hook into global constructor execution:
70// We do not do adaptive spinning before that,
71// but nothing lock-intensive should be going on at that time.
72static SpinLock_InitHelper init_helper;
73
74} // unnamed namespace
75
76// Monitor the lock to see if its value changes within some time period
77// (adaptive_spin_count loop iterations). A timestamp indicating
78// when the thread initially started waiting for the lock is passed in via
79// the initial_wait_timestamp value. The total wait time in cycles for the
80// lock is returned in the wait_cycles parameter. The last value read
81// from the lock is returned from the method.
82Atomic32 SpinLock::SpinLoop(int64 initial_wait_timestamp,
83 Atomic32* wait_cycles) {
84 int c = adaptive_spin_count;
85 while (base::subtle::NoBarrier_Load(&lockword_) != kSpinLockFree && --c > 0) {
86 }
87 Atomic32 spin_loop_wait_cycles = CalculateWaitCycles(initial_wait_timestamp);
88 Atomic32 lock_value =
89 base::subtle::Acquire_CompareAndSwap(&lockword_, kSpinLockFree,
90 spin_loop_wait_cycles);
91 *wait_cycles = spin_loop_wait_cycles;
92 return lock_value;
93}
94
95void SpinLock::SlowLock() {
96 // The lock was not obtained initially, so this thread needs to wait for
97 // it. Record the current timestamp in the local variable wait_start_time
98 // so the total wait time can be stored in the lockword once this thread
99 // obtains the lock.
100 int64 wait_start_time = CycleClock::Now();
101 Atomic32 wait_cycles;
102 Atomic32 lock_value = SpinLoop(wait_start_time, &wait_cycles);
103
104 int lock_wait_call_count = 0;
105 while (lock_value != kSpinLockFree) {
106 // If the lock is currently held, but not marked as having a sleeper, mark
107 // it as having a sleeper.
108 if (lock_value == kSpinLockHeld) {
109 // Here, just "mark" that the thread is going to sleep. Don't store the
110 // lock wait time in the lock as that will cause the current lock
111 // owner to think it experienced contention.
112 lock_value = base::subtle::Acquire_CompareAndSwap(&lockword_,
113 kSpinLockHeld,
114 kSpinLockSleeper);
115 if (lock_value == kSpinLockHeld) {
116 // Successfully transitioned to kSpinLockSleeper. Pass
117 // kSpinLockSleeper to the SpinLockWait routine to properly indicate
118 // the last lock_value observed.
119 lock_value = kSpinLockSleeper;
120 } else if (lock_value == kSpinLockFree) {
121 // Lock is free again, so try and acquire it before sleeping. The
122 // new lock state will be the number of cycles this thread waited if
123 // this thread obtains the lock.
124 lock_value = base::subtle::Acquire_CompareAndSwap(&lockword_,
125 kSpinLockFree,
126 wait_cycles);
127 continue; // skip the delay at the end of the loop
128 }
129 }
130
131 // Wait for an OS specific delay.
132 base::internal::SpinLockDelay(&lockword_, lock_value,
133 ++lock_wait_call_count);
134 // Spin again after returning from the wait routine to give this thread
135 // some chance of obtaining the lock.
136 lock_value = SpinLoop(wait_start_time, &wait_cycles);
137 }
138}
139
140// The wait time for contentionz lock profiling must fit into 32 bits.
141// However, the lower 32-bits of the cycle counter wrap around too quickly
142// with high frequency processors, so a right-shift by 7 is performed to
143// quickly divide the cycles by 128. Using these 32 bits, reduces the
144// granularity of time measurement to 128 cycles, and loses track
145// of wait time for waits greater than 109 seconds on a 5 GHz machine
146// [(2^32 cycles/5 Ghz)*128 = 109.95 seconds]. Waits this long should be
147// very rare and the reduced granularity should not be an issue given
148// processors in the Google fleet operate at a minimum of one billion
149// cycles/sec.
150enum { PROFILE_TIMESTAMP_SHIFT = 7 };
151
152void SpinLock::SlowUnlock(uint64 wait_cycles) {
153 base::internal::SpinLockWake(&lockword_, false); // wake waiter if necessary
154
155 // Collect contentionz profile info, expanding the wait_cycles back out to
156 // the full value. If wait_cycles is <= kSpinLockSleeper, then no wait
157 // was actually performed, so don't record the wait time. Note, that the
158 // CalculateWaitCycles method adds in kSpinLockSleeper cycles
159 // unconditionally to guarantee the wait time is not kSpinLockFree or
160 // kSpinLockHeld. The adding in of these small number of cycles may
161 // overestimate the contention by a slight amount 50% of the time. However,
162 // if this code tried to correct for that addition by subtracting out the
163 // kSpinLockSleeper amount that would underestimate the contention slightly
164 // 50% of the time. Both ways get the wrong answer, so the code
165 // overestimates to be more conservative. Overestimating also makes the code
166 // a little simpler.
167 //
168 if (wait_cycles > kSpinLockSleeper) {
169 base::SubmitSpinLockProfileData(this,
170 wait_cycles << PROFILE_TIMESTAMP_SHIFT);
171 }
172}
173
174inline int32 SpinLock::CalculateWaitCycles(int64 wait_start_time) {
175 int32 wait_cycles = ((CycleClock::Now() - wait_start_time) >>
176 PROFILE_TIMESTAMP_SHIFT);
177 // The number of cycles waiting for the lock is used as both the
178 // wait_cycles and lock value, so it can't be kSpinLockFree or
179 // kSpinLockHeld. Make sure the value returned is at least
180 // kSpinLockSleeper.
181 wait_cycles |= kSpinLockSleeper;
182 return wait_cycles;
183}