Skip to content

Commit aa0d456

Browse files
committed
Add "--top"
1 parent 16440e5 commit aa0d456

4 files changed

Lines changed: 48 additions & 5 deletions

File tree

applications/proc/build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ fi
77

88
# Build configuration
99
ARTIFACT_NAME="proc.bin"
10+
LDFLAGS="-lterminal"
1011

1112
# Include application build tasks
1213
. "../applications.sh"

applications/proc/src/list/list.cpp

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include <stdio.h>
2525
#include <stdlib.h>
2626
#include <string.h>
27+
#include <unordered_map>
28+
29+
#include <libterminal/terminal.hpp>
2730

2831
int countTasks()
2932
{
@@ -68,10 +71,13 @@ int procListCompareByParent(const void* a, const void* b)
6871
return 1;
6972
}
7073

74+
75+
std::unordered_map<g_tid, uint64_t> lastCpuTimes;
76+
7177
/**
7278
*
7379
*/
74-
int procList(int argc, char** argv)
80+
int procList(int argc, char** argv, bool top)
7581
{
7682
bool threads = false;
7783
for(int i = 0; i < argc; i++)
@@ -105,14 +111,42 @@ int procList(int argc, char** argv)
105111
qsort(taskData, taskCount, sizeof(g_kernquery_task_get_data), procListCompareByParent);
106112

107113
// print information
108-
println("%5s %5s %6s %-20s %-38s", "pid", "tid", "mem", "id", "path");
114+
if(top)
115+
{
116+
g_terminal::clear();
117+
g_terminal::setCursor(g_term_cursor_position(0, 0));
118+
}
119+
120+
println("%4s %4s %-20s %-38s %5s %5s", "pid", "tid", "id", "path", "mem", top ? "cpu" : "");
109121
for(uint32_t pos = 0; pos < taskCount; pos++)
110122
{
111123
g_kernquery_task_get_data* entry = &taskData[pos];
112124

113-
if(entry->id != -1 && (entry->type == G_TASK_TYPE_DEFAULT || entry->type == G_TASK_TYPE_VM86) && (threads || entry->id == entry->parent))
125+
if(entry->id != -1 && (entry->type == G_TASK_TYPE_DEFAULT || entry->type == G_TASK_TYPE_VM86) && (
126+
threads || entry->id == entry->parent))
114127
{
115-
println("%5i %5i %6i %-20s %-38s", entry->parent, entry->id, entry->memory_used / 1024, entry->identifier, entry->source_path);
128+
if(top)
129+
{
130+
uint64_t cpuTimeTaken = entry->cpu_time - lastCpuTimes[entry->id];
131+
lastCpuTimes[entry->id] = entry->cpu_time;;
132+
133+
println("%4i %4i %-20s %-38s %5i %5i",
134+
entry->parent,
135+
entry->id,
136+
entry->identifier,
137+
entry->source_path,
138+
entry->memory_used / 1024,
139+
cpuTimeTaken / 1000000);
140+
}
141+
else
142+
{
143+
println("%4i %4i %-20s %-38s %5i",
144+
entry->parent,
145+
entry->id,
146+
entry->identifier,
147+
entry->source_path,
148+
entry->memory_used / 1024);
149+
}
116150
}
117151
}
118152

applications/proc/src/list/list.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@
2626
/**
2727
*
2828
*/
29-
int procList(int argc, char** argv);
29+
int procList(int argc, char** argv, bool top = false);
3030

3131
#endif

applications/proc/src/proc.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ int main(int argc, char** argv)
4242
{
4343
return procList(argc, argv);
4444
}
45+
else if(strcmp(command, "--top") == 0)
46+
{
47+
while(true)
48+
{
49+
procList(argc, argv, true);
50+
g_sleep(1000);
51+
}
52+
}
4553
else if(strcmp(command, "-k") == 0 || strcmp(command, "--kill") == 0)
4654
{
4755
if(argc > 2)

0 commit comments

Comments
 (0)