xref: /haiku/src/bin/ps.c (revision 1214ef1b2100f2b3299fc9d8d6142e46f70a4c3f)
1 /* ps.c - process list
2  * (c) 2002, Fran�ois Revol (mmu_man) for OpenBeOS
3  * released under the MIT licence.
4  *
5  * ChangeLog:
6  * 04-26-2002 v1.0
7  *  Initial.
8  * 10-08-2002
9  *  Added team name filtering (upon suggestion from Dano users :)
10  *
11  */
12 
13 #include <OS.h>
14 #include <stdio.h>
15 #include <string.h>
16 
17 #define SNOOZE_TIME 100000
18 
19 #define PS_HEADER " thread           name      state prio   user  kernel semaphore"
20 #define PS_SEP "-----------------------------------------------------------------------"
21 
22 int main(int argc, char **argv)
23 {
24 	status_t ret;
25 	team_info teaminfo;
26 	thread_info thinfo;
27 	uint32 teamcookie = 0;
28 	int32 thcookie;
29 	sem_info sinfo;
30 	char *thstate;
31 	char *states[] = {"run", "rdy", "msg", "zzz", "sus", "sem" };
32 	system_info sysinfo;
33 	char *string_to_match; // match this in team name
34 
35 	string_to_match = (argc == 2) ? argv[1] : NULL;
36 	puts("");
37 	puts(PS_HEADER);
38 	puts(PS_SEP);
39 	while ((ret = get_next_team_info(&teamcookie, &teaminfo)) >= B_OK) {
40 		if (string_to_match) {
41 			char *p;
42 			p = teaminfo.args;
43 			if ((p = strchr(p, ' ')))
44 				*p = '\0'; /* remove arguments, keep only argv[0] */
45 			p = strrchr(teaminfo.args, '/'); /* forget the path */
46 			if (p == NULL)
47 				p = teaminfo.args;
48 			if (strstr(p, string_to_match) == NULL)
49 				continue;
50 		}
51 		printf("%s (team %ld) (uid %d) (gid %d)\n", teaminfo.args, teaminfo.team, teaminfo.uid, teaminfo.gid);
52 		thcookie = 0;
53 		while ((ret = get_next_thread_info(teaminfo.team, &thcookie, &thinfo)) >= B_OK) {
54 			if (thinfo.state < B_THREAD_RUNNING || thinfo.state >B_THREAD_WAITING)
55 				thstate = "???";
56 			else
57 				thstate = states[thinfo.state-1];
58 			printf("%7ld %20s  %3s %3ld %7lli %7lli ", thinfo.thread, thinfo.name, thstate, thinfo.priority, thinfo.user_time/1000, thinfo.kernel_time/1000);
59 			if (thinfo.state == B_THREAD_WAITING) {
60 				get_sem_info(thinfo.sem, &sinfo);
61 				printf("%s(%ld)\n", sinfo.name, sinfo.sem);
62 			} else
63 				puts("");
64 		}
65 	}
66 	puts("");
67 	// system stats
68 	get_system_info(&sysinfo);
69 	printf("%lik (%li bytes) total memory\n", sysinfo.max_pages*B_PAGE_SIZE/1024, sysinfo.max_pages*B_PAGE_SIZE);
70 	printf("%lik (%li bytes) currently committed\n", sysinfo.used_pages*B_PAGE_SIZE/1024, sysinfo.used_pages*B_PAGE_SIZE);
71 	printf("%lik (%li bytes) currently available\n", (sysinfo.max_pages-sysinfo.used_pages)*B_PAGE_SIZE/1024, (sysinfo.max_pages-sysinfo.used_pages)*B_PAGE_SIZE);
72 	printf("%2.1f%% memory utilisation\n", (float)100 * sysinfo.used_pages / sysinfo.max_pages);
73 	return 0;
74 }
75 
76