If the processor has invariant TSC it can be used to measure time. We keep track of the last nanosecond and TSC values and offset them based on the current TSC. This allows getting current time in userspace. The implementation maps a single RO page to every processes' address space. The page contains the TSC info which gets updated every 100 ms. If the processor does not have invariant TSC, this page will not indicate the capability for TSC based timing. There was the problem about how does a processor know which cpu it is running without doing syscall. TSC counters may or may not be synchronized between cores, so we need a separate TSC info for each processor. I ended up adding sequence of bytes 0..255 at the start of the shared page. When a scheduler gets a new thread, it updates the threads gs/fs segment to point to the byte corresponding to the current cpu. This TSC based timing is also used in kernel. With 64 bit HPET this probably does not bring much of a benefit, but on PIT or 32 bit HPET this removes the need to aquire a spinlock to get the current time. This change does force the userspace to not use gs/fs themselves and they are both now reserved. Other one is used for TLS (this can be technically used if user does not call libc code) and the other for the current processor index (cannot be used as kernel unconditionally resets it after each load balance). I was looking at how many times timer's current time was polled (userspace and kernel combined). When idling in window manager, it was around 8k times/s. When running doom it peaked at over 1 million times per second when loading and settled at ~30k times/s.
73 lines
1.7 KiB
C++
73 lines
1.7 KiB
C++
#pragma once
|
|
|
|
#include <BAN/UniqPtr.h>
|
|
#include <BAN/Vector.h>
|
|
#include <kernel/Timer/RTC.h>
|
|
|
|
#include <time.h>
|
|
|
|
namespace Kernel
|
|
{
|
|
|
|
class Timer
|
|
{
|
|
public:
|
|
virtual ~Timer() {};
|
|
virtual uint64_t ms_since_boot() const = 0;
|
|
virtual uint64_t ns_since_boot() const = 0;
|
|
virtual timespec time_since_boot() const = 0;
|
|
|
|
virtual bool pre_scheduler_sleep_needs_lock() const = 0;
|
|
virtual void pre_scheduler_sleep_ns(uint64_t) = 0;
|
|
|
|
protected:
|
|
bool should_invoke_scheduler() const { return m_should_invoke_scheduler; }
|
|
|
|
private:
|
|
bool m_should_invoke_scheduler { true };
|
|
friend class SystemTimer;
|
|
};
|
|
|
|
class SystemTimer : public Timer
|
|
{
|
|
public:
|
|
static void initialize(bool force_pic);
|
|
static SystemTimer& get();
|
|
static bool is_initialized();
|
|
|
|
void initialize_tsc();
|
|
|
|
virtual uint64_t ms_since_boot() const override;
|
|
virtual uint64_t ns_since_boot() const override;
|
|
virtual timespec time_since_boot() const override;
|
|
|
|
virtual bool pre_scheduler_sleep_needs_lock() const override;
|
|
virtual void pre_scheduler_sleep_ns(uint64_t) override;
|
|
|
|
void sleep_ms(uint64_t ms) const { ASSERT(!BAN::Math::will_multiplication_overflow<uint64_t>(ms, 1'000'000)); return sleep_ns(ms * 1'000'000); }
|
|
void sleep_ns(uint64_t ns) const;
|
|
|
|
void dont_invoke_scheduler() { m_timer->m_should_invoke_scheduler = false; }
|
|
|
|
void update_tsc() const;
|
|
uint64_t ns_since_boot_no_tsc() const;
|
|
|
|
timespec real_time() const;
|
|
|
|
private:
|
|
SystemTimer() = default;
|
|
|
|
void initialize_timers(bool force_pic);
|
|
|
|
uint64_t get_tsc_frequency() const;
|
|
|
|
private:
|
|
uint64_t m_boot_time { 0 };
|
|
BAN::UniqPtr<RTC> m_rtc;
|
|
BAN::UniqPtr<Timer> m_timer;
|
|
bool m_has_invariant_tsc { false };
|
|
mutable uint32_t m_timer_ticks { 0 };
|
|
};
|
|
|
|
}
|