Compare commits

..

No commits in common. "5117b410db783b74cdc896631ac93c8b80a2bafe" and "1db60b5a824cc31b90a29aac3cfb39c7ea680e19" have entirely different histories.

17 changed files with 14 additions and 180 deletions

View File

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

View File

@ -1,45 +0,0 @@
#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,7 +39,6 @@ set(SOURCES
src/sys/link.cpp src/sys/link.cpp
src/sys/uname.cpp src/sys/uname.cpp
src/sys/mount.cpp src/sys/mount.cpp
src/sys/resource.cpp
src/fs/VFS.cpp src/fs/VFS.cpp
src/fs/Pipe.cpp src/fs/Pipe.cpp
src/fs/Mount.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_egid = thread->auth.egid;
proc.ps_state = (int)thread->state; proc.ps_state = (int)thread->state;
proc.ps_flags = thread->is_kernel ? PS_FLAG_KRNL : 0; proc.ps_flags = thread->is_kernel ? PS_FLAG_KRNL : 0;
set_timespec(proc.ps_time, thread->user_ticks_self + thread->kernel_ticks_self); set_timespec(proc.ps_time, thread->ticks);
set_timespec(proc.ps_ktime, thread->kernel_ticks_self); set_timespec(proc.ps_ktime, thread->ticks_in_kernel);
set_timespec(proc.ps_utime, thread->kernel_ticks_children); set_timespec(proc.ps_utime, thread->ticks_in_user);
strlcpy(proc.ps_name, thread->name.chars(), sizeof(proc.ps_name)); 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(), strlcpy(proc.ps_cwd, thread->current_directory_path.is_empty() ? "/" : thread->current_directory_path.chars(),
sizeof(proc.ps_cwd)); sizeof(proc.ps_cwd));

View File

@ -1,32 +0,0 @@
#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,9 +50,6 @@ Result<u64> sys_waitpid(Registers*, SyscallArgs args)
int status = (int)thread->status; int status = (int)thread->status;
u64 id = thread->id; u64 id = thread->id;
current->user_ticks_children += thread->user_ticks_self;
current->kernel_ticks_children += thread->kernel_ticks_self;
thread->state = ThreadState::Dying; thread->state = ThreadState::Dying;
Scheduler::signal_reap_thread(); Scheduler::signal_reap_thread();

View File

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

View File

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

View File

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

View File

@ -1,17 +0,0 @@
/* 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

@ -1,20 +0,0 @@
/* 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

View File

@ -1,13 +0,0 @@
#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,9 +55,6 @@ class String
/* Creates a single String consisting of a list of strings separated by a delimiter. */ /* Creates a single String consisting of a list of strings separated by a delimiter. */
static Result<String> join(const Vector<String>& vec, StringView delim); 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. */ /* Removes all trailing characters contained in delim. */
void trim(StringView 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(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(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(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(dup2) _e(pipe) _e(mount) \
_e(umount) _e(pstat) _e(getrusage) _e(umount) _e(pstat)
enum Syscalls enum Syscalls
{ {

View File

@ -181,23 +181,6 @@ Result<String> String::join(const Vector<String>& vec, StringView delim)
return sb.string(); 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) int String::compare(const String* a, const String* b)
{ {
return strcmp(a->chars(), b->chars()); return strcmp(a->chars(), b->chars());

View File

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

View File

@ -24,19 +24,6 @@ namespace os
return err(errno); 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) Result<void> Process::exec(StringView path, Slice<String> args, Slice<String> env, bool search_in_path)
{ {
Vector<const char*> argv; Vector<const char*> argv;