xref: /haiku/src/bin/ps.c (revision 89755088d790ff4fe36f8aa77dacb2bd15507108)
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 	team_info teaminfo;
25 	thread_info thinfo;
26 	uint32 teamcookie = 0;
27 	int32 thcookie;
28 	sem_info sinfo;
29 	char *thstate;
30 	char *states[] = {"run", "rdy", "msg", "zzz", "sus", "sem" };
31 	system_info sysinfo;
32 	char *string_to_match; // match this in team name
33 
34 	string_to_match = (argc == 2) ? argv[1] : NULL;
35 	puts("");
36 	puts(PS_HEADER);
37 	puts(PS_SEP);
38 	while (get_next_team_info(&teamcookie, &teaminfo) >= B_OK) {
39 		if (string_to_match) {
40 			char *p;
41 			p = teaminfo.args;
42 			if ((p = strchr(p, ' ')))
43 				*p = '\0'; /* remove arguments, keep only argv[0] */
44 			p = strrchr(teaminfo.args, '/'); /* forget the path */
45 			if (p == NULL)
46 				p = teaminfo.args;
47 			if (strstr(p, string_to_match) == NULL)
48 				continue;
49 		}
50 		printf("%s (team %ld) (uid %d) (gid %d)\n", teaminfo.args, teaminfo.team, teaminfo.uid, teaminfo.gid);
51 		thcookie = 0;
52 		while (get_next_thread_info(teaminfo.team, &thcookie, &thinfo) >= B_OK) {
53 			if (thinfo.state < B_THREAD_RUNNING || thinfo.state >B_THREAD_WAITING)
54 				thstate = "???";
55 			else
56 				thstate = states[thinfo.state-1];
57 			printf("%7ld %20s  %3s %3ld %7lli %7lli ", thinfo.thread, thinfo.name, thstate, thinfo.priority, thinfo.user_time/1000, thinfo.kernel_time/1000);
58 			if (thinfo.state == B_THREAD_WAITING) {
59 				status_t err = get_sem_info(thinfo.sem, &sinfo);
60 				if (!err)
61 					printf("%s(%ld)\n", sinfo.name, sinfo.sem);
62 				else
63 					printf("%s(%ld)\n", strerror(err), thinfo.sem);
64 			} else
65 				puts("");
66 		}
67 	}
68 	puts("");
69 	// system stats
70 	get_system_info(&sysinfo);
71 	printf("%lik (%li bytes) total memory\n", sysinfo.max_pages*B_PAGE_SIZE/1024, sysinfo.max_pages*B_PAGE_SIZE);
72 	printf("%lik (%li bytes) currently committed\n", sysinfo.used_pages*B_PAGE_SIZE/1024, sysinfo.used_pages*B_PAGE_SIZE);
73 	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);
74 	printf("%2.1f%% memory utilisation\n", (float)100 * sysinfo.used_pages / sysinfo.max_pages);
75 	return 0;
76 }
77 
78