Skip to content
This repository was archived by the owner on Oct 24, 2022. It is now read-only.

Commit 4a53bbf

Browse files
committed
init
0 parents  commit 4a53bbf

File tree

6 files changed

+165
-0
lines changed

6 files changed

+165
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/target
2+
.gdb_history
3+
vgcore.*

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
zztop
2+
=====
3+
4+
process monitor, in zz, for science
5+
6+
7+
![reddit](reddit.png?raw=true)
8+
9+
![screenshot](screenshot.png?raw=true)

reddit.png

7.88 KB
Loading

screenshot.png

53.6 KB
Loading

src/main.zz

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
using <stdio.h>::{printf};
2+
using <proc/readproc.h> as readproc;
3+
using <string.h>::{memcpy};
4+
using string;
5+
6+
struct Proc {
7+
int mut pid;
8+
string::String+21 mut cmd;
9+
string::String+11 mut user;
10+
u64 mut vmsize;
11+
}
12+
13+
fn update(Proc mut * procs, usize l)
14+
where len(procs) >= l
15+
{
16+
readproc::PROCTAB mut * pt = readproc::openproc (
17+
(int)readproc::PROC_FILLMEM |
18+
(int)readproc::PROC_FILLCOM |
19+
(int)readproc::PROC_FILLUSR
20+
);
21+
22+
for (usize mut i = 0;i<l;i++) {
23+
readproc::proc_t mut * proc = readproc::readproc(pt, 0);
24+
if proc == 0 {
25+
break;
26+
}
27+
static_attest(safe(proc));
28+
29+
rp_get_cmdline (proc, &procs[i].cmd);
30+
rp_get_user (proc, &procs[i].user);
31+
procs[i].pid = rp_get_pid(proc);
32+
procs[i].vmsize = rp_get_vmsize(proc);
33+
34+
readproc::freeproc(proc);
35+
}
36+
37+
readproc::closeproc(pt);
38+
}
39+
40+
fn bsort(Proc mut* procs, usize l)
41+
where len(procs) >= l
42+
where len(procs) < 10000
43+
{
44+
bool mut swapped = true;
45+
while (swapped) {
46+
swapped = false;
47+
for ( usize mut i = 1; i < l; i++) {
48+
static_attest(i > 0); // that's a bug. ssa should know this can't overflow
49+
if procs[i - 1].vmsize < procs[i].vmsize {
50+
Proc mut tmp;
51+
memcpy(&tmp, &procs[i - 1], sizeof(Proc));
52+
memcpy(&procs[i - 1], &procs[i],sizeof(Proc));
53+
memcpy(&procs[i], &tmp, sizeof(Proc));
54+
swapped = true;
55+
}
56+
}
57+
}
58+
}
59+
60+
fn print(Proc * procs, usize l)
61+
where len(procs) >= l
62+
{
63+
printf(" PID USER MEM CMD\n");
64+
for (usize mut i = 0; i < l; i++) {
65+
printf("% 6d %.*s %-10d %.*s\n",
66+
procs[i].pid,
67+
procs[i].user.len, procs[i].user.mem,
68+
procs[i].vmsize / 1024,
69+
procs[i].cmd.len, procs[i].cmd.mem
70+
);
71+
}
72+
}
73+
74+
75+
export fn main() -> int {
76+
Proc mut display[200] = {0};
77+
update(display, static(len(display)));
78+
bsort(display, static(len(display)));
79+
print(display, 10);
80+
81+
82+
return 0;
83+
}
84+
85+
86+
87+
88+
89+
90+
91+
// this is the ugly part don't look
92+
// it will go away once unsafe expressions can be inlined
93+
94+
95+
fn rp_get_pid(readproc::proc_t * p) -> int {
96+
unsafe {
97+
return p->tid;
98+
}
99+
}
100+
101+
fn rp_get_vmsize(readproc::proc_t * p) -> u64 {
102+
unsafe {
103+
return p->size;
104+
}
105+
}
106+
107+
fn rp_get_cmdline(readproc::proc_t *p, string::String+st mut *to)
108+
where st > 2
109+
{
110+
char * mut x;
111+
unsafe {
112+
if p->cmdline == 0 {
113+
return;
114+
}
115+
x = p->cmdline[0];
116+
}
117+
static_attest(nullterm(x));
118+
static_attest(safe(x));
119+
to->append_cstr(x);
120+
while to->len < st - 1 {
121+
to->push(' ');
122+
}
123+
}
124+
125+
fn rp_get_user(readproc::proc_t *p, string::String+st mut *to)
126+
where st > 2
127+
{
128+
char * mut x;
129+
unsafe {
130+
x = p->euser;
131+
}
132+
static_attest(nullterm(x));
133+
static_attest(safe(x));
134+
to->append_cstr(x);
135+
136+
while to->len < st - 1{
137+
to->push(' ');
138+
}
139+
}

zz.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[project]
2+
version = "0.1.0"
3+
name = "zztop"
4+
cincludes = []
5+
cobjects = []
6+
pkgconfig = []
7+
cflags = []
8+
lflags = ["-lprocps"]
9+
10+
[variants]
11+
default = []
12+
13+
[dependencies]
14+
string = "1"

0 commit comments

Comments
 (0)