Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add CPP Servicer #370

Open
wants to merge 45 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
2efcbbe
[WIP] Add cpp servicer
Jul 29, 2024
9da6115
[WIP] Add cpp rpc agent server
Jul 31, 2024
301967f
FIX for C++ rpc agent server
Aug 11, 2024
8811f36
FIX: fix bugs in download and update makefile and unittest
Aug 12, 2024
21bffe7
Merge branch 'main' of github.com:modelscope/agentscope
chenyushuo Aug 12, 2024
ce4ce52
FIX: fix bugs after merge
chenyushuo Aug 12, 2024
a6c44ab
FIX: create cpp_server package
chenyushuo Aug 13, 2024
78274ba
fix for ImportError
chenyushuo Aug 13, 2024
cbfe7ba
fix for unittest
chenyushuo Aug 14, 2024
e8f6954
fix on cpp server
chenyushuo Aug 21, 2024
d4d524a
add cmake and pybind11 for cpp_server
pan-x-c Aug 22, 2024
ec4e919
fix in cpp server
chenyushuo Aug 23, 2024
3358dfc
fix dependencies
chenyushuo Aug 23, 2024
1897d3c
fix dependencies
chenyushuo Aug 23, 2024
b7b9d17
fix dependencies
chenyushuo Aug 23, 2024
603aef8
fix dependencies
chenyushuo Aug 23, 2024
b66b51f
fix dependencies
chenyushuo Aug 23, 2024
58875d9
fix dependencies
chenyushuo Aug 23, 2024
12c621c
fix dependencies
chenyushuo Aug 23, 2024
992aaa5
fix dependencies
chenyushuo Aug 23, 2024
12226d0
fix dependencies
chenyushuo Aug 23, 2024
85cc618
fix dependencies
chenyushuo Aug 23, 2024
710d18d
fix dependencies
chenyushuo Aug 23, 2024
473c069
fix dependencies
chenyushuo Aug 25, 2024
3cfeefc
fix dependencies
chenyushuo Aug 25, 2024
aacd490
fix dependencies
chenyushuo Aug 25, 2024
0197939
fix dependencies
chenyushuo Aug 25, 2024
801bcb3
fix dependencies
chenyushuo Aug 25, 2024
5b156d4
fix setup
chenyushuo Aug 25, 2024
3e14bfb
fix setup
chenyushuo Aug 25, 2024
55fbaef
fix setup
chenyushuo Aug 25, 2024
683214b
fix setup
chenyushuo Aug 25, 2024
f9256ab
fix setup
chenyushuo Aug 25, 2024
0d2b07e
fix setup
chenyushuo Aug 26, 2024
00a9754
fix setup
chenyushuo Aug 26, 2024
161eede
fix setup
chenyushuo Aug 26, 2024
2e09bab
fix setup
chenyushuo Aug 26, 2024
1e61e39
fix setup
chenyushuo Aug 26, 2024
cc8ff90
fix setup
chenyushuo Aug 26, 2024
057b1e5
fix setup
chenyushuo Aug 26, 2024
7c7a2bc
add small object pool
chenyushuo Aug 27, 2024
80036df
fix in worker.cc
chenyushuo Aug 29, 2024
9f481dd
fix in sem
chenyushuo Sep 2, 2024
d1ab450
Merge branch 'main' of github.com:modelscope/agentscope
chenyushuo Sep 5, 2024
9c2cd1b
bug fix after merge
chenyushuo Sep 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix in worker.cc
  • Loading branch information
chenyushuo committed Aug 29, 2024
commit 80036dfb03e2e12a2dcb19c0b5e69b19616299ad
66 changes: 39 additions & 27 deletions src/agentscope/cpp_server/worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Worker::Worker(
_func_ready_sem_prefix("/func_" + port + "_"),
_set_result_sem_prefix("/set_result_" + port + "_"),
_small_obj_pool_shm_name("/small_obj_pool_shm_" + port),
_small_obj_pool_sem_name("/small_obj_pool_sem_" + port),
_small_obj_pool_filename("./logs/small_obj_pool_" + port),
_call_shm_size(1024),
_small_obj_max_num(100000),
_small_obj_size(1000),
Expand All @@ -74,6 +74,20 @@ Worker::Worker(
_max_timeout_seconds(std::max(max_timeout_seconds, 1u))
{
py::gil_scoped_release release;
char *use_logger = getenv("AGENTSCOPE_USE_CPP_LOGGER");
if (use_logger != nullptr && std::string(use_logger) == "True")
{
_use_logger = true;
}
else
{
_use_logger = false;
}
struct stat info;
if (stat("./logs/", &info) != 0)
{
mkdir("./logs", 0755);
}
_small_obj_pool_shm_fd = shm_open(_small_obj_pool_shm_name.c_str(), O_CREAT | O_RDWR, 0666);
if (_small_obj_pool_shm_fd == -1)
{
Expand All @@ -88,26 +102,13 @@ Worker::Worker(
exit(1);
}
memset(_small_obj_pool_shm, 0, _small_obj_max_num * _small_obj_shm_size);
_small_obj_pool_sem = sem_open(_small_obj_pool_sem_name.c_str(), O_CREAT, 0666, 1);
if (_small_obj_pool_sem == SEM_FAILED)
_small_obj_pool_fd = open(_small_obj_pool_filename.c_str(), O_RDWR | O_CREAT, 0644);
if (_small_obj_pool_fd == -1)
{
perror("Error: sem_open in create _small_obj_pool_sem");
perror("Error: open in _small_obj_pool_fd");
exit(1);
}
char *use_logger = getenv("AGENTSCOPE_USE_CPP_LOGGER");
if (use_logger != nullptr && std::string(use_logger) == "True")
{
_use_logger = true;
}
else
{
_use_logger = false;
}
struct stat info;
if (stat("./logs/", &info) != 0)
{
mkdir("./logs", 0755);
}
ftruncate(_small_obj_pool_fd, _small_obj_max_num * _small_obj_size);
for (int i = 0; i < _num_workers; i++)
{
string shm_name = _func_call_shm_prefix + to_string(i);
Expand Down Expand Up @@ -243,7 +244,8 @@ Worker::~Worker() // for main process to release resources
close(_small_obj_pool_shm_fd);
munmap(_small_obj_pool_shm, _small_obj_max_num * _small_obj_shm_size);
shm_unlink(_small_obj_pool_shm_name.c_str());
sem_unlink(_small_obj_pool_sem_name.c_str());
close(_small_obj_pool_fd);
remove(_small_obj_pool_filename.c_str());
for (auto iter : _worker_semaphores)
{
sem_t *worker_avail_sem = iter.first;
Expand Down Expand Up @@ -322,6 +324,7 @@ int Worker::get_call_id()
perror(("Error: sem_open in get_call_id" + set_result_name).c_str());
exit(1);
}
sem_close(set_result_sem);
return call_id;
}

Expand Down Expand Up @@ -376,23 +379,32 @@ void Worker::set_content(const string &prefix, const int call_id, const string &
int pool_idx = call_id % _small_obj_max_num;
char *small_obj_shm = (char *)_small_obj_pool_shm + pool_idx * _small_obj_shm_size;
int *occupied = (int *)small_obj_shm;
while (true)
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = pool_idx * _small_obj_shm_size;
lock.l_len = _small_obj_shm_size;
for (bool running = true; running; std::this_thread::sleep_for(std::chrono::milliseconds(1)))
{
if (*occupied == false)
{
sem_wait(_small_obj_pool_sem);
if (fcntl(_small_obj_pool_fd, F_SETLKW, &lock) == -1)
{
perror(("Error: fcntl in set_content (lock): " + prefix + to_string(call_id)).c_str());
exit(1);
}
if (*occupied == false)
{
*occupied = true;
sem_post(_small_obj_pool_sem);
break;
running = false;
}
else
lock.l_type = F_UNLCK;
if (fcntl(_small_obj_pool_fd, F_SETLK, &lock) == -1)
{
sem_post(_small_obj_pool_sem);
perror(("Error: fcntl in set_content (unlock): " + prefix + to_string(call_id)).c_str());
exit(1);
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
*(int *)(small_obj_shm + sizeof(int)) = call_id;
*(int *)(small_obj_shm + sizeof(int) * 2) = content.size();
Expand Down Expand Up @@ -439,7 +451,7 @@ string Worker::get_result(const int call_id)
sem_t *set_result_sem = sem_open(set_result_name.c_str(), 0);
if (set_result_sem == SEM_FAILED)
{
perror(("Error: sem_open in get_result:" + to_string(call_id)).c_str());
perror(("Error: sem_open in get_result: " + set_result_name).c_str());
exit(1);
}
sem_wait(set_result_sem);
Expand Down
4 changes: 2 additions & 2 deletions src/agentscope/cpp_server/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ class Worker
const string _func_ready_sem_prefix;
const string _set_result_sem_prefix;
const string _small_obj_pool_shm_name;
const string _small_obj_pool_sem_name;
const string _small_obj_pool_filename;
int _small_obj_pool_shm_fd;
int _small_obj_pool_fd;
void *_small_obj_pool_shm;
sem_t *_small_obj_pool_sem;
const unsigned int _call_shm_size;
const unsigned int _small_obj_max_num;
const unsigned int _small_obj_size;
Expand Down
Loading