Skip to content

Commit

Permalink
Better spinlock implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
timpalpant committed Jul 4, 2021
1 parent 915ac99 commit 4df792a
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/_profiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

#include <cstdlib>
#include <memory>
#include "third_party/google/tcmalloc/sampler.h"

#include "heap.h"
#include "log.h"
#include "malloc_patch.h"
#include "scoped_object.h"
#include "third_party/google/tcmalloc/sampler.h"

namespace {

Expand Down
6 changes: 3 additions & 3 deletions src/heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
#define MPROFILE_SRC_HEAP_H_

#include <stdlib.h>

#include <mutex>
#include <vector>

#include "spinlock.h"
#include "stacktraces.h"
#include "third_party/google/tcmalloc/addressmap.h"
#include "third_party/google/tcmalloc/sampler.h"
#include "third_party/greg7mdp/parallel-hashmap/phmap.h"

#include "spinlock.h"
#include "stacktraces.h"

class HeapProfiler {
public:
HeapProfiler() : HeapProfiler(kMaxFramesToCapture) {}
Expand Down
1 change: 0 additions & 1 deletion src/heap_bench.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2019 Timothy Palpant

#include "benchmark/benchmark.h"

#include "heap.h"

static void BM_HandleMalloc(benchmark::State &state) {
Expand Down
5 changes: 3 additions & 2 deletions src/heap_test.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright 2019 Timothy Palpant
//
#include "gtest/gtest.h"
#include "heap.h"

#include <thread>
#include "heap.h"

#include "gtest/gtest.h"

TEST(HeapProfiler, HandleMalloc) {
Sampler::SetSamplePeriod(0);
Expand Down
1 change: 1 addition & 0 deletions src/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "log.h"

#include <Python.h>

#include <iostream>

void Log(const char *level, const char *fmt, ...) {
Expand Down
1 change: 1 addition & 0 deletions src/main_bench.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2019 Timothy Palpant

#include <Python.h>

#include "benchmark/benchmark.h"

int main(int argc, char** argv) {
Expand Down
1 change: 1 addition & 0 deletions src/main_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//

#include <Python.h>

#include "gtest/gtest.h"

int main(int argc, char **argv) {
Expand Down
1 change: 1 addition & 0 deletions src/malloc_patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define MPROFILE_SRC_MALLOC_PATCH_H_

#include <Python.h>

#include <memory>

#include "heap.h"
Expand Down
1 change: 1 addition & 0 deletions src/scoped_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define MPROFILE_SRC_SCOPED_OBJECT_H_

#include <Python.h>

#include <memory>

struct PyObjectDecReffer {
Expand Down
23 changes: 18 additions & 5 deletions src/spinlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

#include <atomic>

// SpinLock is a simple adapter to use an std::atomic_flag
// SpinLock is a simple adapter to use an std::atomic<bool>
// with a std::lock_guard.
//
// Adapted from: https://rigtorp.se/spinlock/
class SpinLock {
public:
SpinLock() {}
Expand All @@ -15,14 +17,25 @@ class SpinLock {
SpinLock &operator=(const SpinLock &) = delete;

void lock() {
while (flag_.test_and_set(std::memory_order_acquire))
;
while (true) {
// Optimistically assume the lock is free on the first try.
if (!flag_.exchange(true, std::memory_order_acquire)) {
break;
}

// Wait for lock to be released without generating cache misses.
while (flag_.load(std::memory_order_relaxed)) {
// Issue X86 PAUSE or ARM YIELD instruction to reduce contention
// between hyper-threads.
__builtin_ia32_pause();
}
}
}

void unlock() { flag_.clear(std::memory_order_release); }
void unlock() { flag_.store(false, std::memory_order_release); }

private:
std::atomic_flag flag_ = ATOMIC_FLAG_INIT;
std::atomic<bool> flag_ = {false};
};

#endif // MPROFILE_SRC_SPINLOCK_H
1 change: 1 addition & 0 deletions src/stacktraces.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define MPROFILE_SRC_STACKTRACES_H_

#include <Python.h>

#include <vector>

#include "third_party/greg7mdp/parallel-hashmap/phmap.h"
Expand Down
4 changes: 2 additions & 2 deletions src/stacktraces_test.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2019 Timothy Palpant
#include "gtest/gtest.h"
#include "stacktraces.h"

#include "gtest/gtest.h"
#include "scoped_object.h"
#include "stacktraces.h"

#if PY_MAJOR_VERSION >= 3
#define STRING_FROMSTRING PyUnicode_FromString
Expand Down

0 comments on commit 4df792a

Please sign in to comment.