Skip to content

Commit ccdeafd

Browse files
bugaevcawesomekling
authored andcommitted
SystemMonitor: Add a devices tab
This tab combines info from /proc/devices with device file paths from /dev.
1 parent 0f8b45c commit ccdeafd

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#include "DevicesModel.h"
2+
#include <AK/JsonArray.h>
3+
#include <AK/JsonObject.h>
4+
#include <AK/JsonValue.h>
5+
#include <LibCore/CDirIterator.h>
6+
#include <LibCore/CFile.h>
7+
#include <sys/stat.h>
8+
9+
NonnullRefPtr<DevicesModel> DevicesModel::create()
10+
{
11+
return adopt(*new DevicesModel);
12+
}
13+
14+
DevicesModel::DevicesModel()
15+
{
16+
}
17+
18+
DevicesModel::~DevicesModel()
19+
{
20+
}
21+
22+
int DevicesModel::row_count(const GModelIndex&) const
23+
{
24+
return m_devices.size();
25+
}
26+
27+
int DevicesModel::column_count(const GModelIndex&) const
28+
{
29+
return Column::__Count;
30+
}
31+
32+
String DevicesModel::column_name(int column) const
33+
{
34+
switch (column) {
35+
case Column::Device:
36+
return "Device";
37+
case Column::Major:
38+
return "Major";
39+
case Column::Minor:
40+
return "Minor";
41+
case Column::ClassName:
42+
return "Class";
43+
case Column::Type:
44+
return "Type";
45+
default:
46+
ASSERT_NOT_REACHED();
47+
}
48+
}
49+
50+
GModel::ColumnMetadata DevicesModel::column_metadata(int column) const
51+
{
52+
switch (column) {
53+
case Column::Device:
54+
return { 70, TextAlignment::CenterLeft };
55+
case Column::Major:
56+
return { 32, TextAlignment::CenterRight };
57+
case Column::Minor:
58+
return { 32, TextAlignment::CenterRight };
59+
case Column::ClassName:
60+
return { 120, TextAlignment::CenterLeft };
61+
case Column::Type:
62+
return { 120, TextAlignment::CenterLeft };
63+
default:
64+
ASSERT_NOT_REACHED();
65+
}
66+
}
67+
68+
GVariant DevicesModel::data(const GModelIndex& index, Role) const
69+
{
70+
ASSERT(is_valid(index));
71+
72+
const DeviceInfo& device = m_devices[index.row()];
73+
switch (index.column()) {
74+
case Column::Device:
75+
return device.path;
76+
case Column::Major:
77+
return device.major;
78+
case Column::Minor:
79+
return device.minor;
80+
case Column::ClassName:
81+
return device.class_name;
82+
case Column::Type:
83+
switch (device.type) {
84+
case DeviceInfo::Type::Block:
85+
return "Block";
86+
case DeviceInfo::Type::Character:
87+
return "Character";
88+
default:
89+
ASSERT_NOT_REACHED();
90+
}
91+
default:
92+
ASSERT_NOT_REACHED();
93+
}
94+
}
95+
96+
void DevicesModel::update()
97+
{
98+
CFile proc_devices { "/proc/devices" };
99+
if (!proc_devices.open(CIODevice::OpenMode::ReadOnly))
100+
ASSERT_NOT_REACHED();
101+
102+
auto json = JsonValue::from_string(proc_devices.read_all()).as_array();
103+
104+
m_devices.clear();
105+
json.for_each([this](auto& value) {
106+
JsonObject device = value.as_object();
107+
DeviceInfo device_info;
108+
109+
device_info.major = device.get("major").to_uint();
110+
device_info.minor = device.get("minor").to_uint();
111+
device_info.class_name = device.get("class_name").to_string();
112+
113+
String type_str = device.get("type").to_string();
114+
if (type_str == "block")
115+
device_info.type = DeviceInfo::Type::Block;
116+
else if (type_str == "character")
117+
device_info.type = DeviceInfo::Type::Character;
118+
else
119+
ASSERT_NOT_REACHED();
120+
121+
m_devices.append(move(device_info));
122+
});
123+
124+
auto fill_in_paths_from_dir = [this](const String& dir) {
125+
CDirIterator dir_iter { dir, CDirIterator::Flags::SkipDots };
126+
while (dir_iter.has_next()) {
127+
auto name = dir_iter.next_path();
128+
auto path = String::format("%s/%s", dir.characters(), name.characters());
129+
struct stat statbuf;
130+
if (lstat(path.characters(), &statbuf) != 0) {
131+
ASSERT_NOT_REACHED();
132+
}
133+
if (!S_ISBLK(statbuf.st_mode) && !S_ISCHR(statbuf.st_mode))
134+
continue;
135+
unsigned major = ::major(statbuf.st_rdev);
136+
unsigned minor = ::minor(statbuf.st_rdev);
137+
138+
auto it = m_devices.find([major, minor](auto& device_info) {
139+
return device_info.major == major && device_info.minor == minor;
140+
});
141+
if (it != m_devices.end()) {
142+
(*it).path = move(path);
143+
}
144+
}
145+
};
146+
147+
fill_in_paths_from_dir("/dev");
148+
fill_in_paths_from_dir("/dev/pts");
149+
150+
did_update();
151+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
3+
#include <AK/AKString.h>
4+
#include <AK/Vector.h>
5+
#include <LibGUI/GModel.h>
6+
7+
class DevicesModel final : public GModel {
8+
public:
9+
enum Column {
10+
Device = 0,
11+
Major,
12+
Minor,
13+
ClassName,
14+
Type,
15+
__Count
16+
};
17+
18+
virtual ~DevicesModel() override;
19+
static NonnullRefPtr<DevicesModel> create();
20+
21+
virtual int row_count(const GModelIndex&) const override;
22+
virtual int column_count(const GModelIndex&) const override;
23+
virtual String column_name(int column) const override;
24+
virtual ColumnMetadata column_metadata(int column) const override;
25+
virtual GVariant data(const GModelIndex&, Role = Role::Display) const override;
26+
virtual void update() override;
27+
28+
private:
29+
DevicesModel();
30+
31+
struct DeviceInfo {
32+
String path;
33+
unsigned major;
34+
unsigned minor;
35+
String class_name;
36+
enum Type {
37+
Block,
38+
Character
39+
};
40+
Type type;
41+
};
42+
43+
Vector<DeviceInfo> m_devices;
44+
};

Applications/SystemMonitor/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ include ../../Makefile.common
22

33
OBJS = \
44
ProcessModel.o \
5+
DevicesModel.o \
56
ProcessTableView.o \
67
MemoryStatsWidget.o \
78
GraphWidget.o \

Applications/SystemMonitor/main.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "DevicesModel.h"
12
#include "GraphWidget.h"
23
#include "MemoryStatsWidget.h"
34
#include "NetworkStatisticsWidget.h"
@@ -39,6 +40,7 @@ static String human_readable_size(u32 size)
3940

4041
static GWidget* build_file_systems_tab();
4142
static GWidget* build_pci_devices_tab();
43+
static GWidget* build_devices_tab();
4244

4345
int main(int argc, char** argv)
4446
{
@@ -94,6 +96,8 @@ int main(int argc, char** argv)
9496

9597
tabwidget->add_widget("PCI devices", build_pci_devices_tab());
9698

99+
tabwidget->add_widget("Devices", build_devices_tab());
100+
97101
auto* network_stats_widget = new NetworkStatisticsWidget(nullptr);
98102
tabwidget->add_widget("Network", network_stats_widget);
99103

@@ -347,3 +351,17 @@ GWidget* build_pci_devices_tab()
347351

348352
return pci_widget;
349353
}
354+
355+
GWidget* build_devices_tab()
356+
{
357+
auto* devices_widget = new GWidget(nullptr);
358+
devices_widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
359+
devices_widget->layout()->set_margins({ 4, 4, 4, 4 });
360+
361+
auto* devices_table_view = new GTableView(devices_widget);
362+
devices_table_view->set_size_columns_to_fit_content(true);
363+
devices_table_view->set_model(GSortingProxyModel::create(DevicesModel::create()));
364+
devices_table_view->model()->update();
365+
366+
return devices_widget;
367+
}

0 commit comments

Comments
 (0)