Compare commits

..

5 Commits

Author SHA1 Message Date
5117b410db
apps: Add time
All checks were successful
continuous-integration/drone/push Build is passing
2023-05-20 12:48:25 +02:00
6de90b3c93
libc: Add getrusage() 2023-05-20 12:48:17 +02:00
01111442d3
kernel: Add the getrusage() system call 2023-05-20 12:48:07 +02:00
1fa8aeecce
libos: Allow Process::exec to take a slice of StringViews instead of Strings as arguments 2023-05-20 12:47:10 +02:00
30d1dad149
libluna: Let String::join take a vector of StringViews instead of Strings 2023-05-20 12:46:33 +02:00
17 changed files with 180 additions and 14 deletions

View File

@ -27,3 +27,4 @@ luna_app(ipc-test.cpp ipc-test)
luna_app(mount.cpp mount)
luna_app(umount.cpp umount)
luna_app(ps.cpp ps)
luna_app(time.cpp time)

45
apps/time.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <os/ArgumentParser.h>
#include <os/File.h>
#include <os/Process.h>
#include <stdio.h>
#include <sys/resource.h>
#include <sys/wait.h>
Result<int> luna_main(int argc, char** argv)
{
Vector<StringView> command;
os::ArgumentParser parser;
parser.add_description("Time a command.");
parser.add_system_program_info("time"_sv);
parser.set_vector_argument(command, true);
TRY(parser.parse(argc, argv));
auto pid = TRY(os::Process::fork());
if (pid == 0)
{
TRY(os::Process::exec(command[0], command.slice()));
unreachable();
}
if (waitpid(pid, nullptr, 0) < 0)
{
perror("waitpid");
return 1;
}
struct rusage usage;
if (getrusage(RUSAGE_CHILDREN, &usage) < 0)
{
perror("getrusage");
return 1;
}
auto cmdline = TRY(String::join(command, " "));
os::println("%s %d.%.2ds user %d.%.2ds system"_sv, cmdline.chars(), usage.ru_utime.tv_sec,
usage.ru_utime.tv_usec / 10000, usage.ru_stime.tv_sec, usage.ru_stime.tv_usec / 10000);
return 0;
}

View File

@ -39,6 +39,7 @@ set(SOURCES
src/sys/link.cpp
src/sys/uname.cpp
src/sys/mount.cpp
src/sys/resource.cpp
src/fs/VFS.cpp
src/fs/Pipe.cpp
src/fs/Mount.cpp

View File

@ -28,9 +28,9 @@ Result<u64> sys_pstat(Registers*, SyscallArgs args)
proc.ps_egid = thread->auth.egid;
proc.ps_state = (int)thread->state;
proc.ps_flags = thread->is_kernel ? PS_FLAG_KRNL : 0;
set_timespec(proc.ps_time, thread->ticks);
set_timespec(proc.ps_ktime, thread->ticks_in_kernel);
set_timespec(proc.ps_utime, thread->ticks_in_user);
set_timespec(proc.ps_time, thread->user_ticks_self + thread->kernel_ticks_self);
set_timespec(proc.ps_ktime, thread->kernel_ticks_self);
set_timespec(proc.ps_utime, thread->kernel_ticks_children);
strlcpy(proc.ps_name, thread->name.chars(), sizeof(proc.ps_name));
strlcpy(proc.ps_cwd, thread->current_directory_path.is_empty() ? "/" : thread->current_directory_path.chars(),
sizeof(proc.ps_cwd));

View File

@ -0,0 +1,32 @@
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
#include <bits/rusage.h>
static void ticks_to_rusage(struct rusage* ru, u64 sticks, u64 uticks)
{
ru->ru_stime.tv_sec = sticks / 1000;
ru->ru_stime.tv_usec = (sticks % 1000) * 1000;
ru->ru_utime.tv_sec = uticks / 1000;
ru->ru_utime.tv_usec = (uticks % 1000) * 1000;
}
Result<u64> sys_getrusage(Registers*, SyscallArgs args)
{
int who = (int)args[0];
struct rusage* ru = (struct rusage*)args[1];
auto* current = Scheduler::current();
struct rusage kru;
switch (who)
{
case RUSAGE_SELF: ticks_to_rusage(&kru, current->kernel_ticks_self, current->user_ticks_self); break;
case RUSAGE_CHILDREN: ticks_to_rusage(&kru, current->kernel_ticks_children, current->user_ticks_children); break;
default: return err(EINVAL);
}
if (!MemoryManager::copy_to_user_typed(ru, &kru)) return err(EFAULT);
return 0;
}

View File

@ -50,6 +50,9 @@ Result<u64> sys_waitpid(Registers*, SyscallArgs args)
int status = (int)thread->status;
u64 id = thread->id;
current->user_ticks_children += thread->user_ticks_self;
current->kernel_ticks_children += thread->kernel_ticks_self;
thread->state = ThreadState::Dying;
Scheduler::signal_reap_thread();

View File

@ -261,11 +261,9 @@ namespace Scheduler
{
CPU::disable_interrupts();
g_current->ticks++;
if (is_in_kernel(regs)) g_current->ticks_in_kernel++;
if (is_in_kernel(regs)) g_current->kernel_ticks_self++;
else
g_current->ticks_in_user++;
g_current->user_ticks_self++;
g_current->ticks_left--;
@ -342,11 +340,11 @@ namespace Scheduler
for (const auto* thread : g_threads)
{
kdbgln("%p %c [%-20s] %4zu, parent = (%-18p,%zu), state = %d, ticks: (t:%04zu,k:%04zu,u:%04zu), status = "
kdbgln("%p %c [%-20s] %4zu, parent = (%-18p,%zu), state = %d, ticks: (k:%04zu,u:%04zu), status = "
"%d, cwd = %s",
thread, thread->is_kernel ? 'k' : 'u', thread->name.chars(), thread->id, thread->parent,
thread->parent ? thread->parent->id : 0, (int)thread->state, thread->ticks, thread->ticks_in_kernel,
thread->ticks_in_user, thread->status,
thread->parent ? thread->parent->id : 0, (int)thread->state, thread->kernel_ticks_self,
thread->user_ticks_self, thread->status,
thread->current_directory_path.is_empty() ? "/" : thread->current_directory_path.chars());
}

View File

@ -59,9 +59,10 @@ struct Thread : public LinkedListNode<Thread>
Credentials auth;
u64 ticks = 0;
u64 ticks_in_user = 0;
u64 ticks_in_kernel = 0;
u64 user_ticks_self = 0;
u64 kernel_ticks_self = 0;
u64 user_ticks_children = 0;
u64 kernel_ticks_children = 0;
u64 ticks_left;
u64 sleep_ticks_left;

View File

@ -26,6 +26,7 @@ set(SOURCES
src/sys/utsname.cpp
src/sys/mount.cpp
src/sys/pstat.cpp
src/sys/resource.cpp
)
if(${LUNA_ARCH} STREQUAL "x86_64")

View File

@ -0,0 +1,17 @@
/* bits/rusage.h: The rusage structure. */
#ifndef _BITS_RUSAGE_H
#define _BITS_RUSAGE_H
#include <bits/timespec.h>
struct rusage
{
struct timeval ru_utime;
struct timeval ru_stime;
};
#define RUSAGE_SELF 0
#define RUSAGE_CHILDREN 1
#endif

View File

@ -0,0 +1,20 @@
/* sys/resource.h: System resource management. */
#ifndef _SYS_RESOURCE_H
#define _SYS_RESOURCE_H
#include <bits/rusage.h>
#ifdef __cplusplus
extern "C"
{
#endif
/* Get this process or its children's resource usage. */
int getrusage(int who, struct rusage* ru);
#ifdef __cplusplus
}
#endif
#endif

13
libc/src/sys/resource.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <bits/errno-return.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <unistd.h>
extern "C"
{
int getrusage(int who, struct rusage* ru)
{
long rc = syscall(SYS_getrusage, who, ru);
__errno_return(rc, int);
}
}

View File

@ -55,6 +55,9 @@ class String
/* Creates a single String consisting of a list of strings separated by a delimiter. */
static Result<String> join(const Vector<String>& vec, StringView delim);
/* Creates a single String consisting of a list of strings separated by a delimiter. */
static Result<String> join(const Vector<StringView>& vec, StringView delim);
/* Removes all trailing characters contained in delim. */
void trim(StringView delim);

View File

@ -5,7 +5,7 @@
_e(lseek) _e(mkdir) _e(execve) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(getuid) _e(geteuid) \
_e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(fchmodat) _e(fchownat) _e(ioctl) \
_e(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(dup2) _e(pipe) _e(mount) \
_e(umount) _e(pstat)
_e(umount) _e(pstat) _e(getrusage)
enum Syscalls
{

View File

@ -181,6 +181,23 @@ Result<String> String::join(const Vector<String>& vec, StringView delim)
return sb.string();
}
Result<String> String::join(const Vector<StringView>& vec, StringView delim)
{
if (vec.size() == 0) return String {};
if (vec.size() == 1) return from_string_view(vec[0]);
StringBuilder sb;
TRY(sb.add(vec[0]));
for (usize i = 1; i < vec.size(); i++)
{
TRY(sb.add(delim));
TRY(sb.add(vec[i]));
}
return sb.string();
}
int String::compare(const String* a, const String* b)
{
return strcmp(a->chars(), b->chars());

View File

@ -11,6 +11,7 @@ namespace os
static Result<pid_t> fork();
static Result<void> exec(StringView path, Slice<String> args, bool search_in_path = true);
static Result<void> exec(StringView path, Slice<StringView> args, bool search_in_path = true);
static Result<void> exec(StringView path, Slice<String> args, Slice<String> env, bool search_in_path = true);
};
}

View File

@ -24,6 +24,19 @@ namespace os
return err(errno);
}
Result<void> Process::exec(StringView path, Slice<StringView> args, bool search_in_path)
{
Vector<const char*> argv;
for (const auto& arg : args) { TRY(argv.try_append(arg.chars())); }
TRY(argv.try_append(nullptr));
if (search_in_path) execvp(path.chars(), const_cast<char**>(argv.data()));
else
execv(path.chars(), const_cast<char**>(argv.data()));
return err(errno);
}
Result<void> Process::exec(StringView path, Slice<String> args, Slice<String> env, bool search_in_path)
{
Vector<const char*> argv;