Skip to content

Commit

Permalink
photon thread alloc using static object
Browse files Browse the repository at this point in the history
Signed-off-by: Coldwings <coldwings@me.com>
  • Loading branch information
Coldwings committed Jul 6, 2023
1 parent b35f310 commit 1dc7dac
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 25 deletions.
8 changes: 4 additions & 4 deletions common/callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ struct Delegate : public Delegate_Base
template<typename U> // Function with U* as the 1st argument
using UFunc = R (*)(U*, Ts...);

Delegate(void* obj, Func func) { bind(obj, func); }
Delegate(Func func, void* obj) { bind(obj, func); }
Delegate(Func0 func0) { bind(func0); }
constexpr Delegate(void* obj, Func func) : _obj(obj), _func(func) {}
constexpr Delegate(Func func, void* obj) : _obj(obj), _func(func) {}
constexpr Delegate(Func0 func0) : _obj(nullptr), _func((Func&)func0) {}

template<typename U>
Delegate(U* obj, UFunc<U> func) { bind(obj, func); }
constexpr Delegate(U* obj, UFunc<U> func) : _obj(obj), _func((Func&)func) {}

template<typename U>
Delegate(U* obj, UMFunc<U> func) { bind(obj, func); }
Expand Down
6 changes: 5 additions & 1 deletion thread/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ add_test(NAME test-tls-order-native COMMAND $<TARGET_FILE:test-tls-order-native>

add_executable(test-tls-order-photon test-tls-order-photon.cpp)
target_link_libraries(test-tls-order-photon PRIVATE photon_shared ${testing_libs})
add_test(NAME test-tls-order-photon COMMAND $<TARGET_FILE:test-tls-order-photon>)
add_test(NAME test-tls-order-photon COMMAND $<TARGET_FILE:test-tls-order-photon>)

add_executable(test-lib-data test-lib-data.cpp)
target_link_libraries(test-lib-data PRIVATE photon_shared ${testing_libs})
add_test(NAME test-lib-data COMMAND $<TARGET_FILE:test-lib-data>)
89 changes: 89 additions & 0 deletions thread/test/test-lib-data.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright 2022 The Photon Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <gtest/gtest.h>
#include <libgen.h>
#include <photon/common/utility.h>
#include <photon/photon.h>
#include <stdio.h>
#include <stdlib.h>

bool popen_test(const std::string& cmd, int expect = 0) {
puts(cmd.c_str());
auto p = popen(cmd.c_str(), "r");
char buffer[4096];
while (fgets(buffer, sizeof(buffer), p) != NULL)
;
auto r = pclose(p);
if (WIFEXITED(r)) return WEXITSTATUS(r) == expect;
puts("Not exit");
return false;
}

std::string popen_read(const std::string& cmd, int expect = 0) {
std::string ret;
puts(cmd.c_str());
auto p = popen(cmd.c_str(), "r");
char buffer[4096];
while (fgets(buffer, sizeof(buffer), p) != NULL) {
ret += buffer;
puts(buffer);
}
pclose(p);
return ret;
}

std::string libpath(uint64_t pid) {
auto path =
popen_read("cat /proc/" + std::to_string(pid) +
"/maps | grep libphoton | tr -s ' ' | cut -f 6 -d ' '"
" | head -n 1");
EXPECT_FALSE(path.empty());
char* tp = strdup(path.c_str());
path = dirname(tp);
puts(path.c_str());
free(tp);
return path;
}

TEST(static_lib, photon_thread_alloc) {
#ifdef __linux__
auto pid = getpid();
auto p = libpath(pid) + "/libphoton.a";
EXPECT_TRUE(popen_test("objdump -tr \"" + p +
"\" | grep photon_thread_allocE | grep .data"));
EXPECT_TRUE(popen_test("objdump -tr \"" + p +
"\" | grep photon_thread_deallocE | grep .data"));
#endif
}

TEST(shared_lib, photon_thread_alloc) {
#ifdef __linux__
auto pid = getpid();
auto p = libpath(pid) + "/libphoton.so";
EXPECT_TRUE(popen_test("objdump -tr \"" + p +
"\" | grep photon_thread_allocE | grep .data"));
EXPECT_TRUE(popen_test("objdump -tr \"" + p +
"\" | grep photon_thread_deallocE | grep .data"));
#endif
}

int main(int argc, char** argv) {
photon::init(photon::INIT_EVENT_NONE, 0);
DEFER(photon::fini());
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
33 changes: 13 additions & 20 deletions thread/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,10 @@ namespace photon
free(ptr);
}

Delegate<void *, size_t> &photon_thread_alloc() {
static Delegate<void *, size_t> _photon_thread_alloc(
&default_photon_thread_stack_alloc, nullptr);
return _photon_thread_alloc;
}

Delegate<void, void *, size_t> &photon_thread_dealloc() {
static Delegate<void, void *, size_t> _photon_thread_dealloc(
&default_photon_thread_stack_dealloc, nullptr);
return _photon_thread_dealloc;
}
static Delegate<void*, size_t> photon_thread_alloc(
&default_photon_thread_stack_alloc, nullptr);
static Delegate<void, void*, size_t> photon_thread_dealloc(
&default_photon_thread_stack_dealloc, nullptr);

struct vcpu_t;
struct thread;
Expand Down Expand Up @@ -290,7 +283,7 @@ namespace photon
assert(state == states::DONE);
// `buf` and `stack_size` will always store on register
// when calling deallocating.
photon_thread_dealloc()(buf, stack_size);
photon_thread_dealloc(buf, stack_size);
}
};

Expand Down Expand Up @@ -845,7 +838,7 @@ R"(
LOG_ERROR_RETURN(ENOSYS, nullptr, "Photon not initialized in this vCPU (OS thread)");
size_t randomizer = (rand() % 32) * (1024 + 8);
stack_size = align_up(randomizer + stack_size + sizeof(thread), PAGE_SIZE);
char *ptr = (char *)photon_thread_alloc()(stack_size);
char* ptr = (char*)photon_thread_alloc(stack_size);
auto p = ptr + stack_size - sizeof(thread) - randomizer;
(uint64_t&)p &= ~63;
auto th = new (p) thread;
Expand Down Expand Up @@ -1815,18 +1808,18 @@ R"(
return --_n_vcpu;
}

void set_photon_thread_stack_allocator(
Delegate<void *, size_t> _photon_thread_alloc,
Delegate<void, void *, size_t> _photon_thread_dealloc) {
photon_thread_alloc() = _photon_thread_alloc;
photon_thread_dealloc() = _photon_thread_dealloc;
}

void* stackful_malloc(size_t size) {
return CURRENT->stackful_malloc(size);
}

void stackful_free(void* ptr) {
CURRENT->stackful_free(ptr);
}

void set_photon_thread_stack_allocator(
Delegate<void *, size_t> _photon_thread_alloc,
Delegate<void, void *, size_t> _photon_thread_dealloc) {
photon_thread_alloc = _photon_thread_alloc;
photon_thread_dealloc = _photon_thread_dealloc;
}
}

0 comments on commit 1dc7dac

Please sign in to comment.