~ubuntu-branches/ubuntu/jaunty/google-perftools/jaunty

« back to all changes in this revision

Viewing changes to src/base/spinlock.cc

  • Committer: Bazaar Package Importer
  • Author(s): Daigo Moriwaki
  • Date: 2008-06-15 23:41:36 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20080615234136-al5gawvdvt5vhdtz
Tags: 0.98-1
* New upstream release. (Closes: #425147)
* Compiled with GCC 4.3. (Closes: #454841)
* debian/watch: can now report upstream's version (Closes: #450294)
* Because of a file conflict between tau and libgoogle-perftools the
  binary pprof is renamed as google-pprof. (Closes: #404001)
  Great thanks to Michael Mende.
* debian/rules: autoconf files are now generated at the build time.
* Bumped up Standards-Version to 3.7.3, no changes are required.
* Split a new package, libtcmallc_minimal0. The upstream supports
  this module for wider platforms. So I leave its architecture to be
  `any'.
* libgoogle-perftools0's architecture is now i386. The upstream
  supports this module for x86 and x86_64. However, x86_64 requires
  libunwind's development head, which Debian does not have yet.
* Removed an unnecessary patch, debian/patches/02_profiler.cc_alpha.diff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2006, Google Inc.
 
2
 * All rights reserved.
 
3
 * 
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are
 
6
 * met:
 
7
 * 
 
8
 *     * Redistributions of source code must retain the above copyright
 
9
 * notice, this list of conditions and the following disclaimer.
 
10
 *     * Redistributions in binary form must reproduce the above
 
11
 * copyright notice, this list of conditions and the following disclaimer
 
12
 * in the documentation and/or other materials provided with the
 
13
 * distribution.
 
14
 *     * Neither the name of Google Inc. nor the names of its
 
15
 * contributors may be used to endorse or promote products derived from
 
16
 * this software without specific prior written permission.
 
17
 * 
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
19
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
20
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
21
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
22
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
23
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
24
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
28
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 *
 
30
 * ---
 
31
 * Author: Sanjay Ghemawat
 
32
 */
 
33
 
 
34
#include "config.h"
 
35
#include <time.h>       /* For nanosleep() */
 
36
#include <sched.h>      /* For sched_yield() */
 
37
#ifdef HAVE_UNISTD_H
 
38
#include <unistd.h>     /* For nanosleep() on Windows, read() */
 
39
#endif
 
40
#include <fcntl.h>      /* for open(), O_RDONLY */
 
41
#include <string.h>     /* for strncmp */
 
42
#include <errno.h>
 
43
#include "base/spinlock.h"
 
44
#include "base/sysinfo.h"   /* for NumCPUs() */
 
45
 
 
46
// We can do contention-profiling of SpinLocks, but the code is in
 
47
// mutex.cc, which is not always linked in with spinlock.  Hence we
 
48
// provide this weak definition, which is used if mutex.cc isn't linked in.
 
49
ATTRIBUTE_WEAK extern void SubmitSpinLockProfileData(const void *, int64);
 
50
void SubmitSpinLockProfileData(const void *, int64) {}
 
51
 
 
52
static int adaptive_spin_count = 0;
 
53
 
 
54
const base::LinkerInitialized SpinLock::LINKER_INITIALIZED =
 
55
    base::LINKER_INITIALIZED;
 
56
 
 
57
struct SpinLock_InitHelper {
 
58
  SpinLock_InitHelper() {
 
59
    // On multi-cpu machines, spin for longer before yielding
 
60
    // the processor or sleeping.  Reduces idle time significantly.
 
61
    if (NumCPUs() > 1) {
 
62
      adaptive_spin_count = 1000;
 
63
    }
 
64
  }
 
65
};
 
66
 
 
67
// Hook into global constructor execution:
 
68
// We do not do adaptive spinning before that,
 
69
// but nothing lock-intensive should be going on at that time.
 
70
static SpinLock_InitHelper init_helper;
 
71
 
 
72
void SpinLock::SlowLock() {
 
73
  int saved_errno = errno; // save and restore errno for signal safety
 
74
  int c = adaptive_spin_count;
 
75
 
 
76
  // Spin a few times in the hope that the lock holder releases the lock
 
77
  while ((c > 0) && (lockword_ != 0)) {
 
78
    c--;
 
79
  }
 
80
 
 
81
  if (lockword_ == 1) {
 
82
    sched_yield();          // Spinning failed. Let's try to be gentle.
 
83
  }
 
84
 
 
85
  while (Acquire_CompareAndSwap(&lockword_, 0, 1) != 0) {
 
86
    // This code was adapted from the ptmalloc2 implementation of
 
87
    // spinlocks which would sched_yield() upto 50 times before
 
88
    // sleeping once for a few milliseconds.  Mike Burrows suggested
 
89
    // just doing one sched_yield() outside the loop and always
 
90
    // sleeping after that.  This change helped a great deal on the
 
91
    // performance of spinlocks under high contention.  A test program
 
92
    // with 10 threads on a dual Xeon (four virtual processors) went
 
93
    // from taking 30 seconds to 16 seconds.
 
94
 
 
95
    // Sleep for a few milliseconds
 
96
    struct timespec tm;
 
97
    tm.tv_sec = 0;
 
98
    tm.tv_nsec = 2000001;
 
99
    nanosleep(&tm, NULL);
 
100
  }
 
101
  errno = saved_errno;
 
102
}