ProcFileSystem.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "ProcFileSystem.h"
  2. #include "Task.h"
  3. static ProcFileSystem* s_the;
  4. ProcFileSystem& ProcFileSystem::the()
  5. {
  6. ASSERT(s_the);
  7. return *s_the;
  8. }
  9. RetainPtr<ProcFileSystem> ProcFileSystem::create()
  10. {
  11. return adopt(*new ProcFileSystem);
  12. }
  13. ProcFileSystem::ProcFileSystem()
  14. {
  15. s_the = this;
  16. }
  17. ProcFileSystem::~ProcFileSystem()
  18. {
  19. }
  20. void ProcFileSystem::addProcess(Task& task)
  21. {
  22. ASSERT_INTERRUPTS_DISABLED();
  23. char buf[16];
  24. ksprintf(buf, "%d", task.pid());
  25. auto dir = addFile(createDirectory(buf));
  26. m_pid2inode.set(task.pid(), dir.index());
  27. addFile(createGeneratedFile("vm", [&task] {
  28. InterruptDisabler disabler;
  29. char* buffer;
  30. auto stringImpl = StringImpl::createUninitialized(80 + task.regionCount() * 80, buffer);
  31. memset(buffer, 0, stringImpl->length());
  32. char* ptr = buffer;
  33. ptr += ksprintf(ptr, "BEGIN END SIZE NAME\n");
  34. for (auto& region : task.regions()) {
  35. ptr += ksprintf(ptr, "%x -- %x %x %s\n",
  36. region->linearAddress.get(),
  37. region->linearAddress.offset(region->size - 1).get(),
  38. region->size,
  39. region->name.characters());
  40. }
  41. *ptr = '\0';
  42. return ByteBuffer::copy((byte*)buffer, ptr - buffer);
  43. }), dir.index());
  44. }
  45. void ProcFileSystem::removeProcess(Task& task)
  46. {
  47. ASSERT_INTERRUPTS_DISABLED();
  48. auto pid = task.pid();
  49. auto it = m_pid2inode.find(pid);
  50. ASSERT(it != m_pid2inode.end());
  51. bool success = removeFile((*it).value);
  52. ASSERT(success);
  53. m_pid2inode.remove(pid);
  54. }
  55. bool ProcFileSystem::initialize()
  56. {
  57. SyntheticFileSystem::initialize();
  58. auto d = addFile(createDirectory("sys"));
  59. addFile(createGeneratedFile("summary", [] {
  60. InterruptDisabler disabler;
  61. auto tasks = Task::allTasks();
  62. char* buffer;
  63. auto stringImpl = StringImpl::createUninitialized(tasks.size() * 256, buffer);
  64. memset(buffer, 0, stringImpl->length());
  65. char* ptr = buffer;
  66. ptr += ksprintf(ptr, "PID OWNER STATE PPID NSCHED FDS NAME\n");
  67. for (auto* task : tasks) {
  68. ptr += ksprintf(ptr, "%w %w:%w %b %w %x %w %s\n",
  69. task->pid(),
  70. task->uid(),
  71. task->gid(),
  72. task->state(),
  73. task->parentPID(),
  74. task->timesScheduled(),
  75. task->fileHandleCount(),
  76. task->name().characters());
  77. }
  78. ptr += ksprintf(ptr, "kmalloc: alloc: %u / free: %u\n", sum_alloc, sum_free);
  79. *ptr = '\0';
  80. return ByteBuffer::copy((byte*)buffer, ptr - buffer);
  81. }));
  82. return true;
  83. }
  84. const char* ProcFileSystem::className() const
  85. {
  86. return "procfs";
  87. }