Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/node/ps.sh
3296 views
1
#!/bin/sh
2
PAGESIZE=`getconf PAGESIZE`;
3
TOTAL_MEMORY=`cat /proc/meminfo | head -n 1 | awk '{print $2}'`;
4
5
# Mimic the output of ps -ax -o pid=,ppid=,pcpu=,pmem=,command=
6
# Read all numeric subdirectories in /proc
7
for pid in `cd /proc && ls -d [0-9]*`
8
do {
9
if [ -e /proc/$pid/stat ]
10
then
11
echo $pid;
12
13
# ppid is the word at index 4 in the stat file for the process
14
awk '{print $4}' /proc/$pid/stat;
15
16
# pcpu - calculation will be done later, this is a placeholder value
17
echo "0.0"
18
19
# pmem - ratio of the process's working set size to total memory.
20
# use the page size to convert to bytes, total memory is in KB
21
# multiplied by 100 to get percentage, extra 10 to be able to move
22
# the decimal over by one place
23
RESIDENT_SET_SIZE=`awk '{print $24}' /proc/$pid/stat`;
24
PERCENT_MEMORY=$(((1000 * $PAGESIZE * $RESIDENT_SET_SIZE) / ($TOTAL_MEMORY * 1024)));
25
if [ $PERCENT_MEMORY -lt 10 ]
26
then
27
# replace the last character with 0. the last character
28
echo $PERCENT_MEMORY | sed 's/.$/0.&/'; #pmem
29
else
30
# insert . before the last character
31
echo $PERCENT_MEMORY | sed 's/.$/.&/';
32
fi
33
34
# cmdline
35
xargs -0 < /proc/$pid/cmdline;
36
fi
37
} | tr "\n" "\t"; # Replace newlines with tab so that all info for a process is shown on one line
38
echo; # But add new lines between processes
39
done
40
41