1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-23 17:55:08 +00:00

Kernel: Move memory statistics helpers from Process to Space

This commit is contained in:
Andreas Kling 2021-02-08 22:11:10 +01:00
parent b1c9f93fa3
commit 8bda30edd2
5 changed files with 103 additions and 102 deletions

View file

@ -806,13 +806,13 @@ static bool procfs$all(InodeIdentifier, KBufferBuilder& builder)
process_object.add("name", process.name());
process_object.add("executable", process.executable() ? process.executable()->absolute_path() : "");
process_object.add("tty", process.tty() ? process.tty()->tty_name() : "notty");
process_object.add("amount_virtual", process.amount_virtual());
process_object.add("amount_resident", process.amount_resident());
process_object.add("amount_dirty_private", process.amount_dirty_private());
process_object.add("amount_clean_inode", process.amount_clean_inode());
process_object.add("amount_shared", process.amount_shared());
process_object.add("amount_purgeable_volatile", process.amount_purgeable_volatile());
process_object.add("amount_purgeable_nonvolatile", process.amount_purgeable_nonvolatile());
process_object.add("amount_virtual", process.space().amount_virtual());
process_object.add("amount_resident", process.space().amount_resident());
process_object.add("amount_dirty_private", process.space().amount_dirty_private());
process_object.add("amount_clean_inode", process.space().amount_clean_inode());
process_object.add("amount_shared", process.space().amount_shared());
process_object.add("amount_purgeable_volatile", process.space().amount_purgeable_volatile());
process_object.add("amount_purgeable_nonvolatile", process.space().amount_purgeable_nonvolatile());
process_object.add("dumpable", process.is_dumpable());
auto thread_array = process_object.add_array("threads");
process.for_each_thread([&](const Thread& thread) {

View file

@ -554,93 +554,6 @@ void Process::die()
kill_all_threads();
}
size_t Process::amount_dirty_private() const
{
// FIXME: This gets a bit more complicated for Regions sharing the same underlying VMObject.
// The main issue I'm thinking of is when the VMObject has physical pages that none of the Regions are mapping.
// That's probably a situation that needs to be looked at in general.
size_t amount = 0;
ScopedSpinLock lock(space().get_lock());
for (auto& region : space().regions()) {
if (!region.is_shared())
amount += region.amount_dirty();
}
return amount;
}
size_t Process::amount_clean_inode() const
{
HashTable<const InodeVMObject*> vmobjects;
{
ScopedSpinLock lock(space().get_lock());
for (auto& region : space().regions()) {
if (region.vmobject().is_inode())
vmobjects.set(&static_cast<const InodeVMObject&>(region.vmobject()));
}
}
size_t amount = 0;
for (auto& vmobject : vmobjects)
amount += vmobject->amount_clean();
return amount;
}
size_t Process::amount_virtual() const
{
size_t amount = 0;
ScopedSpinLock lock(space().get_lock());
for (auto& region : space().regions()) {
amount += region.size();
}
return amount;
}
size_t Process::amount_resident() const
{
// FIXME: This will double count if multiple regions use the same physical page.
size_t amount = 0;
ScopedSpinLock lock(space().get_lock());
for (auto& region : space().regions()) {
amount += region.amount_resident();
}
return amount;
}
size_t Process::amount_shared() const
{
// FIXME: This will double count if multiple regions use the same physical page.
// FIXME: It doesn't work at the moment, since it relies on PhysicalPage ref counts,
// and each PhysicalPage is only reffed by its VMObject. This needs to be refactored
// so that every Region contributes +1 ref to each of its PhysicalPages.
size_t amount = 0;
ScopedSpinLock lock(space().get_lock());
for (auto& region : space().regions()) {
amount += region.amount_shared();
}
return amount;
}
size_t Process::amount_purgeable_volatile() const
{
size_t amount = 0;
ScopedSpinLock lock(space().get_lock());
for (auto& region : space().regions()) {
if (region.vmobject().is_anonymous() && static_cast<const AnonymousVMObject&>(region.vmobject()).is_any_volatile())
amount += region.amount_resident();
}
return amount;
}
size_t Process::amount_purgeable_nonvolatile() const
{
size_t amount = 0;
ScopedSpinLock lock(space().get_lock());
for (auto& region : space().regions()) {
if (region.vmobject().is_anonymous() && !static_cast<const AnonymousVMObject&>(region.vmobject()).is_any_volatile())
amount += region.amount_resident();
}
return amount;
}
void Process::terminate_due_to_signal(u8 signal)
{
ASSERT_INTERRUPTS_DISABLED();

View file

@ -388,14 +388,6 @@ public:
return m_max_open_file_descriptors;
}
size_t amount_clean_inode() const;
size_t amount_dirty_private() const;
size_t amount_virtual() const;
size_t amount_resident() const;
size_t amount_shared() const;
size_t amount_purgeable_volatile() const;
size_t amount_purgeable_nonvolatile() const;
int exec(String path, Vector<String> arguments, Vector<String> environment, int recusion_depth = 0);
enum class ShouldAllocateTls {

View file

@ -28,6 +28,7 @@
#include <Kernel/Process.h>
#include <Kernel/SpinLock.h>
#include <Kernel/VM/AnonymousVMObject.h>
#include <Kernel/VM/InodeVMObject.h>
#include <Kernel/VM/MemoryManager.h>
#include <Kernel/VM/Space.h>
@ -219,4 +220,91 @@ void Space::remove_all_regions(Badge<Process>)
m_regions.clear();
}
size_t Space::amount_dirty_private() const
{
// FIXME: This gets a bit more complicated for Regions sharing the same underlying VMObject.
// The main issue I'm thinking of is when the VMObject has physical pages that none of the Regions are mapping.
// That's probably a situation that needs to be looked at in general.
size_t amount = 0;
ScopedSpinLock lock(m_lock);
for (auto& region : m_regions) {
if (!region.is_shared())
amount += region.amount_dirty();
}
return amount;
}
size_t Space::amount_clean_inode() const
{
HashTable<const InodeVMObject*> vmobjects;
{
ScopedSpinLock lock(m_lock);
for (auto& region : m_regions) {
if (region.vmobject().is_inode())
vmobjects.set(&static_cast<const InodeVMObject&>(region.vmobject()));
}
}
size_t amount = 0;
for (auto& vmobject : vmobjects)
amount += vmobject->amount_clean();
return amount;
}
size_t Space::amount_virtual() const
{
size_t amount = 0;
ScopedSpinLock lock(m_lock);
for (auto& region : m_regions) {
amount += region.size();
}
return amount;
}
size_t Space::amount_resident() const
{
// FIXME: This will double count if multiple regions use the same physical page.
size_t amount = 0;
ScopedSpinLock lock(m_lock);
for (auto& region : m_regions) {
amount += region.amount_resident();
}
return amount;
}
size_t Space::amount_shared() const
{
// FIXME: This will double count if multiple regions use the same physical page.
// FIXME: It doesn't work at the moment, since it relies on PhysicalPage ref counts,
// and each PhysicalPage is only reffed by its VMObject. This needs to be refactored
// so that every Region contributes +1 ref to each of its PhysicalPages.
size_t amount = 0;
ScopedSpinLock lock(m_lock);
for (auto& region : m_regions) {
amount += region.amount_shared();
}
return amount;
}
size_t Space::amount_purgeable_volatile() const
{
size_t amount = 0;
ScopedSpinLock lock(m_lock);
for (auto& region : m_regions) {
if (region.vmobject().is_anonymous() && static_cast<const AnonymousVMObject&>(region.vmobject()).is_any_volatile())
amount += region.amount_resident();
}
return amount;
}
size_t Space::amount_purgeable_nonvolatile() const
{
size_t amount = 0;
ScopedSpinLock lock(m_lock);
for (auto& region : m_regions) {
if (region.vmobject().is_anonymous() && !static_cast<const AnonymousVMObject&>(region.vmobject()).is_any_volatile())
amount += region.amount_resident();
}
return amount;
}
}

View file

@ -70,6 +70,14 @@ public:
SpinLock<u32>& get_lock() const { return m_lock; }
size_t amount_clean_inode() const;
size_t amount_dirty_private() const;
size_t amount_virtual() const;
size_t amount_resident() const;
size_t amount_shared() const;
size_t amount_purgeable_volatile() const;
size_t amount_purgeable_nonvolatile() const;
private:
Space(Process&, NonnullRefPtr<PageDirectory>);