Skip to content

Commit

Permalink
Merge pull request #928 from ElectrodeYT/affinity-1
Browse files Browse the repository at this point in the history
pthread, sysdeps/managarm: add set/get affinity calls
  • Loading branch information
qookei authored Oct 10, 2023
2 parents 72b5e25 + 5cbdcc7 commit 7047232
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 8 deletions.
1 change: 1 addition & 0 deletions options/linux/generic/sched.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <sched.h>

#include <mlibc/linux-sysdeps.hpp>
#include <mlibc/posix-sysdeps.hpp>

int sched_getcpu(void) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_getcpu, -1);
Expand Down
2 changes: 0 additions & 2 deletions options/linux/include/mlibc/linux-sysdeps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result);
[[gnu::weak]] int sys_lremovexattr(const char *path, const char *name);
[[gnu::weak]] int sys_fremovexattr(int fd, const char *name);

[[gnu::weak]] int sys_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);

} // namespace mlibc

#endif // MLIBX_LINUX_SYSDEPS
12 changes: 6 additions & 6 deletions options/posix/generic/pthread-stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,14 @@ int pthread_getattr_np(pthread_t thread, pthread_attr_t *attr) {
return 0;
}

int pthread_getaffinity_np(pthread_t, size_t, cpu_set_t *) {
__ensure(!"Not implemented");
__builtin_unreachable();
int pthread_getaffinity_np(pthread_t thread, size_t cpusetsize, cpu_set_t *mask) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_getthreadaffinity, ENOSYS);
return mlibc::sys_getthreadaffinity(reinterpret_cast<Tcb*>(thread)->tid, cpusetsize, mask);
}

int pthread_setaffinity_np(pthread_t, size_t, const cpu_set_t *) {
__ensure(!"Not implemented");
__builtin_unreachable();
int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize, const cpu_set_t *mask) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_setthreadaffinity, ENOSYS);
return mlibc::sys_setthreadaffinity(reinterpret_cast<Tcb*>(thread)->tid, cpusetsize, mask);
}
#endif // __MLIBC_LINUX_OPTION

Expand Down
6 changes: 6 additions & 0 deletions options/posix/include/mlibc/posix-sysdeps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ int sys_vm_unmap(void *pointer, size_t size);
[[gnu::weak]] int sys_semget(key_t key, int n, int fl, int *id);
[[gnu::weak]] int sys_semctl(int semid, int semnum, int cmd, void *semun, int *ret);

[[gnu::weak]] int sys_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);
[[gnu::weak]] int sys_getthreadaffinity(pid_t tid, size_t cpusetsize, cpu_set_t *mask);

[[gnu::weak]] int sys_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t *mask);
[[gnu::weak]] int sys_setthreadaffinity(pid_t tid, size_t cpusetsize, const cpu_set_t *mask);

} //namespace mlibc

#endif // MLIBC_POSIX_SYSDEPS
98 changes: 98 additions & 0 deletions sysdeps/managarm/generic/sched.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <bits/ensure.h>
#include <unistd.h>

#include <hel.h>
#include <hel-syscalls.h>
#include <mlibc/debug.hpp>
#include <mlibc/allocator.hpp>
#include <mlibc/posix-pipe.hpp>
#include <mlibc/posix-sysdeps.hpp>

#include <posix.frigg_bragi.hpp>

namespace mlibc {

int sys_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask) {
return sys_getthreadaffinity(pid, cpusetsize, mask);
}

int sys_getthreadaffinity(pid_t tid, size_t cpusetsize, cpu_set_t *mask) {
SignalGuard sguard;

managarm::posix::GetAffinityRequest<MemoryAllocator> req(getSysdepsAllocator());

req.set_pid(tid);
req.set_size(cpusetsize);

auto [offer, send_head, recv_resp, recv_data] =
exchangeMsgsSync(
getPosixLane(),
helix_ng::offer(
helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()),
helix_ng::recvInline(),
helix_ng::recvBuffer(mask, cpusetsize)
)
);

HEL_CHECK(offer.error());
HEL_CHECK(send_head.error());
HEL_CHECK(recv_resp.error());

managarm::posix::SvrResponse<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());

if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) {
return EINVAL;
} else if(resp.error() != managarm::posix::Errors::SUCCESS) {
mlibc::infoLogger() << "mlibc: got unexpected error from posix in sys_getaffinity!" << frg::endlog;
return EIEIO;
}
HEL_CHECK(recv_data.error());

return 0;
}

int sys_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t *mask) {
return sys_setthreadaffinity(pid, cpusetsize, mask);
}

int sys_setthreadaffinity(pid_t tid, size_t cpusetsize, const cpu_set_t *mask) {
SignalGuard sguard;

frg::vector<uint8_t, MemoryAllocator> affinity_mask(getSysdepsAllocator());
affinity_mask.resize(cpusetsize);
memcpy(affinity_mask.data(), mask, cpusetsize);
managarm::posix::SetAffinityRequest<MemoryAllocator> req(getSysdepsAllocator());

req.set_pid(tid);
req.set_mask(affinity_mask);

auto [offer, send_head, send_tail, recv_resp] =
exchangeMsgsSync(
getPosixLane(),
helix_ng::offer(
helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()),
helix_ng::recvInline()
)
);

HEL_CHECK(offer.error());
HEL_CHECK(send_head.error());
HEL_CHECK(send_tail.error());
HEL_CHECK(recv_resp.error());

managarm::posix::SvrResponse<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());

if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) {
return EINVAL;
} else if(resp.error() != managarm::posix::Errors::SUCCESS) {
mlibc::infoLogger() << "mlibc: got unexpected error from posix in sys_getaffinity!" << frg::endlog;
return EIEIO;
}

return 0;
}


}
1 change: 1 addition & 0 deletions sysdeps/managarm/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ libc_sources += files(
'generic/memory.cpp',
'generic/mount.cpp',
'generic/net.cpp',
'generic/sched.cpp',
'generic/signals.cpp',
'generic/socket.cpp',
'generic/time.cpp'
Expand Down

0 comments on commit 7047232

Please sign in to comment.