* Reworked ps command a bit as its previous output was just messy.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28835 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Salvatore Benedetto
2008-12-31 16:47:45 +00:00
parent 0832f66cc5
commit 4a39c23620
+86 -53
View File
@@ -1,77 +1,110 @@
/* ps.c - process list /*
* (c) 2002, François Revol (mmu_man) for OpenBeOS * Copyright 2002-2008, Haiku Inc. All rights reserved.
* released under the MIT licence. * Distributed under the terms of the MIT License.
* *
* ChangeLog: * Authors:
* 04-26-2002 v1.0 * Francois Revol (mmu_man)
* Initial. * Salvatore Benedetto <[email protected]>
* 10-08-2002
* Added team name filtering (upon suggestion from Dano users :)
*
*/ */
#include <OS.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <OS.h>
#define SNOOZE_TIME 100000 #define SNOOZE_TIME 100000
#define PS_HEADER " thread name state prio user kernel semaphore"
#define PS_SEP "-----------------------------------------------------------------------"
int main(int argc, char **argv) int
main(int argc, char **argv)
{ {
team_info teaminfo; team_info teamInfo;
thread_info thinfo; thread_info threadInfo;
uint32 teamcookie = 0; uint32 teamCookie = 0;
int32 thcookie; uint32 threadCookie = 0;
sem_info sinfo; sem_info semaphoreInfo;
char *thstate; char *threadState;
char *states[] = {"run", "rdy", "msg", "zzz", "sus", "wait" }; char *states[] = {"run", "rdy", "msg", "zzz", "sus", "wait" };
system_info sysinfo; system_info systemInfo;
char *string_to_match; // match this in team name bool printSystemInfo = true;
// match this in team name
char *string_to_match;
// TODO: parse command line
// Possible command line options:
// -h show help
// ps - by default it only shows all team info
// ps [team] - shows info about this team with all its threads
// -t pstree like output
// -a show threads too (by default only teams are displayed)
// -s show semaphore info
// -i show system info
string_to_match = (argc == 2) ? argv[1] : NULL; string_to_match = (argc == 2) ? argv[1] : NULL;
puts("");
puts(PS_HEADER); printf("%-50s %4s %8s %4s %4s\n", "Team", "Id", "#Threads", "Gid", "Uid");
puts(PS_SEP); while (get_next_team_info(&teamCookie, &teamInfo) >= B_OK) {
while (get_next_team_info(&teamcookie, &teaminfo) >= B_OK) {
if (string_to_match) { if (string_to_match) {
char *p; char *p;
p = teaminfo.args; p = teamInfo.args;
if ((p = strchr(p, ' '))) if ((p = strchr(p, ' ')))
*p = '\0'; /* remove arguments, keep only argv[0] */ *p = '\0'; /* remove arguments, keep only argv[0] */
p = strrchr(teaminfo.args, '/'); /* forget the path */ p = strrchr(teamInfo.args, '/'); /* forget the path */
if (p == NULL) if (p == NULL)
p = teaminfo.args; p = teamInfo.args;
if (strstr(p, string_to_match) == NULL) if (strstr(p, string_to_match) == NULL)
continue; continue;
} // Print team info
printf("%s (team %ld) (uid %d) (gid %d)\n", teaminfo.args, teaminfo.team, teaminfo.uid, teaminfo.gid); printf("%-50s %4ld %8ld %4d %4d\n\n", teamInfo.args, teamInfo.team,
thcookie = 0; teamInfo.thread_count, teamInfo.uid, teamInfo.gid);
while (get_next_thread_info(teaminfo.team, &thcookie, &thinfo) >= B_OK) {
if (thinfo.state < B_THREAD_RUNNING || thinfo.state > B_THREAD_WAITING) printf("%-30s %4s %8s %4s %8s %8s\n", "Thread", "Id", "State",
thstate = "???"; "Prio", "UTime", "KTime");
else // Print all info about its threads too
thstate = states[thinfo.state-1]; while (get_next_thread_info(teamInfo.team, &threadCookie, &threadInfo)
printf("%7ld %20s %4s %3ld %7lli %7lli ", thinfo.thread, thinfo.name, thstate, thinfo.priority, thinfo.user_time/1000, thinfo.kernel_time/1000); >= B_OK) {
if (thinfo.state == B_THREAD_WAITING && thinfo.sem != -1) { if (threadInfo.state < B_THREAD_RUNNING
status_t err = get_sem_info(thinfo.sem, &sinfo); || threadInfo.state > B_THREAD_WAITING)
if (!err) // This should never happen
printf("%s(%ld)\n", sinfo.name, sinfo.sem); threadState = "???";
else else
printf("%s(%ld)\n", strerror(err), thinfo.sem); threadState = states[threadInfo.state - 1];
} else
puts(""); printf("%-30s %4ld %8s %4ld %8llu %8llu ",
threadInfo.name, threadInfo.thread, threadState,
threadInfo.priority, (threadInfo.user_time / 1000),
(threadInfo.kernel_time / 1000));
if (threadInfo.state == B_THREAD_WAITING && threadInfo.sem != -1) {
status_t status = get_sem_info(threadInfo.sem, &semaphoreInfo);
if (status == B_OK)
printf("%s(%ld)\n", semaphoreInfo.name, semaphoreInfo.sem);
else
printf("%s(%ld)\n", strerror(status), threadInfo.sem);
} else
puts("");
}
break;
} else {
printf("%-50s %4ld %8ld %4d %4d\n", teamInfo.args, teamInfo.team,
teamInfo.thread_count, teamInfo.uid, teamInfo.gid);
} }
} }
puts("");
// system stats if (printSystemInfo) {
get_system_info(&sysinfo); // system stats
printf("%lik (%li bytes) total memory\n", sysinfo.max_pages*B_PAGE_SIZE/1024, sysinfo.max_pages*B_PAGE_SIZE); get_system_info(&systemInfo);
printf("%lik (%li bytes) currently committed\n", sysinfo.used_pages*B_PAGE_SIZE/1024, sysinfo.used_pages*B_PAGE_SIZE); printf("\nSystem Info\n");
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); printf("%luk (%lu bytes) total memory\n",
printf("%2.1f%% memory utilisation\n", (float)100 * sysinfo.used_pages / sysinfo.max_pages); (systemInfo.max_pages * B_PAGE_SIZE / 1024),
(systemInfo.max_pages * B_PAGE_SIZE));
printf("%luk (%lu bytes) currently committed\n",
(systemInfo.used_pages * B_PAGE_SIZE / 1024),
(systemInfo.used_pages * B_PAGE_SIZE));
printf("%luk (%lu bytes) currently available\n",
(systemInfo.max_pages - systemInfo.used_pages) * B_PAGE_SIZE / 1024,
(systemInfo.max_pages - systemInfo.used_pages) * B_PAGE_SIZE);
printf("%2.1f%% memory utilisation\n",
(float)100 * systemInfo.used_pages / systemInfo.max_pages);
}
return 0; return 0;
} }